-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetwork.cpp
executable file
·563 lines (434 loc) · 15 KB
/
Network.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
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
// ***********************************************************************
//
// File: Network.cpp
// Programmer: T.J. Eason
// Project: Game Engine
// Description: DirectPlay wrapper class to handle system network messages
// Reference: DirectX Manual & GameDev.net
// Date: 3-3-10
// Revision 1: 3-4-10
// Revision: 2: 3-9-10
// Revision 3: 3-10-10
// Revision 4: 3-24-10
// Revision 5: 3-25-10
//
// ***********************************************************************
#include "Engine.h"
// Network class constructor
Network::Network( GUID guid, void ( *HandleNetworkMessageFunction ) ( ReceivedMessage *msg ) )
{
// Initiallize crtical section
InitializeCriticalSection( &m_sessionCS );
InitializeCriticalSection( &m_playerCS );
InitializeCriticalSection( &m_messageCS );
// Invalidate DirectPlay peer interface and devce
m_dpp = NULL;
m_device = NULL;
// Store game's GUID
memcpy( &m_guid, &guid, sizeof( GUID ) );
// Create new session list
m_sessions = new LinkedList< SessionInfo >;
// Create new player list
m_players = new LinkedList< PlayerInfo >;
// Create new network message list
m_messages = new LinkedList<ReceivedMessage >;
// Load network settings
Script *settings = new Script( "NetworkSettings.txt" );
// Check default network settings
if( settings ->GetNumberData( "processing_time" ) == NULL )
{
m_port = 2509;
m_sendTimeOut = 100;
m_processingTime = 100;
}
// Retrieve local network settings
else
{
m_port = *settings ->GetNumberData( "port" );
m_sendTimeOut = *settings ->GetNumberData( "send_time_out" );
m_processingTime = *settings ->GetNumberData( "processing_time" );
}
// Delete Network settings
SAFE_DELETE( settings );
// Network not allowed to receive messages
m_receiveAllowed = false;
// Set network message handler
HandleNetworkMessage = HandleNetworkMessageFunction;
// Create DirectPlay peer interface
CoCreateInstance( CLSID_DirectPlay8Peer, NULL, CLSCTX_INPROC, IID_IDirectPlay8Peer, ( void** ) &m_dpp );
// Initialize peer interface
m_dpp ->Initialize( ( PVOID ) this, NetworkMessageHandler, DPNINITIALIZE_HINT_LANSESSION );
// Create device address
CoCreateInstance( CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC, IID_IDirectPlay8Address, ( LPVOID* ) &m_device );
// Set up the device address
m_device ->SetSP( &CLSID_DP8SP_TCPIP );
m_device ->AddComponent( DPNA_KEY_PORT, &m_port, sizeof( DWORD ), DPNA_DATATYPE_DWORD );
}
// Network class destructor
Network::~Network()
{
// Save network messages
Script *settings = new Script( "NetworkSettings.txt" );
// Check if orginal network settings exist
if( settings ->GetNumberData( "processing_time" ) == NULL )
{
settings ->AddVariable( "port", VARIABLE_NUMBER, &m_port );
settings ->AddVariable( "send_time_out", VARIABLE_NUMBER, &m_sendTimeOut );
settings ->AddVariable( "processing_time", VARIABLE_NUMBER, &m_processingTime );
}
// Current network settings already exist
else
{
settings ->SetVariable( "port", &m_port );
settings ->SetVariable( "send_time_out", &m_sendTimeOut );
settings ->SetVariable( "processing_time", &m_processingTime );
}
settings ->SaveScript();
SAFE_DELETE( settings );
// Release device address
if( m_device )
{
m_device ->Release();
m_device = NULL;
}
// Close DirectPlay peer interface
if( m_dpp != NULL )
m_dpp ->Close( DPNCLOSE_IMMEDIATE );
// Relase DirectPlay peer interface
if( m_dpp )
{
m_dpp ->Release();
m_dpp = NULL;
}
// Destroy session list
SAFE_DELETE( m_sessions );
// Destroy player list
SAFE_DELETE( m_players );
// Destroy network message list
SAFE_DELETE( m_messages );
// Delete critical sections
DeleteCriticalSection( &m_sessionCS );
DeleteCriticalSection( &m_playerCS );
DeleteCriticalSection( &m_messageCS );
}
// Update network to progress messages
void Network::Update()
{
EnterCriticalSection( &m_messageCS );
ReceivedMessage *message = m_messages ->GetFirst();
unsigned long endTime = timeGetTime() + m_processingTime;
// Handle elapsed time for network messages
while( endTime > timeGetTime() && message != NULL )
{
HandleNetworkMessage( message );
m_messages ->Remove( &message );
message = m_messages ->GetFirst();
}
LeaveCriticalSection( &m_messageCS );
}
// Enumerate local network sessions
void Network::EnumerateSessions()
{
// Emply lists
m_players ->Empty();
m_messages ->Empty();
m_sessions ->Empty();
// Prepare application description
DPN_APPLICATION_DESC description;
ZeroMemory( &description, sizeof( DPN_APPLICATION_DESC ) );
description.dwSize = sizeof( DPN_APPLICATION_DESC );
description.guidApplication = m_guid;
// Synchronize enummerated sessions
m_dpp ->EnumHosts( &description, NULL, m_device, NULL, 0, 1, 0, 0, NULL, NULL, DPNENUMHOSTS_SYNC );
}
// Get a host session
bool Network::Host( char *name, char *session, int players, void *playerData, unsigned long dataSize )
{
WCHAR wide[MAX_PATH];
// Prepare player information
DPN_PLAYER_INFO player;
ZeroMemory( &player, sizeof( DPN_PLAYER_INFO ) );
player.dwSize = sizeof( DPN_PLAYER_INFO );
player.pvData =playerData;
player.dwInfoFlags = DPNINFO_NAME | DPNINFO_DATA;
mbstowcs( wide, name, MAX_PATH );
player.pwszName = wide;
// Setup player information
if( FAILED( m_dpp ->SetPeerInfo( &player, NULL, NULL, DPNSETPEERINFO_SYNC ) ) )
return false;
// Prepare application description
DPN_APPLICATION_DESC description;
ZeroMemory( &description, sizeof( DPN_APPLICATION_DESC ) );
description.dwSize = sizeof( DPN_APPLICATION_DESC );
description.guidApplication = m_guid;
description.dwMaxPlayers = players;
mbstowcs( wide, session, MAX_PATH );
description.pwszSessionName = wide;
// Host session
if( FAILED( m_dpp ->Host( &description, &m_device, 1, NULL, NULL, NULL, 0 ) ) )
return false;
return true;
}
// Join an enumerated session
bool Network::Join( char *name, int session, void *playerData, unsigned long dataSize )
{
WCHAR wide[MAX_PATH];
// Empty lists
m_players ->Empty();
m_messages ->Empty();
// Ignore invalid sessions
if( session < 0 )
return false;
// Prepare player information
DPN_PLAYER_INFO player;
ZeroMemory( &player, sizeof( DPN_PLAYER_INFO ) );
player.dwSize = sizeof( DPN_PLAYER_INFO );
player.pvData =playerData;
player.dwInfoFlags = DPNINFO_NAME | DPNINFO_DATA;
mbstowcs( wide, name, MAX_PATH );
player.pwszName = wide;
// Setup player information
if( FAILED( m_dpp ->SetPeerInfo( &player, NULL, NULL, DPNSETPEERINFO_SYNC ) ) )
return false;
// Enter sessions linked list critical session
EnterCriticalSection( &m_sessionCS );
m_sessions ->Iterate( true );
// Find the host of the enumerated session
for( int s = 0; s < session + 1; s++ )
{
// Check if an session exists
if( m_sessions ->Iterate() == NULL )
{
LeaveCriticalSection( &m_sessionCS );
return false;
}
}
// Join session
if( FAILED( m_dpp ->Connect( &m_sessions ->GetCurrent() ->description, m_sessions ->GetCurrent() ->address, m_device, NULL, NULL, NULL, 0, NULL, NULL, NULL, DPNCONNECT_SYNC ) ) )
{
LeaveCriticalSection( &m_sessionCS );
return false;
}
// Exit session's linked list critical section
LeaveCriticalSection( &m_sessionCS );
return true;
}
// Terminate current session
void Network::Terminate()
{
// Host only has permission to terminate session
if( m_dpnidHost == m_dpnidLocal )
m_dpp -> TerminateSession( NULL, 0, 0 );
// Close connection and uninitialize DirectPlay peer interface
if( m_dpp != NULL )
m_dpp ->Close( DPNCLOSE_IMMEDIATE );
// Initialize DirectPlay peer interface
m_dpp ->Initialize( ( PVOID ) this, NetworkMessageHandler, DPNINITIALIZE_HINT_LANSESSION );
}
// Set received allowed flag
void Network::SetReceivedAllowed( bool allowed )
{
m_receiveAllowed = allowed;
}
// Get next iterated session in list
SessionInfo *Network::GetNextSession( bool restart )
{
EnterCriticalSection( &m_sessionCS );
m_sessions ->Iterate( restart );
// Check session status
if( restart == true )
m_sessions ->Iterate();
LeaveCriticalSection( &m_sessionCS );
return m_sessions ->GetCurrent();
}
// Get player information
PlayerInfo *Network::GetPlayer( DPNID dpnid )
{
EnterCriticalSection( &m_playerCS );
m_players ->Iterate( true );
// Search for player data through iterated list
while( m_players ->Iterate() )
{
// Retrieve current player's stats
if( m_players ->GetCurrent() ->dpnid == dpnid )
{
LeaveCriticalSection( &m_playerCS );
return m_players ->GetCurrent();
}
}
LeaveCriticalSection( &m_playerCS );
return NULL;
}
// Get local player's DirectPlay ID
DPNID Network::GetLocalID()
{
return m_dpnidLocal;
}
// Get host's DirectPlay ID
DPNID Network::GetHostID()
{
return m_dpnidHost;
}
// Determines if player is host or not
bool Network::isHost()
{
// Check if player is host
if( m_dpnidHost == m_dpnidLocal )
return true;
// Player is client
else
return false;
}
// Send network message
void Network::Send( void *data, long size, DPNID dpnid, long flags )
{
DPNHANDLE hAsync;
DPN_BUFFER_DESC dpbd;
// Check buffer size
if ( ( dpbd.dwBufferSize = size ) == 0 )
return;
// Buffer message data
dpbd.pBufferData = ( BYTE* ) data;
// Send message
m_dpp ->SendTo( dpnid, &dpbd, 1, m_sendTimeOut, NULL, &hAsync, flags | DPNSEND_NOCOMPLETE | DPNSEND_COALESCE );
}
// Network message handler
HRESULT WINAPI Network::NetworkMessageHandler( PVOID context, DWORD msgid, PVOID data )
{
// Get pointer to call next network object
Network *network = ( Network* ) context;
// Process incoming message based on type
switch( msgid )
{
case DPN_MSGID_CREATE_PLAYER:
{
unsigned long size = 0;
DPN_PLAYER_INFO *info = NULL;
HRESULT hr = DPNERR_CONNECTING;
PDPNMSG_CREATE_PLAYER msgCreatePlayer = ( PDPNMSG_CREATE_PLAYER ) data;
// Create player information for new player
PlayerInfo *playerInfo = new PlayerInfo;
ZeroMemory( playerInfo, sizeof( PlayerInfo ) );
playerInfo ->dpnid = msgCreatePlayer ->dpnidPlayer;
// Keep calling GetPeerInfo() to try to connect
while( hr == DPNERR_CONNECTING )
hr = network ->m_dpp ->GetPeerInfo( playerInfo ->dpnid, info, &size, 0 );
// Check if GetPeerInfo() has returned size of DPN_PLAYER_INFO structure
if( hr == DPNERR_BUFFERTOOSMALL )
{
info = ( DPN_PLAYER_INFO* ) new BYTE[size];
ZeroMemory( info, size );
info ->dwSize = sizeof ( DPN_PLAYER_INFO );
// Try again using the correct size
if( SUCCEEDED( network ->m_dpp ->GetPeerInfo( playerInfo ->dpnid, info, &size, 0 ) ) )
{
// Store name of new player
playerInfo ->name = new char[wcslen( info ->pwszName ) + 1];
ZeroMemory( playerInfo ->name, wcslen( info ->pwszName) + 1 );
wcstombs( playerInfo ->name, info ->pwszName, wcslen( info ->pwszName ) );
// Store player data
playerInfo ->data = new BYTE[info ->dwDataSize];
memcpy( playerInfo ->data, info ->pvData, info ->dwDataSize );
playerInfo ->size = info ->dwDataSize;
// Store client details
if( info ->dwPlayerFlags & DPNPLAYER_LOCAL )
network ->m_dpnidLocal = playerInfo ->dpnid;
// Store host details
if( info ->dwPlayerFlags & DPNPLAYER_HOST )
network ->m_dpnidHost = playerInfo ->dpnid;
}
SAFE_DELETE_ARRAY( info );
}
// Add new player to list
EnterCriticalSection( &network ->m_playerCS );
network ->m_players ->Add( playerInfo );
LeaveCriticalSection( &network ->m_playerCS );
// Check if message handler exists
if( network ->HandleNetworkMessage == NULL )
break;
// Create a create player message
ReceivedMessage *message = new ReceivedMessage;
message ->msgid = MSGID_CREATE_PLAYER;
message ->dpnid = playerInfo ->dpnid;
// Store message to be processed later
EnterCriticalSection( &network ->m_messageCS );
network ->m_messages ->Add( message );
LeaveCriticalSection( &network ->m_messageCS );
break;
}
case DPN_MSGID_DESTROY_PLAYER:
{
// Find player to destroy and remove from list
EnterCriticalSection( &network ->m_playerCS );
network ->m_players ->Iterate( true );
// Search through list for player to destroy
while( network ->m_players ->Iterate() )
{
// Check status of player
if( network ->m_players ->GetCurrent() ->dpnid == ( ( PDPNMSG_DESTROY_PLAYER ) data ) ->dpnidPlayer )
{
network ->m_players ->Remove( ( PlayerInfo** ) network ->m_players ->GetCurrent() );
break;
}
}
LeaveCriticalSection( &network ->m_playerCS );
// Check message handler exists
if( network ->HandleNetworkMessage == NULL )
break;
// Create a destroy player message
ReceivedMessage *message = new ReceivedMessage;
message->msgid = MSGID_DESTROY_PLAYER;
message ->dpnid = ( ( PDPNMSG_DESTROY_PLAYER ) data ) ->dpnidPlayer;
// Store message so it can be processed later
EnterCriticalSection( &network ->m_messageCS );
network ->m_messages ->Add( message );
LeaveCriticalSection( &network ->m_messageCS );
break;
}
case DPN_MSGID_ENUM_HOSTS_RESPONSE:
{
PDPNMSG_ENUM_HOSTS_RESPONSE response = ( PDPNMSG_ENUM_HOSTS_RESPONSE ) data;
// Create new session information
SessionInfo *sessionInfo = new SessionInfo;
response ->pAddressSender ->Duplicate( &sessionInfo ->address );
memcpy( &sessionInfo ->description, response ->pApplicationDescription, sizeof( DPN_APPLICATION_DESC ) );
// Add new session to the list
EnterCriticalSection( &network ->m_sessionCS );
network ->m_sessions ->Add( sessionInfo );
LeaveCriticalSection( &network ->m_sessionCS );
break;
}
case DPN_MSGID_RECEIVE:
{
// Check if message handler exists
if( network ->HandleNetworkMessage == NULL )
break;
// Check if network is allowed to receive messages
if( network ->m_receiveAllowed == false )
break;
// Create receive message
ReceivedMessage *message = new ReceivedMessage;
memcpy( message, ( ( PDPNMSG_RECEIVE ) data ) ->pReceiveData, ( ( PDPNMSG_RECEIVE ) data ) ->dwReceiveDataSize );
// Store message to be process later
EnterCriticalSection( &network ->m_messageCS );
network ->m_messages ->Add( message );
LeaveCriticalSection( &network ->m_messageCS );
break;
}
case DPN_MSGID_TERMINATE_SESSION:
{
// Check if message handler exists
if( network ->HandleNetworkMessage == NULL )
break;
// Create terminate session message
ReceivedMessage *message = new ReceivedMessage;
message ->msgid = MSGID_TERMINATE_SESSION;
// Store message to be processed later
EnterCriticalSection( &network ->m_messageCS );
network ->m_messages ->Add( message );
LeaveCriticalSection( &network ->m_messageCS );
break;
}
}
return S_OK;
}