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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <windows.h>
#include "messagemgr.h"
#include "tcpsocket.h"
#include "iphelpers.h"
#include "tier0/platform.h"
#include "threadhelpers.h"
#define MSGMGR_LISTEN_PORT_FIRST 22512
#define MSGMGR_LISTEN_PORT_LAST 22520
#define BROADCAST_INTERVAL 2 // Broadcast our presence every N seconds.
#define NUM_QUEUED_MESSAGES 200
class CMessageMgr : public IMessageMgr
{
public:
CMessageMgr();
~CMessageMgr();
bool Init();
void Term();
// IMessageMgr overrides.
public:
virtual void Print( const char *pMsg );
private:
DWORD ThreadFn();
static DWORD WINAPI StaticThreadFn( LPVOID pParameter );
private:
// Only our thread touches this, NOT the main thread.
CUtlLinkedList<ITCPSocket*,int> m_Sockets;
HANDLE m_hThread;
DWORD m_dwThreadID;
HANDLE m_hExitObj; // This is signalled when we want the thread to exit.
HANDLE m_hExitResponseObj; // The thread sets this when it exits.
HANDLE m_hMessageObj; // This signals to the thread that there's a message to send.
HANDLE m_hMessageSentObj; // This signals back to the main thread that the message was sent.
const char *m_pMessageText; // The text to send.
// This is only touched by the thread.
CUtlLinkedList<char*,int> m_MessageQ; // FIFO of NUM_QUEUED_MESSAGES.
ITCPListenSocket *m_pListenSocket;
int m_iListenPort;
ISocket *m_pBroadcastSocket;
double m_flLastBroadcast;
};
CMessageMgr::CMessageMgr()
{
m_pBroadcastSocket = NULL;
m_pListenSocket = NULL;
m_hThread = NULL;
m_hExitObj = m_hExitResponseObj = m_hMessageObj = m_hMessageSentObj = NULL;
}
CMessageMgr::~CMessageMgr()
{
Term();
}
bool CMessageMgr::Init()
{
m_hExitObj = CreateEvent( NULL, false, false, NULL );
m_hExitResponseObj = CreateEvent( NULL, false, false, NULL );
m_hMessageObj = CreateEvent( NULL, false, false, NULL );
m_hMessageSentObj = CreateEvent( NULL, false, false, NULL );
if ( !m_hExitObj || !m_hExitResponseObj || !m_hMessageObj || !m_hMessageSentObj )
return false;
// Create the broadcast socket.
m_pBroadcastSocket = CreateIPSocket();
if ( !m_pBroadcastSocket )
return false;
if ( !m_pBroadcastSocket->BindToAny( 0 ) )
return false;
// Create the listen socket.
m_pListenSocket = NULL;
for ( m_iListenPort=MSGMGR_LISTEN_PORT_FIRST; m_iListenPort <= MSGMGR_LISTEN_PORT_LAST; m_iListenPort++ )
{
m_pListenSocket = CreateTCPListenSocket( m_iListenPort );
if ( m_pListenSocket )
break;
}
if ( !m_pListenSocket )
return false;
// Create our broadcast/connection thread.
m_flLastBroadcast = 0;
m_hThread = CreateThread(
NULL,
0,
&CMessageMgr::StaticThreadFn,
this,
0,
&m_dwThreadID );
if ( !m_hThread )
return false;
Plat_SetThreadName( m_dwThreadID, "MessageMgr" );
return true;
}
void CMessageMgr::Term()
{
// Wait for the thread to exit?
if ( m_hThread )
{
DWORD dwExitCode = 0;
if ( GetExitCodeThread( m_hThread, &dwExitCode ) && dwExitCode == STILL_ACTIVE )
{
SetEvent( m_hExitObj );
WaitForSingleObject( m_hExitResponseObj, INFINITE );
}
CloseHandle( m_hThread );
m_hThread = NULL;
}
CloseHandle( m_hExitObj );
m_hExitObj = NULL;
CloseHandle( m_hExitResponseObj );
m_hExitResponseObj = NULL;
CloseHandle( m_hMessageObj );
m_hMessageObj = NULL;
CloseHandle( m_hMessageSentObj );
m_hMessageSentObj = NULL;
if ( m_pListenSocket )
{
m_pListenSocket->Release();
m_pListenSocket = NULL;
}
if ( m_pBroadcastSocket )
{
m_pBroadcastSocket->Release();
m_pBroadcastSocket = NULL;
}
}
void CMessageMgr::Print( const char *pMsg )
{
m_pMessageText = pMsg;
SetEvent( m_hMessageObj );
WaitForSingleObject( m_hMessageSentObj, INFINITE );
}
DWORD CMessageMgr::ThreadFn()
{
while ( 1 )
{
// Broadcast our presence?
double flCurTime = Plat_FloatTime();
if ( flCurTime - m_flLastBroadcast >= BROADCAST_INTERVAL )
{
// Broadcast our presence.
char msg[9];
msg[0] = MSGMGR_PACKETID_ANNOUNCE_PRESENCE;
*((int*)&msg[1]) = MSGMGR_VERSION;
*((int*)&msg[5]) = m_iListenPort;
m_pBroadcastSocket->Broadcast( msg, sizeof( msg ), MSGMGR_BROADCAST_PORT );
m_flLastBroadcast = flCurTime;
}
// Accept new connections.
CIPAddr addr;
ITCPSocket *pConn = m_pListenSocket->UpdateListen( &addr );
if ( pConn )
{
// Send what's in our queue.
FOR_EACH_LL( m_MessageQ, iQ )
{
char *pMsg = m_MessageQ[iQ];
int bufLen = strlen( pMsg ) + 1;
char packetID = MSGMGR_PACKETID_MSG;
const void *data[2] = { &packetID, pMsg };
int len[2] = { 1, bufLen };
// Send it out to our sockets.
pConn->SendChunks( data, len, 2 );
}
m_Sockets.AddToTail( pConn );
}
// Should we exit?
HANDLE handles[2] = {m_hExitObj, m_hMessageObj};
DWORD ret = WaitForMultipleObjects( 2, handles, FALSE, 200 );
if ( ret == WAIT_OBJECT_0 )
{
break;
}
else if ( ret == (WAIT_OBJECT_0+1) )
{
// Add it to the queue.
int index;
if ( m_MessageQ.Count() >= NUM_QUEUED_MESSAGES )
{
index = m_MessageQ.Tail();
delete m_MessageQ[index];
}
else
{
index = m_MessageQ.AddToTail();
}
int bufLen = strlen( m_pMessageText ) + 1;
m_MessageQ[index] = new char[ bufLen ];
strcpy( m_MessageQ[index], m_pMessageText );
// Ok, send out the message.
char packetID = MSGMGR_PACKETID_MSG;
const void *data[2] = { &packetID, m_pMessageText };
int len[2] = { 1, bufLen };
// Send it out to our sockets.
FOR_EACH_LL( m_Sockets, i )
{
m_Sockets[i]->SendChunks( data, len, 2 );
}
// Notify the main thread that we've sent it.
SetEvent( m_hMessageSentObj );
}
}
// Cleanup all our sockets (the main thread should never touch them).
FOR_EACH_LL( m_Sockets, i )
m_Sockets[i]->Release();
m_Sockets.Purge();
m_MessageQ.PurgeAndDeleteElements();
SetEvent( m_hExitResponseObj );
return 0;
}
DWORD CMessageMgr::StaticThreadFn( LPVOID pParameter )
{
return ((CMessageMgr*)pParameter)->ThreadFn();
}
static CMessageMgr g_MessageMgr;
IMessageMgr* GetMessageMgr()
{
return &g_MessageMgr;
}
|