vkQuake2 doxygen  1.0 dev
net_chan.c File Reference
#include "qcommon.h"

Go to the source code of this file.

Functions

void Netchan_Init (void)
 
void Netchan_OutOfBand (int net_socket, netadr_t adr, int length, byte *data)
 
void Netchan_OutOfBandPrint (int net_socket, netadr_t adr, char *format,...)
 
void Netchan_Setup (netsrc_t sock, netchan_t *chan, netadr_t adr, int qport)
 
qboolean Netchan_CanReliable (netchan_t *chan)
 
qboolean Netchan_NeedReliable (netchan_t *chan)
 
void Netchan_Transmit (netchan_t *chan, int length, byte *data)
 
qboolean Netchan_Process (netchan_t *chan, sizebuf_t *msg)
 

Variables

cvar_tshowpackets
 
cvar_tshowdrop
 
cvar_tqport
 
netadr_t net_from
 
sizebuf_t net_message
 
byte net_message_buffer [MAX_MSGLEN]
 

Function Documentation

◆ Netchan_CanReliable()

qboolean Netchan_CanReliable ( netchan_t chan)

Definition at line 175 of file net_chan.c.

176 {
177  if (chan->reliable_length)
178  return false; // waiting for ack
179  return true;
180 }

◆ Netchan_Init()

void Netchan_Init ( void  )

Definition at line 91 of file net_chan.c.

92 {
93  int port;
94 
95  // pick a port value that should be nice and random
96  port = Sys_Milliseconds() & 0xffff;
97 
98  showpackets = Cvar_Get ("showpackets", "0", 0);
99  showdrop = Cvar_Get ("showdrop", "0", 0);
100  qport = Cvar_Get ("qport", va("%i", port), CVAR_NOSET);
101 }

Referenced by Qcommon_Init().

◆ Netchan_NeedReliable()

qboolean Netchan_NeedReliable ( netchan_t chan)

Definition at line 183 of file net_chan.c.

184 {
185  qboolean send_reliable;
186 
187 // if the remote side dropped the last reliable message, resend it
188  send_reliable = false;
189 
192  send_reliable = true;
193 
194 // if the reliable transmit buffer is empty, copy the current message out
195  if (!chan->reliable_length && chan->message.cursize)
196  {
197  send_reliable = true;
198  }
199 
200  return send_reliable;
201 }

Referenced by Netchan_Transmit().

◆ Netchan_OutOfBand()

void Netchan_OutOfBand ( int  net_socket,
netadr_t  adr,
int  length,
byte data 
)

Definition at line 110 of file net_chan.c.

111 {
112  sizebuf_t send;
113  byte send_buf[MAX_MSGLEN];
114 
115 // write the packet header
116  SZ_Init (&send, send_buf, sizeof(send_buf));
117 
118  MSG_WriteLong (&send, -1); // -1 sequence means out of band
119  SZ_Write (&send, data, length);
120 
121 // send the datagram
122  NET_SendPacket (net_socket, send.cursize, send.data, adr);
123 }

Referenced by Netchan_OutOfBandPrint().

◆ Netchan_OutOfBandPrint()

void Netchan_OutOfBandPrint ( int  net_socket,
netadr_t  adr,
char *  format,
  ... 
)

Definition at line 132 of file net_chan.c.

133 {
134  va_list argptr;
135  static char string[MAX_MSGLEN - 4];
136 
137  va_start (argptr, format);
138  vsnprintf (string,MAX_MSGLEN-4,format,argptr);
139  va_end (argptr);
140 
141  Netchan_OutOfBand (net_socket, adr, (int)strlen(string), (byte *)string);
142 }

Referenced by CL_CheckForResend(), CL_ConnectionlessPacket(), CL_PingServers_f(), CL_SendConnectPacket(), Master_Heartbeat(), Master_Shutdown(), SV_FlushRedirect(), SV_SetMaster_f(), SVC_DirectConnect(), SVC_GetChallenge(), SVC_Info(), SVC_Ping(), and SVC_Status().

