1 #include "NativeFeatureIncludes.h"
2 #if _RAKNET_SUPPORT_ReplicaManager3==1
3
4 #include "ReplicaManager3.h"
5 #include "GetTime.h"
6 #include "MessageIdentifiers.h"
7 #include "RakPeerInterface.h"
8 #include "NetworkIDManager.h"
9
10 using namespace RakNet;
11
12 DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS(LastSerializationResult,Replica3*,replica);
13
14 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
15
operator ==(const PRO & right) const16 bool PRO::operator==( const PRO& right ) const
17 {
18 return priority == right.priority && reliability == right.reliability && orderingChannel == right.orderingChannel && sendReceipt == right.sendReceipt;
19 }
20
operator !=(const PRO & right) const21 bool PRO::operator!=( const PRO& right ) const
22 {
23 return priority != right.priority || reliability != right.reliability || orderingChannel != right.orderingChannel || sendReceipt != right.sendReceipt;
24 }
25
26
27 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
28
LastSerializationResult()29 LastSerializationResult::LastSerializationResult()
30 {
31 replica=0;
32 lastSerializationResultBS=0;
33 }
~LastSerializationResult()34 LastSerializationResult::~LastSerializationResult()
35 {
36 if (lastSerializationResultBS)
37 RakNet::OP_DELETE(lastSerializationResultBS,__FILE__,__LINE__);
38 }
AllocBS(void)39 void LastSerializationResult::AllocBS(void)
40 {
41 if (lastSerializationResultBS==0)
42 {
43 lastSerializationResultBS=RakNet::OP_NEW<LastSerializationResultBS>(__FILE__,__LINE__);
44 }
45 }
46 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
47
ReplicaManager3()48 ReplicaManager3::ReplicaManager3()
49 {
50 defaultSendParameters.orderingChannel=0;
51 defaultSendParameters.priority=HIGH_PRIORITY;
52 defaultSendParameters.reliability=RELIABLE_ORDERED;
53 defaultSendParameters.sendReceipt=0;
54 autoSerializeInterval=30;
55 lastAutoSerializeOccurance=0;
56 worldId=0;
57 autoCreateConnections=true;
58 autoDestroyConnections=true;
59 networkIDManager=0;
60 }
61
62 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
63
~ReplicaManager3()64 ReplicaManager3::~ReplicaManager3()
65 {
66 if (autoDestroyConnections)
67 {
68 // Clear() calls DeallocConnection(), which is pure virtual and cannot be called from the destructor
69 RakAssert(connectionList.GetSize()==0);
70 }
71 Clear();
72 }
73
74 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
75
SetAutoManageConnections(bool autoCreate,bool autoDestroy)76 void ReplicaManager3::SetAutoManageConnections(bool autoCreate, bool autoDestroy)
77 {
78 autoCreateConnections=autoCreate;
79 autoDestroyConnections=autoDestroy;
80 }
81
82 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
83
PushConnection(RakNet::Connection_RM3 * newConnection)84 bool ReplicaManager3::PushConnection(RakNet::Connection_RM3 *newConnection)
85 {
86 if (newConnection==0)
87 return false;
88 if (GetConnectionByGUID(newConnection->GetRakNetGUID()))
89 return false;
90 DataStructures::DefaultIndexType index = connectionList.GetInsertionIndex(newConnection);
91 if (index!=(DataStructures::DefaultIndexType)-1)
92 {
93 connectionList.InsertAtIndex(newConnection,index,__FILE__,__LINE__);
94
95 // Send message to validate the connection
96 newConnection->SendValidation(rakPeerInterface, worldId);
97
98 Connection_RM3::ConstructionMode constructionMode = newConnection->QueryConstructionMode();
99 if (constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
100 {
101 DataStructures::DefaultIndexType pushIdx;
102 for (pushIdx=0; pushIdx < userReplicaList.GetSize(); pushIdx++)
103 newConnection->OnLocalReference(userReplicaList[pushIdx], this);
104 }
105 }
106 return true;
107 }
108
109
110 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
111
PopConnection(DataStructures::DefaultIndexType index)112 RakNet::Connection_RM3 * ReplicaManager3::PopConnection(DataStructures::DefaultIndexType index)
113 {
114 DataStructures::Multilist<ML_STACK, Replica3*> replicaList;
115 DataStructures::Multilist<ML_STACK, Replica3*> destructionList;
116 DataStructures::Multilist<ML_STACK, Replica3*> broadcastList;
117 RakNet::Connection_RM3 *connection;
118 DataStructures::DefaultIndexType index2;
119 RM3ActionOnPopConnection action;
120
121 connection=connectionList[index];
122
123 // Clear out downloadGroup
124 connection->ClearDownloadGroup(rakPeerInterface);
125
126 RakNetGUID guid = connection->GetRakNetGUID();
127 GetReplicasCreatedByGuid(guid, replicaList);
128
129 for (index2=0; index2 < replicaList.GetSize(); index2++)
130 {
131 action = replicaList[index2]->QueryActionOnPopConnection(connection);
132 replicaList[index2]->OnPoppedConnection(connection);
133 if (action==RM3AOPC_DELETE_REPLICA)
134 {
135 destructionList.Push( replicaList[index2], __FILE__, __LINE__ );
136 }
137 else if (action==RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION)
138 {
139 destructionList.Push( replicaList[index2], __FILE__, __LINE__ );
140
141 broadcastList.Push( replicaList[index2], __FILE__, __LINE__ );
142 }
143 }
144
145 BroadcastDestructionList(broadcastList, connection->GetSystemAddress());
146 for (index2=0; index2 < destructionList.GetSize(); index2++)
147 {
148 destructionList[index2]->PreDestruction(connection);
149 destructionList[index2]->DeallocReplica(connection);
150 }
151
152 connectionList.RemoveAtIndex(index,__FILE__,__LINE__);
153 return connection;
154 }
155
156 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
157
PopConnection(RakNetGUID guid)158 RakNet::Connection_RM3 * ReplicaManager3::PopConnection(RakNetGUID guid)
159 {
160 DataStructures::DefaultIndexType index;
161
162 for (index=0; index < connectionList.GetSize(); index++)
163 {
164 if (connectionList[index]->GetRakNetGUID()==guid)
165 {
166 return PopConnection(index);
167 }
168 }
169 return 0;
170 }
171
172 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
173
Reference(RakNet::Replica3 * replica3)174 void ReplicaManager3::Reference(RakNet::Replica3 *replica3)
175 {
176 DataStructures::DefaultIndexType index = ReferenceInternal(replica3);
177
178 if (index!=(DataStructures::DefaultIndexType)-1)
179 {
180 DataStructures::DefaultIndexType pushIdx;
181 for (pushIdx=0; pushIdx < connectionList.GetSize(); pushIdx++)
182 {
183 Connection_RM3::ConstructionMode constructionMode = connectionList[pushIdx]->QueryConstructionMode();
184 if (constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
185 {
186 connectionList[pushIdx]->OnLocalReference(replica3, this);
187 }
188 }
189 }
190 }
191
192 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
193
ReferenceInternal(RakNet::Replica3 * replica3)194 DataStructures::DefaultIndexType ReplicaManager3::ReferenceInternal(RakNet::Replica3 *replica3)
195 {
196 DataStructures::DefaultIndexType index;
197 index = userReplicaList.GetInsertionIndex(replica3);
198 if (index!=(DataStructures::DefaultIndexType)-1)
199 {
200 if (networkIDManager==0)
201 networkIDManager=rakPeerInterface->GetNetworkIDManager();
202 RakAssert(networkIDManager);
203 replica3->SetNetworkIDManager(networkIDManager);
204 if (replica3->creatingSystemGUID==UNASSIGNED_RAKNET_GUID)
205 replica3->creatingSystemGUID=rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
206 replica3->replicaManager=this;
207 userReplicaList.InsertAtIndex(replica3,index,__FILE__,__LINE__);
208 }
209 return index;
210 }
211
212 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
213
Dereference(RakNet::Replica3 * replica3)214 void ReplicaManager3::Dereference(RakNet::Replica3 *replica3)
215 {
216 DataStructures::DefaultIndexType index, index2;
217 for (index=0; index < userReplicaList.GetSize(); index++)
218 {
219 if (userReplicaList[index]==replica3)
220 {
221 userReplicaList.RemoveAtIndex(index,__FILE__,__LINE__);
222 break;
223 }
224 }
225
226 // Remove from all connections
227 for (index2=0; index2 < connectionList.GetSize(); index2++)
228 {
229 connectionList[index2]->OnDereference(replica3, this);
230 }
231 }
232
233 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
234
DereferenceList(DataStructures::Multilist<ML_STACK,Replica3 * > & replicaListIn)235 void ReplicaManager3::DereferenceList(DataStructures::Multilist<ML_STACK, Replica3*> &replicaListIn)
236 {
237 DataStructures::DefaultIndexType index;
238 for (index=0; index < replicaListIn.GetSize(); index++)
239 Dereference(replicaListIn[index]);
240 }
241
242
243 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
244
GetReplicasCreatedByMe(DataStructures::Multilist<ML_STACK,Replica3 * > & replicaListOut)245 void ReplicaManager3::GetReplicasCreatedByMe(DataStructures::Multilist<ML_STACK, Replica3*> &replicaListOut)
246 {
247 RakNetGUID myGuid = rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
248 GetReplicasCreatedByGuid(rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS), replicaListOut);
249 }
250
251 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
252
GetReferencedReplicaList(DataStructures::Multilist<ML_STACK,Replica3 * > & replicaListOut)253 void ReplicaManager3::GetReferencedReplicaList(DataStructures::Multilist<ML_STACK, Replica3*> &replicaListOut)
254 {
255 replicaListOut=userReplicaList;
256 }
257 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
258
GetReplicasCreatedByGuid(RakNetGUID guid,DataStructures::Multilist<ML_STACK,Replica3 * > & replicaListOut)259 void ReplicaManager3::GetReplicasCreatedByGuid(RakNetGUID guid, DataStructures::Multilist<ML_STACK, Replica3*> &replicaListOut)
260 {
261 replicaListOut.Clear(false,__FILE__,__LINE__);
262 DataStructures::DefaultIndexType index;
263 for (index=0; index < userReplicaList.GetSize(); index++)
264 {
265 if (userReplicaList[index]->creatingSystemGUID==guid)
266 replicaListOut.Push(userReplicaList[index],__FILE__,__LINE__);
267 }
268 }
269
270
271 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
272
GetReplicaCount(void) const273 unsigned ReplicaManager3::GetReplicaCount(void) const
274 {
275 return userReplicaList.GetSize();
276 }
277
278 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
279
GetReplicaAtIndex(unsigned index)280 Replica3 *ReplicaManager3::GetReplicaAtIndex(unsigned index)
281 {
282 return userReplicaList[index];
283 }
284
285 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
286
GetConnectionCount(void) const287 DataStructures::DefaultIndexType ReplicaManager3::GetConnectionCount(void) const
288 {
289 return connectionList.GetSize();
290 }
291
292 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
293
GetConnectionAtIndex(unsigned index) const294 Connection_RM3* ReplicaManager3::GetConnectionAtIndex(unsigned index) const
295 {
296 return connectionList[index];
297 }
298
299 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
300
GetConnectionBySystemAddress(SystemAddress sa) const301 Connection_RM3* ReplicaManager3::GetConnectionBySystemAddress(SystemAddress sa) const
302 {
303 DataStructures::DefaultIndexType index;
304 for (index=0; index < connectionList.GetSize(); index++)
305 {
306 if (connectionList[index]->GetSystemAddress()==sa)
307 {
308 return connectionList[index];
309 }
310 }
311 return 0;
312 }
313
314 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
315
GetConnectionByGUID(RakNetGUID guid) const316 Connection_RM3* ReplicaManager3::GetConnectionByGUID(RakNetGUID guid) const
317 {
318 DataStructures::DefaultIndexType index;
319 for (index=0; index < connectionList.GetSize(); index++)
320 {
321 if (connectionList[index]->GetRakNetGUID()==guid)
322 {
323 return connectionList[index];
324 }
325 }
326 return 0;
327 }
328
329 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
330
SetDefaultOrderingChannel(char def)331 void ReplicaManager3::SetDefaultOrderingChannel(char def)
332 {
333 defaultSendParameters.orderingChannel=def;
334 }
335
336 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
337
SetDefaultPacketPriority(PacketPriority def)338 void ReplicaManager3::SetDefaultPacketPriority(PacketPriority def)
339 {
340 defaultSendParameters.priority=def;
341 }
342
343 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
344
SetDefaultPacketReliability(PacketReliability def)345 void ReplicaManager3::SetDefaultPacketReliability(PacketReliability def)
346 {
347 defaultSendParameters.reliability=def;
348 }
349
350 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
351
SetAutoSerializeInterval(RakNetTime intervalMS)352 void ReplicaManager3::SetAutoSerializeInterval(RakNetTime intervalMS)
353 {
354 autoSerializeInterval=intervalMS;
355 }
356
357 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
358
GetConnectionsThatHaveReplicaConstructed(Replica3 * replica,DataStructures::Multilist<ML_STACK,Connection_RM3 * > & connectionsThatHaveConstructedThisReplica)359 void ReplicaManager3::GetConnectionsThatHaveReplicaConstructed(Replica3 *replica, DataStructures::Multilist<ML_STACK, Connection_RM3*> &connectionsThatHaveConstructedThisReplica)
360 {
361 connectionsThatHaveConstructedThisReplica.Clear(false,__FILE__,__LINE__);
362 DataStructures::DefaultIndexType index;
363 for (index=0; index < connectionList.GetSize(); index++)
364 {
365 if (connectionList[index]->HasReplicaConstructed(replica))
366 connectionsThatHaveConstructedThisReplica.Push(connectionList[index],__FILE__,__LINE__);
367 }
368 }
369
370 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
371
Clear(void)372 void ReplicaManager3::Clear(void)
373 {
374 if (autoDestroyConnections)
375 {
376 for (DataStructures::DefaultIndexType i=0; i < connectionList.GetSize(); i++)
377 DeallocConnection(connectionList[i]);
378 }
379 else
380 {
381 // Clear out downloadGroup even if not auto destroying the connection, since the packets need to go back to RakPeer
382 for (DataStructures::DefaultIndexType i=0; i < connectionList.GetSize(); i++)
383 connectionList[i]->ClearDownloadGroup(rakPeerInterface);
384 }
385
386
387
388 connectionList.Clear(true,__FILE__,__LINE__);
389 userReplicaList.Clear(true,__FILE__,__LINE__);
390 }
391
392 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
393
GetDefaultSendParameters(void) const394 PRO ReplicaManager3::GetDefaultSendParameters(void) const
395 {
396 return defaultSendParameters;
397 }
398
399 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
400
SetWorldID(unsigned char id)401 void ReplicaManager3::SetWorldID(unsigned char id)
402 {
403 worldId=id;
404 }
405
406 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
407
GetWorldID(void) const408 unsigned char ReplicaManager3::GetWorldID(void) const
409 {
410 return worldId;
411 }
412
413 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
414
GetNetworkIDManager(void) const415 NetworkIDManager *ReplicaManager3::GetNetworkIDManager(void) const
416 {
417 return networkIDManager;
418 }
419
420 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
421
SetNetworkIDManager(NetworkIDManager * _networkIDManager)422 void ReplicaManager3::SetNetworkIDManager(NetworkIDManager *_networkIDManager)
423 {
424 networkIDManager=_networkIDManager;
425 if (networkIDManager)
426 networkIDManager->SetGuid(rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
427 }
428
429 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
430
OnReceive(Packet * packet)431 PluginReceiveResult ReplicaManager3::OnReceive(Packet *packet)
432 {
433 if (packet->length<2)
434 return RR_CONTINUE_PROCESSING;
435
436 unsigned char incomingWorldId;
437
438 RakNetTime timestamp=0;
439 unsigned char packetIdentifier, packetDataOffset;
440 if ( ( unsigned char ) packet->data[ 0 ] == ID_TIMESTAMP )
441 {
442 if ( packet->length > sizeof( unsigned char ) + sizeof( RakNetTime ) )
443 {
444 packetIdentifier = ( unsigned char ) packet->data[ sizeof( unsigned char ) + sizeof( RakNetTime ) ];
445 // Required for proper endian swapping
446 RakNet::BitStream tsBs(packet->data+sizeof(MessageID),packet->length-1,false);
447 tsBs.Read(timestamp);
448 incomingWorldId=packet->data[sizeof( unsigned char )*2 + sizeof( RakNetTime )];
449 packetDataOffset=sizeof( unsigned char )*3 + sizeof( RakNetTime );
450 }
451 else
452 return RR_STOP_PROCESSING_AND_DEALLOCATE;
453 }
454 else
455 {
456 packetIdentifier = ( unsigned char ) packet->data[ 0 ];
457 incomingWorldId=packet->data[sizeof( unsigned char )];
458 packetDataOffset=sizeof( unsigned char )*2;
459 }
460
461 switch (packetIdentifier)
462 {
463 case ID_REPLICA_MANAGER_CONSTRUCTION:
464 if (incomingWorldId!=worldId)
465 return RR_CONTINUE_PROCESSING;
466 return OnConstruction(packet, packet->data, packet->length, packet->guid, packetDataOffset);
467 case ID_REPLICA_MANAGER_SERIALIZE:
468 if (incomingWorldId!=worldId)
469 return RR_CONTINUE_PROCESSING;
470 return OnSerialize(packet, packet->data, packet->length, packet->guid, timestamp, packetDataOffset);
471 case ID_REPLICA_MANAGER_3_LOCAL_CONSTRUCTION_REJECTED:
472 if (incomingWorldId!=worldId)
473 return RR_CONTINUE_PROCESSING;
474 OnLocalConstructionRejected(packet->data, packet->length, packet->guid, packetDataOffset);
475 break;
476 case ID_REPLICA_MANAGER_3_LOCAL_CONSTRUCTION_ACCEPTED:
477 if (incomingWorldId!=worldId)
478 return RR_CONTINUE_PROCESSING;
479 OnLocalConstructionAccepted(packet->data, packet->length, packet->guid, packetDataOffset);
480 break;
481 case ID_REPLICA_MANAGER_DOWNLOAD_STARTED:
482 if (incomingWorldId!=worldId)
483 return RR_CONTINUE_PROCESSING;
484 return OnDownloadStarted(packet, packet->data, packet->length, packet->guid, packetDataOffset);
485 case ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE:
486 if (incomingWorldId!=worldId)
487 return RR_CONTINUE_PROCESSING;
488 return OnDownloadComplete(packet, packet->data, packet->length, packet->guid, packetDataOffset);
489 case ID_REPLICA_MANAGER_3_SERIALIZE_CONSTRUCTION_EXISTING:
490 if (incomingWorldId!=worldId)
491 return RR_CONTINUE_PROCESSING;
492 OnConstructionExisting(packet->data, packet->length, packet->guid, packetDataOffset);
493 break;
494 case ID_REPLICA_MANAGER_SCOPE_CHANGE:
495 {
496 if (incomingWorldId!=worldId)
497 return RR_CONTINUE_PROCESSING;
498
499 Connection_RM3 *connection = GetConnectionByGUID(packet->guid);
500 if (connection && connection->isValidated==false)
501 {
502 // This connection is now confirmed bidirectional
503 connection->isValidated=true;
504 // Reply back on validation
505 connection->SendValidation(rakPeerInterface,worldId);
506 }
507 }
508 }
509
510 return RR_CONTINUE_PROCESSING;
511 }
512
513 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
514
AutoConstructByQuery(ReplicaManager3 * replicaManager3)515 void Connection_RM3::AutoConstructByQuery(ReplicaManager3 *replicaManager3)
516 {
517 ValidateLists(replicaManager3);
518
519 ConstructionMode constructionMode = QueryConstructionMode();
520
521 DataStructures::DefaultIndexType index;
522 RM3ConstructionState constructionState;
523 LastSerializationResult *lsr;
524 index=0;
525
526 constructedReplicasCulled.Clear(false,__FILE__,__LINE__);
527 destroyedReplicasCulled.Clear(false,__FILE__,__LINE__);
528
529 if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
530 {
531 while (index < queryToConstructReplicaList.GetSize())
532 {
533 lsr=queryToConstructReplicaList[index];
534 constructionState=lsr->replica->QueryConstruction(this, replicaManager3);
535 if (constructionState==RM3CS_ALREADY_EXISTS_REMOTELY)
536 {
537 OnReplicaAlreadyExists(index, replicaManager3);
538
539 // Serialize construction data to this connection
540 RakNet::BitStream bsOut;
541 bsOut.Write((MessageID)ID_REPLICA_MANAGER_3_SERIALIZE_CONSTRUCTION_EXISTING);
542 bsOut.Write(replicaManager3->GetWorldID());
543 NetworkID networkId;
544 networkId=lsr->replica->GetNetworkID();
545 bsOut.Write(networkId);
546 BitSize_t bitsWritten = bsOut.GetNumberOfBitsUsed();
547 lsr->replica->SerializeConstructionExisting(&bsOut, this);
548 if (bsOut.GetNumberOfBitsUsed()!=bitsWritten)
549 replicaManager3->SendUnified(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,GetSystemAddress(), false);
550
551 // Serialize first serialization to this connection.
552 // This is done here, as it isn't done in PushConstruction
553 SerializeParameters sp;
554 RakNet::BitStream emptyBs;
555 for (index=0; index < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; index++)
556 {
557 sp.lastSentBitstream[index]=&emptyBs;
558 sp.pro[index]=replicaManager3->GetDefaultSendParameters();
559 }
560 sp.bitsWrittenSoFar=0;
561 sp.destinationConnection=this;
562 sp.messageTimestamp=0;
563 sp.whenLastSerialized=0;
564
565 RakNet::Replica3 *replica = lsr->replica;
566
567 RM3SerializationResult res = replica->Serialize(&sp);
568 if (res!=RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION &&
569 res!=RM3SR_DO_NOT_SERIALIZE &&
570 res!=RM3SR_SERIALIZED_UNIQUELY)
571 {
572 bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
573 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
574 {
575 sp.bitsWrittenSoFar+=sp.outputBitstream[z].GetNumberOfBitsUsed();
576 allIndices[z]=true;
577 }
578 if (SendSerialize(replica, allIndices, sp.outputBitstream, sp.messageTimestamp, sp.pro, replicaManager3->GetRakPeerInterface(), replicaManager3->GetWorldID())==SSICR_SENT_DATA)
579 lsr->replica->whenLastSerialized=RakNet::GetTime();
580 }
581 }
582 else if (constructionState==RM3CS_SEND_CONSTRUCTION)
583 {
584 OnConstructToThisConnection(index, replicaManager3);
585 constructedReplicasCulled.Push(lsr->replica,lsr->replica,__FILE__,__LINE__);
586 }
587 else if (constructionState==RM3CS_NEVER_CONSTRUCT)
588 {
589 OnNeverConstruct(index, replicaManager3);
590 }
591 else// if (constructionState==RM3CS_NO_ACTION)
592 {
593 // Do nothing
594 index++;
595 }
596 }
597
598 if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
599 {
600 RM3DestructionState destructionState;
601 index=0;
602 while (index < queryToDestructReplicaList.GetSize())
603 {
604 lsr=queryToDestructReplicaList[index];
605 destructionState=lsr->replica->QueryDestruction(this, replicaManager3);
606 if (destructionState==RM3DS_SEND_DESTRUCTION)
607 {
608 OnSendDestructionFromQuery(index, replicaManager3);
609 destroyedReplicasCulled.Push(lsr->replica,lsr->replica,__FILE__,__LINE__);
610 }
611 else if (destructionState==RM3DS_DO_NOT_QUERY_DESTRUCTION)
612 {
613 OnDoNotQueryDestruction(index, replicaManager3);
614 }
615 else// if (destructionState==RM3CS_NO_ACTION)
616 {
617 // Do nothing
618 index++;
619 }
620 }
621 }
622 }
623 else if (constructionMode==QUERY_CONNECTION_FOR_REPLICA_LIST)
624 {
625 QueryReplicaList(constructedReplicasCulled,destroyedReplicasCulled);
626
627 DataStructures::DefaultIndexType idx1, idx2;
628 #ifdef _DEBUG
629 // The user should not construct a replica that already exists
630 for (idx2=0; idx2 < constructedReplicasCulled.GetSize(); idx2++)
631 {
632 RakAssert(constructedReplicaList.GetIndexOf(constructedReplicasCulled[idx2])==(DataStructures::DefaultIndexType)-1);
633 }
634
635 #endif
636
637 // Create new
638 for (idx2=0; idx2 < constructedReplicasCulled.GetSize(); idx2++)
639 OnConstructToThisConnection(constructedReplicasCulled[idx2], replicaManager3);
640
641 bool exists;
642 for (idx2=0; idx2 < destroyedReplicasCulled.GetSize(); idx2++)
643 {
644 exists=false;
645 idx1=constructedReplicaList.GetIndexOf(destroyedReplicasCulled[idx2]);
646 RakAssert(idx1!=(DataStructures::DefaultIndexType)-1);
647 if (idx1!=(DataStructures::DefaultIndexType)-1)
648 {
649 OnSendDestructionFromQuery(idx1,replicaManager3);
650 }
651
652 // If this assert hits, the user tried to destroy a replica that doesn't exist on the remote system
653 RakAssert(exists);
654 }
655 }
656
657 SendConstruction(constructedReplicasCulled,destroyedReplicasCulled,replicaManager3->defaultSendParameters,replicaManager3->rakPeerInterface,replicaManager3->worldId);
658
659 }
Update(void)660 void ReplicaManager3::Update(void)
661 {
662 DataStructures::DefaultIndexType index,index2;
663
664 for (index=0; index < connectionList.GetSize(); index++)
665 {
666 if (connectionList[index]->isValidated==false)
667 continue;
668 connectionList[index]->AutoConstructByQuery(this);
669 }
670
671 if (autoSerializeInterval>0)
672 {
673 RakNetTime time = RakNet::GetTime();
674
675 if (time - lastAutoSerializeOccurance > autoSerializeInterval)
676 {
677 for (index=0; index < userReplicaList.GetSize(); index++)
678 {
679 userReplicaList[index]->forceSendUntilNextUpdate=false;
680 userReplicaList[index]->OnUserReplicaPreSerializeTick();
681 }
682
683
684 DataStructures::DefaultIndexType index;
685 SerializeParameters sp;
686 sp.curTime=time;
687 Connection_RM3 *connection;
688 SendSerializeIfChangedResult ssicr;
689 sp.messageTimestamp=0;
690 for (int i=0; i < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; i++)
691 sp.pro[i]=defaultSendParameters;
692 index2=0;
693 for (index=0; index < connectionList.GetSize(); index++)
694 {
695 connection = connectionList[index];
696 sp.bitsWrittenSoFar=0;
697 index2=0;
698 while (index2 < connection->queryToSerializeReplicaList.GetSize())
699 {
700 sp.destinationConnection=connection;
701 sp.whenLastSerialized=connection->queryToSerializeReplicaList[index2]->replica->whenLastSerialized;
702 ssicr=connection->SendSerializeIfChanged(index2, &sp, GetRakPeerInterface(), GetWorldID(), this);
703 if (ssicr==SSICR_SENT_DATA)
704 {
705 connection->queryToSerializeReplicaList[index2]->replica->whenLastSerialized=time;
706 index2++;
707 }
708 else if (ssicr==SSICR_NEVER_SERIALIZE)
709 {
710 // Removed from the middle of the list
711 }
712 else
713 index2++;
714 }
715 }
716
717 lastAutoSerializeOccurance=time;
718 }
719 }
720 }
721
722 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
723
OnClosedConnection(SystemAddress systemAddress,RakNetGUID rakNetGUID,PI2_LostConnectionReason lostConnectionReason)724 void ReplicaManager3::OnClosedConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
725 {
726 (void) lostConnectionReason;
727 (void) systemAddress;
728 if (autoDestroyConnections)
729 {
730 Connection_RM3 *connection = PopConnection(rakNetGUID);
731 if (connection)
732 DeallocConnection(connection);
733 }
734 }
735
736 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
737
OnNewConnection(SystemAddress systemAddress,RakNetGUID rakNetGUID,bool isIncoming)738 void ReplicaManager3::OnNewConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
739 {
740 (void) isIncoming;
741 if (autoCreateConnections)
742 {
743 Connection_RM3 *connection = AllocConnection(systemAddress, rakNetGUID);
744 if (connection)
745 PushConnection(connection);
746 }
747 }
748
749 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
750
OnRakPeerShutdown(void)751 void ReplicaManager3::OnRakPeerShutdown(void)
752 {
753 if (autoDestroyConnections)
754 {
755 while (connectionList.GetSize())
756 {
757 Connection_RM3 *connection = PopConnection(connectionList.GetSize()-1);
758 if (connection)
759 DeallocConnection(connection);
760 }
761 }
762
763 Clear();
764 }
765
766 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
767
OnDetach(void)768 void ReplicaManager3::OnDetach(void)
769 {
770 OnRakPeerShutdown();
771 }
772
773 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
774
OnConstructionExisting(unsigned char * packetData,int packetDataLength,RakNetGUID senderGuid,unsigned char packetDataOffset)775 void ReplicaManager3::OnConstructionExisting(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
776 {
777 Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
778 if (connection==0)
779 {
780 // Almost certainly a bug
781 RakAssert("Got OnConstruction but no connection yet" && 0);
782 return;
783 }
784
785 RakNet::BitStream bsIn(packetData,packetDataLength,false);
786 bsIn.IgnoreBytes(packetDataOffset);
787
788 if (networkIDManager==0)
789 networkIDManager=rakPeerInterface->GetNetworkIDManager();
790 RakAssert(networkIDManager);
791
792 NetworkID networkId;
793 bsIn.Read(networkId);
794 Replica3* existingReplica = networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
795 if (existingReplica)
796 {
797 existingReplica->DeserializeConstructionExisting(&bsIn, connection);
798 }
799 }
800
801 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
802
OnConstruction(Packet * packet,unsigned char * packetData,int packetDataLength,RakNetGUID senderGuid,unsigned char packetDataOffset)803 PluginReceiveResult ReplicaManager3::OnConstruction(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
804 {
805 Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
806 if (connection==0)
807 {
808 // Almost certainly a bug
809 RakAssert("Got OnConstruction but no connection yet" && 0);
810 return RR_CONTINUE_PROCESSING;
811 }
812 if (connection->groupConstructionAndSerialize)
813 {
814 connection->downloadGroup.Push(packet, __FILE__, __LINE__);
815 return RR_STOP_PROCESSING;
816 }
817
818 RakNet::BitStream bsIn(packetData,packetDataLength,false);
819 bsIn.IgnoreBytes(packetDataOffset);
820 DataStructures::DefaultIndexType objectListSize, index, index2;
821 BitSize_t bitOffset;
822 Replica3 *replica;
823 uint32_t allocationNumber=0;
824 NetworkID networkId;
825 RakNetGUID creatingSystemGuid;
826
827 if (networkIDManager==0)
828 networkIDManager=rakPeerInterface->GetNetworkIDManager();
829 RakAssert(networkIDManager);
830
831 bsIn.Read(objectListSize);
832 for (index=0; index < objectListSize; index++)
833 {
834 bsIn.Read(bitOffset);
835 bsIn.Read(networkId);
836 Replica3* existingReplica = networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
837 if (existingReplica)
838 {
839 existingReplica->replicaManager=this;
840
841 // Network ID already in use
842 connection->OnDownloadExisting(existingReplica, this);
843
844 bsIn.SetReadOffset(bitOffset);
845 continue;
846 }
847
848 replica = connection->AllocReplica(&bsIn, this);
849 if (replica==0)
850 {
851 bsIn.SetReadOffset(bitOffset);
852 continue;
853 }
854
855 replica->SetNetworkIDManager(networkIDManager);
856
857 if (networkId==UNASSIGNED_NETWORK_ID)
858 {
859 if (networkIDManager->IsNetworkIDAuthority()==false)
860 {
861 // Can't assign network ID
862 replica->replicaManager=0;
863 replica->DeallocReplica(connection);
864 bsIn.SetReadOffset(bitOffset);
865 continue;
866 }
867
868 bsIn.Read(allocationNumber);
869 }
870 else
871 {
872
873 replica->SetNetworkID(networkId);
874 }
875
876 replica->replicaManager=this;
877 bsIn.Read(creatingSystemGuid);
878 replica->creatingSystemGUID=creatingSystemGuid;
879
880 if (!replica->QueryRemoteConstruction(connection) ||
881 !replica->DeserializeConstruction(&bsIn, connection))
882 {
883 // Overtake this message to mean construction rejected
884 if (networkId==UNASSIGNED_NETWORK_ID)
885 {
886 RakNet::BitStream bsOut;
887 bsOut.Write((MessageID)ID_REPLICA_MANAGER_3_LOCAL_CONSTRUCTION_REJECTED);
888 bsOut.Write(worldId);
889 bsOut.Write(allocationNumber);
890 replica->SerializeConstructionRequestRejected(&bsOut,connection);
891 rakPeerInterface->Send(&bsOut, defaultSendParameters.priority, defaultSendParameters.reliability, defaultSendParameters.orderingChannel, connection->GetSystemAddress(), false, defaultSendParameters.sendReceipt);
892 }
893
894 replica->replicaManager=0;
895 replica->DeallocReplica(connection);
896 bsIn.SetReadOffset(bitOffset);
897 continue;
898 }
899
900 bsIn.SetReadOffset(bitOffset);
901 replica->PostDeserializeConstruction(connection);
902
903 if (networkId==UNASSIGNED_NETWORK_ID)
904 {
905 // Overtake this message to mean construction accepted
906 RakNet::BitStream bsOut;
907 bsOut.Write((MessageID)ID_REPLICA_MANAGER_3_LOCAL_CONSTRUCTION_ACCEPTED);
908 bsOut.Write(worldId);
909 bsOut.Write(allocationNumber);
910 bsOut.Write(replica->GetNetworkID());
911 replica->SerializeConstructionRequestAccepted(&bsOut,connection);
912 rakPeerInterface->Send(&bsOut, defaultSendParameters.priority, defaultSendParameters.reliability, defaultSendParameters.orderingChannel, connection->GetSystemAddress(), false, defaultSendParameters.sendReceipt);
913 }
914 bsIn.AlignReadToByteBoundary();
915
916 // Register the replica
917 ReferenceInternal(replica);
918
919 // Tell the connection(s) that this object exists since they just sent it to us
920 connection->OnDownloadFromThisSystem(replica, this);
921
922 for (index2=0; index2 < connectionList.GetSize(); index2++)
923 {
924 if (connectionList[index2]!=connection)
925 connectionList[index2]->OnDownloadFromOtherSystem(replica, this);
926 }
927 }
928
929 // Destructions
930 bool b = bsIn.Read(objectListSize);
931 RakAssert(b);
932 for (index=0; index < objectListSize; index++)
933 {
934 bsIn.Read(networkId);
935 bsIn.Read(bitOffset);
936 replica = networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
937 if (replica==0)
938 {
939 // Unknown object
940 bsIn.SetReadOffset(bitOffset);
941 continue;
942 }
943 bsIn.Read(replica->deletingSystemGUID);
944 if (replica->DeserializeDestruction(&bsIn,connection))
945 {
946 // Make sure it wasn't deleted in DeserializeDestruction
947 if (networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId))
948 {
949 replica->PreDestruction(connection);
950
951 // Forward deletion by remote system
952 BroadcastDestruction(replica,connection->GetSystemAddress());
953 Dereference(replica);
954 replica->replicaManager=0; // Prevent BroadcastDestruction from being called again
955 replica->DeallocReplica(connection);
956 }
957 }
958 else
959 {
960 replica->PreDestruction(connection);
961 connection->OnDereference(replica, this);
962 }
963
964 bsIn.AlignReadToByteBoundary();
965 }
966 return RR_CONTINUE_PROCESSING;
967 }
968
969 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
970
OnSerialize(Packet * packet,unsigned char * packetData,int packetDataLength,RakNetGUID senderGuid,RakNetTime timestamp,unsigned char packetDataOffset)971 PluginReceiveResult ReplicaManager3::OnSerialize(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, RakNetTime timestamp, unsigned char packetDataOffset)
972 {
973 Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
974 if (connection==0)
975 return RR_CONTINUE_PROCESSING;
976 if (connection->groupConstructionAndSerialize)
977 {
978 connection->downloadGroup.Push(packet, __FILE__, __LINE__);
979 return RR_STOP_PROCESSING;
980 }
981 if (networkIDManager==0)
982 networkIDManager=rakPeerInterface->GetNetworkIDManager();
983 RakAssert(networkIDManager);
984 RakNet::BitStream bsIn(packetData,packetDataLength,false);
985 bsIn.IgnoreBytes(packetDataOffset);
986
987 struct DeserializeParameters ds;
988 ds.timeStamp=timestamp;
989 ds.sourceConnection=connection;
990
991 Replica3 *replica;
992 NetworkID networkId;
993 BitSize_t bitsUsed;
994 bsIn.Read(networkId);
995 replica = networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
996 if (replica)
997 {
998 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
999 {
1000 bsIn.Read(ds.bitstreamWrittenTo[z]);
1001 if (ds.bitstreamWrittenTo[z])
1002 {
1003 bsIn.ReadCompressed(bitsUsed);
1004 bsIn.AlignReadToByteBoundary();
1005 bsIn.Read(ds.serializationBitstream[z], bitsUsed);
1006 }
1007 }
1008 replica->Deserialize(&ds);
1009 }
1010 return RR_CONTINUE_PROCESSING;
1011 }
1012 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1013
OnDownloadStarted(Packet * packet,unsigned char * packetData,int packetDataLength,RakNetGUID senderGuid,unsigned char packetDataOffset)1014 PluginReceiveResult ReplicaManager3::OnDownloadStarted(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
1015 {
1016 Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
1017 if (connection==0)
1018 return RR_CONTINUE_PROCESSING;
1019 if (connection->QueryGroupDownloadMessages() &&
1020 // ID_DOWNLOAD_STARTED will be processed twice, being processed the second time once ID_DOWNLOAD_COMPLETE arrives.
1021 // However, the second time groupConstructionAndSerialize will be set to true so it won't be processed a third time
1022 connection->groupConstructionAndSerialize==false
1023 )
1024 {
1025 // These messages will be held by the plugin and returned when the download is complete
1026 connection->groupConstructionAndSerialize=true;
1027 RakAssert(connection->downloadGroup.GetSize()==0);
1028 connection->downloadGroup.Push(packet, __FILE__, __LINE__);
1029 return RR_STOP_PROCESSING;
1030 }
1031
1032 connection->groupConstructionAndSerialize=false;
1033 RakNet::BitStream bsIn(packetData,packetDataLength,false);
1034 bsIn.IgnoreBytes(packetDataOffset);
1035 connection->DeserializeOnDownloadStarted(&bsIn);
1036 return RR_CONTINUE_PROCESSING;
1037 }
1038
1039 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1040
OnDownloadComplete(Packet * packet,unsigned char * packetData,int packetDataLength,RakNetGUID senderGuid,unsigned char packetDataOffset)1041 PluginReceiveResult ReplicaManager3::OnDownloadComplete(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
1042 {
1043 Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
1044 if (connection==0)
1045 return RR_CONTINUE_PROCESSING;
1046
1047 if (connection->groupConstructionAndSerialize==true && connection->downloadGroup.GetSize()>0)
1048 {
1049 // Push back buffered packets in front of this one
1050 DataStructures::DefaultIndexType i;
1051 for (i=0; i < connection->downloadGroup.GetSize(); i++)
1052 rakPeerInterface->PushBackPacket(connection->downloadGroup[i],false);
1053
1054 // Push this one to be last too. It will be processed again, but the second time
1055 // groupConstructionAndSerialize will be false and downloadGroup will be empty, so it will go past this block
1056 connection->downloadGroup.Clear(false,__FILE__,__LINE__);
1057 rakPeerInterface->PushBackPacket(packet,false);
1058
1059 return RR_STOP_PROCESSING;
1060 }
1061
1062 RakNet::BitStream bsIn(packetData,packetDataLength,false);
1063 bsIn.IgnoreBytes(packetDataOffset);
1064 connection->DeserializeOnDownloadComplete(&bsIn);
1065 return RR_CONTINUE_PROCESSING;
1066 }
1067
1068 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1069
OnLocalConstructionRejected(unsigned char * packetData,int packetDataLength,RakNetGUID senderGuid,unsigned char packetDataOffset)1070 void ReplicaManager3::OnLocalConstructionRejected(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
1071 {
1072 Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
1073 if (connection==0)
1074 return;
1075 RakNet::BitStream bsIn(packetData,packetDataLength,false);
1076 bsIn.IgnoreBytes(packetDataOffset);
1077 uint32_t allocationNumber;
1078 bsIn.Read(allocationNumber);
1079 DataStructures::DefaultIndexType index;
1080 for (index=0; index < userReplicaList.GetSize(); index++)
1081 {
1082 if (userReplicaList[index]->GetAllocationNumber()==allocationNumber)
1083 {
1084 userReplicaList[index]->DeserializeConstructionRequestRejected(&bsIn,connection);
1085 break;
1086 }
1087 }
1088
1089 }
1090
1091 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1092
OnLocalConstructionAccepted(unsigned char * packetData,int packetDataLength,RakNetGUID senderGuid,unsigned char packetDataOffset)1093 void ReplicaManager3::OnLocalConstructionAccepted(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
1094 {
1095 Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
1096 if (connection==0)
1097 return;
1098 RakNet::BitStream bsIn(packetData,packetDataLength,false);
1099 bsIn.IgnoreBytes(packetDataOffset);
1100 uint32_t allocationNumber;
1101 bsIn.Read(allocationNumber);
1102 NetworkID assignedNetworkId;
1103 bsIn.Read(assignedNetworkId);
1104 DataStructures::DefaultIndexType index;
1105 DataStructures::DefaultIndexType index2;
1106 Replica3 *replica;
1107 SerializeParameters sp;
1108 sp.whenLastSerialized=0;
1109 RakNet::BitStream emptyBs;
1110 for (index=0; index < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; index++)
1111 sp.lastSentBitstream[index]=&emptyBs;
1112 RM3SerializationResult res;
1113 for (index=0; index < userReplicaList.GetSize(); index++)
1114 {
1115 if (userReplicaList[index]->GetAllocationNumber()==allocationNumber)
1116 {
1117 replica=userReplicaList[index];
1118 index2=connection->constructedReplicaList.GetIndexOf(replica);
1119 if (index2!=(DataStructures::DefaultIndexType)-1)
1120 {
1121 LastSerializationResult *lsr = connection->constructedReplicaList[index2];
1122
1123 replica->SetNetworkID(assignedNetworkId);
1124 replica->DeserializeConstructionRequestAccepted(&bsIn,connection);
1125 sp.destinationConnection=connection;
1126
1127 // Immediately serialize
1128 res = replica->Serialize(&sp);
1129 if (res!=RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION &&
1130 res!=RM3SR_DO_NOT_SERIALIZE &&
1131 res!=RM3SR_SERIALIZED_UNIQUELY)
1132 {
1133 sp.destinationConnection=connection;
1134 sp.messageTimestamp=0;
1135 for (int i=0; i < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; i++)
1136 sp.pro[i]=defaultSendParameters;
1137 bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
1138 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1139 {
1140 allIndices[z]=true;
1141 }
1142 if (connection->SendSerialize(replica, allIndices, sp.outputBitstream, sp.messageTimestamp, sp.pro, rakPeerInterface, worldId)==SSICR_SENT_DATA)
1143 lsr->replica->whenLastSerialized=RakNet::GetTime();
1144 }
1145
1146 // Start serialization queries
1147 connection->queryToSerializeReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1148
1149 return;
1150 }
1151 }
1152 }
1153 }
1154
1155 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1156
GetReplicaByNetworkID(NetworkID networkId)1157 Replica3* ReplicaManager3::GetReplicaByNetworkID(NetworkID networkId)
1158 {
1159 DataStructures::DefaultIndexType i;
1160 for (i=0; i < userReplicaList.GetSize(); i++)
1161 {
1162 if (userReplicaList[i]->GetNetworkID()==networkId)
1163 return userReplicaList[i];
1164 }
1165 return 0;
1166 }
1167
1168
1169 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1170
1171
BroadcastDestructionList(DataStructures::Multilist<ML_STACK,Replica3 * > & replicaList,SystemAddress exclusionAddress)1172 void ReplicaManager3::BroadcastDestructionList(DataStructures::Multilist<ML_STACK, Replica3*> &replicaList, SystemAddress exclusionAddress)
1173 {
1174 RakNet::BitStream bsOut;
1175 DataStructures::DefaultIndexType i,j;
1176
1177 for (i=0; i < replicaList.GetSize(); i++)
1178 {
1179
1180 if (replicaList[i]->deletingSystemGUID==UNASSIGNED_RAKNET_GUID)
1181 replicaList[i]->deletingSystemGUID=GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
1182 }
1183
1184 for (j=0; j < connectionList.GetSize(); j++)
1185 {
1186 if (connectionList[j]->GetSystemAddress()==exclusionAddress)
1187 continue;
1188
1189 bsOut.Reset();
1190 bsOut.Write((MessageID)ID_REPLICA_MANAGER_CONSTRUCTION);
1191 bsOut.Write(worldId);
1192 DataStructures::DefaultIndexType cnt=0;
1193 bsOut.Write(cnt); // No construction
1194 cnt=replicaList.GetSize();
1195 bsOut.Write(cnt);
1196
1197 for (i=0; i < replicaList.GetSize(); i++)
1198 {
1199 if (connectionList[j]->HasReplicaConstructed(replicaList[i])==false)
1200 continue;
1201
1202 NetworkID networkId;
1203 networkId=replicaList[i]->GetNetworkID();
1204 bsOut.Write(networkId);
1205 BitSize_t offsetStart, offsetEnd;
1206 offsetStart=bsOut.GetWriteOffset();
1207 bsOut.Write(offsetStart);
1208 bsOut.Write(replicaList[i]->deletingSystemGUID);
1209 replicaList[i]->SerializeDestruction(&bsOut, connectionList[j]);
1210 bsOut.AlignWriteToByteBoundary();
1211 offsetEnd=bsOut.GetWriteOffset();
1212 bsOut.SetWriteOffset(offsetStart);
1213 bsOut.Write(offsetEnd);
1214 bsOut.SetWriteOffset(offsetEnd);
1215 }
1216
1217 rakPeerInterface->Send(&bsOut,defaultSendParameters.priority,defaultSendParameters.reliability,defaultSendParameters.orderingChannel,connectionList[j]->GetSystemAddress(),false, defaultSendParameters.sendReceipt);
1218 }
1219 }
1220
1221 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1222
1223
BroadcastDestruction(Replica3 * replica,SystemAddress exclusionAddress)1224 void ReplicaManager3::BroadcastDestruction(Replica3 *replica, SystemAddress exclusionAddress)
1225 {
1226 DataStructures::Multilist<ML_STACK, Replica3*> replicaList;
1227 replicaList.Push(replica, __FILE__, __LINE__ );
1228 BroadcastDestructionList(replicaList,exclusionAddress);
1229 }
1230
1231 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1232
1233
1234 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1235
Connection_RM3(SystemAddress _systemAddress,RakNetGUID _guid)1236 Connection_RM3::Connection_RM3(SystemAddress _systemAddress, RakNetGUID _guid)
1237 : systemAddress(_systemAddress), guid(_guid)
1238 {
1239 isValidated=false;
1240 isFirstConstruction=true;
1241 groupConstructionAndSerialize=false;
1242 }
1243
1244 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1245
~Connection_RM3()1246 Connection_RM3::~Connection_RM3()
1247 {
1248 constructedReplicaList.ClearPointers(true,__FILE__,__LINE__);
1249 queryToConstructReplicaList.ClearPointers(true,__FILE__,__LINE__);
1250 }
1251
1252 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1253
GetConstructedReplicas(DataStructures::Multilist<ML_STACK,Replica3 * > & objectsTheyDoHave)1254 void Connection_RM3::GetConstructedReplicas(DataStructures::Multilist<ML_STACK, Replica3*> &objectsTheyDoHave)
1255 {
1256 objectsTheyDoHave.Clear(true,__FILE__,__LINE__);
1257 for (DataStructures::DefaultIndexType idx=0; idx < constructedReplicaList.GetSize(); idx++)
1258 {
1259 objectsTheyDoHave.Push(constructedReplicaList[idx]->replica, __FILE__, __LINE__ );
1260 }
1261 objectsTheyDoHave.TagSorted();
1262 }
1263
1264 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1265
HasReplicaConstructed(RakNet::Replica3 * replica)1266 bool Connection_RM3::HasReplicaConstructed(RakNet::Replica3 *replica)
1267 {
1268 return constructedReplicaList.GetIndexOf(replica)!=(DataStructures::DefaultIndexType)-1;
1269 }
1270
1271 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SendSerializeHeader(RakNet::Replica3 * replica,RakNetTime timestamp,RakNet::BitStream * bs,unsigned char worldId)1272 void Connection_RM3::SendSerializeHeader(RakNet::Replica3 *replica, RakNetTime timestamp, RakNet::BitStream *bs, unsigned char worldId)
1273 {
1274 bs->Reset();
1275
1276 if (timestamp!=0)
1277 {
1278 bs->Write((MessageID)ID_TIMESTAMP);
1279 bs->Write(timestamp);
1280 }
1281 bs->Write((MessageID)ID_REPLICA_MANAGER_SERIALIZE);
1282 bs->Write(worldId);
1283 bs->Write(replica->GetNetworkID());
1284 }
1285 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ClearDownloadGroup(RakPeerInterface * rakPeerInterface)1286 void Connection_RM3::ClearDownloadGroup(RakPeerInterface *rakPeerInterface)
1287 {
1288 DataStructures::DefaultIndexType i;
1289 for (i=0; i < downloadGroup.GetSize(); i++)
1290 rakPeerInterface->DeallocatePacket(downloadGroup[i]);
1291 downloadGroup.Clear(false,__FILE__,__LINE__);
1292 }
1293 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SendSerialize(RakNet::Replica3 * replica,bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS],RakNet::BitStream serializationData[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS],RakNetTime timestamp,PRO sendParameters[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS],RakPeerInterface * rakPeer,unsigned char worldId)1294 SendSerializeIfChangedResult Connection_RM3::SendSerialize(RakNet::Replica3 *replica, bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::BitStream serializationData[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNetTime timestamp, PRO sendParameters[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakPeerInterface *rakPeer, unsigned char worldId)
1295 {
1296 bool channelHasData;
1297 BitSize_t sum=0;
1298 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1299 {
1300 if (indicesToSend[z])
1301 sum+=serializationData[z].GetNumberOfBitsUsed();
1302 }
1303 if (sum==0)
1304 return SSICR_DID_NOT_SEND_DATA;
1305
1306 RakAssert(replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID);
1307
1308 RakNet::BitStream out;
1309 BitSize_t bitsUsed;
1310
1311 int channelIndex;
1312 PRO lastPro=sendParameters[0];
1313
1314 for (channelIndex=0; channelIndex < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; channelIndex++)
1315 {
1316 if (channelIndex==0)
1317 {
1318 SendSerializeHeader(replica, timestamp, &out, worldId);
1319 }
1320 else if (lastPro!=sendParameters[channelIndex])
1321 {
1322 // Write out remainder
1323 for (int channelIndex2=channelIndex; channelIndex2 < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; channelIndex2++)
1324 out.Write(false);
1325
1326 // Send remainder
1327 replica->OnSerializeTransmission(&out, systemAddress);
1328 rakPeer->Send(&out,lastPro.priority,lastPro.reliability,lastPro.orderingChannel,systemAddress,false,lastPro.sendReceipt);
1329
1330 // If no data left to send, quit out
1331 bool anyData=false;
1332 for (int channelIndex2=channelIndex; channelIndex2 < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; channelIndex2++)
1333 {
1334 if (serializationData[channelIndex2].GetNumberOfBitsUsed()>0)
1335 {
1336 anyData=true;
1337 break;
1338 }
1339 }
1340 if (anyData==false)
1341 return SSICR_SENT_DATA;
1342
1343 // Restart stream
1344 SendSerializeHeader(replica, timestamp, &out, worldId);
1345
1346 for (int channelIndex2=0; channelIndex2 < channelIndex; channelIndex2++)
1347 out.Write(false);
1348 lastPro=sendParameters[channelIndex];
1349 }
1350
1351 bitsUsed=serializationData[channelIndex].GetNumberOfBitsUsed();
1352 channelHasData = indicesToSend[channelIndex]==true && bitsUsed>0;
1353 out.Write(channelHasData);
1354 if (channelHasData)
1355 {
1356 out.WriteCompressed(bitsUsed);
1357 out.AlignWriteToByteBoundary();
1358 out.Write(serializationData[channelIndex]);
1359 // Crap, forgot this line, was a huge bug in that I'd only send to the first 3 systems
1360 serializationData[channelIndex].ResetReadPointer();
1361 }
1362 }
1363 replica->OnSerializeTransmission(&out, systemAddress);
1364 rakPeer->Send(&out,lastPro.priority,lastPro.reliability,lastPro.orderingChannel,systemAddress,false,lastPro.sendReceipt);
1365 return SSICR_SENT_DATA;
1366 }
1367
1368 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1369
SendSerializeIfChanged(DataStructures::DefaultIndexType queryToSerializeIndex,SerializeParameters * sp,RakPeerInterface * rakPeer,unsigned char worldId,ReplicaManager3 * replicaManager)1370 SendSerializeIfChangedResult Connection_RM3::SendSerializeIfChanged(DataStructures::DefaultIndexType queryToSerializeIndex, SerializeParameters *sp, RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager)
1371 {
1372 RakNet::Replica3 *replica = queryToSerializeReplicaList[queryToSerializeIndex]->replica;
1373
1374 if (replica->GetNetworkID()==UNASSIGNED_NETWORK_ID)
1375 return SSICR_DID_NOT_SEND_DATA;
1376
1377 RM3QuerySerializationResult rm3qsr = replica->QuerySerialization(this);
1378 if (rm3qsr==RM3QSR_NEVER_CALL_SERIALIZE)
1379 {
1380 // Never again for this connection and replica pair
1381 OnNeverSerialize(queryToSerializeIndex, replicaManager);
1382 return SSICR_NEVER_SERIALIZE;
1383 }
1384
1385 if (rm3qsr==RM3QSR_DO_NOT_CALL_SERIALIZE)
1386 return SSICR_DID_NOT_SEND_DATA;
1387
1388 if (replica->forceSendUntilNextUpdate)
1389 {
1390 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1391 {
1392 if (replica->lastSentSerialization.indicesToSend[z])
1393 sp->bitsWrittenSoFar+=replica->lastSentSerialization.bitStream[z].GetNumberOfBitsUsed();
1394 }
1395 return SendSerialize(replica, replica->lastSentSerialization.indicesToSend, replica->lastSentSerialization.bitStream, sp->messageTimestamp, sp->pro, rakPeer, worldId);
1396 }
1397
1398 for (int i=0; i < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; i++)
1399 {
1400 sp->outputBitstream[i].Reset();
1401 if (queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS)
1402 sp->lastSentBitstream[i]=&queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[i];
1403 else
1404 sp->lastSentBitstream[i]=&replica->lastSentSerialization.bitStream[i];
1405 }
1406
1407 RM3SerializationResult serializationResult = replica->Serialize(sp);
1408
1409 if (serializationResult==RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION)
1410 {
1411 // Never again for this connection and replica pair
1412 OnNeverSerialize(queryToSerializeIndex, replicaManager);
1413 return SSICR_NEVER_SERIALIZE;
1414 }
1415
1416 if (serializationResult==RM3SR_DO_NOT_SERIALIZE)
1417 {
1418 // Don't serialize this tick only
1419 return SSICR_DID_NOT_SEND_DATA;
1420 }
1421
1422 // This is necessary in case the user in the Serialize() function for some reason read the bitstream they also wrote
1423 // WIthout this code, the Write calls to another bitstream would not write the entire bitstream
1424 BitSize_t sum=0;
1425 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1426 {
1427 sp->outputBitstream[z].ResetReadPointer();
1428 sum+=sp->outputBitstream[z].GetNumberOfBitsUsed();
1429 }
1430
1431 if (sum==0)
1432 {
1433 // Don't serialize this tick only
1434 return SSICR_DID_NOT_SEND_DATA;
1435 }
1436
1437 if (serializationResult==RM3SR_SERIALIZED_ALWAYS)
1438 {
1439 bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
1440 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1441 {
1442 sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
1443 allIndices[z]=true;
1444
1445 queryToSerializeReplicaList[queryToSerializeIndex]->AllocBS();
1446 queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].Reset();
1447 queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].Write(&sp->outputBitstream[z]);
1448 sp->outputBitstream[z].ResetReadPointer();
1449 }
1450 return SendSerialize(replica, allIndices, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId);
1451 }
1452
1453 if (serializationResult==RM3SR_SERIALIZED_ALWAYS_IDENTICALLY)
1454 {
1455 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1456 {
1457 replica->lastSentSerialization.indicesToSend[z]=sp->outputBitstream[z].GetNumberOfBitsUsed()>0;
1458 sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
1459 replica->lastSentSerialization.bitStream[z].Reset();
1460 replica->lastSentSerialization.bitStream[z].Write(&sp->outputBitstream[z]);
1461 sp->outputBitstream[z].ResetReadPointer();
1462 replica->forceSendUntilNextUpdate=true;
1463 }
1464 return SendSerialize(replica, replica->lastSentSerialization.indicesToSend, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId);
1465 }
1466
1467 bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
1468 if (serializationResult==RM3SR_BROADCAST_IDENTICALLY || serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION)
1469 {
1470 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1471 {
1472 if (sp->outputBitstream[z].GetNumberOfBitsUsed() > 0 &&
1473 (serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION ||
1474 ((sp->outputBitstream[z].GetNumberOfBitsUsed()!=replica->lastSentSerialization.bitStream[z].GetNumberOfBitsUsed() ||
1475 memcmp(sp->outputBitstream[z].GetData(), replica->lastSentSerialization.bitStream[z].GetData(), sp->outputBitstream[z].GetNumberOfBytesUsed())!=0))))
1476 {
1477 indicesToSend[z]=true;
1478 replica->lastSentSerialization.indicesToSend[z]=true;
1479 sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
1480 replica->lastSentSerialization.bitStream[z].Reset();
1481 replica->lastSentSerialization.bitStream[z].Write(&sp->outputBitstream[z]);
1482 sp->outputBitstream[z].ResetReadPointer();
1483 replica->forceSendUntilNextUpdate=true;
1484 }
1485 else
1486 {
1487 indicesToSend[z]=false;
1488 replica->lastSentSerialization.indicesToSend[z]=false;
1489 }
1490 }
1491 }
1492 else
1493 {
1494 queryToSerializeReplicaList[queryToSerializeIndex]->AllocBS();
1495
1496 // RM3SR_SERIALIZED_UNIQUELY
1497 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1498 {
1499 if (sp->outputBitstream[z].GetNumberOfBitsUsed() > 0 &&
1500 (sp->outputBitstream[z].GetNumberOfBitsUsed()!=queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].GetNumberOfBitsUsed() ||
1501 memcmp(sp->outputBitstream[z].GetData(), queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].GetData(), sp->outputBitstream[z].GetNumberOfBytesUsed())!=0)
1502 )
1503 {
1504 indicesToSend[z]=true;
1505 sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
1506 queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].Reset();
1507 queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].Write(&sp->outputBitstream[z]);
1508 sp->outputBitstream[z].ResetReadPointer();
1509 }
1510 else
1511 {
1512 indicesToSend[z]=false;
1513 }
1514 }
1515 }
1516
1517
1518 if (serializationResult==RM3SR_BROADCAST_IDENTICALLY || serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION)
1519 replica->forceSendUntilNextUpdate=true;
1520
1521 // Send out the data
1522 return SendSerialize(replica, indicesToSend, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId);
1523 }
1524
1525 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
OnLocalReference(Replica3 * replica3,ReplicaManager3 * replicaManager)1526 void Connection_RM3::OnLocalReference(Replica3* replica3, ReplicaManager3 *replicaManager)
1527 {
1528 ConstructionMode constructionMode = QueryConstructionMode();
1529 RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
1530 (void) replicaManager;
1531
1532 LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(__FILE__,__LINE__);
1533 lsr->replica=replica3;
1534 queryToConstructReplicaList.Push(lsr,replica3,__FILE__,__LINE__);
1535 }
1536
1537 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1538
OnDereference(Replica3 * replica3,ReplicaManager3 * replicaManager)1539 void Connection_RM3::OnDereference(Replica3* replica3, ReplicaManager3 *replicaManager)
1540 {
1541 ValidateLists(replicaManager);
1542
1543 LastSerializationResult* lsr=0;
1544 DataStructures::DefaultIndexType idx;
1545 idx=constructedReplicaList.GetIndexOf(replica3);
1546 if (idx!=(DataStructures::DefaultIndexType)-1)
1547 {
1548 lsr=constructedReplicaList[idx];
1549 constructedReplicaList.RemoveAtIndex(idx,__FILE__,__LINE__);
1550 }
1551
1552 for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
1553 {
1554 if (queryToConstructReplicaList[idx]->replica==replica3)
1555 {
1556 lsr=queryToConstructReplicaList[idx];
1557 queryToConstructReplicaList.RemoveAtIndex(idx,__FILE__,__LINE__);
1558 break;
1559 }
1560 }
1561
1562 for (idx=0; idx < queryToSerializeReplicaList.GetSize(); idx++)
1563 {
1564 if (queryToSerializeReplicaList[idx]->replica==replica3)
1565 {
1566 lsr=queryToSerializeReplicaList[idx];
1567 queryToSerializeReplicaList.RemoveAtIndex(idx,__FILE__,__LINE__);
1568 break;
1569 }
1570 }
1571
1572 for (idx=0; idx < queryToDestructReplicaList.GetSize(); idx++)
1573 {
1574 if (queryToDestructReplicaList[idx]->replica==replica3)
1575 {
1576 lsr=queryToDestructReplicaList[idx];
1577 queryToDestructReplicaList.RemoveAtIndex(idx,__FILE__,__LINE__);
1578 break;
1579 }
1580 }
1581
1582 ValidateLists(replicaManager);
1583
1584 if (lsr)
1585 RakNet::OP_DELETE(lsr,__FILE__,__LINE__);
1586
1587 ValidateLists(replicaManager);
1588 }
1589
1590 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1591
OnDownloadFromThisSystem(Replica3 * replica3,ReplicaManager3 * replicaManager)1592 void Connection_RM3::OnDownloadFromThisSystem(Replica3* replica3, ReplicaManager3 *replicaManager)
1593 {
1594 ValidateLists(replicaManager);
1595 LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(__FILE__,__LINE__);
1596 lsr->replica=replica3;
1597
1598 ConstructionMode constructionMode = QueryConstructionMode();
1599 if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
1600 {
1601 queryToConstructReplicaList.RemoveAtKey(replica3,false,__FILE__,__LINE__);
1602 queryToDestructReplicaList.Push(lsr,replica3,__FILE__,__LINE__);
1603 }
1604
1605 constructedReplicaList.Push(lsr,replica3,__FILE__,__LINE__);
1606 //assert(queryToSerializeReplicaList.GetIndexOf(replica3)==(DataStructures::DefaultIndexType)-1);
1607 queryToSerializeReplicaList.Push(lsr,replica3,__FILE__,__LINE__);
1608
1609 ValidateLists(replicaManager);
1610 }
1611
1612 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1613
OnDownloadFromOtherSystem(Replica3 * replica3,ReplicaManager3 * replicaManager)1614 void Connection_RM3::OnDownloadFromOtherSystem(Replica3* replica3, ReplicaManager3 *replicaManager)
1615 {
1616 ConstructionMode constructionMode = QueryConstructionMode();
1617 if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
1618 {
1619 if (queryToConstructReplicaList.GetIndexOf(replica3)==(DataStructures::DefaultIndexType)-1)
1620 OnLocalReference(replica3, replicaManager);
1621 }
1622 }
1623
1624 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1625
OnNeverConstruct(DataStructures::DefaultIndexType queryToConstructIdx,ReplicaManager3 * replicaManager)1626 void Connection_RM3::OnNeverConstruct(DataStructures::DefaultIndexType queryToConstructIdx, ReplicaManager3 *replicaManager)
1627 {
1628 ConstructionMode constructionMode = QueryConstructionMode();
1629 RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
1630
1631 ValidateLists(replicaManager);
1632 LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
1633 queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx,__FILE__,__LINE__);
1634 RakNet::OP_DELETE(lsr,__FILE__,__LINE__);
1635 ValidateLists(replicaManager);
1636 }
1637
1638 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1639
OnConstructToThisConnection(DataStructures::DefaultIndexType queryToConstructIdx,ReplicaManager3 * replicaManager)1640 void Connection_RM3::OnConstructToThisConnection(DataStructures::DefaultIndexType queryToConstructIdx, ReplicaManager3 *replicaManager)
1641 {
1642 ConstructionMode constructionMode = QueryConstructionMode();
1643 RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
1644
1645 ValidateLists(replicaManager);
1646 LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
1647 queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx,__FILE__,__LINE__);
1648 //assert(constructedReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
1649 constructedReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1650 //assert(queryToDestructReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
1651 queryToDestructReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1652 //assert(queryToSerializeReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
1653 if (lsr->replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID)
1654 queryToSerializeReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1655 ValidateLists(replicaManager);
1656 }
1657
1658 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1659
OnConstructToThisConnection(Replica3 * replica,ReplicaManager3 * replicaManager)1660 void Connection_RM3::OnConstructToThisConnection(Replica3 *replica, ReplicaManager3 *replicaManager)
1661 {
1662 RakAssert(QueryConstructionMode()==QUERY_CONNECTION_FOR_REPLICA_LIST);
1663 (void) replicaManager;
1664
1665 LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(__FILE__,__LINE__);
1666 lsr->replica=replica;
1667 constructedReplicaList.Push(lsr,replica,__FILE__,__LINE__);
1668 if (lsr->replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID)
1669 queryToSerializeReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1670 }
1671
1672 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1673
OnNeverSerialize(DataStructures::DefaultIndexType queryToSerializeIndex,ReplicaManager3 * replicaManager)1674 void Connection_RM3::OnNeverSerialize(DataStructures::DefaultIndexType queryToSerializeIndex, ReplicaManager3 *replicaManager)
1675 {
1676 ValidateLists(replicaManager);
1677 queryToSerializeReplicaList.RemoveAtIndex(queryToSerializeIndex,__FILE__,__LINE__);
1678 ValidateLists(replicaManager);
1679 }
1680
1681 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1682
OnReplicaAlreadyExists(DataStructures::DefaultIndexType queryToConstructIdx,ReplicaManager3 * replicaManager)1683 void Connection_RM3::OnReplicaAlreadyExists(DataStructures::DefaultIndexType queryToConstructIdx, ReplicaManager3 *replicaManager)
1684 {
1685 ConstructionMode constructionMode = QueryConstructionMode();
1686 RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
1687
1688 ValidateLists(replicaManager);
1689 LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
1690 queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx,__FILE__,__LINE__);
1691 //assert(constructedReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
1692 constructedReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1693 //assert(queryToDestructReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
1694 queryToDestructReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1695 //assert(queryToSerializeReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
1696 queryToSerializeReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1697 ValidateLists(replicaManager);
1698 }
1699
1700 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1701
OnDownloadExisting(Replica3 * replica3,ReplicaManager3 * replicaManager)1702 void Connection_RM3::OnDownloadExisting(Replica3* replica3, ReplicaManager3 *replicaManager)
1703 {
1704 ValidateLists(replicaManager);
1705
1706 ConstructionMode constructionMode = QueryConstructionMode();
1707 if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
1708 {
1709 DataStructures::DefaultIndexType idx;
1710 for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
1711 {
1712 if (queryToConstructReplicaList[idx]->replica==replica3)
1713 {
1714 OnConstructToThisConnection(idx, replicaManager);
1715 return;
1716 }
1717 }
1718 }
1719 else
1720 {
1721 OnConstructToThisConnection(replica3, replicaManager);
1722 }
1723 }
1724 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1725
OnSendDestructionFromQuery(DataStructures::DefaultIndexType queryToDestructIdx,ReplicaManager3 * replicaManager)1726 void Connection_RM3::OnSendDestructionFromQuery(DataStructures::DefaultIndexType queryToDestructIdx, ReplicaManager3 *replicaManager)
1727 {
1728 ConstructionMode constructionMode = QueryConstructionMode();
1729 RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
1730
1731 ValidateLists(replicaManager);
1732 LastSerializationResult* lsr = queryToDestructReplicaList[queryToDestructIdx];
1733 queryToDestructReplicaList.RemoveAtIndex(queryToDestructIdx,__FILE__,__LINE__);
1734 queryToSerializeReplicaList.RemoveAtKey(lsr->replica,false,__FILE__,__LINE__);
1735 constructedReplicaList.RemoveAtKey(lsr->replica,true,__FILE__,__LINE__);
1736 //assert(queryToConstructReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
1737 queryToConstructReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
1738 ValidateLists(replicaManager);
1739 }
1740
1741 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1742
OnDoNotQueryDestruction(DataStructures::DefaultIndexType queryToDestructIdx,ReplicaManager3 * replicaManager)1743 void Connection_RM3::OnDoNotQueryDestruction(DataStructures::DefaultIndexType queryToDestructIdx, ReplicaManager3 *replicaManager)
1744 {
1745 ValidateLists(replicaManager);
1746 queryToDestructReplicaList.RemoveAtIndex(queryToDestructIdx,__FILE__,__LINE__);
1747 ValidateLists(replicaManager);
1748 }
1749
1750 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1751
ValidateLists(ReplicaManager3 * replicaManager) const1752 void Connection_RM3::ValidateLists(ReplicaManager3 *replicaManager) const
1753 {
1754 (void) replicaManager;
1755 /*
1756 #ifdef _DEBUG
1757 // Each object should exist only once in either constructedReplicaList or queryToConstructReplicaList
1758 // replicaPointer from LastSerializationResult should be same among all lists
1759 DataStructures::DefaultIndexType idx, idx2;
1760 for (idx=0; idx < constructedReplicaList.GetSize(); idx++)
1761 {
1762 idx2=queryToConstructReplicaList.GetIndexOf(constructedReplicaList[idx]->replica);
1763 if (idx2!=(DataStructures::DefaultIndexType)-1)
1764 {
1765 int a=5;
1766 assert(a==0);
1767 int *b=0;
1768 *b=5;
1769 }
1770 }
1771
1772 for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
1773 {
1774 idx2=constructedReplicaList.GetIndexOf(queryToConstructReplicaList[idx]->replica);
1775 if (idx2!=(DataStructures::DefaultIndexType)-1)
1776 {
1777 int a=5;
1778 assert(a==0);
1779 int *b=0;
1780 *b=5;
1781 }
1782 }
1783
1784 LastSerializationResult *lsr, *lsr2;
1785 for (idx=0; idx < constructedReplicaList.GetSize(); idx++)
1786 {
1787 lsr=constructedReplicaList[idx];
1788
1789 idx2=queryToSerializeReplicaList.GetIndexOf(lsr->replica);
1790 if (idx2!=(DataStructures::DefaultIndexType)-1)
1791 {
1792 lsr2=queryToSerializeReplicaList[idx2];
1793 if (lsr2!=lsr)
1794 {
1795 int a=5;
1796 assert(a==0);
1797 int *b=0;
1798 *b=5;
1799 }
1800 }
1801
1802 idx2=queryToDestructReplicaList.GetIndexOf(lsr->replica);
1803 if (idx2!=(DataStructures::DefaultIndexType)-1)
1804 {
1805 lsr2=queryToDestructReplicaList[idx2];
1806 if (lsr2!=lsr)
1807 {
1808 int a=5;
1809 assert(a==0);
1810 int *b=0;
1811 *b=5;
1812 }
1813 }
1814 }
1815 for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
1816 {
1817 lsr=queryToConstructReplicaList[idx];
1818
1819 idx2=queryToSerializeReplicaList.GetIndexOf(lsr->replica);
1820 if (idx2!=(DataStructures::DefaultIndexType)-1)
1821 {
1822 lsr2=queryToSerializeReplicaList[idx2];
1823 if (lsr2!=lsr)
1824 {
1825 int a=5;
1826 assert(a==0);
1827 int *b=0;
1828 *b=5;
1829 }
1830 }
1831
1832 idx2=queryToDestructReplicaList.GetIndexOf(lsr->replica);
1833 if (idx2!=(DataStructures::DefaultIndexType)-1)
1834 {
1835 lsr2=queryToDestructReplicaList[idx2];
1836 if (lsr2!=lsr)
1837 {
1838 int a=5;
1839 assert(a==0);
1840 int *b=0;
1841 *b=5;
1842 }
1843 }
1844 }
1845
1846 // Verify pointer integrity
1847 for (idx=0; idx < constructedReplicaList.GetSize(); idx++)
1848 {
1849 if (constructedReplicaList[idx]->replica->replicaManager!=replicaManager)
1850 {
1851 int a=5;
1852 assert(a==0);
1853 int *b=0;
1854 *b=5;
1855 }
1856 }
1857
1858 // Verify pointer integrity
1859 for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
1860 {
1861 if (queryToConstructReplicaList[idx]->replica->replicaManager!=replicaManager)
1862 {
1863 int a=5;
1864 assert(a==0);
1865 int *b=0;
1866 *b=5;
1867 }
1868 }
1869 #endif
1870 */
1871 }
1872 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1873
SendConstruction(DataStructures::Multilist<ML_STACK,Replica3 *,Replica3 * > & newObjects,DataStructures::Multilist<ML_STACK,Replica3 *,Replica3 * > & deletedObjects,PRO sendParameters,RakPeerInterface * rakPeer,unsigned char worldId)1874 void Connection_RM3::SendConstruction(DataStructures::Multilist<ML_STACK, Replica3*, Replica3*> &newObjects, DataStructures::Multilist<ML_STACK, Replica3*, Replica3*> &deletedObjects, PRO sendParameters, RakPeerInterface *rakPeer, unsigned char worldId)
1875 {
1876 if (newObjects.GetSize()==0 && deletedObjects.GetSize()==0)
1877 return;
1878
1879 // All construction and destruction takes place in the same network message
1880 // Otherwise, if objects rely on each other being created the same tick to be valid, this won't always be true
1881 // DataStructures::Multilist<ML_STACK, LastSerializationResult* > serializedObjects;
1882 BitSize_t offsetStart, offsetEnd;
1883 DataStructures::DefaultIndexType newListIndex, oldListIndex;
1884 RakNet::BitStream bsOut;
1885 NetworkID networkId;
1886 if (isFirstConstruction)
1887 {
1888 bsOut.Write((MessageID)ID_REPLICA_MANAGER_DOWNLOAD_STARTED);
1889 bsOut.Write(worldId);
1890 SerializeOnDownloadStarted(&bsOut);
1891 rakPeer->Send(&bsOut,sendParameters.priority,RELIABLE_ORDERED,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
1892 }
1893
1894
1895 // LastSerializationResult* lsr;
1896 bsOut.Reset();
1897 bsOut.Write((MessageID)ID_REPLICA_MANAGER_CONSTRUCTION);
1898 bsOut.Write(worldId);
1899 bsOut.Write(newObjects.GetSize());
1900 // Construction
1901 for (newListIndex=0; newListIndex < newObjects.GetSize(); newListIndex++)
1902 {
1903 offsetStart=bsOut.GetWriteOffset();
1904 bsOut.Write(offsetStart);
1905 networkId=newObjects[newListIndex]->GetNetworkID();
1906 bsOut.Write(networkId);
1907 newObjects[newListIndex]->WriteAllocationID(&bsOut);
1908 if (networkId==UNASSIGNED_NETWORK_ID)
1909 bsOut.Write(newObjects[newListIndex]->GetAllocationNumber());
1910 bsOut.Write(newObjects[newListIndex]->creatingSystemGUID);
1911 newObjects[newListIndex]->SerializeConstruction(&bsOut, this);
1912 bsOut.AlignWriteToByteBoundary();
1913 offsetEnd=bsOut.GetWriteOffset();
1914 bsOut.SetWriteOffset(offsetStart);
1915 bsOut.Write(offsetEnd);
1916 bsOut.SetWriteOffset(offsetEnd);
1917 // lsr = Reference(newObjects[newListIndex],false);
1918 // serializedObjects.Push(newObjects[newListIndex]);
1919 }
1920
1921 // Destruction
1922 DataStructures::DefaultIndexType listSize=deletedObjects.GetSize();
1923 bsOut.Write(listSize);
1924 for (oldListIndex=0; oldListIndex < deletedObjects.GetSize(); oldListIndex++)
1925 {
1926 networkId=deletedObjects[oldListIndex]->GetNetworkID();
1927 bsOut.Write(networkId);
1928 offsetStart=bsOut.GetWriteOffset();
1929 bsOut.Write(offsetStart);
1930 deletedObjects[oldListIndex]->deletingSystemGUID=rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
1931 bsOut.Write(deletedObjects[oldListIndex]->deletingSystemGUID);
1932 deletedObjects[oldListIndex]->SerializeDestruction(&bsOut, this);
1933 bsOut.AlignWriteToByteBoundary();
1934 offsetEnd=bsOut.GetWriteOffset();
1935 bsOut.SetWriteOffset(offsetStart);
1936 bsOut.Write(offsetEnd);
1937 bsOut.SetWriteOffset(offsetEnd);
1938 }
1939 rakPeer->Send(&bsOut,sendParameters.priority,RELIABLE_ORDERED,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
1940
1941 // Initial Download serialize to a new system
1942 // Immediately send serialize after construction if the replica object already has saved data
1943 // If the object was serialized identically, and does not change later on, then the new connection never gets the data
1944 SerializeParameters sp;
1945 sp.whenLastSerialized=0;
1946 RakNet::BitStream emptyBs;
1947 for (int index=0; index < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; index++)
1948 {
1949 sp.lastSentBitstream[index]=&emptyBs;
1950 sp.pro[index]=sendParameters;
1951 sp.pro[index].reliability=RELIABLE_ORDERED;
1952 }
1953
1954 sp.bitsWrittenSoFar=0;
1955 RakNetTime t = RakNet::GetTime();
1956 for (newListIndex=0; newListIndex < newObjects.GetSize(); newListIndex++)
1957 {
1958 sp.destinationConnection=this;
1959 sp.messageTimestamp=0;
1960 RakNet::Replica3 *replica = newObjects[newListIndex];
1961 // 8/22/09 Forgot ResetWritePointer
1962 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1963 {
1964 sp.outputBitstream[z].ResetWritePointer();
1965 }
1966
1967 RM3SerializationResult res = replica->Serialize(&sp);
1968 if (replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID)
1969 {
1970 if (res!=RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION &&
1971 res!=RM3SR_DO_NOT_SERIALIZE &&
1972 res!=RM3SR_SERIALIZED_UNIQUELY)
1973 {
1974 bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
1975 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
1976 {
1977 sp.bitsWrittenSoFar+=sp.outputBitstream[z].GetNumberOfBitsUsed();
1978 allIndices[z]=true;
1979 }
1980 SendSerialize(replica, allIndices, sp.outputBitstream, sp.messageTimestamp, sp.pro, rakPeer, worldId);
1981 newObjects[newListIndex]->whenLastSerialized=t;
1982
1983 }
1984 }
1985 // else wait for construction request accepted before serializing
1986 }
1987
1988 if (isFirstConstruction)
1989 {
1990 bsOut.Reset();
1991 bsOut.Write((MessageID)ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE);
1992 bsOut.Write(worldId);
1993 SerializeOnDownloadComplete(&bsOut);
1994 rakPeer->Send(&bsOut,sendParameters.priority,RELIABLE_ORDERED,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
1995 }
1996
1997 isFirstConstruction=false;
1998
1999 }
2000
2001 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2002
SendValidation(RakPeerInterface * rakPeer,unsigned char worldId)2003 void Connection_RM3::SendValidation(RakPeerInterface *rakPeer, unsigned char worldId)
2004 {
2005 // Hijack to mean sendValidation
2006 RakNet::BitStream bsOut;
2007 bsOut.Write((MessageID)ID_REPLICA_MANAGER_SCOPE_CHANGE);
2008 bsOut.Write(worldId);
2009 rakPeer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,systemAddress,false);
2010 }
2011
2012 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2013
Replica3()2014 Replica3::Replica3()
2015 {
2016 creatingSystemGUID=UNASSIGNED_RAKNET_GUID;
2017 deletingSystemGUID=UNASSIGNED_RAKNET_GUID;
2018 replicaManager=0;
2019 forceSendUntilNextUpdate=false;
2020 whenLastSerialized=0;
2021 }
2022
2023 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2024
~Replica3()2025 Replica3::~Replica3()
2026 {
2027 if (replicaManager)
2028 {
2029 replicaManager->Dereference(this);
2030 }
2031 }
2032
2033 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2034
BroadcastDestruction(void)2035 void Replica3::BroadcastDestruction(void)
2036 {
2037 if (replicaManager)
2038 replicaManager->BroadcastDestruction(this,UNASSIGNED_SYSTEM_ADDRESS);
2039 }
2040
2041 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2042
GetCreatingSystemGUID(void) const2043 RakNetGUID Replica3::GetCreatingSystemGUID(void) const
2044 {
2045 return creatingSystemGUID;
2046 }
2047
2048 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2049
QueryConstruction_ClientConstruction(RakNet::Connection_RM3 * destinationConnection)2050 RM3ConstructionState Replica3::QueryConstruction_ClientConstruction(RakNet::Connection_RM3 *destinationConnection)
2051 {
2052 (void) destinationConnection;
2053 if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
2054 return RM3CS_SEND_CONSTRUCTION;
2055 // Send back to the owner client too, because they couldn't assign the network ID
2056 if (networkIDManager->IsNetworkIDAuthority())
2057 return RM3CS_SEND_CONSTRUCTION;
2058 return RM3CS_NEVER_CONSTRUCT;
2059 }
2060
2061 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2062
QueryRemoteConstruction_ClientConstruction(RakNet::Connection_RM3 * sourceConnection)2063 bool Replica3::QueryRemoteConstruction_ClientConstruction(RakNet::Connection_RM3 *sourceConnection)
2064 {
2065 (void) sourceConnection;
2066
2067 // OK to create
2068 return true;
2069 }
2070
2071 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2072
QueryConstruction_ServerConstruction(RakNet::Connection_RM3 * destinationConnection)2073 RM3ConstructionState Replica3::QueryConstruction_ServerConstruction(RakNet::Connection_RM3 *destinationConnection)
2074 {
2075 (void) destinationConnection;
2076
2077 if (networkIDManager->IsNetworkIDAuthority())
2078 return RM3CS_SEND_CONSTRUCTION;
2079 return RM3CS_NEVER_CONSTRUCT;
2080 }
2081
2082 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2083
QueryRemoteConstruction_ServerConstruction(RakNet::Connection_RM3 * sourceConnection)2084 bool Replica3::QueryRemoteConstruction_ServerConstruction(RakNet::Connection_RM3 *sourceConnection)
2085 {
2086 (void) sourceConnection;
2087 if (networkIDManager->IsNetworkIDAuthority())
2088 return false;
2089 return true;
2090 }
2091
2092 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2093
QueryConstruction_PeerToPeer(RakNet::Connection_RM3 * destinationConnection)2094 RM3ConstructionState Replica3::QueryConstruction_PeerToPeer(RakNet::Connection_RM3 *destinationConnection)
2095 {
2096 (void) destinationConnection;
2097
2098 // We send to all, others do nothing
2099 if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
2100 return RM3CS_SEND_CONSTRUCTION;
2101 return RM3CS_NEVER_CONSTRUCT;
2102 }
2103
2104 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2105
QueryRemoteConstruction_PeerToPeer(RakNet::Connection_RM3 * sourceConnection)2106 bool Replica3::QueryRemoteConstruction_PeerToPeer(RakNet::Connection_RM3 *sourceConnection)
2107 {
2108 (void) sourceConnection;
2109
2110 return true;
2111 }
2112
2113 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2114
QuerySerialization_ClientSerializable(RakNet::Connection_RM3 * destinationConnection)2115 RM3QuerySerializationResult Replica3::QuerySerialization_ClientSerializable(RakNet::Connection_RM3 *destinationConnection)
2116 {
2117 // Owner client sends to all
2118 if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
2119 return RM3QSR_CALL_SERIALIZE;
2120 // Server sends to all but owner client
2121 if (networkIDManager->IsNetworkIDAuthority() && destinationConnection->GetRakNetGUID()!=creatingSystemGUID)
2122 return RM3QSR_CALL_SERIALIZE;
2123 // Remote clients do not send
2124 return RM3QSR_NEVER_CALL_SERIALIZE;
2125 }
2126
2127 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2128
QuerySerialization_ServerSerializable(RakNet::Connection_RM3 * destinationConnection)2129 RM3QuerySerializationResult Replica3::QuerySerialization_ServerSerializable(RakNet::Connection_RM3 *destinationConnection)
2130 {
2131 (void) destinationConnection;
2132 // Server sends to all
2133 if (networkIDManager->IsNetworkIDAuthority())
2134 return RM3QSR_CALL_SERIALIZE;
2135
2136 // Clients do not send
2137 return RM3QSR_NEVER_CALL_SERIALIZE;
2138 }
2139
2140 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2141
QuerySerialization_PeerToPeer(RakNet::Connection_RM3 * destinationConnection)2142 RM3QuerySerializationResult Replica3::QuerySerialization_PeerToPeer(RakNet::Connection_RM3 *destinationConnection)
2143 {
2144 (void) destinationConnection;
2145
2146 // Owner peer sends to all
2147 if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
2148 return RM3QSR_CALL_SERIALIZE;
2149
2150 // Remote peers do not send
2151 return RM3QSR_NEVER_CALL_SERIALIZE;
2152 }
2153
2154 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2155
QueryActionOnPopConnection_Client(RakNet::Connection_RM3 * droppedConnection) const2156 RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_Client(RakNet::Connection_RM3 *droppedConnection) const
2157 {
2158 (void) droppedConnection;
2159 return RM3AOPC_DELETE_REPLICA;
2160 }
2161
2162 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2163
QueryActionOnPopConnection_Server(RakNet::Connection_RM3 * droppedConnection) const2164 RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_Server(RakNet::Connection_RM3 *droppedConnection) const
2165 {
2166 (void) droppedConnection;
2167 return RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION;
2168 }
2169
2170 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2171
QueryActionOnPopConnection_PeerToPeer(RakNet::Connection_RM3 * droppedConnection) const2172 RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_PeerToPeer(RakNet::Connection_RM3 *droppedConnection) const
2173 {
2174 (void) droppedConnection;
2175 return RM3AOPC_DELETE_REPLICA;
2176 }
2177
2178 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2179
2180 #endif // _RAKNET_SUPPORT_*
2181