123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #ifndef __PACKET_PROCESSOR_H__
- #define __PACKET_PROCESSOR_H__
- class idPacketProcessor {
- public:
- static const int RETURN_TYPE_NONE = 0;
- static const int RETURN_TYPE_OOB = 1;
- static const int RETURN_TYPE_INBAND = 2;
-
- typedef uint16 sessionId_t;
- static const int NUM_LOBBY_TYPE_BITS = 2;
- static const int LOBBY_TYPE_MASK = ( 1 << NUM_LOBBY_TYPE_BITS ) - 1;
- static const sessionId_t SESSION_ID_INVALID = 0;
- static const sessionId_t SESSION_ID_CONNECTIONLESS_PARTY = 1;
- static const sessionId_t SESSION_ID_CONNECTIONLESS_GAME = 2;
- static const sessionId_t SESSION_ID_CONNECTIONLESS_GAME_STATE = 3;
-
- static const int BANDWIDTH_AVERAGE_PERIOD = 250;
- idPacketProcessor() {
- Reset();
- }
- void Reset() {
- msgWritePos = 0;
- fragmentSequence = 0;
- droppedFrags = 0;
- fragmentedSend = false;
- reliable = idDataQueue< MAX_RELIABLE_QUEUE, MAX_MSG_SIZE >();
- reliableSequenceSend = 1;
- reliableSequenceRecv = 0;
- numReliable = 0;
-
- queuedReliableAck = -1;
- unsentMsg = idBitMsg();
- lastSendTime = 0;
- outgoingRateTime = 0;
- outgoingRateBytes = 0.0f;
- incomingRateTime = 0;
- incomingRateBytes = 0.0f;
-
- outgoingBytes = 0;
- incomingBytes = 0;
- currentOutgoingRate = 0;
- lastOutgoingRateTime = 0;
- lastOutgoingBytes = 0;
- currentIncomingRate = 0;
- lastIncomingRateTime = 0;
- lastIncomingBytes = 0;
- fragmentAccumulator = 0;
-
- }
- static const int MAX_MSG_SIZE = 8000;
- static const int MAX_FINAL_PACKET_SIZE = 1200;
- static const int MAX_RELIABLE_QUEUE = 64;
-
-
- static const int MAX_PACKET_SIZE = MAX_FINAL_PACKET_SIZE - 6 - 2;
- static const int MAX_OOB_MSG_SIZE = MAX_PACKET_SIZE - 1;
-
- private:
- void QueueReliableAck( int lastReliable );
- int FinalizeRead( idBitMsg & inMsg, idBitMsg & outMsg, int & userValue );
- public:
- bool CanSendMoreData() const;
- void UpdateOutgoingRate( const int time, const int size );
- void UpdateIncomingRate( const int time, const int size );
-
- void RefreshRates( int time ) { UpdateOutgoingRate( time, 0 ); UpdateIncomingRate( time, 0 ); }
-
- bool QueueReliableMessage( byte type, const byte * data, int dataLen );
-
- bool ProcessOutgoing( const int time, const idBitMsg & msg, bool isOOB, int userData );
-
- bool GetSendFragment( const int time, sessionId_t sessionID, idBitMsg & outMsg );
-
- int ProcessIncoming( int time, sessionId_t expectedSessionID, idBitMsg & msg, idBitMsg & out, int & userData, const int peerNum );
-
- bool HasMoreFragments() const { return ( unsentMsg.GetRemainingData() > 0 ); }
-
-
- int NumQueuedReliables() { return reliable.Num(); }
-
- int NeedToSendReliableAck() { return queuedReliableAck >= 0 ? true : false; }
-
-
-
- static bool ProcessConnectionlessOutgoing( idBitMsg & msg, idBitMsg & out, int lobbyType, int userData );
- static bool ProcessConnectionlessIncoming( idBitMsg & msg, idBitMsg & out, int & userData );
-
-
- static sessionId_t GetSessionID( idBitMsg & msg );
- int GetNumReliables() const { return numReliable; }
- const byte * GetReliable( int i ) const { return reliableMsgPtrs[ i ]; }
- int GetReliableSize( int i ) const { return reliableMsgSize[ i ]; }
-
- void SetLastSendTime( int i ) { lastSendTime = i; }
- int GetLastSendTime() const { return lastSendTime; }
- float GetOutgoingRateBytes() const { return outgoingRateBytes; }
- int GetOutgoingBytes() const { return outgoingBytes; }
- float GetIncomingRateBytes() const { return incomingRateBytes; }
- int GetIncomingBytes() const { return incomingBytes; }
-
- int GetOutgoingRate2() const { return currentOutgoingRate; }
- int GetIncomingRate2() const { return currentIncomingRate; }
-
- bool TickFragmentAccumulator() { if ( fragmentAccumulator > 0 ) { fragmentAccumulator--; return true; } return false; }
- int GetReliableDataSize() const { return reliable.GetDataLength(); }
- void VerifyEmptyReliableQueue( byte keepMsgBelowThis, byte replaceWithThisMsg );
- private:
-
-
- static const int PACKET_TYPE_INBAND = 0;
- static const int PACKET_TYPE_OOB = 1;
- static const int PACKET_TYPE_RELIABLE_ACK = 2;
- static const int PACKET_TYPE_FRAGMENTED = 3;
-
- static const int FRAGMENT_START = 0;
- static const int FRAGMENT_MIDDLE = 1;
- static const int FRAGMENT_END = 2;
-
- class idOuterPacketHeader {
- public:
- idOuterPacketHeader() : sessionID( SESSION_ID_INVALID ) {}
- idOuterPacketHeader( sessionId_t sessionID_ ) : sessionID( sessionID_ ) {}
- void WriteToMsg( idBitMsg & msg ) {
- msg.WriteUShort( sessionID );
- }
- void ReadFromMsg( idBitMsg & msg ) {
- sessionID = msg.ReadUShort();
- }
- sessionId_t GetSessionID() { return sessionID; }
- private:
- sessionId_t sessionID;
- };
- class idInnerPacketHeader {
- public:
- idInnerPacketHeader() : type( 0 ), userData( 0 ) {}
- idInnerPacketHeader( int inType, int inData ) : type( inType ), userData( inData ) {}
- void WriteToMsg( idBitMsg & msg ) {
- msg.WriteBits( type, 2 );
- msg.WriteBits( userData, 6 );
- }
-
- void ReadFromMsg( idBitMsg & msg ) {
- type = msg.ReadBits( 2 );
- userData = msg.ReadBits( 6 );
- }
- int Type() { return type; }
- int Value() { return userData; }
-
- private:
- int type;
- int userData;
- };
- byte msgBuffer[ MAX_MSG_SIZE ];
- int msgWritePos;
- int fragmentSequence;
- int droppedFrags;
- bool fragmentedSend;
- idDataQueue< MAX_RELIABLE_QUEUE, MAX_MSG_SIZE > reliable;
-
- int reliableSequenceSend;
- int reliableSequenceRecv;
-
- int numReliable;
- byte reliableBuffer[ MAX_MSG_SIZE ];
- const byte * reliableMsgPtrs[ MAX_RELIABLE_QUEUE ];
- int reliableMsgSize[ MAX_RELIABLE_QUEUE ];
-
- int queuedReliableAck;
-
- idBitMsg unsentMsg;
- byte unsentBuffer[ MAX_MSG_SIZE ];
- int lastSendTime;
-
- int outgoingRateTime;
- float outgoingRateBytes;
- int incomingRateTime;
- float incomingRateBytes;
- int outgoingBytes;
- int incomingBytes;
- int currentOutgoingRate;
- int lastOutgoingRateTime;
- int lastOutgoingBytes;
- int currentIncomingRate;
- int lastIncomingRateTime;
- int lastIncomingBytes;
- int fragmentAccumulator;
- };
- #endif /* !__PACKET_PROCESSOR_H__ */
|