◆ Netchan_Process()

qboolean Netchan_Process ( netchan_t chan,
sizebuf_t msg 
)

Definition at line 298 of file net_chan.c.

299 {
300  unsigned sequence, sequence_ack;
301  unsigned reliable_ack, reliable_message;
302  int qport;
303 
304 // get sequence numbers
306  sequence = MSG_ReadLong (msg);
307  sequence_ack = MSG_ReadLong (msg);
308 
309  // read the qport if we are a server
310  if (chan->sock == NS_SERVER)
311  qport = MSG_ReadShort (msg);
312 
313  reliable_message = sequence >> 31;
314  reliable_ack = sequence_ack >> 31;
315 
316  sequence &= ~(1<<31);
317  sequence_ack &= ~(1<<31);
318 
319  if (showpackets->value)
320  {
321  if (reliable_message)
322  Com_Printf ("recv %4i : s=%i reliable=%i ack=%i rack=%i\n"
323  , msg->cursize
324  , sequence
325  , chan->incoming_reliable_sequence ^ 1
326  , sequence_ack
327  , reliable_ack);
328  else
329  Com_Printf ("recv %4i : s=%i ack=%i rack=%i\n"
330  , msg->cursize
331  , sequence
332  , sequence_ack
333  , reliable_ack);
334  }
335 
336 //
337 // discard stale or duplicated packets
338 //
339  if (sequence <= chan->incoming_sequence)
340  {
341  if (showdrop->value)
342  Com_Printf ("%s:Out of order packet %i at %i\n"
344  , sequence
345  , chan->incoming_sequence);
346  return false;
347  }
348 
349 //
350 // dropped packets don't keep the message from being used
351 //
352  chan->dropped = sequence - (chan->incoming_sequence+1);
353  if (chan->dropped > 0)
354  {
355  if (showdrop->value)
356  Com_Printf ("%s:Dropped %i packets at %i\n"
358  , chan->dropped
359  , sequence);
360  }
361 
362 //
363 // if the current outgoing reliable message has been acknowledged
364 // clear the buffer to make way for the next
365 //
366  if (reliable_ack == chan->reliable_sequence)
367  chan->reliable_length = 0; // it has been received
368 
369 //
370 // if this message contains a reliable message, bump incoming_reliable_sequence
371 //
372  chan->incoming_sequence = sequence;
373  chan->incoming_acknowledged = sequence_ack;
374  chan->incoming_reliable_acknowledged = reliable_ack;
375  if (reliable_message)
376  {
377  chan->incoming_reliable_sequence ^= 1;
378  }
379 
380 //
381 // the message can now be read from the current message pointer
382 //
383  chan->last_received = curtime;
384 
385  return true;
386 }

Referenced by CL_ReadPackets(), and SV_ReadPackets().

◆ Netchan_Setup()

void Netchan_Setup ( netsrc_t  sock,
netchan_t chan,
netadr_t  adr,
int  qport 
)

Definition at line 152 of file net_chan.c.

153 {
154  memset (chan, 0, sizeof(*chan));
155 
156  chan->sock = sock;
157  chan->remote_address = adr;
158  chan->qport = qport;
159  chan->last_received = curtime;
160  chan->incoming_sequence = 0;
161  chan->outgoing_sequence = 1;
162 
163  SZ_Init (&chan->message, chan->message_buf, sizeof(chan->message_buf));
164  chan->message.allowoverflow = true;
165 }

Referenced by CL_ConnectionlessPacket(), and SVC_DirectConnect().

◆ Netchan_Transmit()

void Netchan_Transmit ( netchan_t chan,
int  length,
byte data 
)

Definition at line 213 of file net_chan.c.

