home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quakeworld_src / client / net_chan.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-17  |  11.5 KB  |  475 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20.  
  21. #include "quakedef.h"
  22.  
  23. #ifdef _WIN32
  24. #include "winquake.h"
  25. #endif
  26.  
  27. #ifdef AMIGA
  28. #pragma amiga-align
  29. #if defined(__PPC__) && defined(WOS)
  30. #include <clib/powerpc_protos.h>
  31. #else
  32. #include <proto/timer.h>
  33. #endif
  34. #pragma default-align
  35. #endif
  36.  
  37. #define PACKET_HEADER 8
  38.  
  39. /*
  40.  
  41. packet header
  42. -------------
  43. 31  sequence
  44. 1 does this message contain a reliable payload
  45. 31  acknowledge sequence
  46. 1 acknowledge receipt of even/odd message
  47. 16  qport
  48.  
  49. The remote connection never knows if it missed a reliable message, the
  50. local side detects that it has been dropped by seeing a sequence acknowledge
  51. higher thatn the last reliable sequence, but without the correct evon/odd
  52. bit for the reliable set.
  53.  
  54. If the sender notices that a reliable message has been dropped, it will be
  55. retransmitted.  It will not be retransmitted again until a message after
  56. the retransmit has been acknowledged and the reliable still failed to get there.
  57.  
  58. if the sequence number is -1, the packet should be handled without a netcon
  59.  
  60. The reliable message can be added to at any time by doing
  61. MSG_Write* (&netchan->message, <data>).
  62.  
  63. If the message buffer is overflowed, either by a single message, or by
  64. multiple frames worth piling up while the last reliable transmit goes
  65. unacknowledged, the netchan signals a fatal error.
  66.  
  67. Reliable messages are allways placed first in a packet, then the unreliable
  68. message is included if there is sufficient room.
  69.  
  70. To the receiver, there is no distinction between the reliable and unreliable
  71. parts of the message, they are just processed out as a single larger message.
  72.  
  73. Illogical packet sequence numbers cause the packet to be dropped, but do
  74. not kill the connection.  This, combined with the tight window of valid
  75. reliable acknowledgement numbers provides protection against malicious
  76. address spoofing.
  77.  
  78. The qport field is a workaround for bad address translating routers that
  79. sometimes remap the client's source port on a packet during gameplay.
  80.  
  81. If the base part of the net address matches and the qport matches, then the
  82. channel matches even if the IP port differs.  The IP port should be updated
  83. to the new value before sending out any replies.
  84.  
  85.  
  86. */
  87.  
  88. int   net_drop;
  89. cvar_t  showpackets = {"showpackets", "0"};
  90. cvar_t  showdrop = {"showdrop", "0"};
  91. cvar_t  qport = {"qport", "0"};
  92.  
  93. /*
  94. ===============
  95. Netchan_Init
  96.  
  97. ===============
  98. */
  99. void Netchan_Init (void)
  100. {
  101. #ifdef AMIGA
  102.   struct timeval tv;
  103. #endif
  104.   int   port;
  105.  
  106.   // pick a port value that should be nice and random
  107. #ifdef _WIN32
  108.   port = ((int)(timeGetTime()*1000) * time(NULL)) & 0xffff;
  109. #else
  110. #ifdef AMIGA
  111. #if defined(__PPC__) && defined(WOS)
  112.   GetSysTimePPC(&tv);
  113. #else
  114.   GetSysTime(&tv);
  115. #endif
  116.   port = ((int)(tv.tv_micro*1000) * time(NULL)) & 0xffff;
  117. #else
  118.   port = ((int)(getpid()+getuid()*1000) * time(NULL)) & 0xffff;
  119. #endif
  120. #endif
  121.  
  122.   Cvar_RegisterVariable (&showpackets);
  123.   Cvar_RegisterVariable (&showdrop);
  124.   Cvar_RegisterVariable (&qport);
  125.   Cvar_SetValue("qport", port);
  126. }
  127.  
  128. /*
  129. ===============
  130. Netchan_OutOfBand
  131.  
  132. Sends an out-of-band datagram
  133. ================
  134. */
  135. void Netchan_OutOfBand (netadr_t adr, int length, byte *data)
  136. {
  137.   sizebuf_t send;
  138.   byte    send_buf[MAX_MSGLEN + PACKET_HEADER];
  139.  
  140. // write the packet header
  141.   send.data = send_buf;
  142.   send.maxsize = sizeof(send_buf);
  143.   send.cursize = 0;
  144.   
  145.   MSG_WriteLong (&send, -1);  // -1 sequence means out of band
  146.   SZ_Write (&send, data, length);
  147.  
  148. // send the datagram
  149.   //zoid, no input in demo playback mode
  150. #ifndef SERVERONLY
  151.   if (!cls.demoplayback)
  152. #endif
  153.     NET_SendPacket (send.cursize, send.data, adr);
  154. }
  155.  
  156. /*
  157. ===============
  158. Netchan_OutOfBandPrint
  159.  
  160. Sends a text message in an out-of-band datagram
  161. ================
  162. */
  163. void Netchan_OutOfBandPrint (netadr_t adr, char *format, ...)
  164. {
  165.   va_list   argptr;
  166.   static char   string[8192];   // ??? why static?
  167.   
  168.   va_start (argptr, format);
  169.   vsprintf (string, format,argptr);
  170.   va_end (argptr);
  171.  
  172.  
  173.   Netchan_OutOfBand (adr, strlen(string), (byte *)string);
  174. }
  175.  
  176.  
  177. /*
  178. ==============
  179. Netchan_Setup
  180.  
  181. called to open a channel to a remote system
  182. ==============
  183. */
  184. void Netchan_Setup (netchan_t *chan, netadr_t adr, int qport)
  185. {
  186.   memset (chan, 0, sizeof(*chan));
  187.   
  188.   chan->remote_address = adr;
  189.   chan->last_received = realtime;
  190.   
  191.   chan->message.data = chan->message_buf;
  192.   chan->message.allowoverflow = true;
  193.   chan->message.maxsize = sizeof(chan->message_buf);
  194.  
  195.   chan->qport = qport;
  196.   
  197.   chan->rate = 1.0/2500;
  198. }
  199.  
  200.  
  201. /*
  202. ===============
  203. Netchan_CanPacket
  204.  
  205. Returns true if the bandwidth choke isn't active
  206. ================
  207. */
  208. #define MAX_BACKUP  200
  209. qboolean Netchan_CanPacket (netchan_t *chan)
  210. {
  211.   if (chan->cleartime < realtime + MAX_BACKUP*chan->rate)
  212.     return true;
  213.   return false;
  214. }
  215.  
  216.  
  217. /*
  218. ===============
  219. Netchan_CanReliable
  220.  
  221. Returns true if the bandwidth choke isn't 
  222. ================
  223. */
  224. qboolean Netchan_CanReliable (netchan_t *chan)
  225. {
  226.   if (chan->reliable_length)
  227.     return false;     // waiting for ack
  228.   return Netchan_CanPacket (chan);
  229. }
  230.  
  231. #ifdef SERVERONLY
  232. qboolean ServerPaused(void);
  233. #endif
  234.  
  235. /*
  236. ===============
  237. Netchan_Transmit
  238.  
  239. tries to send an unreliable message to a connection, and handles the
  240. transmition / retransmition of the reliable messages.
  241.  
  242. A 0 length will still generate a packet and deal with the reliable messages.
  243. ================
  244. */
  245. void Netchan_Transmit (netchan_t *chan, int length, byte *data)
  246. {
  247.   sizebuf_t send;
  248.   byte    send_buf[MAX_MSGLEN + PACKET_HEADER];
  249.   qboolean  send_reliable;
  250.   unsigned  w1, w2;
  251.   int     i;
  252.  
  253. // check for message overflow
  254.   if (chan->message.overflowed)
  255.   {
  256.     chan->fatal_error = true;
  257.     Con_Printf ("%s:Outgoing message overflow\n"
  258.       , NET_AdrToString (chan->remote_address));
  259.     return;
  260.   }
  261.  
  262. // if the remote side dropped the last reliable message, resend it
  263.   send_reliable = false;
  264.  
  265.   if (chan->incoming_acknowledged > chan->last_reliable_sequence
  266.   && chan->incoming_reliable_acknowledged != chan->reliable_sequence)
  267.     send_reliable = true;
  268.  
  269. // if the reliable transmit buffer is empty, copy the current message out
  270.   if (!chan->reliable_length && chan->message.cursize)
  271.   {
  272.     memcpy (chan->reliable_buf, chan->message_buf, chan->message.cursize);
  273.     chan->reliable_length = chan->message.cursize;
  274.     chan->message.cursize = 0;
  275.     chan->reliable_sequence ^= 1;
  276.     send_reliable = true;
  277.   }
  278.  
  279. // write the packet header
  280.   send.data = send_buf;
  281.   send.maxsize = sizeof(send_buf);
  282.   send.cursize = 0;
  283.  
  284.   w1 = chan->outgoing_sequence | (send_reliable<<31);
  285.   w2 = chan->incoming_sequence | (chan->incoming_reliable_sequence<<31);
  286.  
  287.   chan->outgoing_sequence++;
  288.  
  289.   MSG_WriteLong (&send, w1);
  290.   MSG_WriteLong (&send, w2);
  291.  
  292.   // send the qport if we are a client
  293. #ifndef SERVERONLY
  294.   MSG_WriteShort (&send, cls.qport);
  295. #endif
  296.  
  297. // copy the reliable message to the packet first
  298.   if (send_reliable)
  299.   {
  300.     SZ_Write (&send, chan->reliable_buf, chan->reliable_length);
  301.     chan->last_reliable_sequence = chan->outgoing_sequence;
  302.   }
  303.   
  304. // add the unreliable part if space is available
  305.   if (send.maxsize - send.cursize >= length)
  306.     SZ_Write (&send, data, length);
  307.  
  308. // send the datagram
  309.   i = chan->outgoing_sequence & (MAX_LATENT-1);
  310.   chan->outgoing_size[i] = send.cursize;
  311.   chan->outgoing_time[i] = realtime;
  312.  
  313.   //zoid, no input in demo playback mode
  314. #ifndef SERVERONLY
  315.   if (!cls.demoplayback)
  316. #endif
  317.     NET_SendPacket (send.cursize, send.data, chan->remote_address);
  318.  
  319.   if (chan->cleartime < realtime)
  320.     chan->cleartime = realtime + send.cursize*chan->rate;
  321.   else
  322.     chan->cleartime += send.cursize*chan->rate;
  323. #ifdef SERVERONLY
  324.   if (ServerPaused())
  325.     chan->cleartime = realtime;
  326. #endif
  327.  
  328.   if (showpackets.value)
  329.     Con_Printf ("--> s=%i(%i) a=%i(%i) %i\n"
  330.       , chan->outgoing_sequence
  331.       , send_reliable
  332.       , chan->incoming_sequence
  333.       , chan->incoming_reliable_sequence
  334.       , send.cursize);
  335.  
  336. }
  337.  
  338. /*
  339. =================
  340. Netchan_Process
  341.  
  342. called when the current net_message is from remote_address
  343. modifies net_message so that it points to the packet payload
  344. =================
  345. */
  346. qboolean Netchan_Process (netchan_t *chan)
  347. {
  348.   unsigned    sequence, sequence_ack;
  349.   unsigned    reliable_ack, reliable_message;
  350. #ifdef SERVERONLY
  351.   int     qport;
  352. #endif
  353.   int i;
  354.  
  355.   if (
  356. #ifndef SERVERONLY
  357.       !cls.demoplayback && 
  358. #endif
  359.       !NET_CompareAdr (net_from, chan->remote_address))
  360.     return false;
  361.   
  362. // get sequence numbers   
  363.   MSG_BeginReading ();
  364.   sequence = MSG_ReadLong ();
  365.   sequence_ack = MSG_ReadLong ();
  366.  
  367.   // read the qport if we are a server
  368. #ifdef SERVERONLY
  369.   qport = MSG_ReadShort ();
  370. #endif
  371.  
  372.   reliable_message = sequence >> 31;
  373.   reliable_ack = sequence_ack >> 31;
  374.  
  375.   sequence &= ~(1<<31); 
  376.   sequence_ack &= ~(1<<31); 
  377.  
  378.   if (showpackets.value)
  379.     Con_Printf ("<-- s=%i(%i) a=%i(%i) %i\n"
  380.       , sequence
  381.       , reliable_message
  382.       , sequence_ack
  383.       , reliable_ack
  384.       , net_message.cursize);
  385.  
  386. // get a rate estimation
  387. #if 0
  388.   if (chan->outgoing_sequence - sequence_ack < MAX_LATENT)
  389.   {
  390.     int       i;
  391.     double      time, rate;
  392.   
  393.     i = sequence_ack & (MAX_LATENT - 1);
  394.     time = realtime - chan->outgoing_time[i];
  395.     time -= 0.1;  // subtract 100 ms
  396.     if (time <= 0)
  397.     { // gotta be a digital link for <100 ms ping
  398.       if (chan->rate > 1.0/5000)
  399.         chan->rate = 1.0/5000;
  400.     }
  401.     else
  402.     {
  403.       if (chan->outgoing_size[i] < 512)
  404.       { // only deal with small messages
  405.         rate = chan->outgoing_size[i]/time;
  406.         if (rate > 5000)
  407.           rate = 5000;
  408.         rate = 1.0/rate;
  409.         if (chan->rate > rate)
  410.           chan->rate = rate;
  411.       }
  412.     }
  413.   }
  414. #endif
  415.  
  416. //
  417. // discard stale or duplicated packets
  418. //
  419.   if (sequence <= (unsigned)chan->incoming_sequence)
  420.   {
  421.     if (showdrop.value)
  422.       Con_Printf ("%s:Out of order packet %i at %i\n"
  423.         , NET_AdrToString (chan->remote_address)
  424.         ,  sequence
  425.         , chan->incoming_sequence);
  426.     return false;
  427.   }
  428.  
  429. //
  430. // dropped packets don't keep the message from being used
  431. //
  432.   net_drop = sequence - (chan->incoming_sequence+1);
  433.   if (net_drop > 0)
  434.   {
  435.     chan->drop_count += 1;
  436.  
  437.     if (showdrop.value)
  438.       Con_Printf ("%s:Dropped %i packets at %i\n"
  439.       , NET_AdrToString (chan->remote_address)
  440.       , sequence-(chan->incoming_sequence+1)
  441.       , sequence);
  442.   }
  443.  
  444. //
  445. // if the current outgoing reliable message has been acknowledged
  446. // clear the buffer to make way for the next
  447. //
  448.   if (reliable_ack == (unsigned)chan->reliable_sequence)
  449.     chan->reliable_length = 0;  // it has been received
  450.   
  451. //
  452. // if this message contains a reliable message, bump incoming_reliable_sequence 
  453. //
  454.   chan->incoming_sequence = sequence;
  455.   chan->incoming_acknowledged = sequence_ack;
  456.   chan->incoming_reliable_acknowledged = reliable_ack;
  457.   if (reliable_message)
  458.     chan->incoming_reliable_sequence ^= 1;
  459.  
  460. //
  461. // the message can now be read from the current message pointer
  462. // update statistics counters
  463. //
  464.   chan->frame_latency = chan->frame_latency*OLD_AVG
  465.     + (chan->outgoing_sequence-sequence_ack)*(1.0-OLD_AVG);
  466.   chan->frame_rate = chan->frame_rate*OLD_AVG
  467.     + (realtime-chan->last_received)*(1.0-OLD_AVG);   
  468.   chan->good_count += 1;
  469.  
  470.   chan->last_received = realtime;
  471.  
  472.   return true;
  473. }
  474.  
  475.