/
NetworkManager.cpp
767 lines (657 loc) · 18.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
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
#include "RoboCatPCH.h"
#include <zlib.h>
unique_ptr< NetworkManager > NetworkManager::sInstance;
namespace
{
const float kTimeBetweenDelayHeartbeat = 1.f;
const float kStartDelay = 3.0f;
const int kSubTurnsPerTurn = 3;
const int kMaxPlayerCount = 4;
}
bool NetworkManager::StaticInit()
{
sInstance = std::make_unique< NetworkManager >();
return sInstance->Init();
}
NetworkManager::NetworkManager() :
mBytesSentThisFrame( 0 ),
mDropPacketChance( 0.f ),
mSimulatedLatency( 0.f ),
mBytesReceivedPerSecond( WeightedTimedMovingAverage( 1.f ) ),
mBytesSentPerSecond(WeightedTimedMovingAverage( 1.f )),
mPlayerId( 0 ),
mLobbyId( 0 ),
mNewNetworkId( 1 ),
mIsMasterPeer( false ),
mState( NMS_Unitialized ),
mPlayerCount( 0 ),
mReadyCount( 0 ),
mDelayHeartbeat( kTimeBetweenDelayHeartbeat ),
mTimeToStart( -1.0f ),
//we always start on turn -2 b/c we need 2 frames before we can actually play
mTurnNumber( -2 ),
mSubTurnNumber( 0 )
{
//this is enough for a 166 minute game...
//so let's avoid realloc/copies and just construct all the empty maps, too
mTurnData.resize( 100000 );
}
NetworkManager::~NetworkManager()
{
}
bool NetworkManager::Init()
{
//set my player info from steam
mPlayerId = GamerServices::sInstance->GetLocalPlayerId();
mName = GamerServices::sInstance->GetLocalPlayerName();
//begin the search for a lobby
mState = NMS_Searching;
GamerServices::sInstance->LobbySearchAsync();
return true;
}
void NetworkManager::ProcessIncomingPackets()
{
ReadIncomingPacketsIntoQueue();
ProcessQueuedPackets();
UpdateBytesSentLastFrame();
}
void NetworkManager::SendOutgoingPackets()
{
switch ( mState )
{
case NMS_Starting:
UpdateStarting();
break;
case NMS_Playing:
UpdateSendTurnPacket();
break;
default:
break;
}
}
void NetworkManager::UpdateDelay()
{
//first process incoming packets, in case that removes us from delay
NetworkManager::sInstance->ProcessIncomingPackets();
if( mState == NMS_Delay )
{
mDelayHeartbeat -= Timing::sInstance.GetDeltaTime();
if( mDelayHeartbeat <= 0.0f )
{
mDelayHeartbeat = kTimeBetweenDelayHeartbeat;
}
//find out who's missing and send them a heartbeat
unordered_set< uint64_t > playerSet;
for( auto& iter : mPlayerNameMap )
{
playerSet.emplace( iter.first );
}
Int64ToTurnDataMap& turnData = mTurnData[ mTurnNumber + 1 ];
for( auto& iter : turnData )
{
playerSet.erase( iter.first );
}
OutputMemoryBitStream packet;
packet.Write( kDelayCC );
//whoever's left is who's missing
for( auto& iter : playerSet )
{
SendPacket( packet, iter );
}
}
}
void NetworkManager::UpdateStarting()
{
mTimeToStart -= Timing::sInstance.GetDeltaTime();
if ( mTimeToStart <= 0.0f )
{
EnterPlayingState();
}
}
void NetworkManager::UpdateSendTurnPacket()
{
mSubTurnNumber++;
if ( mSubTurnNumber == kSubTurnsPerTurn )
{
//create our turn data
TurnData data(mPlayerId, RandGen::sInstance->GetRandomUInt32(0, UINT32_MAX),
ComputeGlobalCRC(), InputManager::sInstance->GetCommandList());
//we need to send a turn packet to all of our peers
OutputMemoryBitStream packet;
packet.Write( kTurnCC );
//we're sending data for 2 turns from now
packet.Write( mTurnNumber + 2 );
packet.Write( mPlayerId );
data.Write( packet );
for ( auto &iter : mPlayerNameMap )
{
if( iter.first != mPlayerId )
{
SendPacket( packet, iter.first );
}
}
//save our turn data for turn + 2
mTurnData[ mTurnNumber + 2 ].emplace( mPlayerId, data );
InputManager::sInstance->ClearCommandList();
if ( mTurnNumber >= 0 )
{
TryAdvanceTurn();
}
else
{
//a negative turn means there's no possible commands yet
mTurnNumber++;
mSubTurnNumber = 0;
}
}
}
void NetworkManager::TryAdvanceTurn()
{
//only advance the turn IF we received the data for everyone
if ( mTurnData[ mTurnNumber + 1 ].size() == mPlayerCount )
{
if ( mState == NMS_Delay )
{
//throw away any input accrued during delay
InputManager::sInstance->ClearCommandList();
mState = NMS_Playing;
//wait 100ms to give the slow peer a chance to catch up
SDL_Delay( 100 );
}
mTurnNumber++;
mSubTurnNumber = 0;
if ( CheckSync( mTurnData[ mTurnNumber ] ) )
{
//process all the moves for this turn
for ( auto& iter : mTurnData[ mTurnNumber ] )
{
iter.second.GetCommandList().ProcessCommands( iter.first );
}
//since we're still in sync, let's check for achievements
CheckForAchievements();
}
else
{
//for simplicity, just kill the game if it desyncs
LOG( "DESYNC: Game over man, game over." );
Engine::sInstance->SetShouldKeepRunning( false );
}
}
else
{
//don't have all player's turn data, we have to delay :(
mState = NMS_Delay;
LOG( "Going into delay state, don't have all the info for turn %d", mTurnNumber + 1);
}
}
void NetworkManager::ProcessPacket( InputMemoryBitStream& inInputStream, uint64_t inFromPlayer )
{
switch ( mState )
{
case NMS_Lobby:
ProcessPacketsLobby( inInputStream, inFromPlayer );
break;
case NMS_Ready:
ProcessPacketsReady( inInputStream, inFromPlayer );
break;
case NMS_Starting:
//if I'm starting and get a packet, treat this as playing
ProcessPacketsPlaying( inInputStream, inFromPlayer );
break;
case NMS_Playing:
ProcessPacketsPlaying( inInputStream, inFromPlayer );
break;
case NMS_Delay:
ProcessPacketsDelay( inInputStream, inFromPlayer );
break;
default:
break;
}
}
void NetworkManager::ProcessPacketsLobby( InputMemoryBitStream& inInputStream, uint64_t inFromPlayer )
{
//should only be a ready packet
uint32_t packetType;
inInputStream.Read( packetType );
switch ( packetType )
{
case kReadyCC:
HandleReadyPacket( inInputStream, inFromPlayer );
break;
default:
//ignore anything else
break;
}
}
void NetworkManager::ProcessPacketsReady( InputMemoryBitStream& inInputStream, uint64_t inFromPlayer )
{
//could be another ready packet or a start packet
uint32_t packetType;
inInputStream.Read( packetType );
switch( packetType )
{
case kReadyCC:
HandleReadyPacket( inInputStream, inFromPlayer );
break;
case kStartCC:
HandleStartPacket( inInputStream, inFromPlayer );
break;
default:
//ignore anything else
break;
}
}
void NetworkManager::HandleReadyPacket( InputMemoryBitStream& inInputStream, uint64_t inFromPlayer )
{
//if this is my first ready packet, I need to let everyone else know I'm ready
if( mReadyCount == 0 )
{
SendReadyPacketsToPeers();
//I'm ready now also, so an extra increment here
mReadyCount++;
mState = NMS_Ready;
}
mReadyCount++;
TryStartGame();
}
void NetworkManager::SendReadyPacketsToPeers()
{
OutputMemoryBitStream outPacket;
outPacket.Write( kReadyCC );
for( auto& iter : mPlayerNameMap )
{
if( iter.first != mPlayerId )
{
SendPacket( outPacket, iter.first );
}
}
}
void NetworkManager::HandleStartPacket( InputMemoryBitStream& inInputStream, uint64_t inFromPlayer )
{
//make sure this is the master peer, cause we don't want any funny business
if ( inFromPlayer == mMasterPeerId )
{
LOG( "Got the orders to go!" );
//get the rng seed
uint32_t seed;
inInputStream.Read( seed );
RandGen::sInstance->Seed( seed );
//for now, assume that we're one frame off, but ideally we would RTT to adjust
//the time to start, based on latency/jitter
mState = NMS_Starting;
mTimeToStart = kStartDelay - Timing::sInstance.GetDeltaTime();
}
}
void NetworkManager::ProcessPacketsPlaying( InputMemoryBitStream& inInputStream, uint64_t inFromPlayer )
{
uint32_t packetType;
inInputStream.Read( packetType );
switch ( packetType )
{
case kTurnCC:
HandleTurnPacket( inInputStream, inFromPlayer );
break;
default:
//ignore anything else
break;
}
}
void NetworkManager::HandleTurnPacket( InputMemoryBitStream& inInputStream, uint64_t inFromPlayer )
{
int turnNum;
uint64_t playerId;
inInputStream.Read( turnNum );
inInputStream.Read( playerId );
if ( playerId != inFromPlayer )
{
LOG( "We received turn data for a different player Id...stop trying to cheat!" );
return;
}
TurnData data;
data.Read( inInputStream );
mTurnData[ turnNum ].emplace( playerId, data );
}
void NetworkManager::ProcessPacketsDelay( InputMemoryBitStream& inInputStream, uint64_t inFromPlayer )
{
//the only packet we can even consider here is an input one, since we
//only can only enter delay after we've been playing
uint32_t packetType;
inInputStream.Read( packetType );
if ( packetType == kTurnCC )
{
HandleTurnPacket( inInputStream, inFromPlayer );
//if we're lucky, maybe this was the packet we were waiting on?
TryAdvanceTurn();
}
}
void NetworkManager::HandleConnectionReset( uint64_t inFromPlayer )
{
//remove this player from our maps
if( mPlayerNameMap.find( inFromPlayer ) != mPlayerNameMap.end() )
{
LOG( "Player %llu disconnected", inFromPlayer );
mPlayerNameMap.erase( inFromPlayer );
ScoreBoardManager::sInstance->RemoveEntry( inFromPlayer );
mPlayerCount--;
//if we were in delay, then let's see if we can continue now that this player DC'd?
if ( mState == NMS_Delay )
{
TryAdvanceTurn();
}
}
}
void NetworkManager::ReadIncomingPacketsIntoQueue()
{
//should we just keep a static one?
char packetMem[ 1500 ];
size_t packetSize = sizeof( packetMem );
uint32_t incomingSize = 0;
InputMemoryBitStream inputStream( packetMem, packetSize * 8 );
uint64_t fromPlayer;
//keep reading until we don't have anything to read ( or we hit a max number that we'll process per frame )
int receivedPackedCount = 0;
int totalReadByteCount = 0;
while( GamerServices::sInstance->IsP2PPacketAvailable( incomingSize ) &&
receivedPackedCount < kMaxPacketsPerFrameCount )
{
if( incomingSize <= packetSize )
{
uint32_t readByteCount = GamerServices::sInstance->ReadP2PPacket( packetMem, packetSize, fromPlayer );
if( readByteCount > 0 )
{
inputStream.ResetToCapacity( readByteCount );
++receivedPackedCount;
totalReadByteCount += readByteCount;
//shove the packet into the queue and we'll handle it as soon as we should...
//we'll pretend it wasn't received until simulated latency from now
//this doesn't sim jitter, for that we would need to.....
float simulatedReceivedTime = Timing::sInstance.GetTimef() + mSimulatedLatency;
mPacketQueue.emplace( simulatedReceivedTime, inputStream, fromPlayer );
}
}
}
if( totalReadByteCount > 0 )
{
mBytesReceivedPerSecond.UpdatePerSecond( static_cast< float >( totalReadByteCount ) );
}
}
void NetworkManager::ProcessQueuedPackets()
{
//look at the front packet...
while( !mPacketQueue.empty() )
{
ReceivedPacket& nextPacket = mPacketQueue.front();
if( Timing::sInstance.GetTimef() > nextPacket.GetReceivedTime() )
{
ProcessPacket( nextPacket.GetPacketBuffer(), nextPacket.GetFromPlayer() );
mPacketQueue.pop();
}
else
{
break;
}
}
}
void NetworkManager::EnterPlayingState()
{
mState = NMS_Playing;
//leave the lobby now that we're playing
GamerServices::sInstance->LeaveLobby( mLobbyId );
//create scoreboard entry for each player
for ( auto& iter : mPlayerNameMap )
{
ScoreBoardManager::sInstance->AddEntry( iter.first, iter.second );
//everyone gets a score of 3 cause 3 cats
ScoreBoardManager::sInstance->GetEntry( iter.first )->SetScore( 3 );
}
//spawn a cat for each player
float halfWidth = kWorldWidth / 2.0f;
float halfHeight = kWorldHeight / 2.0f;
// ( pos.x, pos.y, rot )
std::array<Vector3, 4> spawnLocs = {
Vector3( -halfWidth + halfWidth / 5, -halfHeight + halfHeight / 5, 2.35f ), // UP-LEFT
Vector3( -halfWidth + halfWidth / 5, halfHeight - halfHeight / 4, -5.49f ), // DOWN-LEFT
Vector3( halfWidth - halfWidth / 5, halfHeight - halfHeight / 4, -0.78f ), // DOWN-RIGHT
Vector3( halfWidth - halfWidth / 5, -halfHeight + halfHeight / 4, -2.35f ), // UP-RIGHT
};
//use this to randomize location of cats
std::array<int, 4> indices = { 0, 1, 2, 3 };
std::shuffle( indices.begin(), indices.end(), RandGen::sInstance->GetGeneratorRef() );
const float kCatOffset = 1.0f;
int i = 0;
for ( auto& iter : mPlayerNameMap )
{
Vector3 spawnVec = spawnLocs[ indices[ i ] ];
//spawn 3 cats per player
SpawnCat( iter.first, spawnVec );
if ( spawnVec.mX > 0.0f )
{
SpawnCat( iter.first, Vector3( spawnVec.mX - kCatOffset, spawnVec.mY, spawnVec.mZ ) );
}
else
{
SpawnCat( iter.first, Vector3( spawnVec.mX + kCatOffset, spawnVec.mY, spawnVec.mZ ) );
}
if ( spawnVec.mY > 0.0f )
{
SpawnCat( iter.first, Vector3( spawnVec.mX, spawnVec.mY - kCatOffset, spawnVec.mZ ) );
}
else
{
SpawnCat( iter.first, Vector3( spawnVec.mX, spawnVec.mY + kCatOffset, spawnVec.mZ ) );
}
i++;
}
//Increment games played stat
GamerServices::sInstance->AddToStat( GamerServices::Stat_NumGames, 1 );
}
void NetworkManager::SpawnCat( uint64_t inPlayerId, const Vector3& inSpawnVec )
{
RoboCatPtr cat = std::static_pointer_cast< RoboCat >( GameObjectRegistry::sInstance->CreateGameObject( 'RCAT' ) );
cat->SetColor( ScoreBoardManager::sInstance->GetEntry( inPlayerId )->GetColor() );
cat->SetPlayerId( inPlayerId );
cat->SetLocation( Vector3( inSpawnVec.mX, inSpawnVec.mY, 0.0f ) );
cat->SetRotation( inSpawnVec.mZ );
}
void NetworkManager::CheckForAchievements()
{
for( int i = 0; i < GamerServices::MAX_ACHIEVEMENT; ++i )
{
GamerServices::Achievement ach = static_cast< GamerServices::Achievement >( i );
switch( ach )
{
case GamerServices::ACH_WIN_ONE_GAME:
if( GamerServices::sInstance->GetStatInt( GamerServices::Stat_NumWins ) > 0 )
{
GamerServices::sInstance->UnlockAchievement( ach );
}
break;
case GamerServices::ACH_WIN_100_GAMES:
if( GamerServices::sInstance->GetStatInt( GamerServices::Stat_NumWins ) >= 100 )
{
GamerServices::sInstance->UnlockAchievement( ach );
}
break;
case GamerServices::ACH_TRAVEL_FAR_ACCUM:
case GamerServices::ACH_TRAVEL_FAR_SINGLE:
default:
//nothing for these
break;
}
}
}
bool NetworkManager::CheckSync( Int64ToTurnDataMap& inTurnMap )
{
auto iter = inTurnMap.begin();
uint32_t expectedRand = iter->second.GetRandomValue();
uint32_t expectedCRC = iter->second.GetCRC();
++iter;
while ( iter != inTurnMap.end() )
{
if ( expectedRand != iter->second.GetRandomValue() )
{
LOG( "Random is out of sync for player %llu on turn %d", iter->second.GetPlayerId(), mTurnNumber );
return false;
}
if ( expectedCRC != iter->second.GetCRC() )
{
LOG( "CRC is out of sync for player %llu on turn %d", iter->second.GetPlayerId(), mTurnNumber );
return false;
}
++iter;
}
return true;
}
void NetworkManager::SendPacket( const OutputMemoryBitStream& inOutputStream, uint64_t inToPlayer )
{
GamerServices::sInstance->SendP2PReliable( inOutputStream, inToPlayer );
}
void NetworkManager::EnterLobby( uint64_t inLobbyId )
{
mLobbyId = inLobbyId;
mState = NMS_Lobby;
UpdateLobbyPlayers();
}
void NetworkManager::UpdateLobbyPlayers()
{
//we only want to update player counts in lobby before we're starting
if( mState < NMS_Starting )
{
mPlayerCount = GamerServices::sInstance->GetLobbyNumPlayers( mLobbyId );
mMasterPeerId = GamerServices::sInstance->GetMasterPeerId( mLobbyId );
//am I the master peer now?
if( mMasterPeerId == mPlayerId )
{
mIsMasterPeer = true;
}
GamerServices::sInstance->GetLobbyPlayerMap( mLobbyId, mPlayerNameMap );
//this might allow us to start
TryStartGame();
}
}
void NetworkManager::TryStartGame()
{
if ( mState == NMS_Ready && IsMasterPeer() && mPlayerCount == mReadyCount )
{
LOG( "Starting!" );
//let everyone know
OutputMemoryBitStream outPacket;
outPacket.Write( kStartCC );
//select a seed value
uint32_t seed = RandGen::sInstance->GetRandomUInt32( 0, UINT32_MAX );
RandGen::sInstance->Seed( seed );
outPacket.Write( seed );
for ( auto &iter : mPlayerNameMap )
{
if( iter.first != mPlayerId )
{
SendPacket( outPacket, iter.first );
}
}
mTimeToStart = kStartDelay;
mState = NMS_Starting;
}
}
void NetworkManager::TryReadyGame()
{
if( mState == NMS_Lobby && IsMasterPeer() )
{
LOG( "Master peer readying up!" );
//let the gamer services know we're readying up
GamerServices::sInstance->SetLobbyReady( mLobbyId );
SendReadyPacketsToPeers();
mReadyCount = 1;
mState = NMS_Ready;
//we might be ready to start
TryStartGame();
}
}
void NetworkManager::UpdateBytesSentLastFrame()
{
if( mBytesSentThisFrame > 0 )
{
mBytesSentPerSecond.UpdatePerSecond( static_cast< float >( mBytesSentThisFrame ) );
mBytesSentThisFrame = 0;
}
}
NetworkManager::ReceivedPacket::ReceivedPacket( float inReceivedTime, InputMemoryBitStream& ioInputMemoryBitStream, uint64_t inFromPlayer ) :
mReceivedTime( inReceivedTime ),
mFromPlayer( inFromPlayer ),
mPacketBuffer( ioInputMemoryBitStream )
{
}
GameObjectPtr NetworkManager::GetGameObject( uint32_t inNetworkId ) const
{
auto gameObjectIt = mNetworkIdToGameObjectMap.find( inNetworkId );
if ( gameObjectIt != mNetworkIdToGameObjectMap.end() )
{
return gameObjectIt->second;
}
else
{
return GameObjectPtr();
}
}
GameObjectPtr NetworkManager::RegisterAndReturn( GameObject* inGameObject )
{
GameObjectPtr toRet( inGameObject );
RegisterGameObject( toRet );
return toRet;
}
void NetworkManager::UnregisterGameObject( GameObject* inGameObject )
{
int networkId = inGameObject->GetNetworkId();
auto iter = mNetworkIdToGameObjectMap.find( networkId );
if ( iter != mNetworkIdToGameObjectMap.end() )
{
mNetworkIdToGameObjectMap.erase( iter );
}
}
bool NetworkManager::IsPlayerInGame( uint64_t inPlayerId )
{
if( mPlayerNameMap.find( inPlayerId ) != mPlayerNameMap.end() )
{
return true;
}
else
{
return false;
}
}
void NetworkManager::AddToNetworkIdToGameObjectMap( GameObjectPtr inGameObject )
{
mNetworkIdToGameObjectMap[ inGameObject->GetNetworkId() ] = inGameObject;
}
void NetworkManager::RemoveFromNetworkIdToGameObjectMap( GameObjectPtr inGameObject )
{
mNetworkIdToGameObjectMap.erase( inGameObject->GetNetworkId() );
}
void NetworkManager::RegisterGameObject( GameObjectPtr inGameObject )
{
//assign network id
int newNetworkId = GetNewNetworkId();
inGameObject->SetNetworkId( newNetworkId );
//add mapping from network id to game object
mNetworkIdToGameObjectMap[ newNetworkId ] = inGameObject;
}
uint32_t NetworkManager::GetNewNetworkId()
{
uint32_t toRet = mNewNetworkId++;
if ( mNewNetworkId < toRet )
{
LOG( "Network ID Wrap Around!!! You've been playing way too long...", 0 );
}
return toRet;
}
uint32_t NetworkManager::ComputeGlobalCRC()
{
//save into bit stream to reduce CRC calls
OutputMemoryBitStream crcStream;
uint32_t crc = static_cast<uint32_t>( crc32( 0, Z_NULL, 0 ) );
for ( auto& iter : mNetworkIdToGameObjectMap )
{
iter.second->WriteForCRC( crcStream );
}
crc = static_cast<uint32_t>( crc32( crc, reinterpret_cast<const Bytef*>(crcStream.GetBufferPtr()), crcStream.GetByteLength() ) );
return crc;
}