2000-05-10 20:33:16 +00:00
|
|
|
/*
|
2000-05-11 16:03:29 +00:00
|
|
|
net_chan.c
|
2000-05-10 20:33:16 +00:00
|
|
|
|
2000-05-11 16:03:29 +00:00
|
|
|
(description)
|
2000-05-10 20:33:16 +00:00
|
|
|
|
2000-05-11 16:03:29 +00:00
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
2000-05-10 20:33:16 +00:00
|
|
|
|
2000-05-11 16:03:29 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
2000-05-10 20:33:16 +00:00
|
|
|
|
2000-05-11 16:03:29 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
2000-05-10 20:33:16 +00:00
|
|
|
|
2000-05-11 16:03:29 +00:00
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
$Id$
|
2000-05-10 20:33:16 +00:00
|
|
|
*/
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2000-05-17 10:03:19 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2000-05-21 08:24:45 +00:00
|
|
|
# include "config.h"
|
2000-05-17 10:03:19 +00:00
|
|
|
#endif
|
2001-01-10 22:13:06 +00:00
|
|
|
#ifndef _WIN32
|
2000-05-14 17:04:13 +00:00
|
|
|
# include <unistd.h>
|
2000-05-13 10:45:29 +00:00
|
|
|
#else
|
2000-05-14 17:04:13 +00:00
|
|
|
# include <windows.h>
|
2000-05-12 01:49:43 +00:00
|
|
|
#endif
|
2000-05-26 03:55:27 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
2001-02-07 05:45:59 +00:00
|
|
|
# include <string.h>
|
2000-05-26 03:55:27 +00:00
|
|
|
#endif
|
2000-12-30 02:16:36 +00:00
|
|
|
#ifdef HAVE_STRINGS_H
|
2001-02-07 05:45:59 +00:00
|
|
|
# include <strings.h>
|
2000-12-30 02:16:36 +00:00
|
|
|
#endif
|
2000-05-13 10:45:29 +00:00
|
|
|
|
2000-05-21 08:24:45 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2000-12-30 02:16:36 +00:00
|
|
|
#include "client.h"
|
|
|
|
#include "commdef.h"
|
2000-05-21 08:24:45 +00:00
|
|
|
#include "console.h"
|
|
|
|
#include "cvar.h"
|
2000-12-30 02:16:36 +00:00
|
|
|
#include "msg.h"
|
2000-05-21 08:24:45 +00:00
|
|
|
#include "net.h"
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
#define PACKET_HEADER 8
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
packet header
|
|
|
|
-------------
|
|
|
|
31 sequence
|
|
|
|
1 does this message contain a reliable payload
|
|
|
|
31 acknowledge sequence
|
|
|
|
1 acknowledge receipt of even/odd message
|
|
|
|
16 qport
|
|
|
|
|
|
|
|
The remote connection never knows if it missed a reliable message, the
|
|
|
|
local side detects that it has been dropped by seeing a sequence acknowledge
|
|
|
|
higher thatn the last reliable sequence, but without the correct evon/odd
|
|
|
|
bit for the reliable set.
|
|
|
|
|
|
|
|
If the sender notices that a reliable message has been dropped, it will be
|
|
|
|
retransmitted. It will not be retransmitted again until a message after
|
|
|
|
the retransmit has been acknowledged and the reliable still failed to get there.
|
|
|
|
|
|
|
|
if the sequence number is -1, the packet should be handled without a netcon
|
|
|
|
|
|
|
|
The reliable message can be added to at any time by doing
|
|
|
|
MSG_Write* (&netchan->message, <data>).
|
|
|
|
|
|
|
|
If the message buffer is overflowed, either by a single message, or by
|
|
|
|
multiple frames worth piling up while the last reliable transmit goes
|
|
|
|
unacknowledged, the netchan signals a fatal error.
|
|
|
|
|
|
|
|
Reliable messages are allways placed first in a packet, then the unreliable
|
|
|
|
message is included if there is sufficient room.
|
|
|
|
|
|
|
|
To the receiver, there is no distinction between the reliable and unreliable
|
|
|
|
parts of the message, they are just processed out as a single larger message.
|
|
|
|
|
|
|
|
Illogical packet sequence numbers cause the packet to be dropped, but do
|
|
|
|
not kill the connection. This, combined with the tight window of valid
|
|
|
|
reliable acknowledgement numbers provides protection against malicious
|
|
|
|
address spoofing.
|
|
|
|
|
|
|
|
The qport field is a workaround for bad address translating routers that
|
|
|
|
sometimes remap the client's source port on a packet during gameplay.
|
|
|
|
|
|
|
|
If the base part of the net address matches and the qport matches, then the
|
|
|
|
channel matches even if the IP port differs. The IP port should be updated
|
|
|
|
to the new value before sending out any replies.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
int net_drop;
|
|
|
|
cvar_t *showpackets;
|
|
|
|
cvar_t *showdrop;
|
|
|
|
cvar_t *qport;
|
|
|
|
extern qboolean is_server;
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
/*
|
2001-02-09 02:53:09 +00:00
|
|
|
Netchan_Init
|
2000-05-10 11:29:38 +00:00
|
|
|
*/
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
Netchan_Init (void)
|
2000-05-10 11:29:38 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
int port;
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
// pick a port value that should be nice and random
|
|
|
|
#ifdef _WIN32
|
2000-12-08 06:51:37 +00:00
|
|
|
port = ((int) (timeGetTime () * 1000) * time (NULL)) & 0xffff;
|
2000-05-10 11:29:38 +00:00
|
|
|
#else
|
2000-12-08 06:51:37 +00:00
|
|
|
port = ((int) (getpid () + getuid () * 1000) * time (NULL)) & 0xffff;
|
2000-05-10 11:29:38 +00:00
|
|
|
#endif
|
|
|
|
|
2000-10-29 15:35:24 +00:00
|
|
|
Cvar_SetValue (qport, port);
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
Netchan_Init_Cvars (void)
|
2000-10-29 15:35:24 +00:00
|
|
|
{
|
2001-05-13 05:10:02 +00:00
|
|
|
showpackets = Cvar_Get ("showpackets", "0", CVAR_NONE, NULL, "Show all network packets");
|
|
|
|
showdrop = Cvar_Get ("showdrop", "0", CVAR_NONE, NULL, "Toggle the display of how many packets you are dropping");
|
|
|
|
qport = Cvar_Get ("qport", "0", CVAR_NONE, NULL, "The internal port number for the game networking code."
|
2000-12-31 09:05:25 +00:00
|
|
|
"Useful for clients who use multiple connections through one IP address (NAT/IP-MASQ) because default port is random.");
|
2000-05-10 11:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-02-09 02:53:09 +00:00
|
|
|
Netchan_OutOfBand
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2001-02-09 02:53:09 +00:00
|
|
|
Sends an out-of-band datagram
|
2000-05-10 11:29:38 +00:00
|
|
|
*/
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
Netchan_OutOfBand (netadr_t adr, int length, byte * data)
|
2000-05-10 11:29:38 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
sizebuf_t send;
|
|
|
|
byte send_buf[MAX_MSGLEN + PACKET_HEADER];
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
// write the packet header
|
|
|
|
send.data = send_buf;
|
2000-12-08 06:51:37 +00:00
|
|
|
send.maxsize = sizeof (send_buf);
|
2000-05-10 11:29:38 +00:00
|
|
|
send.cursize = 0;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
|
|
|
MSG_WriteLong (&send, -1); // -1 sequence means out of band
|
2000-05-10 11:29:38 +00:00
|
|
|
SZ_Write (&send, data, length);
|
|
|
|
|
|
|
|
// send the datagram
|
2000-12-08 06:51:37 +00:00
|
|
|
// zoid, no input in demo playback mode
|
|
|
|
if (!is_server) {
|
2000-05-14 20:25:49 +00:00
|
|
|
if (!cls.demoplayback)
|
|
|
|
NET_SendPacket (send.cursize, send.data, adr);
|
2000-12-08 06:51:37 +00:00
|
|
|
} else
|
2000-05-10 11:29:38 +00:00
|
|
|
NET_SendPacket (send.cursize, send.data, adr);
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-10 11:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-02-09 02:53:09 +00:00
|
|
|
Netchan_OutOfBandPrint
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2001-02-09 02:53:09 +00:00
|
|
|
Sends a text message in an out-of-band datagram
|
2000-05-10 11:29:38 +00:00
|
|
|
*/
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
Netchan_OutOfBandPrint (netadr_t adr, char *format, ...)
|
2000-05-10 11:29:38 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
va_list argptr;
|
|
|
|
static char string[8192]; // ?? why static?
|
|
|
|
|
2000-05-10 11:29:38 +00:00
|
|
|
va_start (argptr, format);
|
2000-12-08 06:51:37 +00:00
|
|
|
vsnprintf (string, sizeof (string), format, argptr);
|
2000-05-10 11:29:38 +00:00
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
Netchan_OutOfBand (adr, strlen (string), (byte *) string);
|
2000-05-10 11:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-02-09 02:53:09 +00:00
|
|
|
Netchan_Setup
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2001-02-09 02:53:09 +00:00
|
|
|
called to open a channel to a remote system
|
2000-05-10 11:29:38 +00:00
|
|
|
*/
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
Netchan_Setup (netchan_t *chan, netadr_t adr, int qport)
|
2000-05-10 11:29:38 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
memset (chan, 0, sizeof (*chan));
|
|
|
|
|
2000-05-10 11:29:38 +00:00
|
|
|
chan->remote_address = adr;
|
|
|
|
chan->last_received = realtime;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
2000-05-10 11:29:38 +00:00
|
|
|
chan->message.data = chan->message_buf;
|
|
|
|
chan->message.allowoverflow = true;
|
2000-12-08 06:51:37 +00:00
|
|
|
chan->message.maxsize = sizeof (chan->message_buf);
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
chan->qport = qport;
|
2000-12-08 06:51:37 +00:00
|
|
|
|
|
|
|
chan->rate = 1.0 / 2500;
|
2000-05-10 11:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-02-09 02:53:09 +00:00
|
|
|
Netchan_CanPacket
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2001-02-09 02:53:09 +00:00
|
|
|
Returns true if the bandwidth choke isn't active
|
2000-05-10 11:29:38 +00:00
|
|
|
*/
|
|
|
|
#define MAX_BACKUP 200
|
2000-12-08 06:51:37 +00:00
|
|
|
qboolean
|
|
|
|
Netchan_CanPacket (netchan_t *chan)
|
2000-05-10 11:29:38 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
if (chan->cleartime < realtime + MAX_BACKUP * chan->rate)
|
2000-05-10 11:29:38 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-02-09 02:53:09 +00:00
|
|
|
Netchan_CanReliable
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2001-02-09 02:53:09 +00:00
|
|
|
Returns true if the bandwidth choke isn't
|
2000-05-10 11:29:38 +00:00
|
|
|
*/
|
2000-12-08 06:51:37 +00:00
|
|
|
qboolean
|
|
|
|
Netchan_CanReliable (netchan_t *chan)
|
2000-05-10 11:29:38 +00:00
|
|
|
{
|
|
|
|
if (chan->reliable_length)
|
2000-12-08 06:51:37 +00:00
|
|
|
return false; // waiting for ack
|
2000-05-10 11:29:38 +00:00
|
|
|
return Netchan_CanPacket (chan);
|
|
|
|
}
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
qboolean ServerPaused (void);
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
/*
|
2001-02-09 02:53:09 +00:00
|
|
|
Netchan_Transmit
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2001-02-09 02:53:09 +00:00
|
|
|
tries to send an unreliable message to a connection, and handles the
|
|
|
|
transmition / retransmition of the reliable messages.
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2001-02-09 02:53:09 +00:00
|
|
|
A 0 length will still generate a packet and deal with the reliable messages.
|
2000-05-10 11:29:38 +00:00
|
|
|
*/
|
2000-12-08 06:51:37 +00:00
|
|
|
void
|
|
|
|
Netchan_Transmit (netchan_t *chan, int length, byte * data)
|
2000-05-10 11:29:38 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
sizebuf_t send;
|
|
|
|
byte send_buf[MAX_MSGLEN + PACKET_HEADER];
|
|
|
|
qboolean send_reliable;
|
|
|
|
unsigned int w1, w2;
|
|
|
|
int i;
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
// check for message overflow
|
2000-12-08 06:51:37 +00:00
|
|
|
if (chan->message.overflowed) {
|
2000-05-10 11:29:38 +00:00
|
|
|
chan->fatal_error = true;
|
2000-12-08 06:51:37 +00:00
|
|
|
Con_Printf ("%s:Outgoing message overflow\n",
|
|
|
|
NET_AdrToString (chan->remote_address));
|
2000-05-10 11:29:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if the remote side dropped the last reliable message, resend it
|
|
|
|
send_reliable = false;
|
|
|
|
|
|
|
|
if (chan->incoming_acknowledged > chan->last_reliable_sequence
|
2000-12-08 06:51:37 +00:00
|
|
|
&& chan->incoming_reliable_acknowledged != chan->reliable_sequence)
|
2000-05-10 11:29:38 +00:00
|
|
|
send_reliable = true;
|
|
|
|
|
|
|
|
// if the reliable transmit buffer is empty, copy the current message out
|
2000-12-08 06:51:37 +00:00
|
|
|
if (!chan->reliable_length && chan->message.cursize) {
|
2000-05-10 11:29:38 +00:00
|
|
|
memcpy (chan->reliable_buf, chan->message_buf, chan->message.cursize);
|
|
|
|
chan->reliable_length = chan->message.cursize;
|
|
|
|
chan->message.cursize = 0;
|
|
|
|
chan->reliable_sequence ^= 1;
|
|
|
|
send_reliable = true;
|
|
|
|
}
|
|
|
|
// write the packet header
|
|
|
|
send.data = send_buf;
|
2000-12-08 06:51:37 +00:00
|
|
|
send.maxsize = sizeof (send_buf);
|
2000-05-10 11:29:38 +00:00
|
|
|
send.cursize = 0;
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
w1 = chan->outgoing_sequence | (send_reliable << 31);
|
|
|
|
w2 = chan->incoming_sequence | (chan->incoming_reliable_sequence << 31);
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
chan->outgoing_sequence++;
|
|
|
|
|
|
|
|
MSG_WriteLong (&send, w1);
|
|
|
|
MSG_WriteLong (&send, w2);
|
|
|
|
|
|
|
|
// send the qport if we are a client
|
2000-12-08 06:51:37 +00:00
|
|
|
if (!is_server)
|
2000-05-12 00:11:41 +00:00
|
|
|
MSG_WriteShort (&send, cls.qport);
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
// copy the reliable message to the packet first
|
2000-12-08 06:51:37 +00:00
|
|
|
if (send_reliable) {
|
2000-05-10 11:29:38 +00:00
|
|
|
SZ_Write (&send, chan->reliable_buf, chan->reliable_length);
|
|
|
|
chan->last_reliable_sequence = chan->outgoing_sequence;
|
|
|
|
}
|
|
|
|
// add the unreliable part if space is available
|
|
|
|
if (send.maxsize - send.cursize >= length)
|
|
|
|
SZ_Write (&send, data, length);
|
|
|
|
|
|
|
|
// send the datagram
|
2000-12-08 06:51:37 +00:00
|
|
|
i = chan->outgoing_sequence & (MAX_LATENT - 1);
|
2000-05-10 11:29:38 +00:00
|
|
|
chan->outgoing_size[i] = send.cursize;
|
|
|
|
chan->outgoing_time[i] = realtime;
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
// zoid, no input in demo playback mode
|
|
|
|
if (!is_server) {
|
2000-05-14 20:25:49 +00:00
|
|
|
if (!cls.demoplayback)
|
|
|
|
NET_SendPacket (send.cursize, send.data, chan->remote_address);
|
2000-12-08 06:51:37 +00:00
|
|
|
} else
|
2000-05-10 11:29:38 +00:00
|
|
|
NET_SendPacket (send.cursize, send.data, chan->remote_address);
|
|
|
|
|
|
|
|
if (chan->cleartime < realtime)
|
2000-12-08 06:51:37 +00:00
|
|
|
chan->cleartime = realtime + send.cursize * chan->rate;
|
2000-05-10 11:29:38 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
chan->cleartime += send.cursize * chan->rate;
|
|
|
|
if (ServerPaused ())
|
2000-05-10 11:29:38 +00:00
|
|
|
chan->cleartime = realtime;
|
|
|
|
|
2000-10-17 03:17:42 +00:00
|
|
|
if (showpackets->int_val)
|
2000-12-08 06:51:37 +00:00
|
|
|
Con_Printf ("--> s=%i(%i) a=%i(%i) %i\n", chan->outgoing_sequence,
|
|
|
|
send_reliable, chan->incoming_sequence,
|
|
|
|
chan->incoming_reliable_sequence, send.cursize);
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-02-09 02:53:09 +00:00
|
|
|
Netchan_Process
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2001-02-09 02:53:09 +00:00
|
|
|
called when the current net_message is from remote_address
|
|
|
|
modifies net_message so that it points to the packet payload
|
2000-05-10 11:29:38 +00:00
|
|
|
*/
|
2000-12-08 06:51:37 +00:00
|
|
|
qboolean
|
|
|
|
Netchan_Process (netchan_t *chan)
|
2000-05-10 11:29:38 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
unsigned int sequence, sequence_ack;
|
|
|
|
unsigned int reliable_ack, reliable_message;
|
|
|
|
int qport;
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
if (is_server) {
|
|
|
|
if (!NET_CompareAdr (net_from, chan->remote_address))
|
2000-05-14 20:25:49 +00:00
|
|
|
return false;
|
2000-12-08 06:51:37 +00:00
|
|
|
} else {
|
|
|
|
if (!cls.demoplayback &&
|
|
|
|
!NET_CompareAdr (net_from, chan->remote_address)) return false;
|
2000-05-14 20:25:49 +00:00
|
|
|
}
|
2000-05-12 00:11:41 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
// get sequence numbers
|
2000-05-10 11:29:38 +00:00
|
|
|
MSG_BeginReading ();
|
|
|
|
sequence = MSG_ReadLong ();
|
|
|
|
sequence_ack = MSG_ReadLong ();
|
|
|
|
|
|
|
|
// read the qport if we are a server
|
2000-05-12 00:11:41 +00:00
|
|
|
if (is_server)
|
|
|
|
qport = MSG_ReadShort ();
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
reliable_message = sequence >> 31;
|
|
|
|
reliable_ack = sequence_ack >> 31;
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
sequence &= ~(1 << 31);
|
|
|
|
sequence_ack &= ~(1 << 31);
|
2000-05-10 11:29:38 +00:00
|
|
|
|
2000-10-17 03:17:42 +00:00
|
|
|
if (showpackets->int_val)
|
2000-12-08 06:51:37 +00:00
|
|
|
Con_Printf ("<-- s=%i(%i) a=%i(%i) %i\n", sequence, reliable_message,
|
|
|
|
sequence_ack, reliable_ack, net_message.cursize);
|
2000-05-10 11:29:38 +00:00
|
|
|
|
|
|
|
// get a rate estimation
|
|
|
|
#if 0
|
2000-12-08 06:51:37 +00:00
|
|
|
if (chan->outgoing_sequence - sequence_ack < MAX_LATENT) {
|
|
|
|
int i;
|
|
|
|
double time, rate;
|
|
|
|
|
2000-05-10 11:29:38 +00:00
|
|
|
i = sequence_ack & (MAX_LATENT - 1);
|
|
|
|
time = realtime - chan->outgoing_time[i];
|
2000-12-08 06:51:37 +00:00
|
|
|
time -= 0.1; // subtract 100 ms
|
|
|
|
if (time <= 0) { // gotta be a digital link for <100
|
|
|
|
// ms ping
|
|
|
|
if (chan->rate > 1.0 / 5000)
|
|
|
|
chan->rate = 1.0 / 5000;
|
|
|
|
} else {
|
|
|
|
if (chan->outgoing_size[i] < 512) { // only deal with small
|
|
|
|
// messages
|
|
|
|
rate = chan->outgoing_size[i] / time;
|
2000-05-10 11:29:38 +00:00
|
|
|
if (rate > 5000)
|
|
|
|
rate = 5000;
|
2000-12-08 06:51:37 +00:00
|
|
|
rate = 1.0 / rate;
|
2000-05-10 11:29:38 +00:00
|
|
|
if (chan->rate > rate)
|
|
|
|
chan->rate = rate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// discard stale or duplicated packets
|
|
|
|
//
|
2000-12-08 06:51:37 +00:00
|
|
|
if (sequence <= (unsigned int) chan->incoming_sequence) {
|
2000-10-17 03:17:42 +00:00
|
|
|
if (showdrop->int_val)
|
2000-12-08 06:51:37 +00:00
|
|
|
Con_Printf ("%s:Out of order packet %i at %i\n",
|
|
|
|
NET_AdrToString (chan->remote_address), sequence,
|
|
|
|
chan->incoming_sequence);
|
2000-05-10 11:29:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// dropped packets don't keep the message from being used
|
|
|
|
//
|
2000-12-08 06:51:37 +00:00
|
|
|
net_drop = sequence - (chan->incoming_sequence + 1);
|
|
|
|
if (net_drop > 0) {
|
2000-05-10 11:29:38 +00:00
|
|
|
chan->drop_count += 1;
|
|
|
|
|
2000-10-17 03:17:42 +00:00
|
|
|
if (showdrop->int_val)
|
2000-12-08 06:51:37 +00:00
|
|
|
Con_Printf ("%s:Dropped %i packets at %i\n",
|
|
|
|
NET_AdrToString (chan->remote_address),
|
|
|
|
sequence - (chan->incoming_sequence + 1)
|
|
|
|
, sequence);
|
2000-05-10 11:29:38 +00:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// if the current outgoing reliable message has been acknowledged
|
|
|
|
// clear the buffer to make way for the next
|
|
|
|
//
|
2000-12-08 06:51:37 +00:00
|
|
|
if (reliable_ack == (unsigned int) chan->reliable_sequence)
|
|
|
|
chan->reliable_length = 0; // it has been received
|
|
|
|
|
2000-05-10 11:29:38 +00:00
|
|
|
//
|
|
|
|
// if this message contains a reliable message, bump incoming_reliable_sequence
|
|
|
|
//
|
|
|
|
chan->incoming_sequence = sequence;
|
|
|
|
chan->incoming_acknowledged = sequence_ack;
|
|
|
|
chan->incoming_reliable_acknowledged = reliable_ack;
|
|
|
|
if (reliable_message)
|
|
|
|
chan->incoming_reliable_sequence ^= 1;
|
|
|
|
|
|
|
|
//
|
|
|
|
// the message can now be read from the current message pointer
|
|
|
|
// update statistics counters
|
|
|
|
//
|
2000-12-08 06:51:37 +00:00
|
|
|
chan->frame_latency = chan->frame_latency * OLD_AVG
|
|
|
|
+ (chan->outgoing_sequence - sequence_ack) * (1.0 - OLD_AVG);
|
|
|
|
chan->frame_rate = chan->frame_rate * OLD_AVG
|
|
|
|
+ (realtime - chan->last_received) * (1.0 - OLD_AVG);
|
2000-05-10 11:29:38 +00:00
|
|
|
chan->good_count += 1;
|
|
|
|
|
|
|
|
chan->last_received = realtime;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2001-05-29 03:45:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Netchan_AckPacket
|
|
|
|
|
|
|
|
Send a nop packet so that any unacked reliable packets get acked.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Netchan_AckPacket (netchan_t *chan)
|
|
|
|
{
|
|
|
|
Netchan_Transmit (chan, 1, (byte*)"\001");
|
|
|
|
}
|