forked from XutaxKamay/css_enhanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetworkmanager.cpp
393 lines (331 loc) · 11.6 KB
/
networkmanager.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: The main manager of the networking code in the game
//
// $Revision: $
// $NoKeywords: $
//===========================================================================//
#include "networkmanager.h"
#include "legion.h"
#include "networksystem/inetworkmessage.h"
#include "tier1/bitbuf.h"
#include "inetworkmessagelistener.h"
#include "tier0/icommandline.h"
#include "tier2/tier2.h"
//-----------------------------------------------------------------------------
// Singleton accessor
//-----------------------------------------------------------------------------
static CNetworkManager s_NetworkManager;
extern CNetworkManager *g_pNetworkManager = &s_NetworkManager;
//-----------------------------------------------------------------------------
// Static members.
// NOTE: Do *not* set this to 0; it could cause us to lose some registered
// menus since that list is set up during construction
//-----------------------------------------------------------------------------
INetworkMessageFactory *CNetworkManager::m_pFirstFactory;
//-----------------------------------------------------------------------------
// Call to register methods to register network messages
//-----------------------------------------------------------------------------
INetworkMessageFactory *CNetworkManager::RegisterNetworkMessage( INetworkMessageFactory *pNetworkMessageFactory )
{
// NOTE: This method is expected to be called during global constructor
// time, so it must not require any global constructors to be called to work
INetworkMessageFactory *pPrevFactory = m_pFirstFactory;
m_pFirstFactory = pNetworkMessageFactory;
return pPrevFactory;
}
//-----------------------------------------------------------------------------
// Init, shutdown
//-----------------------------------------------------------------------------
bool CNetworkManager::Init()
{
m_bIsClient = m_bIsServer = false;
m_pClientToServerConnection = NULL;
// Make a listener array for each message type
// Register all network messages
INetworkMessageFactory *pFactory;
for ( pFactory = m_pFirstFactory; pFactory; pFactory = pFactory->GetNextFactory() )
{
INetworkMessage *pNetworkMessage = pFactory->CreateNetworkMessage();
g_pNetworkSystem->RegisterMessage( pNetworkMessage );
}
return true;
}
void CNetworkManager::Shutdown()
{
ShutdownClient();
ShutdownServer();
}
//-----------------------------------------------------------------------------
// Start a server up
//-----------------------------------------------------------------------------
bool CNetworkManager::StartServer( unsigned short nServerListenPort )
{
ShutdownServer( );
if ( nServerListenPort == LEGION_DEFAULT_SERVER_PORT )
{
nServerListenPort = CommandLine()->ParmValue( "-serverport", NETWORKSYSTEM_DEFAULT_SERVER_PORT );
}
m_bIsServer = g_pNetworkSystem->StartServer( nServerListenPort );
return m_bIsServer;
}
void CNetworkManager::ShutdownServer( )
{
if ( m_bIsServer )
{
g_pNetworkSystem->ShutdownServer( );
m_bIsServer = false;
}
}
//-----------------------------------------------------------------------------
// Start a client up
//-----------------------------------------------------------------------------
bool CNetworkManager::StartClient( unsigned short nClientListenPort )
{
ShutdownClient( );
if ( nClientListenPort == LEGION_DEFAULT_CLIENT_PORT )
{
nClientListenPort = CommandLine()->ParmValue( "-clientport", NETWORKSYSTEM_DEFAULT_CLIENT_PORT );
}
m_bIsClient = g_pNetworkSystem->StartClient( nClientListenPort );
return m_bIsClient;
}
void CNetworkManager::ShutdownClient( )
{
if ( m_bIsClient )
{
DisconnectClientFromServer();
g_pNetworkSystem->ShutdownClient( );
m_bIsClient = false;
}
}
//-----------------------------------------------------------------------------
// Connects/disconnects the client to a server
//-----------------------------------------------------------------------------
bool CNetworkManager::ConnectClientToServer( const char *pServerName, unsigned short nServerListenPort )
{
DisconnectClientFromServer();
if ( !IsClient() )
return false;
if ( nServerListenPort == LEGION_DEFAULT_SERVER_PORT )
{
nServerListenPort = CommandLine()->ParmValue( "-serverport", NETWORKSYSTEM_DEFAULT_SERVER_PORT );
}
m_pClientToServerConnection = g_pNetworkSystem->ConnectClientToServer( pServerName, nServerListenPort );
return ( m_pClientToServerConnection != NULL );
}
void CNetworkManager::DisconnectClientFromServer( )
{
if ( m_pClientToServerConnection )
{
g_pNetworkSystem->DisconnectClientFromServer( m_pClientToServerConnection );
m_pClientToServerConnection = NULL;
}
}
//-----------------------------------------------------------------------------
// Start up a game where the local player is playing
//-----------------------------------------------------------------------------
bool CNetworkManager::HostGame()
{
if ( !StartServer() )
return false;
if ( !StartClient() )
{
ShutdownServer();
return false;
}
if ( !ConnectClientToServer( "localhost" ) )
{
ShutdownClient();
ShutdownServer();
return false;
}
return true;
}
void CNetworkManager::StopHostingGame()
{
ShutdownClient();
ShutdownServer();
}
//-----------------------------------------------------------------------------
// Are we a client?/Are we a server? (we can be both)
//-----------------------------------------------------------------------------
bool CNetworkManager::IsClient()
{
return m_bIsClient;
}
bool CNetworkManager::IsServer()
{
return m_bIsServer;
}
//-----------------------------------------------------------------------------
// If we are a client, are we connected to the server?
//-----------------------------------------------------------------------------
bool CNetworkManager::IsClientConnected()
{
return m_bIsClient && ( m_pClientToServerConnection != NULL ) && ( m_pClientToServerConnection->GetConnectionState() == CONNECTION_STATE_CONNECTED );
}
//-----------------------------------------------------------------------------
// Post a network message to the server
//-----------------------------------------------------------------------------
void CNetworkManager::PostClientToServerMessage( INetworkMessage *pMessage )
{
if ( IsClientConnected() )
{
m_pClientToServerConnection->AddNetMsg( pMessage );
}
}
//-----------------------------------------------------------------------------
// Broadcast a network message to all clients
//-----------------------------------------------------------------------------
void CNetworkManager::BroadcastServerToClientMessage( INetworkMessage *pMessage )
{
if ( IsServer() )
{
int nCount = m_ServerToClientConnection.Count();
for ( int i = 0; i < nCount; ++i )
{
m_ServerToClientConnection[i]->AddNetMsg( pMessage );
}
}
}
//-----------------------------------------------------------------------------
// Add, remove network message listeners
//-----------------------------------------------------------------------------
void CNetworkManager::AddListener( NetworkMessageRoute_t route, int nGroup, int nType, INetworkMessageListener *pListener )
{
CUtlVector< CUtlVector< CUtlVector < INetworkMessageListener* > > > &listeners = m_Listeners[ route ];
if ( listeners.Count() <= nGroup )
{
listeners.AddMultipleToTail( nGroup - listeners.Count() + 1 );
}
if ( listeners[nGroup].Count() <= nType )
{
listeners[nGroup].AddMultipleToTail( nType - listeners[nGroup].Count() + 1 );
}
Assert( listeners[nGroup][nType].Find( pListener ) < 0 );
listeners[nGroup][nType].AddToTail( pListener );
}
void CNetworkManager::RemoveListener( NetworkMessageRoute_t route, int nGroup, int nType, INetworkMessageListener *pListener )
{
CUtlVector< CUtlVector< CUtlVector < INetworkMessageListener* > > > &listeners = m_Listeners[ route ];
if ( listeners.Count() > nGroup )
{
if( listeners[nGroup].Count() > nType )
{
// Maintain order of listeners (not sure if it matters)
listeners[nGroup][nType].FindAndRemove( pListener );
}
}
}
//-----------------------------------------------------------------------------
// Notifies listeners about a network message
//-----------------------------------------------------------------------------
void CNetworkManager::NotifyListeners( NetworkMessageRoute_t route, INetworkMessage *pMessage )
{
CUtlVector< CUtlVector< CUtlVector < INetworkMessageListener* > > > &listeners = m_Listeners[ route ];
// based on id, search for installed listeners
int nGroup = pMessage->GetGroup();
if ( listeners.Count() > nGroup )
{
int nType = pMessage->GetType();
if ( listeners[nGroup].Count() > nType )
{
CUtlVector< INetworkMessageListener* > &list = listeners[nGroup][nType];
int nCount = list.Count();
for ( int i = 0; i < nCount; ++i )
{
list[i]->OnNetworkMessage( route, pMessage );
}
}
}
}
//-----------------------------------------------------------------------------
// Process messages received by the client
//-----------------------------------------------------------------------------
void CNetworkManager::ProcessClientMessages()
{
NetworkEvent_t *pEvent = g_pNetworkSystem->FirstNetworkEvent();
for ( ; pEvent; pEvent = g_pNetworkSystem->NextNetworkEvent( ) )
{
switch ( pEvent->m_nType )
{
/*
case NETWORK_EVENT_CONNECTED:
{
NetworkConnectionEvent_t* pConnectEvent = static_cast<NetworkConnectionEvent_t*>( pEvent );
m_pClientToServerConnection = pConnectEvent->m_pChannel;
}
break;
case NETWORK_EVENT_DISCONNECTED:
{
NetworkDisconnectionEvent_t* pDisconnectEvent = static_cast<NetworkDisconnectionEvent_t*>( pEvent );
Assert( m_pClientToServerConnection == pDisconnectEvent->m_pChannel );
if ( m_pClientToServerConnection == pDisconnectEvent->m_pChannel )
{
m_pClientToServerConnection = NULL;
}
}
break;
*/
case NETWORK_EVENT_MESSAGE_RECEIVED:
{
NetworkMessageReceivedEvent_t* pPacketEvent = static_cast<NetworkMessageReceivedEvent_t*>( pEvent );
NotifyListeners( NETWORK_MESSAGE_SERVER_TO_CLIENT, pPacketEvent->m_pNetworkMessage );
}
break;
}
}
}
//-----------------------------------------------------------------------------
// Process messages received by the server
//-----------------------------------------------------------------------------
void CNetworkManager::ProcessServerMessages()
{
NetworkEvent_t *pEvent = g_pNetworkSystem->FirstNetworkEvent();
for ( ; pEvent; pEvent = g_pNetworkSystem->NextNetworkEvent( ) )
{
switch ( pEvent->m_nType )
{
case NETWORK_EVENT_CONNECTED:
{
NetworkConnectionEvent_t* pConnectEvent = static_cast<NetworkConnectionEvent_t*>( pEvent );
m_ServerToClientConnection.AddToTail( pConnectEvent->m_pChannel );
}
break;
case NETWORK_EVENT_DISCONNECTED:
{
NetworkDisconnectionEvent_t* pDisconnectEvent = static_cast<NetworkDisconnectionEvent_t*>( pEvent );
m_ServerToClientConnection.FindAndRemove( pDisconnectEvent->m_pChannel );
}
break;
case NETWORK_EVENT_MESSAGE_RECEIVED:
{
NetworkMessageReceivedEvent_t* pPacketEvent = static_cast<NetworkMessageReceivedEvent_t*>( pEvent );
NotifyListeners( NETWORK_MESSAGE_CLIENT_TO_SERVER, pPacketEvent->m_pNetworkMessage );
}
break;
}
}
}
//-----------------------------------------------------------------------------
// Per-frame update
//-----------------------------------------------------------------------------
void CNetworkManager::Update( )
{
if ( IsClient() )
{
g_pNetworkSystem->ClientSendMessages();
}
if ( IsServer() )
{
g_pNetworkSystem->ServerReceiveMessages();
ProcessServerMessages();
g_pNetworkSystem->ServerSendMessages();
}
if ( IsClient() )
{
g_pNetworkSystem->ClientReceiveMessages();
ProcessClientMessages();
}
}