summaryrefslogtreecommitdiff
path: root/utils/vmpi/tcpsocket.cpp
blob: 8ab67cf6b8ecf1a4be552bf0af640307d151b464 (plain) (blame)
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//


//#define PARANOID

#if defined( PARANOID )
	#include <stdlib.h>
	#include <crtdbg.h>
#endif

#include <winsock2.h>
#include <mswsock.h>
#include "tcpsocket.h"
#include "tier1/utllinkedlist.h"
#include <stdio.h>
#include "threadhelpers.h"
#include "tier0/dbg.h"



#error "I am TCPSocket and I suck. Use IThreadedTCPSocket or ThreadedTCPSocketEmu instead."


extern TIMEVAL SetupTimeVal( double flTimeout );
extern void IPAddrToSockAddr( const CIPAddr *pIn, sockaddr_in *pOut );
extern void SockAddrToIPAddr( const sockaddr_in *pIn, CIPAddr *pOut );


#define SENTINEL_DISCONNECT	-1
#define SENTINEL_KEEPALIVE	-2


#define KEEPALIVE_INTERVAL_MS		3000	// keepalives are sent every N MS
#define KEEPALIVE_TIMEOUT_SECONDS	15.0	// connections timeout after this long


static bool g_bEnableTCPTimeout = true;


class CRecvData
{
public:
	int				m_Count;
	unsigned char	m_Data[1];
};
	


