doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MsgChannel.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __MSGCHANNEL_H__
30 #define __MSGCHANNEL_H__
31 
32 
33 /*
34 ===============================================================================
35 
36  Network channel.
37 
38  Handles message fragmentation and out of order / duplicate suppression.
39  Unreliable messages are not garrenteed to arrive but when they do, they
40  arrive in order and without duplicates. Reliable messages always arrive,
41  and they also arrive in order without duplicates. Reliable messages piggy
42  back on unreliable messages. As such an unreliable message stream is
43  required for the reliable messages to be delivered.
44 
45 ===============================================================================
46 */
47 
48 #define MAX_MESSAGE_SIZE 16384 // max length of a message, which may
49  // be fragmented into multiple packets
50 #define CONNECTIONLESS_MESSAGE_ID -1 // id for connectionless messages
51 #define CONNECTIONLESS_MESSAGE_ID_MASK 0x7FFF // value to mask away connectionless message id
52 
53 #define MAX_MSG_QUEUE_SIZE 16384 // must be a power of 2
54 
55 
56 class idMsgQueue {
57 public:
58  idMsgQueue();
59 
60  void Init( int sequence );
61 
62  bool Add( const byte *data, const int size );
63  bool Get( byte *data, int &size );
64  int GetTotalSize( void ) const;
65  int GetSpaceLeft( void ) const;
66  int GetFirst( void ) const { return first; }
67  int GetLast( void ) const { return last; }
68  void CopyToBuffer( byte *buf ) const;
69 
70 private:
72  int first; // sequence number of first message in queue
73  int last; // sequence number of last message in queue
74  int startIndex; // index pointing to the first byte of the first message
75  int endIndex; // index pointing to the first byte after the last message
76 
77  void WriteByte( byte b );
78  byte ReadByte( void );
79  void WriteShort( int s );
80  int ReadShort( void );
81  void WriteLong( int l );
82  int ReadLong( void );
83  void WriteData( const byte *data, const int size );
84  void ReadData( byte *data, const int size );
85 };
86 
87 
88 class idMsgChannel {
89 public:
90  idMsgChannel();
91 
92  void Init( const netadr_t adr, const int id );
93  void Shutdown( void );
94  void ResetRate( void );
95 
96  // Sets the maximum outgoing rate.
97  void SetMaxOutgoingRate( int rate ) { maxRate = rate; }
98 
99  // Gets the maximum outgoing rate.
100  int GetMaxOutgoingRate( void ) { return maxRate; }
101 
102  // Returns the address of the entity at the other side of the channel.
103  netadr_t GetRemoteAddress( void ) const { return remoteAddress; }
104 
105  // Returns the average outgoing rate over the last second.
106  int GetOutgoingRate( void ) const { return outgoingRateBytes; }
107 
108  // Returns the average incoming rate over the last second.
109  int GetIncomingRate( void ) const { return incomingRateBytes; }
110 
111  // Returns the average outgoing compression ratio over the last second.
112  float GetOutgoingCompression( void ) const { return outgoingCompression; }
113 
114  // Returns the average incoming compression ratio over the last second.
115  float GetIncomingCompression( void ) const { return incomingCompression; }
116 
117  // Returns the average incoming packet loss over the last 5 seconds.
118  float GetIncomingPacketLoss( void ) const;
119 
120  // Returns true if the channel is ready to send new data based on the maximum rate.
121  bool ReadyToSend( const int time ) const;
122 
123  // Sends an unreliable message, in order and without duplicates.
124  int SendMessage( idPort &port, const int time, const idBitMsg &msg );
125 
126  // Sends the next fragment if the last message was too large to send at once.
127  void SendNextFragment( idPort &port, const int time );
128 
129  // Returns true if there are unsent fragments left.
130  bool UnsentFragmentsLeft( void ) const { return unsentFragments; }
131 
132  // Processes the incoming message. Returns true when a complete message
133  // is ready for further processing. In that case the read pointer of msg
134  // points to the first byte ready for reading, and sequence is set to
135  // the sequence number of the message.
136  bool Process( const netadr_t from, int time, idBitMsg &msg, int &sequence );
137 
138  // Sends a reliable message, in order and without duplicates.
139  bool SendReliableMessage( const idBitMsg &msg );
140 
141  // Returns true if a new reliable message is available and stores the message.
142  bool GetReliableMessage( idBitMsg &msg );
143 
144  // Removes any pending outgoing or incoming reliable messages.
145  void ClearReliableMessages( void );
146 
147 private:
148  netadr_t remoteAddress; // address of remote host
149  int id; // our identification used instead of port number
150  int maxRate; // maximum number of bytes that may go out per second
151  idCompressor * compressor; // compressor used for data compression
152 
153  // variables to control the outgoing rate
154  int lastSendTime; // last time data was sent out
155  int lastDataBytes; // bytes left to send at last send time
156 
157  // variables to keep track of the rate
162 
163  // variables to keep track of the compression ratio
166 
167  // variables to keep track of the incoming packet loss
171 
172  // sequencing variables
175 
176  // outgoing fragment buffer
181 
182  // incoming fragment assembly buffer
186 
187  // reliable messages
190 
191 private:
192  void WriteMessageData( idBitMsg &out, const idBitMsg &msg );
193  bool ReadMessageData( idBitMsg &out, const idBitMsg &msg );
194 
195  void UpdateOutgoingRate( const int time, const int size );
196  void UpdateIncomingRate( const int time, const int size );
197 
198  void UpdatePacketLoss( const int time, const int numReceived, const int numDropped );
199 };
200 
201 #endif /* !__MSGCHANNEL_H__ */
void WriteData(const byte *data, const int size)
Definition: MsgChannel.cpp:219
void UpdateIncomingRate(const int time, const int size)
Definition: MsgChannel.cpp:745
int SendMessage(idPort &port, const int time, const idBitMsg &msg)
Definition: MsgChannel.cpp:471
int unsentFragmentStart
Definition: MsgChannel.h:178
void ReadData(byte *data, const int size)
Definition: MsgChannel.cpp:230
bool Process(const netadr_t from, int time, idBitMsg &msg, int &sequence)
Definition: MsgChannel.cpp:539
byte ReadByte(void)
Definition: MsgChannel.cpp:168
float outgoingCompression
Definition: MsgChannel.h:164
int ReadLong(void)
Definition: MsgChannel.cpp:210
void SetMaxOutgoingRate(int rate)
Definition: MsgChannel.h:97
int GetOutgoingRate(void) const
Definition: MsgChannel.h:106
byte unsentBuffer[MAX_MESSAGE_SIZE]
Definition: MsgChannel.h:179
int endIndex
Definition: MsgChannel.h:75
int incomingPacketLossTime
Definition: MsgChannel.h:170
int fragmentLength
Definition: MsgChannel.h:184
int outgoingRateTime
Definition: MsgChannel.h:158
bool SendReliableMessage(const idBitMsg &msg)
Definition: MsgChannel.cpp:670
void Init(const netadr_t adr, const int id)
Definition: MsgChannel.cpp:259
idCompressor * compressor
Definition: MsgChannel.h:151
void WriteShort(int s)
Definition: MsgChannel.cpp:179
byte fragmentBuffer[MAX_MESSAGE_SIZE]
Definition: MsgChannel.h:185
idMsgQueue reliableSend
Definition: MsgChannel.h:188
void SendNextFragment(idPort &port, const int time)
Definition: MsgChannel.cpp:414
int lastSendTime
Definition: MsgChannel.h:154
GLdouble s
Definition: glext.h:2935
int startIndex
Definition: MsgChannel.h:74
idMsgQueue reliableReceive
Definition: MsgChannel.h:189
netadr_t GetRemoteAddress(void) const
Definition: MsgChannel.h:103
netadr_t remoteAddress
Definition: MsgChannel.h:148
list l
Definition: prepare.py:17
int fragmentSequence
Definition: MsgChannel.h:183
idBitMsg unsentMsg
Definition: MsgChannel.h:180
void CopyToBuffer(byte *buf) const
Definition: MsgChannel.cpp:144
void Init(int sequence)
Definition: MsgChannel.cpp:72
float incomingDroppedPackets
Definition: MsgChannel.h:169
int GetTotalSize(void) const
Definition: MsgChannel.cpp:118
bool GetReliableMessage(idBitMsg &msg)
Definition: MsgChannel.cpp:690
int incomingSequence
Definition: MsgChannel.h:174
bool Get(byte *data, int &size)
Definition: MsgChannel.cpp:99
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:2853
GLuint buffer
Definition: glext.h:3108
void ClearReliableMessages(void)
Definition: MsgChannel.cpp:705
bool ReadyToSend(const int time) const
Definition: MsgChannel.cpp:315
bool unsentFragments
Definition: MsgChannel.h:177
int outgoingRateBytes
Definition: MsgChannel.h:159
void Shutdown(void)
Definition: MsgChannel.cpp:291
int incomingRateBytes
Definition: MsgChannel.h:161
float incomingCompression
Definition: MsgChannel.h:165
GLubyte GLubyte b
Definition: glext.h:4662
float GetIncomingCompression(void) const
Definition: MsgChannel.h:115
void UpdatePacketLoss(const int time, const int numReceived, const int numDropped)
Definition: MsgChannel.cpp:762
float incomingReceivedPackets
Definition: MsgChannel.h:168
bool ReadMessageData(idBitMsg &out, const idBitMsg &msg)
Definition: MsgChannel.cpp:366
bool UnsentFragmentsLeft(void) const
Definition: MsgChannel.h:130
#define MAX_MESSAGE_SIZE
Definition: MsgChannel.h:48
unsigned char byte
Definition: Lib.h:75
int GetIncomingRate(void) const
Definition: MsgChannel.h:109
float GetIncomingPacketLoss(void) const
Definition: MsgChannel.cpp:785
GLsizeiptr size
Definition: glext.h:3112
void WriteByte(byte b)
Definition: MsgChannel.cpp:158
bool Add(const byte *data, const int size)
Definition: MsgChannel.cpp:82
int outgoingSequence
Definition: MsgChannel.h:173
int GetFirst(void) const
Definition: MsgChannel.h:66
int ReadShort(void)
Definition: MsgChannel.cpp:189
int GetMaxOutgoingRate(void)
Definition: MsgChannel.h:100
void ResetRate(void)
Definition: MsgChannel.cpp:301
int lastDataBytes
Definition: MsgChannel.h:155
void WriteLong(int l)
Definition: MsgChannel.cpp:198
int GetSpaceLeft(void) const
Definition: MsgChannel.cpp:131
void UpdateOutgoingRate(const int time, const int size)
Definition: MsgChannel.cpp:715
int GetLast(void) const
Definition: MsgChannel.h:67
#define MAX_MSG_QUEUE_SIZE
Definition: MsgChannel.h:53
void WriteMessageData(idBitMsg &out, const idBitMsg &msg)
Definition: MsgChannel.cpp:333
float GetOutgoingCompression(void) const
Definition: MsgChannel.h:112
int incomingRateTime
Definition: MsgChannel.h:160