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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <windows.h>
#include "tcpsocket.h"
#include "IThreadedTCPSocket.h"
#include "ThreadedTCPSocketEmu.h"
#include "ThreadHelpers.h"
// ---------------------------------------------------------------------------------------- //
// CThreadedTCPSocketEmu. This uses IThreadedTCPSocket to emulate the polling-type interface
// in ITCPSocket.
// ---------------------------------------------------------------------------------------- //
// This class uses the IThreadedTCPSocket interface to emulate the old ITCPSocket.
class CThreadedTCPSocketEmu : public ITCPSocket, public ITCPSocketHandler, public IHandlerCreator
{
public:
CThreadedTCPSocketEmu()
{
m_pSocket = NULL;
m_LocalPort = 0xFFFF;
m_pConnectSocket = NULL;
m_RecvPacketsEvent.Init( false, false );
m_bError = false;
}
virtual ~CThreadedTCPSocketEmu()
{
Term();
}
void Init( IThreadedTCPSocket *pSocket )
{
m_pSocket = pSocket;
}
void Term()
{
if ( m_pSocket )
{
m_pSocket->Release();
m_pSocket = NULL;
}
if ( m_pConnectSocket )
{
m_pConnectSocket->Release();
m_pConnectSocket = NULL;
}
}
// ITCPSocketHandler implementation.
private:
virtual void OnPacketReceived( CTCPPacket *pPacket )
{
CCriticalSectionLock csLock( &m_RecvPacketsCS );
csLock.Lock();
m_RecvPackets.AddToTail( pPacket );
m_RecvPacketsEvent.SetEvent();
}
virtual void OnError( int errorCode, const char *pErrorString )
{
CCriticalSectionLock csLock( &m_ErrorStringCS );
csLock.Lock();
m_ErrorString.CopyArray( pErrorString, strlen( pErrorString ) + 1 );
m_bError = true;
}
// IHandlerCreator implementation.
public:
// This is used for connecting.
virtual ITCPSocketHandler* CreateNewHandler()
{
return this;
}
// ITCPSocket implementation.
public:
virtual void Release()
{
delete this;
}
virtual bool BindToAny( const unsigned short port )
{
m_LocalPort = port;
return true;
}
virtual bool BeginConnect( const CIPAddr &addr )
{
// They should have "bound" to a port before trying to connect.
Assert( m_LocalPort != 0xFFFF );
if ( m_pConnectSocket )
m_pConnectSocket->Release();
m_pConnectSocket = ThreadedTCP_CreateConnector(
addr,
CIPAddr( 0, 0, 0, 0, m_LocalPort ),
this );
return m_pConnectSocket != 0;
}
virtual bool UpdateConnect()
{
Assert( !m_pSocket );
if ( !m_pConnectSocket )
return false;
if ( m_pConnectSocket->Update( &m_pSocket ) )
{
if ( m_pSocket )
{
// Ok, we're connected now.
m_pConnectSocket->Release();
m_pConnectSocket = NULL;
return true;
}
else
{
return false;
}
}
else
{
Assert( false );
m_pConnectSocket->Release();
m_pConnectSocket = NULL;
return false;
}
}
virtual bool IsConnected()
{
if ( m_bError )
{
Term();
return false;
}
else
{
return m_pSocket != NULL;
}
}
virtual void GetDisconnectReason( CUtlVector<char> &reason )
{
CCriticalSectionLock csLock( &m_ErrorStringCS );
csLock.Lock();
reason = m_ErrorString;
}
virtual bool Send( const void *pData, int size )
{
Assert( m_pSocket );
if ( !m_pSocket )
return false;
return m_pSocket->Send( pData, size );
}
virtual bool SendChunks( void const * const *pChunks, const int *pChunkLengths, int nChunks )
{
Assert( m_pSocket );
if ( !m_pSocket || !m_pSocket->IsValid() )
return false;
return m_pSocket->SendChunks( pChunks, pChunkLengths, nChunks );
}
virtual bool Recv( CUtlVector<unsigned char> &data, double flTimeout )
{
// Use our m_RecvPacketsEvent event to determine if there is data to receive yet.
DWORD nMilliseconds = (DWORD)( flTimeout * 1000.0f );
DWORD ret = WaitForSingleObject( m_RecvPacketsEvent.GetEventHandle(), nMilliseconds );
if ( ret == WAIT_OBJECT_0 )
{
// Ok, there's a packet.
CCriticalSectionLock csLock( &m_RecvPacketsCS );
csLock.Lock();
Assert( m_RecvPackets.Count() > 0 );
int iHead = m_RecvPackets.Head();
CTCPPacket *pPacket = m_RecvPackets[ iHead ];
data.CopyArray( (const unsigned char*)pPacket->GetData(), pPacket->GetLen() );
pPacket->Release();
m_RecvPackets.Remove( iHead );
// Re-set the event if there are more packets left to receive.
if ( m_RecvPackets.Count() > 0 )
{
m_RecvPacketsEvent.SetEvent();
}
return true;
}
else
{
return false;
}
}
private:
IThreadedTCPSocket *m_pSocket;
unsigned short m_LocalPort; // The port we bind to when we want to connect.
ITCPConnectSocket *m_pConnectSocket;
// All the received data is stored in here.
CEvent m_RecvPacketsEvent;
CCriticalSection m_RecvPacketsCS;
CUtlLinkedList<CTCPPacket*, int> m_RecvPackets;
CCriticalSection m_ErrorStringCS;
CUtlVector<char> m_ErrorString;
bool m_bError; // Set to true when there's an error. Next chance we get in the main thread, we'll close the socket.
};
ITCPSocket* CreateTCPSocketEmu()
{
return new CThreadedTCPSocketEmu;
}
// ---------------------------------------------------------------------------------------- //
// CThreadedTCPListenSocketEmu implementation.
// ---------------------------------------------------------------------------------------- //
class CThreadedTCPListenSocketEmu : public ITCPListenSocket, public IHandlerCreator
{
public:
CThreadedTCPListenSocketEmu()
{
m_pListener = NULL;
m_pLastCreatedSocket = NULL;
}
virtual ~CThreadedTCPListenSocketEmu()
{
if ( m_pListener )
m_pListener->Release();
}
bool StartListening( const unsigned short port, int nQueueLength )
{
m_pListener = ThreadedTCP_CreateListener(
this,
port,
nQueueLength );
return m_pListener != 0;
}
// ITCPListenSocket implementation.
private:
virtual void Release()
{
delete this;
}
virtual ITCPSocket* UpdateListen( CIPAddr *pAddr )
{
if ( !m_pListener )
return NULL;
IThreadedTCPSocket *pSocket;
if ( m_pListener->Update( &pSocket ) && pSocket )
{
*pAddr = pSocket->GetRemoteAddr();
// This is pretty hacky, but this stuff is just around for test code.
CThreadedTCPSocketEmu *pLast = m_pLastCreatedSocket;
pLast->Init( pSocket );
m_pLastCreatedSocket = NULL;
return pLast;
}
else
{
return NULL;
}
}
// IHandlerCreator implementation.
private:
virtual ITCPSocketHandler* CreateNewHandler()
{
m_pLastCreatedSocket = new CThreadedTCPSocketEmu;
return m_pLastCreatedSocket;
}
private:
ITCPConnectSocket *m_pListener;
CThreadedTCPSocketEmu *m_pLastCreatedSocket;
};
ITCPListenSocket* CreateTCPListenSocketEmu( const unsigned short port, int nQueueLength )
{
CThreadedTCPListenSocketEmu *pSocket = new CThreadedTCPListenSocketEmu;
if ( pSocket->StartListening( port, nQueueLength ) )
{
return pSocket;
}
else
{
delete pSocket;
return NULL;
}
}
|