Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions engine/net_chan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ extern int NET_ReceiveStream( socket_handle nSock, char * buf, int len, int fla

#define BYTES2FRAGMENTS(i) (((i)+FRAGMENT_SIZE-1)/FRAGMENT_SIZE)

#define FLIPBIT(v,b) if (v&b) v &= ~b; else v |= b;

// We only need to checksum packets on the PC and only when we're actually sending them over the network.
static bool ShouldChecksumPackets()
{
Expand Down Expand Up @@ -105,9 +103,7 @@ void CNetChan::Clear()
{
if ( m_SubChannels[i].state == SUBCHANNEL_TOSEND )
{
int bit = 1<<i; // flip bit back since data was send yet

FLIPBIT(m_nOutReliableState, bit);
m_nOutReliableState.Flip(i);

m_SubChannels[i].Free();
}
Expand Down Expand Up @@ -438,8 +434,8 @@ CNetChan::CNetChan()
m_nOutSequenceNr = 1; // otherwise it looks like a
m_nInSequenceNr = 0;
m_nOutSequenceNrAck = 0;
m_nOutReliableState = 0; // our current reliable state
m_nInReliableState = 0; // last remote reliable state
m_nOutReliableState.ClearAll(); // our current reliable state
m_nInReliableState.ClearAll(); // last remote reliable state
// m_nLostPackets = 0;

m_ChallengeNr = 0;
Expand Down Expand Up @@ -525,8 +521,8 @@ void CNetChan::Setup(intp sock, netadr_t *adr, const char * name, INetChannelHan
m_nOutSequenceNr = 1; // otherwise it looks like a
m_nInSequenceNr = 0;
m_nOutSequenceNrAck = 0;
m_nOutReliableState = 0; // our current reliable state
m_nInReliableState = 0; // last remote reliable state
m_nOutReliableState.ClearAll(); // our current reliable state
m_nInReliableState.ClearAll(); // last remote reliable state
m_nChokedPackets = 0;
m_fClearTime = 0.0;

Expand Down Expand Up @@ -1182,7 +1178,7 @@ bool CNetChan::SendSubChannelData( bf_write &buf )
return false; // no data to send in any subchannel

// first write subchannel index
buf.WriteUBitLong( i, 3 );
buf.WriteUBitLong( i, SUBCHANNEL_BITS );

// write fragemnts for both streams
for ( i=0; i<MAX_STREAMS; i++ )
Expand Down Expand Up @@ -1493,9 +1489,7 @@ void CNetChan::UpdateSubChannels()
if ( bSendData )
{
// flip channel bit
int bit = 1<<freeSubChan->index;

FLIPBIT(m_nOutReliableState, bit);
m_nOutReliableState.Flip(freeSubChan->index);

freeSubChan->state = SUBCHANNEL_TOSEND;
freeSubChan->sendSeqNr = 0;
Expand Down Expand Up @@ -1631,7 +1625,7 @@ int CNetChan::SendDatagram(bf_write *datagram)
// Note, this only matters on the PC
int nCheckSumStart = send.GetNumBytesWritten();

send.WriteByte ( m_nInReliableState );
send.WriteBits( m_nInReliableState.Base(), MAX_SUBCHANNELS ); // RaphaelIT7: Originally used WriteByte- was changed to support easy changing of MAX_SUBCHANNELS

if ( m_nChokedPackets > 0 )
{
Expand Down Expand Up @@ -2240,7 +2234,8 @@ int CNetChan::ProcessPacketHeader( netpacket_t * packet )
}
}

int relState = packet->message.ReadByte(); // reliable state of 8 subchannels
CBitVec<MAX_SUBCHANNELS> relState; // RaphaelIT7: Added to allow easy changing of MAX_SUBCHANNELS
packet->message.ReadBits( relState.Base(), MAX_SUBCHANNELS ); // reliable state of all subchannels (RaphaelIT7: Previously ued ReadByte though was changed)
int nChoked = 0; // read later if choked flag is set
int i,j;

Expand Down Expand Up @@ -2309,14 +2304,12 @@ int CNetChan::ProcessPacketHeader( netpacket_t * packet )

for ( i = 0; i<MAX_SUBCHANNELS; i++ )
{
int bitmask = (1<<i);

// data of channel i has been acknowledged
subChannel_s * subchan = &m_SubChannels[i];

Assert( subchan->index == i);
Assert( subchan->index == i );

if ( (m_nOutReliableState & bitmask) == (relState & bitmask) )
if ( m_nOutReliableState.CompareBit(relState, i) )
{
if ( subchan->state == SUBCHANNEL_DIRTY )
{
Expand Down Expand Up @@ -2366,9 +2359,7 @@ int CNetChan::ProcessPacketHeader( netpacket_t * packet )
else if ( subchan->state == SUBCHANNEL_DIRTY )
{
// remote host lost dirty channel data, flip bit back
int bit = 1<<subchan->index; // flip bit back since data was send yet

FLIPBIT(m_nOutReliableState, bit);
m_nOutReliableState.Flip(subchan->index);

subchan->Free();
}
Expand Down Expand Up @@ -2447,9 +2438,8 @@ void CNetChan::ProcessPacket( netpacket_t * packet, bool bHasHeader )

if ( flags & PACKET_FLAG_RELIABLE )
{
int i, bit = 1<<msg.ReadUBitLong( 3 );

for ( i=0; i<MAX_STREAMS; i++ )
int subChannelIndex = msg.ReadUBitLong( SUBCHANNEL_BITS );
for ( int i=0; i<MAX_STREAMS; i++ )
{
if ( msg.ReadOneBit() != 0 )
{
Expand All @@ -2459,9 +2449,9 @@ void CNetChan::ProcessPacket( netpacket_t * packet, bool bHasHeader )
}

// flip subChannel bit to signal successful receiving
FLIPBIT(m_nInReliableState, bit);
m_nInReliableState.Flip(subChannelIndex);

for ( i=0; i<MAX_STREAMS; i++ )
for ( int i=0; i<MAX_STREAMS; i++ )
{
if ( !CheckReceivingList( i ) )
return; // error while processing
Expand Down
8 changes: 5 additions & 3 deletions engine/net_chan.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "tier1/utlbuffer.h"
#include "const.h"
#include "inetchannel.h"
#include "bitvec.h"

// How fast to converge flow estimates
#define FLOW_AVG ( 3.0F / 4.0F )
Expand All @@ -29,7 +30,8 @@

#define NET_FRAMES_BACKUP 64 // must be power of 2
#define NET_FRAMES_MASK (NET_FRAMES_BACKUP-1)
#define MAX_SUBCHANNELS 8 // we have 8 alternative send&wait bits
constexpr int MAX_SUBCHANNELS = 8; // we have x alternative send&wait channels
constexpr int SUBCHANNEL_BITS = RequiredBits(MAX_SUBCHANNELS);

#define SUBCHANNEL_FREE 0 // subchannel is free to use
#define SUBCHANNEL_TOSEND 1 // subchannel has data, but not send yet
Expand Down Expand Up @@ -266,9 +268,9 @@ class CNetChan : public INetChannel
int m_nOutSequenceNrAck;

// state of outgoing reliable data (0/1) flip flop used for loss detection
int m_nOutReliableState;
CBitVec<MAX_SUBCHANNELS> m_nOutReliableState; // RaphaelIT7: Changed to use a CBitVec to allow easy changing of MAX_SUBCHANNELS
// state of incoming reliable data
int m_nInReliableState;
CBitVec<MAX_SUBCHANNELS> m_nInReliableState; // RaphaelIT7: Changed to use a CBitVec to allow easy changing of MAX_SUBCHANNELS

int m_nChokedPackets; //number of choked packets

Expand Down
21 changes: 20 additions & 1 deletion public/bitvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class CBitVecT : public BASE_OPS
void CopyTo(CBitVecT *out) const;
void Copy( const CBitVecT<BASE_OPS> &other, int nBits=-1 );
bool Compare( const CBitVecT<BASE_OPS> &other, int nBits=-1 ) const;
bool CompareBit( const CBitVecT<BASE_OPS> &other, int nBit ) const;

bool IsAllClear(void) const; // Are all bits zero?
bool IsAllSet(void) const; // Are all bits one?
Expand All @@ -254,7 +255,8 @@ class CBitVecT : public BASE_OPS
bool IsBitSet( int bitNum ) const;
void Set( int bitNum );
void Set( int bitNum, bool bNewVal );
void Clear(int bitNum);
void Clear( int bitNum );
void Flip( int bitNum );

bool TestAndSet(int bitNum);

Expand Down Expand Up @@ -664,6 +666,14 @@ inline void CBitVecT<BASE_OPS>::Clear(int bitNum)
*pInt &= ~BitVec_Bit( bitNum );
}

template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Flip(int bitNum)
{
Assert( bitNum >= 0 && bitNum < this->GetNumBits() );
uint32 *pInt = this->Base() + BitVec_Int( bitNum );
*pInt ^= BitVec_Bit(bitNum);
}

//-----------------------------------------------------------------------------

template <class BASE_OPS>
Expand Down Expand Up @@ -917,6 +927,15 @@ inline bool CBitVecT<BASE_OPS>::Compare( const CBitVecT<BASE_OPS> &other, int nB
return ( memcmp( this->Base(), other.Base(), nBytes ) == 0 );
}

template <class BASE_OPS>
inline bool CBitVecT<BASE_OPS>::CompareBit(const CBitVecT<BASE_OPS> &other, int bitNum) const
{
Assert(bitNum >= 0 && bitNum < this->GetNumBits());

return (this->Base()[BitVec_Int(bitNum)] & BitVec_Bit(bitNum)) ==
(other.Base()[BitVec_Int(bitNum)] & BitVec_Bit(bitNum));
}

//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline uint32 CBitVecT<BASE_OPS>::GetDWord(int i) const
Expand Down
10 changes: 10 additions & 0 deletions public/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
#pragma once
#endif

// RaphaelIT7: log function for constexpr numbers - mainly used to figure out how many bits for networking are needed for a limit
constexpr int RequiredBits(int v)
{
int bits = 0;
while ((1 << bits) < v)
++bits;

return bits;
}

// the command line param that tells the engine to use steam
#define STEAM_PARM "-steam"
// the command line param to tell dedicated server to restart
Expand Down
Loading