SOCKET TCPBind( const CIPAddr *pAddr )
{
	// Create a socket to send and receive through.
	SOCKET sock = WSASocket( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
	if ( sock == INVALID_SOCKET )
	{
		Assert( false );
		return INVALID_SOCKET;
	}

	// bind to it!
	sockaddr_in addr;
	IPAddrToSockAddr( pAddr, &addr );

	int status = bind( sock, (sockaddr*)&addr, sizeof(addr) );
	if ( status == 0 )
	{
		return sock;
	}
	else
	{
		closesocket( sock );
		return INVALID_SOCKET;
	}
}



// ---------------------------------------------------------------------------------------- //
// TCP sockets.
// ---------------------------------------------------------------------------------------- //

enum
{
	OP_RECV=111,
	OP_SEND
};

// We use this for all OVERLAPPED structures.
class COverlappedPlus : public WSAOVERLAPPED
{
public:
				COverlappedPlus()
				{
					memset( this, 0, sizeof( WSAOVERLAPPED ) );
				}

	int					m_OPType;		// One of the OP_ defines.
};

typedef struct SendBuf_t
{
	COverlappedPlus		m_Overlapped;
	int					m_Index;		// Index into m_SendBufs.
	int					m_DataLength;
	char				m_Data[1];
} SendBuf_s;


// These manage a thread that calls SendKeepalive() on all TCPSockets.
// AddGlobalTCPSocket shouldn't be called until you're ready for SendKeepalive() to be called.
class CTCPSocket;
void AddGlobalTCPSocket( CTCPSocket *pSocket );
void RemoveGlobalTCPSocket( CTCPSocket *pSocket );



// ------------------------------------------------------------------------------------------ //
// CTCPSocket implementation.
// ------------------------------------------------------------------------------------------ //

class CTCPSocket : public ITCPSocket
{
friend class CTCPListenSocket;

public:

					CTCPSocket()
					{
						m_Socket = INVALID_SOCKET;
						m_bConnected = false;
						
						m_hIOCP = NULL;
						
						m_bShouldExitThreads = false;
						m_bConnectionLost = false;
						m_nSizeBytesReceived = 0;

						m_pIncomingData = NULL;

						memset( &m_RecvOverlapped, 0, sizeof( m_RecvOverlapped ) );
						m_RecvOverlapped.m_OPType = OP_RECV;

						m_hRecvSignal = CreateEvent( NULL, FALSE, FALSE, NULL );
						m_RecvStage = -1;

						m_MainThreadID = GetCurrentThreadId();
					}
	
	virtual			~CTCPSocket()
	{
		Term();
		CloseHandle( m_hRecvSignal );
	}

	void Term()
	{
		Assert( GetCurrentThreadId() == m_MainThreadID );

		RemoveGlobalTCPSocket( this );

		if ( m_Socket != SOCKET_ERROR && !m_bConnectionLost )
		{
			SendDisconnectSentinel();
	
			// Give the sends a second to complete. SO_LINGER is having trouble for some reason.
			WaitForSendsToComplete( 1 );
		}


		StopThreads();

		if ( m_Socket != INVALID_SOCKET )
		{
			closesocket( m_Socket );
			m_Socket = INVALID_SOCKET;
		}

		if ( m_hIOCP )
		{
			CloseHandle( m_hIOCP );
			m_hIOCP = NULL;
		}

		m_bConnected = false;
		m_bConnectionLost = true;
		m_RecvStage = -1;
		
		FOR_EACH_LL( m_SendBufs, i )
		{
			SendBuf_t *pSendBuf = m_SendBufs[i];
			ParanoidMemoryCheck( pSendBuf );
			free( pSendBuf );
		}
		m_SendBufs.Purge();

		FOR_EACH_LL( m_RecvDatas, j )
		{
			CRecvData *pRecvData = m_RecvDatas[j];
			ParanoidMemoryCheck( pRecvData );
			free( pRecvData );
		}
		m_RecvDatas.Purge();

		if ( m_pIncomingData )
		{
			ParanoidMemoryCheck( m_pIncomingData );
			free( m_pIncomingData );
			m_pIncomingData = 0;
		}
	}

	virtual void	Release()
	{
		delete this;
	}


	void ParanoidMemoryCheck( void *ptr = NULL )
	{
#if defined( PARANOID )
		Assert( _CrtIsValidHeapPointer( this ) );

		if ( ptr )
		{
			Assert( _CrtIsValidHeapPointer( ptr ) );
		}

		Assert( _CrtCheckMemory() == TRUE );
#endif
	}

	
	virtual bool	BindToAny( const unsigned short port )
	{
		Term();

		CIPAddr addr( 0, 0, 0, 0, port ); // INADDR_ANY
		m_Socket = TCPBind( &addr );
		if ( m_Socket == INVALID_SOCKET )
		{
			return false;
		}
		else
		{
			SetInitialSocketOptions();
			return true;
		}
	}

	
	// Set the initial socket options that we want.
	void SetInitialSocketOptions()
	{
		// Set nodelay to improve latency.
		BOOL val = TRUE;
		setsockopt( m_Socket, IPPROTO_TCP, TCP_NODELAY, (const char FAR *)&val, sizeof(BOOL) );

		// Make it linger for 3 seconds when it exits.
		LINGER linger;
		linger.l_onoff = 1;
		linger.l_linger = 3;
		setsockopt( m_Socket, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof( linger ) );
	}


	// Called only by main thread interface functions.
	// Returns true if the connection is lost.
	bool CheckConnectionLost()
	{
		Assert( GetCurrentThreadId() == m_MainThreadID );

		if ( m_Socket == SOCKET_ERROR )
			return true;

		// Have we timed out?
		if ( g_bEnableTCPTimeout && (Plat_FloatTime() - m_LastRecvTime > KEEPALIVE_TIMEOUT_SECONDS) )
		{
			SetConnectionLost( "Connection timed out." );
		}

		// Has any thread posted that the connection has been lost?
		CCriticalSectionLock postLock( &m_ConnectionLostCS );
		postLock.Lock();
		if ( m_bConnectionLost )
		{
			Term();
			return true;
		}
		else
		{
			return false;
		}
	}

	// Called by any thread. All interface functions call CheckConnectionLost() and return errors if it's lost.
	void SetConnectionLost( const char *pErrorString, int err = -1 )
	{
		CCriticalSectionLock postLock( &m_ConnectionLostCS );
		postLock.Lock();
			m_bConnectionLost = true;
		postLock.Unlock();

		// Handle it right away if we're in the main thread. If we're in an IO thread,
		// it has to wait until the next interface function calls CheckConnectionLost().
		if ( GetCurrentThreadId() == m_MainThreadID )
		{
			Term();
		}
		
		if ( pErrorString )
		{
			m_ErrorString.CopyArray( pErrorString, strlen( pErrorString ) + 1 );
		}
		else
		{
			char *lpMsgBuf;
			FormatMessage( 
				FORMAT_MESSAGE_ALLOCATE_BUFFER | 
				FORMAT_MESSAGE_FROM_SYSTEM | 
				FORMAT_MESSAGE_IGNORE_INSERTS,
				NULL,
				err,
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
				(LPTSTR) &lpMsgBuf,
				0,
				NULL 
			);

			m_ErrorString.CopyArray( lpMsgBuf, strlen( lpMsgBuf ) + 1 );
			LocalFree( lpMsgBuf );
		}
	}
		

	// -------------------------------------------------------------------------------------------------- //
	// The receive code.
	// -------------------------------------------------------------------------------------------------- //

	virtual bool StartWaitingForSize( bool bFresh )
	{
		Assert( m_Socket != INVALID_SOCKET );
		Assert( m_bConnected );

		m_RecvStage = 0;
		m_RecvDataSize = -1;
		if ( bFresh )
			m_nSizeBytesReceived = 0;

		DWORD dwNumBytesReceived = 0;
		WSABUF buf = { sizeof( &m_RecvDataSize ) - m_nSizeBytesReceived, ((char*)&m_RecvDataSize) + m_nSizeBytesReceived };
		DWORD dwFlags = 0;

		int status = WSARecv( 
			m_Socket,
			&buf,
			1,
			&dwNumBytesReceived,
			&dwFlags,
			&m_RecvOverlapped,
			NULL );

		int err = -1;
		if ( status == SOCKET_ERROR && (err = WSAGetLastError()) != ERROR_IO_PENDING )
		{
			SetConnectionLost( NULL, err );
			return false;
		}
		else
		{
			return true;
		}
	}


	bool PostNextDataPart()
	{
		DWORD dwNumBytesReceived = 0;
		WSABUF buf = { m_RecvDataSize - m_AmountReceived, (char*)m_pIncomingData->m_Data + m_AmountReceived }; 
		DWORD dwFlags = 0;

		int status = WSARecv( 
			m_Socket,
			&buf,
			1,
			&dwNumBytesReceived,
			&dwFlags,
			&m_RecvOverlapped,
			NULL );

		int err = -1;
		if ( status == SOCKET_ERROR && (err = WSAGetLastError()) != ERROR_IO_PENDING )
		{
			SetConnectionLost( NULL, err );
			return false;
		}
		else
		{
			return true;
		}
	}


	bool StartWaitingForData()
	{
		Assert( m_Socket != INVALID_SOCKET );
		Assert( m_RecvStage == 0 );
		Assert( m_bConnected );
		Assert( m_RecvDataSize > 0 );

		m_RecvStage = 1;

		// Add a CRecvData element.
		ParanoidMemoryCheck();
		m_pIncomingData = (CRecvData*)malloc( sizeof( CRecvData ) - 1 + m_RecvDataSize );
		if ( !m_pIncomingData )
		{
			char str[512];
			_snprintf( str, sizeof( str ), "malloc() failed. m_RecvDataSize = %d\n", m_RecvDataSize );
			SetConnectionLost( str );
			return false;
		}

		m_pIncomingData->m_Count = m_RecvDataSize;

		m_AmountReceived = 0;

		return PostNextDataPart();
	}

	virtual bool	Recv( CUtlVector<unsigned char> &data, double flTimeout )
	{
		if ( CheckConnectionLost() )
			return false;

		// Wait in 50ms chunks, checking for disconnections along the way.
		bool bGotData = false;
		DWORD msToWait = (DWORD)( flTimeout * 1000.0 );
		do
		{
			DWORD curWaitTime = min( msToWait, 50 );
			DWORD ret = WaitForSingleObject( m_hRecvSignal, curWaitTime );
			if ( ret == WAIT_OBJECT_0 )
			{
				bGotData = true;
				break;
			}

			// Did the connection timeout?
			if ( CheckConnectionLost() )
				return false;

			msToWait -= curWaitTime;
		} while ( msToWait );
		
		// If we never got a WAIT_OBJECT_0, then we never received anything.
		if ( !bGotData )
			return false;
		
		
		CCriticalSectionLock csLock( &m_RecvDataCS );
		csLock.Lock();

		// Pickup the head m_RecvDatas element.
		CRecvData *pRecvData = m_RecvDatas[ m_RecvDatas.Head() ];
		data.CopyArray( pRecvData->m_Data, pRecvData->m_Count );

		// Now free it.
		m_RecvDatas.Remove( m_RecvDatas.Head() );
		ParanoidMemoryCheck( pRecvData );
		free( pRecvData );

		// Set the event again for the next time around, if there is more data waiting.
		if ( m_RecvDatas.Count() > 0 )
			SetEvent( m_hRecvSignal );

		return true;
	}

	// INSIDE IO THREAD.
	void HandleRecvCompletion( COverlappedPlus *pInfo, DWORD dwNumBytes )
	{
		if ( dwNumBytes == 0 )
		{
			SetConnectionLost( "Got 0 bytes in HandleRecvCompletion" );
			return;
		}

		m_LastRecvTime = Plat_FloatTime();
		if ( m_RecvStage == 0 )
		{
			m_nSizeBytesReceived += dwNumBytes;
			if ( m_nSizeBytesReceived == sizeof( m_RecvDataSize ) )
			{
				// Size of -1 means the other size is breaking the connection.
				if ( m_RecvDataSize == SENTINEL_DISCONNECT )
				{
					SetConnectionLost( "Got a graceful disconnect message." );
					return;
				}
				else if ( m_RecvDataSize == SENTINEL_KEEPALIVE )
				{
					// No data follows this. Just let m_LastRecvTime get updated.
					StartWaitingForSize( true );
					return;
				}

				StartWaitingForData();
			}
			else if ( m_nSizeBytesReceived < sizeof( m_RecvDataSize ) )
			{
				// Handle the case where we only got some of the data (maybe one of the clients got disconnected).
				StartWaitingForSize( false );
			}
			else
			{
				// This case should never ever happen!
#if defined( _DEBUG )
					__asm int 3;
#endif

				SetConnectionLost( "Received too much data in a packet!" );
				return;
			}
		}
		else if ( m_RecvStage == 1 )
		{
			// Got the data, make sure we got it all.
			m_AmountReceived += dwNumBytes;

			// Sanity check.
#if defined( _DEBUG )
			Assert( m_RecvDataSize == m_pIncomingData->m_Count );
			Assert( m_AmountReceived <= m_RecvDataSize );	// TODO: make this threadsafe for multiple IO threads.
#endif

			if ( m_AmountReceived == m_RecvDataSize )
			{
				m_RecvStage = 2;
				
				// Add the data to the list of packets waiting to be picked up.
				CCriticalSectionLock csLock( &m_RecvDataCS );
				csLock.Lock();
				
				m_RecvDatas.AddToTail( m_pIncomingData );
				m_pIncomingData = NULL;

				if ( m_RecvDatas.Count() == 1 )
					SetEvent( m_hRecvSignal );	// Notify the Recv() function.

				StartWaitingForSize( true );
			}
			else
			{
				PostNextDataPart();
			}
		}
		else
		{
			Assert( false );
		}
	}
	

	// -------------------------------------------------------------------------------------------------- //
	// The send code.
	// -------------------------------------------------------------------------------------------------- //

	virtual void	WaitForSendsToComplete( double flTimeout )
	{
		CWaitTimer waitTimer( flTimeout );
		while ( 1 )
		{
			CCriticalSectionLock sendBufLock( &m_SendCS );
			sendBufLock.Lock();
				if( m_SendBufs.Count() == 0 )
					return;
			sendBufLock.Unlock();

			if ( waitTimer.ShouldKeepWaiting() )
				Sleep( 10 );
			else
				break;
		}
	}


	// This is called in the keepalive thread.
	void SendKeepalive()
	{
		// Send a message saying we're exiting.
		ParanoidMemoryCheck();
		SendBuf_t *pBuf = (SendBuf_t*)malloc( sizeof( SendBuf_t ) - 1 + sizeof( int ) );
		if ( !pBuf )
		{
			SetConnectionLost( "malloc() in SendKeepalive() failed." );
			return;
		}

		pBuf->m_DataLength = sizeof( int );
		*((int*)pBuf->m_Data) = SENTINEL_KEEPALIVE;
		InternalSendDataBuf( pBuf );
	}


	void SendDisconnectSentinel()
	{
		// Send a message saying we're exiting.
		ParanoidMemoryCheck();
		SendBuf_t *pBuf = (SendBuf_t*)malloc( sizeof( SendBuf_t ) - 1 + sizeof( int ) );
		if ( pBuf )
		{
			pBuf->m_DataLength = sizeof( int );
			*((int*)pBuf->m_Data) = SENTINEL_DISCONNECT;	// This signifies that we're exiting.
			InternalSendDataBuf( pBuf );
		}
	}


	virtual bool	Send( const void *pData, int len )
	{
		const void *pChunks[1] = { pData };
		int chunkLengths[1] = { len };
		return SendChunks( pChunks, chunkLengths, 1 );
	}

	
	virtual bool	SendChunks( void const * const *pChunks, const int *pChunkLengths, int nChunks )
	{
		if ( CheckConnectionLost() )
			return false;
		
		CChunkWalker walker( pChunks, pChunkLengths, nChunks );
		int totalLength = walker.GetTotalLength();

		if ( !totalLength )
			return true;

		// Create a buffer to hold the data and copy the data in.
		ParanoidMemoryCheck();
		SendBuf_t *pBuf = (SendBuf_t*)malloc( sizeof( SendBuf_t ) - 1 + totalLength + sizeof( int ) );
		if ( !pBuf )
		{
			char str[512];
			_snprintf( str, sizeof( str ), "malloc() in SendChunks() failed. totalLength = %d.", totalLength );
			SetConnectionLost( str );
			return false;
		}

		pBuf->m_DataLength = totalLength + sizeof( int );

		int *pByteCountPos = (int*)pBuf->m_Data;
		*pByteCountPos = totalLength;

		char *pDataPos = &pBuf->m_Data[ sizeof( int ) ];
		walker.CopyTo( pDataPos, totalLength );

		int status = InternalSendDataBuf( pBuf );
		int err = -1;
		if ( status == SOCKET_ERROR && (err = WSAGetLastError()) != ERROR_IO_PENDING )
		{
			SetConnectionLost( NULL, err );
			return false;
		}
		else
		{
			return true;
		}
	}


	int InternalSendDataBuf( SendBuf_t *pBuf )
	{
		// Protect against interference from the keepalive thread.
		CCriticalSectionLock csLock( &m_SendCS );
		csLock.Lock();


		pBuf->m_Overlapped.m_OPType = OP_SEND;
		pBuf->m_Overlapped.hEvent = NULL;

		// Add it to our list of buffers.
		pBuf->m_Index = m_SendBufs.AddToTail( pBuf );

		// Tell Winsock to send it.
		WSABUF buf = { pBuf->m_DataLength, pBuf->m_Data };
		
		DWORD dwNumBytesSent = 0;
		return WSASend(
			m_Socket,
			&buf,
			1,
			&dwNumBytesSent,
			0,
			&pBuf->m_Overlapped,
			NULL );
	}


	// INSIDE IO THREAD.
	void HandleSendCompletion( COverlappedPlus *pInfo, DWORD dwNumBytes )
	{
		if ( dwNumBytes == 0 )
		{
			SetConnectionLost( "0 bytes in HandleSendCompletion." );
			return;
		}

		// Just free the buffer.
		SendBuf_t *pBuf = (SendBuf_t*)pInfo;
		Assert( dwNumBytes == (DWORD)pBuf->m_DataLength );

		CCriticalSectionLock sendBufLock( &m_SendCS );
		sendBufLock.Lock();
			m_SendBufs.Remove( pBuf->m_Index );
		sendBufLock.Unlock();

		ParanoidMemoryCheck( pBuf );
		free( pBuf );
	}


	// -------------------------------------------------------------------------------------------------- //
	// The connect code.
	// -------------------------------------------------------------------------------------------------- //

	virtual bool BeginConnect( const CIPAddr &inputAddr )
	{
		sockaddr_in addr;
		IPAddrToSockAddr( &inputAddr, &addr );

		m_bConnected = false;
		int ret = connect( m_Socket, (struct sockaddr*)&addr, sizeof( addr ) );
		ret=ret;

		return true;
	}


	virtual bool UpdateConnect()
	{
		// We're still ok.. just wait until the socket becomes writable (is connected) or we timeout.
		fd_set writeSet;
		writeSet.fd_count = 1;
		writeSet.fd_array[0] = m_Socket;
		TIMEVAL timeVal = SetupTimeVal( 0 );

		// See if it has a packet waiting.
		int status = select( 0, NULL, &writeSet, NULL, &timeVal );
		if ( status > 0 )
		{
			SetupConnected();
			return true;
		}

		return false;
	}


	void SetupConnected()
	{
		m_bConnected = true;
		m_bConnectionLost = false;
		m_LastRecvTime = Plat_FloatTime(); 

		CreateThreads();
		StartWaitingForSize( true );
		AddGlobalTCPSocket( this );
	}


	virtual bool	IsConnected()
	{
		CheckConnectionLost();
		return m_bConnected;
	}


	virtual void GetDisconnectReason( CUtlVector<char> &reason )
	{
		reason = m_ErrorString;
	}


	// -------------------------------------------------------------------------------------------------- //
	// Threads code.
	// -------------------------------------------------------------------------------------------------- //

	// Create our IO Completion Port threads.
	bool CreateThreads()
	{
		int nThreads = 1;
		SetShouldExitThreads( false );

        // Create our IO completion port and hook it to our socket.
		m_hIOCP = CreateIoCompletionPort(        
            INVALID_HANDLE_VALUE, NULL, 0, 0); 

		m_hIOCP = CreateIoCompletionPort( (HANDLE)m_Socket, m_hIOCP, (unsigned long)this, nThreads ); 

		for ( int i=0; i < nThreads; i++ )
		{
			DWORD dwThreadID = 0;
			HANDLE hThread = CreateThread( 
				NULL,
				0,
				&CTCPSocket::StaticThreadFn,
				this,
				0,
				&dwThreadID );

			if ( hThread )
			{
				SetThreadPriority( hThread, THREAD_PRIORITY_ABOVE_NORMAL );
				m_Threads.AddToTail( hThread );
			}
			else
			{
				StopThreads();
				return false;
			}
		}
	
		return true;
	}

	
	void StopThreads()
	{
		// Tell the threads to exit, then wait for them to do so.
		SetShouldExitThreads( true );
		WaitForMultipleObjects( m_Threads.Count(), m_Threads.Base(), TRUE, INFINITE );

		for ( int i=0; i < m_Threads.Count(); i++ )
		{
			CloseHandle( m_Threads[i] );
		}
		m_Threads.Purge();
	}


	void SetShouldExitThreads( bool bShouldExit )
	{
		CCriticalSectionLock lock( &m_ThreadsCS );
		lock.Lock();
		m_bShouldExitThreads = bShouldExit;
	}


	bool ShouldExitThreads()
	{
		CCriticalSectionLock lock( &m_ThreadsCS );
		lock.Lock();

		bool bRet = m_bShouldExitThreads;
		return bRet;
	}


	DWORD ThreadFn()
	{
		while ( 1 )
		{
			DWORD dwNumBytes = 0;
			unsigned long pInputTCPSocket;
			LPOVERLAPPED pOverlapped;

			if ( GetQueuedCompletionStatus( 
				m_hIOCP,		// the port we're listening on
				&dwNumBytes,	// # bytes received on the port
				&pInputTCPSocket,// "completion key" = CTCPSocket*
				&pOverlapped,	// the overlapped info that was passed into AcceptEx, WSARecv, or WSASend.
				100				// listen for 100ms at a time so we can exit gracefully when the socket is deleted.
				) )
			{
				COverlappedPlus *pInfo = (COverlappedPlus*)pOverlapped;
				ParanoidMemoryCheck( pInfo );
				
				if ( pInfo->m_OPType == OP_RECV )
				{
					Assert( pInfo == &m_RecvOverlapped );
					HandleRecvCompletion( pInfo, dwNumBytes );
				}
				else
				{
					Assert( pInfo->m_OPType == OP_SEND );
					HandleSendCompletion( pInfo, dwNumBytes );
				}
			}
			
			if ( ShouldExitThreads() )
				break;
		}

		return 0;
	}
	

	static DWORD WINAPI StaticThreadFn( LPVOID pParameter )
	{
		return ((CTCPSocket*)pParameter)->ThreadFn();
	}

	

private:

	SOCKET		m_Socket;
	bool		m_bConnected;


	// m_RecvOverlapped is setup to first wait for the size, then the data.
	// Then it is not posted until the app grabs the data.
	HANDLE						m_hRecvSignal;	// Tells Recv() when we have data.
	COverlappedPlus				m_RecvOverlapped;
	int							m_RecvStage;	// -1 = not initialized
												//  0 = waiting for size
												//  1 = waiting for data
												//  2 = waiting for app to pickup the data

	CUtlLinkedList<CRecvData*,int>	m_RecvDatas;	// The head element is the next one to be picked up.
	CRecvData					*m_pIncomingData;	// The packet we're currently receiving.
	CCriticalSection			m_RecvDataCS;		// This protects adds and removes in the list.

	// These reference the element at the tail of m_RecvData. It is the current one getting 
	volatile int				m_nSizeBytesReceived;	// How much of m_RecvDataSize have we received yet?
	int							m_RecvDataSize;			// this is received over the network
	int							m_AmountReceived;		// How much we've received so far.

	// Last time we received anything from this connection. Used to determine if the connection is 
	// still active.
	double						m_LastRecvTime;


	// Outgoing send buffers.
	CUtlLinkedList<SendBuf_t*,int>	m_SendBufs;
	CCriticalSection				m_SendCS;
	

	// All the threads waiting for IO.
	CUtlVector<HANDLE>		m_Threads;
	HANDLE					m_hIOCP;

	// Used during shutdown.
	volatile bool			m_bShouldExitThreads;
	CCriticalSection		m_ThreadsCS;

	// For debugging.
	DWORD					m_MainThreadID;

	// Set by the main thread or IO threads to signal connection lost.
	bool					m_bConnectionLost;
	CCriticalSection		m_ConnectionLostCS;

	// This is set when we get disconnected.
	CUtlVector<char>		m_ErrorString;
};


// ------------------------------------------------------------------------------------------ //
// ITCPListenSocket implementation.
// ------------------------------------------------------------------------------------------ //

class CTCPListenSocket : public ITCPListenSocket
{
public:

						CTCPListenSocket()
						{
							m_Socket = INVALID_SOCKET;
						}


	virtual				~CTCPListenSocket()
	{
		if ( m_Socket != INVALID_SOCKET )
		{
			closesocket( m_Socket );
		}
	}	


	// The main function to create one of these suckers.
	static ITCPListenSocket*	Create( const unsigned short port, int nQueueLength )
	{
		CTCPListenSocket *pRet = new CTCPListenSocket;
		if ( !pRet )
			return NULL;

		// Bind it to a socket and start listening.
		CIPAddr addr( 0, 0, 0, 0, port ); // INADDR_ANY
		pRet->m_Socket = TCPBind( &addr );
		if ( pRet->m_Socket == INVALID_SOCKET || 
			listen( pRet->m_Socket, nQueueLength == -1 ? SOMAXCONN : nQueueLength ) != 0 )
		{
			pRet->Release();
			return false;
		}

		return pRet;
	}


	virtual void		Release()
	{
		delete this;
	}


	virtual ITCPSocket*	UpdateListen( CIPAddr *pAddr )
	{
		// We're still ok.. just wait until the socket becomes writable (is connected) or we timeout.
		fd_set readSet;
		readSet.fd_count = 1;
		readSet.fd_array[0] = m_Socket;
		TIMEVAL timeVal = SetupTimeVal( 0 );

		// Wait until it connects.
		int status = select( 0, &readSet, NULL, NULL, &timeVal );
		if ( status > 0 )
		{
			sockaddr_in addr;
			int addrSize = sizeof( addr );

			// Now accept the final connection.
			SOCKET newSock = accept( m_Socket, (struct sockaddr*)&addr, &addrSize );
			if ( newSock == INVALID_SOCKET )
			{
				Assert( false );
			}
			else
			{
				CTCPSocket *pRet = new CTCPSocket;
				if ( !pRet )
				{
					closesocket( newSock );
					return NULL;
				}

				pRet->m_Socket = newSock;
				pRet->SetInitialSocketOptions();
				pRet->SetupConnected();

				// Report the address..
				SockAddrToIPAddr( &addr, pAddr );

				return pRet;
			}
		}

		return NULL;
	}


private:
	SOCKET		m_Socket;	
};



ITCPListenSocket* CreateTCPListenSocket( const unsigned short port, int nQueueLength )
{
	return CTCPListenSocket::Create( port, nQueueLength );
}


ITCPSocket* CreateTCPSocket()
{
	return new CTCPSocket;
}


void TCPSocket_EnableTimeout( bool bEnable )
{
	g_bEnableTCPTimeout = bEnable;
}


// --------------------------------------------------------------------------------- //	
// This thread sends keepalives on all active TCP sockets.
// --------------------------------------------------------------------------------- //	

HANDLE							g_hKeepaliveThread;
HANDLE							g_hKeepaliveThreadSignal;
HANDLE							g_hKeepaliveThreadReply;
CUtlLinkedList<CTCPSocket*,int>	g_TCPSockets;
CCriticalSection				g_TCPSocketsCS;


DWORD WINAPI TCPKeepaliveThread( LPVOID pParameter )
{
	while ( 1 )
	{
		if ( WaitForSingleObject( g_hKeepaliveThreadSignal, KEEPALIVE_INTERVAL_MS ) == WAIT_OBJECT_0 )
			break;

		// Tell all TCP sockets to send a keepalive.
		CCriticalSectionLock csLock( &g_TCPSocketsCS );
		csLock.Lock();

		FOR_EACH_LL( g_TCPSockets, i )
		{
			g_TCPSockets[i]->SendKeepalive();
		}
	}

	SetEvent( g_hKeepaliveThreadReply );
	return 0;
}


void AddGlobalTCPSocket( CTCPSocket *pSocket )
{
	CCriticalSectionLock csLock( &g_TCPSocketsCS );
	csLock.Lock();
	
	Assert( g_TCPSockets.Find( pSocket ) == g_TCPSockets.InvalidIndex() );
	g_TCPSockets.AddToTail( pSocket );

	// If this is the first one, create the keepalive thread.
	if ( g_TCPSockets.Count() == 1 )
	{
		g_hKeepaliveThreadSignal = CreateEvent( NULL, false, false, NULL );
		g_hKeepaliveThreadReply = CreateEvent( NULL, false, false, NULL );

		DWORD dwThreadID = 0;
		g_hKeepaliveThread = CreateThread(
			NULL,
			0,
			TCPKeepaliveThread,
			NULL,
			0,
			&dwThreadID
			);
	}
}


void RemoveGlobalTCPSocket( CTCPSocket *pSocket )
{
	bool bThreadRunning = false;
	DWORD dwExitCode = 0;
	if ( GetExitCodeThread( g_hKeepaliveThread, &dwExitCode ) && dwExitCode == STILL_ACTIVE )
	{
		bThreadRunning = true;
	}

	CCriticalSectionLock csLock( &g_TCPSocketsCS );
	csLock.Lock();
	
	int index = g_TCPSockets.Find( pSocket );
	if ( index != g_TCPSockets.InvalidIndex() )
	{
		g_TCPSockets.Remove( index );

		// If this was the last one, delete the thread.
		if ( g_TCPSockets.Count() == 0 )
		{
			csLock.Unlock();

			if ( bThreadRunning )
			{
				SetEvent( g_hKeepaliveThreadSignal );
				WaitForSingleObject( g_hKeepaliveThreadReply, INFINITE );
			}

			CloseHandle( g_hKeepaliveThreadSignal );
			CloseHandle( g_hKeepaliveThreadReply );
			CloseHandle( g_hKeepaliveThread );
			return;
		}
	}

	csLock.Unlock();
}