214 {
215  sizebuf_t send;
216  byte send_buf[MAX_MSGLEN];
217  qboolean send_reliable;
218  unsigned w1, w2;
219 
220 // check for message overflow
221  if (chan->message.overflowed)
222  {
223  chan->fatal_error = true;
224  Com_Printf ("%s:Outgoing message overflow\n"
225  , NET_AdrToString (chan->remote_address));
226  return;
227  }
228 
229  send_reliable = Netchan_NeedReliable (chan);
230 
231  if (!chan->reliable_length && chan->message.cursize)
232  {
233  memcpy (chan->reliable_buf, chan->message_buf, chan->message.cursize);
234  chan->reliable_length = chan->message.cursize;
235  chan->message.cursize = 0;
236  chan->reliable_sequence ^= 1;
237  }
238 
239 
240 // write the packet header
241  SZ_Init (&send, send_buf, sizeof(send_buf));
242 
243  w1 = ( chan->outgoing_sequence & ~(1<<31) ) | (send_reliable<<31);
244  w2 = ( chan->incoming_sequence & ~(1<<31) ) | (chan->incoming_reliable_sequence<<31);
245 
246  chan->outgoing_sequence++;
247  chan->last_sent = curtime;
248 
249  MSG_WriteLong (&send, w1);
250  MSG_WriteLong (&send, w2);
251 
252  // send the qport if we are a client
253  if (chan->sock == NS_CLIENT)
254  MSG_WriteShort (&send, qport->value);
255 
256 // copy the reliable message to the packet first
257  if (send_reliable)
258  {
259  SZ_Write (&send, chan->reliable_buf, chan->reliable_length);
261  }
262 
263 // add the unreliable part if space is available
264  if (send.maxsize - send.cursize >= length)
265  SZ_Write (&send, data, length);
266  else
267  Com_Printf ("Netchan_Transmit: dumped unreliable\n");
268 
269 // send the datagram
270  NET_SendPacket (chan->sock, send.cursize, send.data, chan->remote_address);
271 
272  if (showpackets->value)
273  {
274  if (send_reliable)
275  Com_Printf ("send %4i : s=%i reliable=%i ack=%i rack=%i\n"
276  , send.cursize
277  , chan->outgoing_sequence - 1
278  , chan->reliable_sequence
279  , chan->incoming_sequence
281  else
282  Com_Printf ("send %4i : s=%i ack=%i rack=%i\n"
283  , send.cursize
284  , chan->outgoing_sequence - 1
285  , chan->incoming_sequence
287  }
288 }

Referenced by CL_Disconnect(), CL_SendCmd(), SV_FinalMessage(), SV_SendClientDatagram(), and SV_SendClientMessages().

Variable Documentation

◆ net_from

◆ net_message

◆ net_message_buffer

byte net_message_buffer[MAX_MSGLEN]

Definition at line 83 of file net_chan.c.

Referenced by CL_Init(), and SV_Init().

◆ qport

◆ showdrop

cvar_t* showdrop

Definition at line 78 of file net_chan.c.

Referenced by Netchan_Init(), and Netchan_Process().

◆ showpackets

cvar_t* showpackets

Definition at line 77 of file net_chan.c.

Referenced by Netchan_Init(), Netchan_Process(), and Netchan_Transmit().

curtime
int curtime
Definition: q_shwin.c:119
sizebuf_s
Definition: qcommon.h:92
netchan_t::sock
netsrc_t sock
Definition: qcommon.h:586
netchan_t::incoming_sequence
int incoming_sequence
Definition: qcommon.h:597
netchan_t::last_reliable_sequence
int last_reliable_sequence
Definition: qcommon.h:605
netchan_t::incoming_acknowledged
int incoming_acknowledged
Definition: qcommon.h:598
NS_SERVER
@ NS_SERVER
Definition: qcommon.h:549
CVAR_NOSET
#define CVAR_NOSET
Definition: q_shared.h:319
MSG_ReadShort
int MSG_ReadShort(sizebuf_t *msg_read)
Definition: common.c:716
netchan_t::message
sizebuf_t message
Definition: qcommon.h:608
sizebuf_s::overflowed
qboolean overflowed
Definition: qcommon.h:95
qboolean
qboolean
Definition: q_shared.h:63
Netchan_OutOfBand
void Netchan_OutOfBand(int net_socket, netadr_t adr, int length, byte *data)
Definition: net_chan.c:110
netchan_t::dropped
int dropped
Definition: qcommon.h:588
MSG_WriteLong
void MSG_WriteLong(sizebuf_t *sb, int c)
Definition: common.c:330
NET_AdrToString
char * NET_AdrToString(netadr_t a)
Definition: net_wins.c:161
SZ_Init
void SZ_Init(sizebuf_t *buf, byte *data, int length)
Definition: common.c:885
sizebuf_s::data
byte * data
Definition: qcommon.h:96
Cvar_Get
cvar_t * Cvar_Get(char *var_name, char *var_value, int flags)
Definition: cvar.c:127
SZ_Write
void SZ_Write(sizebuf_t *buf, void *data, int length)
Definition: common.c:921
va
char * va(char *format,...)
Definition: q_shared.c:1050
msg
cvar_t * msg
Definition: cl_main.c:83
showdrop
cvar_t * showdrop
Definition: net_chan.c:78
sizebuf_s::maxsize
int maxsize
Definition: qcommon.h:97
netchan_t::fatal_error
qboolean fatal_error
Definition: qcommon.h:584
netchan_t::incoming_reliable_acknowledged
int incoming_reliable_acknowledged
Definition: qcommon.h:599
NET_SendPacket
void NET_SendPacket(netsrc_t sock, int length, void *data, netadr_t to)
Definition: net_wins.c:400
netchan_t::last_sent
int last_sent
Definition: qcommon.h:591
cvar_s::value
float value
Definition: q_shared.h:331
MSG_WriteShort
void MSG_WriteShort(sizebuf_t *sb, int c)
Definition: common.c:316
netchan_t::remote_address
netadr_t remote_address
Definition: qcommon.h:593
MAX_MSGLEN
#define MAX_MSGLEN
Definition: qcommon.h:544
netchan_t::reliable_length
int reliable_length
Definition: qcommon.h:612
qport
cvar_t * qport
Definition: net_chan.c:79
netchan_t::outgoing_sequence
int outgoing_sequence
Definition: qcommon.h:603
MSG_ReadLong
int MSG_ReadLong(sizebuf_t *msg_read)
Definition: common.c:731
netchan_t::reliable_sequence
int reliable_sequence
Definition: qcommon.h:604
NS_CLIENT
@ NS_CLIENT
Definition: qcommon.h:549
netchan_t::last_received
int last_received
Definition: qcommon.h:590
netchan_t::reliable_buf
byte reliable_buf[MAX_MSGLEN-16]
Definition: qcommon.h:613
sizebuf_s::allowoverflow
qboolean allowoverflow
Definition: qcommon.h:94
netchan_t::incoming_reliable_sequence
int incoming_reliable_sequence
Definition: qcommon.h:601
sizebuf_s::cursize
int cursize
Definition: qcommon.h:98
format
GLsizei GLenum format
Definition: qgl_win.c:131
Com_Printf
void Com_Printf(char *fmt,...)
Definition: common.c:104
Sys_Milliseconds
int Sys_Milliseconds(void)
Definition: q_shwin.c:120
netchan_t::message_buf
byte message_buf[MAX_MSGLEN-16]
Definition: qcommon.h:609
showpackets
cvar_t * showpackets
Definition: net_chan.c:77
Netchan_NeedReliable
qboolean Netchan_NeedReliable(netchan_t *chan)
Definition: net_chan.c:183
MSG_BeginReading
void MSG_BeginReading(sizebuf_t *msg)
Definition: common.c:684
netchan_t::qport
int qport
Definition: qcommon.h:594