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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include <string.h>
#include <assert.h>
#include "msgbuffer.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#pragma warning(disable: 4244) // warning C4244: '=' : conversion from 'int' to 'unsigned char', possible loss of data
//-----------------------------------------------------------------------------
// Purpose: Allocate message buffer
// Input : *buffername -
// *ef -
//-----------------------------------------------------------------------------
CMsgBuffer::CMsgBuffer( const char *buffername, void (*ef)( const char *fmt, ... ) /*= NULL*/ )
{
m_pszBufferName = buffername;
m_pfnErrorFunc = ef;
m_bAllowOverflow = false; // if false, Error
m_bOverFlowed = false; // set to true if the buffer size failed
m_nMaxSize = NET_MAXMESSAGE;
m_nPushedCount = 0;
m_bPushed = false;
m_nReadCount = 0;
m_bBadRead = false;
Clear();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CMsgBuffer::~CMsgBuffer( void )
{
}
//-----------------------------------------------------------------------------
// Purpose: Temporarily remember the read position so we can reset it
//-----------------------------------------------------------------------------
void CMsgBuffer::Push( void )
{
// ??? Allow multiple pushes without matching pops ???
assert( !m_bPushed );
m_nPushedCount = m_nReadCount;
m_bPushed = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMsgBuffer::Pop( void )
{
assert( m_bPushed );
m_nReadCount = m_nPushedCount;
m_bPushed = false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : allowed -
//-----------------------------------------------------------------------------
void CMsgBuffer::SetOverflow( bool allowed )
{
m_bAllowOverflow = allowed;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int
//-----------------------------------------------------------------------------
int CMsgBuffer::GetMaxSize( void )
{
return m_nMaxSize;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : void *
//-----------------------------------------------------------------------------
void * CMsgBuffer::GetData( void )
{
return m_rgData;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int
//-----------------------------------------------------------------------------
int CMsgBuffer::GetCurSize( void )
{
return m_nCurSize;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int
//-----------------------------------------------------------------------------
int CMsgBuffer::GetReadCount( void )
{
return m_nReadCount;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void CMsgBuffer::SetTime(float time)
{
m_fRecvTime = time;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
float CMsgBuffer::GetTime()
{
return m_fRecvTime;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void CMsgBuffer::SetNetAddress(netadr_t &adr)
{
m_NetAddr = adr;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
netadr_t &CMsgBuffer::GetNetAddress()
{
return m_NetAddr;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMsgBuffer::WriteByte( int c )
{
unsigned char *buf;
buf = (unsigned char *)GetSpace( 1 );
buf[0] = c;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : c -
//-----------------------------------------------------------------------------
void CMsgBuffer::WriteShort ( int c )
{
unsigned char *buf;
buf = (unsigned char *)GetSpace( 2 );
buf[0] = c&0xff;
buf[1] = c>>8;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : c -
//-----------------------------------------------------------------------------
void CMsgBuffer::WriteLong (int c)
{
unsigned char *buf;
buf = (unsigned char *)GetSpace( 4 );
buf[0] = c&0xff;
buf[1] = (c>>8)&0xff;
buf[2] = (c>>16)&0xff;
buf[3] = c>>24;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : f -
//-----------------------------------------------------------------------------
void CMsgBuffer::WriteFloat (float f)
{
union
{
float f;
int l;
} dat;
dat.f = f;
Write( &dat.l, 4 );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *s -
//-----------------------------------------------------------------------------
void CMsgBuffer::WriteString (const char *s)
{
if ( !s )
{
Write ("", 1);
}
else
{
Write ( s, strlen( s ) + 1 );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : iSize -
// *buf -
//-----------------------------------------------------------------------------
void CMsgBuffer::WriteBuf( int iSize, void *buf )
{
if ( !buf )
{
return;
}
Write( buf, iSize );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMsgBuffer::BeginReading (void)
{
m_nReadCount = 0;
m_bBadRead = false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int CMsgBuffer::ReadByte
//-----------------------------------------------------------------------------
int CMsgBuffer::ReadByte (void)
{
int c;
if ( m_nReadCount + 1 > m_nCurSize )
{
m_bBadRead = true;
return -1;
}
c = ( unsigned char )m_rgData[ m_nReadCount ];
m_nReadCount++;
return c;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int CMsgBuffer::ReadShort
//-----------------------------------------------------------------------------
int CMsgBuffer::ReadShort (void)
{
int c;
if ( m_nReadCount + 2 > m_nCurSize )
{
m_bBadRead = true;
return -1;
}
c = (short)(m_rgData[m_nReadCount] + (m_rgData[m_nReadCount+1]<<8));
m_nReadCount += 2;
return c;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int CMsgBuffer::ReadLong
//-----------------------------------------------------------------------------
int CMsgBuffer::ReadLong (void)
{
int c;
if (m_nReadCount+4 > m_nCurSize)
{
m_bBadRead = true;
return -1;
}
c = m_rgData[m_nReadCount]
+ (m_rgData[m_nReadCount+1]<<8)
+ (m_rgData[m_nReadCount+2]<<16)
+ (m_rgData[m_nReadCount+3]<<24);
m_nReadCount += 4;
return c;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : float CMsgBuffer::ReadFloat
//-----------------------------------------------------------------------------
float CMsgBuffer::ReadFloat (void)
{
union
{
unsigned char b[4];
float f;
} dat;
dat.b[0] = m_rgData[m_nReadCount];
dat.b[1] = m_rgData[m_nReadCount+1];
dat.b[2] = m_rgData[m_nReadCount+2];
dat.b[3] = m_rgData[m_nReadCount+3];
m_nReadCount += 4;
return dat.f;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : iSize -
// *pbuf -
// Output : int
//-----------------------------------------------------------------------------
int CMsgBuffer::ReadBuf( int iSize, void *pbuf )
{
if (m_nReadCount + iSize > m_nCurSize)
{
m_bBadRead = true;
return -1;
}
memcpy( pbuf, &m_rgData[m_nReadCount], iSize );
m_nReadCount += iSize;
return 1;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : char *
//-----------------------------------------------------------------------------
char *CMsgBuffer::ReadString (void)
{
static char string[ NET_MAXMESSAGE ];
int l,c;
l = 0;
do
{
c = (char)ReadByte();
if ( c == -1 || c == 0 )
break;
string[l] = c;
l++;
} while ( l < sizeof(string)-1 );
string[ l ] = 0;
return string;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMsgBuffer::Clear( void )
{
m_nCurSize = 0;
m_bOverFlowed = false;
m_nReadCount = 0;
m_bBadRead = false;
memset( m_rgData, 0, sizeof( m_rgData ) );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : length -
//-----------------------------------------------------------------------------
void *CMsgBuffer::GetSpace( int length )
{
void *d;
if (m_nCurSize + length > m_nMaxSize)
{
if ( !m_bAllowOverflow )
{
if ( m_pfnErrorFunc )
{
( *m_pfnErrorFunc )( "CMsgBuffer(%s), no room for %i bytes, %i / %i already in use\n",
m_pszBufferName, length, m_nCurSize, m_nMaxSize );
}
return NULL;
}
if (length > m_nMaxSize)
{
if ( m_pfnErrorFunc )
{
( *m_pfnErrorFunc )( "CMsgBuffer(%s), no room for %i bytes, %i is max\n",
m_pszBufferName, length, m_nMaxSize );
}
return NULL;
}
m_bOverFlowed = true;
Clear();
}
d = m_rgData + m_nCurSize;
m_nCurSize += length;
return d;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *m_rgData -
// length -
//-----------------------------------------------------------------------------
void CMsgBuffer::Write(const void *m_rgData, int length)
{
memcpy( GetSpace(length), m_rgData, length );
}
|