This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/common/net_chan.c

357 lines
11 KiB
C
Raw Normal View History

2008-07-11 22:00:00 +02:00
//=======================================================================
// Copyright XashXT Group 2008 <20>
// net_chan.c - network channel
//=======================================================================
2007-06-21 22:00:00 +02:00
2008-06-09 22:00:00 +02:00
#include "common.h"
2008-01-12 22:00:00 +01:00
#include "mathlib.h"
2008-07-23 22:00:00 +02:00
#include "byteorder.h"
2010-08-04 22:00:00 +02:00
#include "protocol.h"
#include "net_encode.h"
2007-06-21 22:00:00 +02:00
/*
2008-07-12 22:00:00 +02:00
packet header ( size in bits )
2007-06-21 22:00:00 +02:00
-------------
31 sequence
1 does this message contain a reliable payload
31 acknowledge sequence
1 acknowledge receipt of even/odd message
16 qport
2008-07-11 22:00:00 +02:00
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
2010-08-04 22:00:00 +02:00
BF_Write* (&netchan->message, <data>).
2008-07-11 22:00:00 +02:00
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.
2010-06-20 22:00:00 +02:00
Reliable messages are allways placed first in a packet, then the unreliable
2008-07-11 22:00:00 +02:00
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.
2007-06-21 22:00:00 +02:00
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.
2008-07-11 22:00:00 +02:00
If there is no information that needs to be transfered on a given frame,
such as during the connection stage while waiting for the client to load,
then a packet only needs to be delivered if there is something in the
unacknowledged reliable
2007-06-21 22:00:00 +02:00
*/
2008-07-11 22:00:00 +02:00
cvar_t *net_showpackets;
cvar_t *net_showdrop;
2010-07-26 22:00:00 +02:00
cvar_t *net_speeds;
2008-07-11 22:00:00 +02:00
cvar_t *net_qport;
2009-06-22 22:00:00 +02:00
netadr_t net_from;
2010-08-06 22:00:00 +02:00
sizebuf_t net_message;
2009-06-22 22:00:00 +02:00
byte net_message_buffer[MAX_MSGLEN];
2008-07-11 22:00:00 +02:00
2007-06-21 22:00:00 +02:00
/*
===============
2008-07-06 22:00:00 +02:00
Netchan_Init
===============
2007-06-21 22:00:00 +02:00
*/
2008-07-12 22:00:00 +02:00
void Netchan_Init( void )
2007-06-21 22:00:00 +02:00
{
2010-07-26 22:00:00 +02:00
int port;
2008-07-11 22:00:00 +02:00
2008-07-06 22:00:00 +02:00
// pick a port value that should be nice and random
2010-03-06 22:00:00 +01:00
port = Com_RandomLong( 1, 65535 );
2007-06-21 22:00:00 +02:00
2010-07-26 22:00:00 +02:00
net_showpackets = Cvar_Get ("net_showpackets", "0", 0, "show network packets" );
net_showdrop = Cvar_Get ("net_showdrop", "0", 0, "show packets that are dropped" );
net_speeds = Cvar_Get ("net_speeds", "0", CVAR_ARCHIVE, "show network packets" );
net_qport = Cvar_Get ("net_qport", va( "%i", port ), CVAR_INIT, "current quake netport" );
2010-08-04 22:00:00 +02:00
Huff_Init (); // initialize huffman compression
BF_InitMasks (); // initialize bit-masks
2007-06-21 22:00:00 +02:00
}
/*
==============
Netchan_Setup
called to open a channel to a remote system
==============
*/
2008-07-12 22:00:00 +02:00
void Netchan_Setup( netsrc_t sock, netchan_t *chan, netadr_t adr, int qport )
2007-06-21 22:00:00 +02:00
{
2009-06-22 22:00:00 +02:00
Mem_Set( chan, 0, sizeof( *chan ));
2007-06-21 22:00:00 +02:00
chan->sock = sock;
chan->remote_address = adr;
chan->qport = qport;
2010-07-23 22:00:00 +02:00
chan->last_received = Sys_Milliseconds ();
2007-06-21 22:00:00 +02:00
chan->incoming_sequence = 0;
chan->outgoing_sequence = 1;
2010-08-05 22:00:00 +02:00
chan->compress = true;
2008-07-10 22:00:00 +02:00
2010-08-04 22:00:00 +02:00
BF_Init( &chan->message, "NetData", chan->message_buf, sizeof( chan->message_buf ));
2008-07-11 22:00:00 +02:00
}
/*
===============
Netchan_OutOfBand
Sends an out-of-band datagram
================
*/
2008-07-12 22:00:00 +02:00
void Netchan_OutOfBand( int net_socket, netadr_t adr, int length, byte *data )
2008-07-11 22:00:00 +02:00
{
2010-08-06 22:00:00 +02:00
sizebuf_t send;
2008-07-12 22:00:00 +02:00
byte send_buf[MAX_MSGLEN];
2008-07-11 22:00:00 +02:00
// write the packet header
2010-08-04 22:00:00 +02:00
BF_Init( &send, "SequencePacket", send_buf, sizeof( send_buf ));
2008-07-11 22:00:00 +02:00
2010-08-04 22:00:00 +02:00
BF_WriteLong( &send, -1 ); // -1 sequence means out of band
BF_WriteBytes( &send, data, length );
2008-07-11 22:00:00 +02:00
// send the datagram
2010-08-04 22:00:00 +02:00
NET_SendPacket( net_socket, BF_GetNumBytesWritten( &send ), BF_GetData( &send ), adr );
2008-07-11 22:00:00 +02:00
}
/*
===============
Netchan_OutOfBandPrint
Sends a text message in an out-of-band datagram
================
*/
2008-07-12 22:00:00 +02:00
void Netchan_OutOfBandPrint( int net_socket, netadr_t adr, char *format, ... )
2008-07-11 22:00:00 +02:00
{
2008-07-12 22:00:00 +02:00
va_list argptr;
2010-08-04 22:00:00 +02:00
char string[MAX_SYSPATH];
2008-07-12 22:00:00 +02:00
va_start( argptr, format );
2008-07-15 22:00:00 +02:00
com.vsprintf( string, format, argptr );
2008-07-12 22:00:00 +02:00
va_end( argptr );
2008-07-11 22:00:00 +02:00
2008-07-15 22:00:00 +02:00
Netchan_OutOfBand( net_socket, adr, com.strlen( string ), string );
2007-06-21 22:00:00 +02:00
}
2008-07-12 22:00:00 +02:00
bool Netchan_NeedReliable( netchan_t *chan )
2008-07-10 22:00:00 +02:00
{
2008-07-12 22:00:00 +02:00
bool send_reliable = false;
2007-06-21 22:00:00 +02:00
2008-07-10 22:00:00 +02:00
// if the remote side dropped the last reliable message, resend it
send_reliable = false;
2008-07-06 22:00:00 +02:00
2008-07-12 22:00:00 +02:00
if( chan->incoming_acknowledged > chan->last_reliable_sequence && chan->incoming_reliable_acknowledged != chan->reliable_sequence )
2008-07-10 22:00:00 +02:00
send_reliable = true;
2008-07-06 22:00:00 +02:00
2008-07-10 22:00:00 +02:00
// if the reliable transmit buffer is empty, copy the current message out
2010-08-04 22:00:00 +02:00
if( !chan->reliable_length && BF_GetNumBitsWritten( &chan->message ))
2008-07-10 22:00:00 +02:00
send_reliable = true;
2010-08-04 22:00:00 +02:00
if( !chan->reliable_length && BF_GetNumBitsWritten( &chan->message ))
2009-06-22 22:00:00 +02:00
{
2010-08-04 22:00:00 +02:00
Mem_Copy( chan->reliable_buf, chan->message_buf, BF_GetNumBytesWritten( &chan->message ));
chan->reliable_length = BF_GetNumBitsWritten( &chan->message );
BF_Clear( &chan->message );
2009-06-22 22:00:00 +02:00
chan->reliable_sequence ^= 1;
}
2008-07-10 22:00:00 +02:00
return send_reliable;
2007-06-21 22:00:00 +02:00
}
/*
===============
Netchan_Transmit
2008-07-11 22:00:00 +02:00
tries to send an unreliable message to a connection, and handles the
transmition / retransmition of the reliable messages.
A 0 length will still generate a packet and deal with the reliable messages.
2007-06-21 22:00:00 +02:00
================
*/
2010-08-04 22:00:00 +02:00
void Netchan_Transmit( netchan_t *chan, int lengthInBytes, byte *data )
2007-06-21 22:00:00 +02:00
{
2010-08-04 22:00:00 +02:00
Netchan_TransmitBits( chan, lengthInBytes << 3, data );
}
void Netchan_TransmitBits( netchan_t *chan, int lengthInBits, byte *data )
{
2010-08-06 22:00:00 +02:00
sizebuf_t send;
2008-07-12 22:00:00 +02:00
static bool overflow = false;
byte send_buf[MAX_MSGLEN];
bool send_reliable;
2009-09-15 22:00:00 +02:00
size_t size1, size2;
2010-07-02 22:00:00 +02:00
uint w1, w2;
2007-06-21 22:00:00 +02:00
2008-07-10 22:00:00 +02:00
// check for message overflow
2010-08-04 22:00:00 +02:00
if( BF_CheckOverflow( &chan->message ))
2007-06-21 22:00:00 +02:00
{
2008-07-11 22:00:00 +02:00
chan->fatal_error = true;
2008-07-12 22:00:00 +02:00
MsgDev( D_ERROR, "%s:outgoing message overflow\n", NET_AdrToString( chan->remote_address ));
2007-06-21 22:00:00 +02:00
return;
}
2008-07-15 22:00:00 +02:00
2008-07-12 22:00:00 +02:00
send_reliable = Netchan_NeedReliable( chan );
2008-07-10 22:00:00 +02:00
2008-07-06 22:00:00 +02:00
// write the packet header
2010-08-04 22:00:00 +02:00
BF_Init( &send, "NetSend", send_buf, sizeof( send_buf ));
2008-07-10 22:00:00 +02:00
2008-07-12 22:00:00 +02:00
w1 = (chan->outgoing_sequence & ~(1<<31)) | (send_reliable<<31);
w2 = (chan->incoming_sequence & ~(1<<31)) | (chan->incoming_reliable_sequence<<31);
2008-07-10 22:00:00 +02:00
2008-07-06 22:00:00 +02:00
chan->outgoing_sequence++;
2010-07-23 22:00:00 +02:00
chan->last_sent = Sys_Milliseconds ();
2008-07-10 22:00:00 +02:00
2010-08-04 22:00:00 +02:00
BF_WriteLong( &send, w1 );
BF_WriteLong( &send, w2 );
2008-07-06 22:00:00 +02:00
// send the qport if we are a client
2010-08-04 22:00:00 +02:00
if( chan->sock == NS_CLIENT ) BF_WriteWord( &send, Cvar_VariableValue( "net_qport" ));
2008-07-06 22:00:00 +02:00
2008-07-10 22:00:00 +02:00
// copy the reliable message to the packet first
2008-07-12 22:00:00 +02:00
if( send_reliable )
2008-07-10 22:00:00 +02:00
{
2010-08-04 22:00:00 +02:00
BF_WriteBits( &send, chan->reliable_buf, chan->reliable_length );
2008-07-10 22:00:00 +02:00
chan->last_reliable_sequence = chan->outgoing_sequence;
}
2008-07-12 22:00:00 +02:00
2008-07-10 22:00:00 +02:00
// add the unreliable part if space is available
2010-08-04 22:00:00 +02:00
if( BF_GetNumBitsLeft( &send ) >= lengthInBits )
2008-07-12 22:00:00 +02:00
{
2010-08-04 22:00:00 +02:00
BF_WriteBits( &send, data, lengthInBits );
2008-07-12 22:00:00 +02:00
overflow = false;
}
else
{
// don't flood with multiple messages
if( !overflow ) MsgDev( D_WARN, "Netchan_Transmit: unreliable msg overflow\n" );
overflow = true;
}
2009-06-22 22:00:00 +02:00
2010-08-04 22:00:00 +02:00
size1 = BF_GetNumBytesWritten( &send );
2010-08-05 22:00:00 +02:00
if( chan->compress ) Huff_CompressPacket( &send, ( chan->sock == NS_CLIENT ) ? 10 : 8 );
2010-08-04 22:00:00 +02:00
size2 = BF_GetNumBytesWritten( &send );
2009-06-22 22:00:00 +02:00
2010-07-26 22:00:00 +02:00
chan->total_sended += size2;
chan->total_sended_uncompressed += size1;
2008-07-10 22:00:00 +02:00
// send the datagram
2010-08-04 22:00:00 +02:00
NET_SendPacket( chan->sock, BF_GetNumBytesWritten( &send ), BF_GetData( &send ), chan->remote_address );
2008-07-06 22:00:00 +02:00
2010-07-26 22:00:00 +02:00
if( net_showpackets->integer == 1 )
2009-06-22 22:00:00 +02:00
{
2009-09-15 22:00:00 +02:00
const char *s1, *s2;
2009-06-22 22:00:00 +02:00
if( chan->sock == NS_CLIENT ) MsgDev( D_INFO, "CL " );
else if( chan->sock == NS_SERVER ) MsgDev( D_INFO, "SV " );
2009-09-15 22:00:00 +02:00
s1 = memprint( size1 ); // uncompressed size
s2 = memprint( size2 ); // compressed size
MsgDev( D_INFO, "Netchan_Transmit: %s[%s] : %sreliable\n", s1, s2, send_reliable ? "" : "un" );
2009-06-22 22:00:00 +02:00
}
2008-07-06 22:00:00 +02:00
}
/*
=================
Netchan_Process
2008-07-12 22:00:00 +02:00
called when the current net message is from remote_address
modifies net message so that it points to the packet payload
2008-07-06 22:00:00 +02:00
=================
*/
2010-08-06 22:00:00 +02:00
bool Netchan_Process( netchan_t *chan, sizebuf_t *msg )
2008-07-06 22:00:00 +02:00
{
2008-07-10 22:00:00 +02:00
uint sequence, sequence_ack;
2008-07-12 22:00:00 +02:00
uint reliable_ack, recv_reliable;
2009-09-15 22:00:00 +02:00
size_t size1, size2;
2008-07-06 22:00:00 +02:00
int qport;
// get sequence numbers
2010-08-04 22:00:00 +02:00
BF_Clear( msg );
sequence = BF_ReadLong( msg );
sequence_ack = BF_ReadLong( msg );
2008-07-06 22:00:00 +02:00
// read the qport if we are a server
2008-07-12 22:00:00 +02:00
if( chan->sock == NS_SERVER )
2010-08-04 22:00:00 +02:00
qport = BF_ReadShort( msg );
2008-07-06 22:00:00 +02:00
2008-07-12 22:00:00 +02:00
recv_reliable = sequence>>31;
reliable_ack = sequence_ack>>31;
2008-07-10 22:00:00 +02:00
sequence &= ~(1<<31);
sequence_ack &= ~(1<<31);
2007-06-21 22:00:00 +02:00
2008-07-10 22:00:00 +02:00
// discard stale or duplicated packets
2008-07-12 22:00:00 +02:00
if( sequence <= chan->incoming_sequence )
2008-07-06 22:00:00 +02:00
{
2008-07-12 22:00:00 +02:00
if( net_showdrop->value )
MsgDev( D_WARN, "%s:Out of order packet\n", NET_AdrToString( chan->remote_address ));
2008-07-06 22:00:00 +02:00
return false;
}
// dropped packets don't keep the message from being used
2008-07-12 22:00:00 +02:00
chan->dropped = sequence - (chan->incoming_sequence + 1);
if( chan->dropped > 0 )
2008-07-06 22:00:00 +02:00
{
2009-09-14 22:00:00 +02:00
chan->drop_count += 1;
2008-07-12 22:00:00 +02:00
if( net_showdrop->value )
MsgDev( D_WARN, "%s:dropped %i packets\n", NET_AdrToString( chan->remote_address ), chan->dropped );
2008-07-06 22:00:00 +02:00
}
2008-07-12 22:00:00 +02:00
// if the current outgoing reliable message has been acknowledged
// clear the buffer to make way for the next
if( reliable_ack == chan->reliable_sequence )
chan->reliable_length = 0; // it has been received
2008-07-10 22:00:00 +02: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;
2008-07-12 22:00:00 +02:00
if( recv_reliable ) chan->incoming_reliable_sequence ^= 1;
2010-08-04 22:00:00 +02:00
size1 = BF_GetMaxBytes( msg );
2008-07-16 22:00:00 +02:00
if( chan->compress ) Huff_DecompressPacket( msg, ( chan->sock == NS_SERVER) ? 10 : 8 );
2010-08-04 22:00:00 +02:00
size2 = BF_GetMaxBytes( msg );
2009-09-15 22:00:00 +02:00
2010-07-26 22:00:00 +02:00
chan->total_received += size1;
chan->total_received_uncompressed += size2;
if( net_showpackets->integer == 2 )
2009-09-15 22:00:00 +02:00
{
const char *s1, *s2;
if( chan->sock == NS_CLIENT ) MsgDev( D_INFO, "CL " );
else if( chan->sock == NS_SERVER ) MsgDev( D_INFO, "SV " );
s1 = memprint( size2 ); // compressed size
s2 = memprint( size1 ); // uncompressed size
MsgDev( D_INFO, "Netchan_Process: %s[%s] : %sreliable\n", s1, s2, recv_reliable ? "" : "un" );
}
2009-06-22 22:00:00 +02:00
chan->good_count += 1;
2008-07-06 22:00:00 +02:00
2009-06-22 22:00:00 +02:00
// the message can now be read from the current message pointer
2010-07-23 22:00:00 +02:00
chan->last_received = Sys_Milliseconds ();
2007-06-21 22:00:00 +02:00
2008-07-06 22:00:00 +02:00
return true;
}