File: MsgChannel.h

package info (click to toggle)
dhewm3 1.5.1~pre%2Bgit20200905%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 21,664 kB
  • sloc: cpp: 408,868; ansic: 1,188; objc: 1,034; python: 330; sh: 94; makefile: 11
file content (204 lines) | stat: -rw-r--r-- 7,657 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
===========================================================================

Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.

This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").

Doom 3 Source Code 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 3 of the License, or
(at your option) any later version.

Doom 3 Source Code 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.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.

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.

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.

===========================================================================
*/

#ifndef __MSGCHANNEL_H__
#define __MSGCHANNEL_H__

#include "sys/sys_public.h"

class idCompressor;

/*
===============================================================================

  Network channel.

  Handles message fragmentation and out of order / duplicate suppression.
  Unreliable messages are not garrenteed to arrive but when they do, they
  arrive in order and without duplicates. Reliable messages always arrive,
  and they also arrive in order without duplicates. Reliable messages piggy
  back on unreliable messages. As such an unreliable message stream is
  required for the reliable messages to be delivered.

===============================================================================
*/

#define	MAX_MESSAGE_SIZE				16384		// max length of a message, which may
													// be fragmented into multiple packets
#define CONNECTIONLESS_MESSAGE_ID		-1			// id for connectionless messages
#define CONNECTIONLESS_MESSAGE_ID_MASK	0x7FFF		// value to mask away connectionless message id

#define MAX_MSG_QUEUE_SIZE				16384		// must be a power of 2


class idMsgQueue {
public:
					idMsgQueue();

	void			Init( int sequence );

	bool			Add( const byte *data, const int size );
	bool			Get( byte *data, int &size );
	int				GetTotalSize( void ) const;
	int				GetSpaceLeft( void ) const;
	int				GetFirst( void ) const { return first; }
	int				GetLast( void ) const { return last; }
	void			CopyToBuffer( byte *buf ) const;

private:
	byte			buffer[MAX_MSG_QUEUE_SIZE];
	int				first;			// sequence number of first message in queue
	int				last;			// sequence number of last message in queue
	int				startIndex;		// index pointing to the first byte of the first message
	int				endIndex;		// index pointing to the first byte after the last message

	void			WriteByte( byte b );
	byte			ReadByte( void );
	void			WriteShort( int s );
	int				ReadShort( void );
	void			WriteInt( int l );
	int				ReadInt( void );
	void			WriteData( const byte *data, const int size );
	void			ReadData( byte *data, const int size );
};


class idMsgChannel {
public:
					idMsgChannel();

	void			Init( const netadr_t adr, const int id );
	void			Shutdown( void );
	void			ResetRate( void );

					// Sets the maximum outgoing rate.
	void			SetMaxOutgoingRate( int rate ) { maxRate = rate; }

					// Gets the maximum outgoing rate.
	int				GetMaxOutgoingRate( void ) { return maxRate; }

					// Returns the address of the entity at the other side of the channel.
	netadr_t		GetRemoteAddress( void ) const { return remoteAddress; }

					// Returns the average outgoing rate over the last second.
	int				GetOutgoingRate( void ) const { return outgoingRateBytes; }

					// Returns the average incoming rate over the last second.
	int				GetIncomingRate( void ) const { return incomingRateBytes; }

					// Returns the average outgoing compression ratio over the last second.
	float			GetOutgoingCompression( void ) const { return outgoingCompression; }

					// Returns the average incoming compression ratio over the last second.
	float			GetIncomingCompression( void ) const { return incomingCompression; }

					// Returns the average incoming packet loss over the last 5 seconds.
	float			GetIncomingPacketLoss( void ) const;

					// Returns true if the channel is ready to send new data based on the maximum rate.
	bool			ReadyToSend( const int time ) const;

					// Sends an unreliable message, in order and without duplicates.
	int				SendMessage( idPort &port, const int time, const idBitMsg &msg );

					// Sends the next fragment if the last message was too large to send at once.
	void			SendNextFragment( idPort &port, const int time );

					// Returns true if there are unsent fragments left.
	bool			UnsentFragmentsLeft( void ) const { return unsentFragments; }

					// Processes the incoming message. Returns true when a complete message
					// is ready for further processing. In that case the read pointer of msg
					// points to the first byte ready for reading, and sequence is set to
					// the sequence number of the message.
	bool			Process( const netadr_t from, int time, idBitMsg &msg, int &sequence );

					// Sends a reliable message, in order and without duplicates.
	bool			SendReliableMessage( const idBitMsg &msg );

					// Returns true if a new reliable message is available and stores the message.
	bool			GetReliableMessage( idBitMsg &msg );

					// Removes any pending outgoing or incoming reliable messages.
	void			ClearReliableMessages( void );

private:
	netadr_t		remoteAddress;	// address of remote host
	int				id;				// our identification used instead of port number
	int				maxRate;		// maximum number of bytes that may go out per second
	idCompressor *	compressor;		// compressor used for data compression

	// variables to control the outgoing rate
	int				lastSendTime;	// last time data was sent out
	int				lastDataBytes;	// bytes left to send at last send time

	// variables to keep track of the rate
	int				outgoingRateTime;
	int				outgoingRateBytes;
	int				incomingRateTime;
	int				incomingRateBytes;

	// variables to keep track of the compression ratio
	float			outgoingCompression;
	float			incomingCompression;

	// variables to keep track of the incoming packet loss
	float			incomingReceivedPackets;
	float			incomingDroppedPackets;
	int				incomingPacketLossTime;

	// sequencing variables
	int				outgoingSequence;
	int				incomingSequence;

	// outgoing fragment buffer
	bool			unsentFragments;
	int				unsentFragmentStart;
	byte			unsentBuffer[MAX_MESSAGE_SIZE];
	idBitMsg		unsentMsg;

	// incoming fragment assembly buffer
	int				fragmentSequence;
	int				fragmentLength;
	byte			fragmentBuffer[MAX_MESSAGE_SIZE];

	// reliable messages
	idMsgQueue		reliableSend;
	idMsgQueue		reliableReceive;

private:
	void			WriteMessageData( idBitMsg &out, const idBitMsg &msg );
	bool			ReadMessageData( idBitMsg &out, const idBitMsg &msg );

	void			UpdateOutgoingRate( const int time, const int size );
	void			UpdateIncomingRate( const int time, const int size );

	void			UpdatePacketLoss( const int time, const int numReceived, const int numDropped );
};

#endif /* !__MSGCHANNEL_H__ */