• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ReplicaManager3.cpp

Go to the documentation of this file.
00001 #include "NativeFeatureIncludes.h"
00002 #if _RAKNET_SUPPORT_ReplicaManager3==1
00003 
00004 #include "ReplicaManager3.h"
00005 #include "GetTime.h"
00006 #include "MessageIdentifiers.h"
00007 #include "RakPeerInterface.h"
00008 #include "NetworkIDManager.h"
00009 
00010 using namespace RakNet;
00011 
00012 DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS(LastSerializationResult,Replica3*,replica);
00013 
00014 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00015 
00016 bool PRO::operator==( const PRO& right ) const
00017 {
00018         return priority == right.priority && reliability == right.reliability && orderingChannel == right.orderingChannel && sendReceipt == right.sendReceipt;
00019 }
00020 
00021 bool PRO::operator!=( const PRO& right ) const
00022 {
00023         return priority != right.priority || reliability != right.reliability || orderingChannel != right.orderingChannel || sendReceipt != right.sendReceipt;
00024 }
00025 
00026 
00027 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00028 
00029 LastSerializationResult::LastSerializationResult()
00030 {
00031         replica=0;
00032         lastSerializationResultBS=0;
00033 }
00034 LastSerializationResult::~LastSerializationResult()
00035 {
00036         if (lastSerializationResultBS)
00037                 RakNet::OP_DELETE(lastSerializationResultBS,__FILE__,__LINE__);
00038 }
00039 void LastSerializationResult::AllocBS(void)
00040 {
00041         if (lastSerializationResultBS==0)
00042         {
00043                 lastSerializationResultBS=RakNet::OP_NEW<LastSerializationResultBS>(__FILE__,__LINE__);
00044         }
00045 }
00046 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00047 
00048 ReplicaManager3::ReplicaManager3()
00049 {
00050         defaultSendParameters.orderingChannel=0;
00051         defaultSendParameters.priority=HIGH_PRIORITY;
00052         defaultSendParameters.reliability=RELIABLE_ORDERED;
00053         defaultSendParameters.sendReceipt=0;
00054         autoSerializeInterval=30;
00055         lastAutoSerializeOccurance=0;
00056         worldId=0;
00057         autoCreateConnections=true;
00058         autoDestroyConnections=true;
00059         networkIDManager=0;
00060 }
00061 
00062 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00063 
00064 ReplicaManager3::~ReplicaManager3()
00065 {
00066         Clear();
00067 }
00068 
00069 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00070 
00071 void ReplicaManager3::SetAutoManageConnections(bool autoCreate, bool autoDestroy)
00072 {
00073         autoCreateConnections=autoCreate;
00074         autoDestroyConnections=autoDestroy;
00075 }
00076 
00077 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00078 
00079 bool ReplicaManager3::PushConnection(RakNet::Connection_RM3 *newConnection)
00080 {
00081         if (newConnection==0)
00082                 return false;
00083         if (GetConnectionByGUID(newConnection->GetRakNetGUID()))
00084                 return false;
00085         DataStructures::DefaultIndexType index = connectionList.GetInsertionIndex(newConnection);
00086         if (index!=(DataStructures::DefaultIndexType)-1)
00087         {
00088                 connectionList.InsertAtIndex(newConnection,index,__FILE__,__LINE__);
00089 
00090                 // Send message to validate the connection
00091                 newConnection->SendValidation(rakPeerInterface, worldId);
00092 
00093                 Connection_RM3::ConstructionMode constructionMode = newConnection->QueryConstructionMode();
00094                 if (constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
00095                 {
00096                         DataStructures::DefaultIndexType pushIdx;
00097                         for (pushIdx=0; pushIdx < userReplicaList.GetSize(); pushIdx++)
00098                                 newConnection->OnLocalReference(userReplicaList[pushIdx], this);
00099                 }               
00100         }
00101         return true;
00102 }
00103 
00104 
00105 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00106 
00107 RakNet::Connection_RM3 * ReplicaManager3::PopConnection(DataStructures::DefaultIndexType index)
00108 {
00109         DataStructures::Multilist<ML_STACK, Replica3*> replicaList;
00110         DataStructures::Multilist<ML_STACK, Replica3*> destructionList;
00111         DataStructures::Multilist<ML_STACK, Replica3*> broadcastList;
00112         RakNet::Connection_RM3 *connection;
00113         DataStructures::DefaultIndexType index2;
00114         RM3ActionOnPopConnection action;
00115 
00116         connection=connectionList[index];
00117 
00118         RakNetGUID guid = connection->GetRakNetGUID();
00119         GetReplicasCreatedByGuid(guid, replicaList);
00120 
00121         for (index2=0; index2 < replicaList.GetSize(); index2++)
00122         {
00123                 action = replicaList[index2]->QueryActionOnPopConnection(connection);
00124                 replicaList[index2]->OnPoppedConnection(connection);
00125                 if (action==RM3AOPC_DELETE_REPLICA)
00126                 {
00127                         destructionList.Push( replicaList[index2], __FILE__, __LINE__  );
00128                 }
00129                 else if (action==RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION)
00130                 {
00131                         destructionList.Push( replicaList[index2], __FILE__, __LINE__  );
00132 
00133                         broadcastList.Push( replicaList[index2], __FILE__, __LINE__  );
00134                 }
00135         }
00136 
00137         BroadcastDestructionList(broadcastList, connection->GetSystemAddress());
00138         for (index2=0; index2 < destructionList.GetSize(); index2++)
00139         {
00140                 destructionList[index2]->PreDestruction(connection);
00141                 destructionList[index2]->DeallocReplica(connection);
00142         }
00143 
00144         connectionList.RemoveAtIndex(index,__FILE__,__LINE__);
00145         return connection;
00146 }
00147 
00148 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00149 
00150 RakNet::Connection_RM3 * ReplicaManager3::PopConnection(RakNetGUID guid)
00151 {
00152         DataStructures::DefaultIndexType index;
00153 
00154         for (index=0; index < connectionList.GetSize(); index++)
00155         {
00156                 if (connectionList[index]->GetRakNetGUID()==guid)
00157                 {
00158                         return PopConnection(index);
00159                 }
00160         }
00161         return 0;
00162 }
00163 
00164 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00165 
00166 void ReplicaManager3::Reference(RakNet::Replica3 *replica3)
00167 {
00168         DataStructures::DefaultIndexType index = ReferenceInternal(replica3);
00169 
00170         if (index!=(DataStructures::DefaultIndexType)-1)
00171         {
00172                 DataStructures::DefaultIndexType pushIdx;
00173                 for (pushIdx=0; pushIdx < connectionList.GetSize(); pushIdx++)
00174                 {
00175                         Connection_RM3::ConstructionMode constructionMode = connectionList[pushIdx]->QueryConstructionMode();
00176                         if (constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
00177                         {
00178                                 connectionList[pushIdx]->OnLocalReference(replica3, this);
00179                         }
00180                 }
00181         }
00182 }
00183 
00184 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00185 
00186 DataStructures::DefaultIndexType ReplicaManager3::ReferenceInternal(RakNet::Replica3 *replica3)
00187 {
00188         DataStructures::DefaultIndexType index;
00189         index = userReplicaList.GetInsertionIndex(replica3);
00190         if (index!=(DataStructures::DefaultIndexType)-1)
00191         {
00192                 if (networkIDManager==0)
00193                         networkIDManager=rakPeerInterface->GetNetworkIDManager();
00194                 RakAssert(networkIDManager);
00195                 replica3->SetNetworkIDManager(networkIDManager);
00196                 if (replica3->creatingSystemGUID==UNASSIGNED_RAKNET_GUID)
00197                         replica3->creatingSystemGUID=rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
00198                 replica3->replicaManager=this;
00199                 userReplicaList.InsertAtIndex(replica3,index,__FILE__,__LINE__);
00200         }
00201         return index;
00202 }
00203 
00204 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00205 
00206 void ReplicaManager3::Dereference(RakNet::Replica3 *replica3)
00207 {
00208         DataStructures::DefaultIndexType index, index2;
00209         for (index=0; index < userReplicaList.GetSize(); index++)
00210         {
00211                 if (userReplicaList[index]==replica3)
00212                 {
00213                         userReplicaList.RemoveAtIndex(index,__FILE__,__LINE__);
00214                         break;
00215                 }
00216         }
00217 
00218         // Remove from all connections
00219         for (index2=0; index2 < connectionList.GetSize(); index2++)
00220         {
00221                 connectionList[index2]->OnDereference(replica3, this);
00222         }
00223 }
00224 
00225 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00226 
00227 void ReplicaManager3::DereferenceList(DataStructures::Multilist<ML_STACK, Replica3*> &replicaListIn)
00228 {
00229         DataStructures::DefaultIndexType index;
00230         for (index=0; index < userReplicaList.GetSize(); index++)
00231                 Dereference(replicaListIn[index]);
00232 }
00233 
00234 
00235 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00236 
00237 void ReplicaManager3::GetReplicasCreatedByMe(DataStructures::Multilist<ML_STACK, Replica3*> &replicaListOut)
00238 {
00239         RakNetGUID myGuid = rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
00240         GetReplicasCreatedByGuid(rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS), replicaListOut);
00241 }
00242 
00243 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00244 
00245 void ReplicaManager3::GetReferencedReplicaList(DataStructures::Multilist<ML_STACK, Replica3*> &replicaListOut)
00246 {
00247         replicaListOut=userReplicaList;
00248 }
00249 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00250 
00251 void ReplicaManager3::GetReplicasCreatedByGuid(RakNetGUID guid, DataStructures::Multilist<ML_STACK, Replica3*> &replicaListOut)
00252 {
00253         replicaListOut.Clear(false,__FILE__,__LINE__);
00254         DataStructures::DefaultIndexType index;
00255         for (index=0; index < userReplicaList.GetSize(); index++)
00256         {
00257                 if (userReplicaList[index]->creatingSystemGUID==guid)
00258                         replicaListOut.Push(userReplicaList[index],__FILE__,__LINE__);
00259         }
00260 }
00261 
00262 
00263 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00264 
00265 unsigned ReplicaManager3::GetReplicaCount(void) const
00266 {
00267         return userReplicaList.GetSize();
00268 }
00269 
00270 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00271 
00272 Replica3 *ReplicaManager3::GetReplicaAtIndex(unsigned index)
00273 {
00274         return userReplicaList[index];
00275 }
00276 
00277 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00278 
00279 DataStructures::DefaultIndexType ReplicaManager3::GetConnectionCount(void) const
00280 {
00281         return connectionList.GetSize();
00282 }
00283 
00284 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00285 
00286 Connection_RM3* ReplicaManager3::GetConnectionAtIndex(unsigned index) const
00287 {
00288         return connectionList[index];
00289 }
00290 
00291 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00292 
00293 Connection_RM3* ReplicaManager3::GetConnectionBySystemAddress(SystemAddress sa) const
00294 {
00295         DataStructures::DefaultIndexType index;
00296         for (index=0; index < connectionList.GetSize(); index++)
00297         {
00298                 if (connectionList[index]->GetSystemAddress()==sa)
00299                 {
00300                         return connectionList[index];
00301                 }
00302         }
00303         return 0;
00304 }
00305 
00306 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00307 
00308 Connection_RM3* ReplicaManager3::GetConnectionByGUID(RakNetGUID guid) const
00309 {
00310         DataStructures::DefaultIndexType index;
00311         for (index=0; index < connectionList.GetSize(); index++)
00312         {
00313                 if (connectionList[index]->GetRakNetGUID()==guid)
00314                 {
00315                         return connectionList[index];
00316                 }
00317         }
00318         return 0;
00319 }
00320 
00321 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00322 
00323 void ReplicaManager3::SetDefaultOrderingChannel(char def)
00324 {
00325         defaultSendParameters.orderingChannel=def;
00326 }
00327 
00328 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00329 
00330 void ReplicaManager3::SetDefaultPacketPriority(PacketPriority def)
00331 {
00332         defaultSendParameters.priority=def;
00333 }
00334 
00335 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00336 
00337 void ReplicaManager3::SetDefaultPacketReliability(PacketReliability def)
00338 {
00339         defaultSendParameters.reliability=def;
00340 }
00341 
00342 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00343 
00344 void ReplicaManager3::SetAutoSerializeInterval(RakNetTime intervalMS)
00345 {
00346         autoSerializeInterval=intervalMS;
00347 }
00348 
00349 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00350 
00351 void ReplicaManager3::GetConnectionsThatHaveReplicaConstructed(Replica3 *replica, DataStructures::Multilist<ML_STACK, Connection_RM3*> &connectionsThatHaveConstructedThisReplica)
00352 {
00353         connectionsThatHaveConstructedThisReplica.Clear(false,__FILE__,__LINE__);
00354         DataStructures::DefaultIndexType index;
00355         for (index=0; index < connectionList.GetSize(); index++)
00356         {
00357                 if (connectionList[index]->HasReplicaConstructed(replica))
00358                         connectionsThatHaveConstructedThisReplica.Push(connectionList[index],__FILE__,__LINE__);
00359         }
00360 }
00361 
00362 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00363 
00364 void ReplicaManager3::Clear(void)
00365 {
00366         if (autoDestroyConnections)
00367         {
00368                 for (DataStructures::DefaultIndexType i=0; i < connectionList.GetSize(); i++)
00369                         DeallocConnection(connectionList[i]);
00370         }
00371         connectionList.Clear(true,__FILE__,__LINE__);
00372         userReplicaList.Clear(true,__FILE__,__LINE__);
00373 }
00374 
00375 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00376 
00377 PRO ReplicaManager3::GetDefaultSendParameters(void) const
00378 {
00379         return defaultSendParameters;
00380 }
00381 
00382 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00383 
00384 void ReplicaManager3::SetWorldID(unsigned char id)
00385 {
00386         worldId=id;
00387 }
00388 
00389 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00390 
00391 unsigned char ReplicaManager3::GetWorldID(void) const
00392 {
00393         return worldId;
00394 }
00395 
00396 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00397 
00398 NetworkIDManager *ReplicaManager3::GetNetworkIDManager(void) const
00399 {
00400         return networkIDManager;
00401 }
00402 
00403 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00404 
00405 void ReplicaManager3::SetNetworkIDManager(NetworkIDManager *_networkIDManager)
00406 {
00407         networkIDManager=_networkIDManager;
00408         if (networkIDManager)
00409                 networkIDManager->SetGuid(rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
00410 }
00411 
00412 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00413 
00414 PluginReceiveResult ReplicaManager3::OnReceive(Packet *packet)
00415 {
00416         if (packet->length<2)
00417                 return RR_CONTINUE_PROCESSING;
00418 
00419         unsigned char incomingWorldId;
00420 
00421         RakNetTime timestamp=0;
00422         unsigned char packetIdentifier, packetDataOffset;
00423         if ( ( unsigned char ) packet->data[ 0 ] == ID_TIMESTAMP )
00424         {
00425                 if ( packet->length > sizeof( unsigned char ) + sizeof( RakNetTime ) )
00426                 {
00427                         packetIdentifier = ( unsigned char ) packet->data[ sizeof( unsigned char ) + sizeof( RakNetTime ) ];
00428                         // Required for proper endian swapping
00429                         RakNet::BitStream tsBs(packet->data+sizeof(MessageID),packet->length-1,false);
00430                         tsBs.Read(timestamp);
00431                         incomingWorldId=packet->data[sizeof( unsigned char )*2 + sizeof( RakNetTime )];
00432                         packetDataOffset=sizeof( unsigned char )*3 + sizeof( RakNetTime );
00433                 }
00434                 else
00435                         return RR_STOP_PROCESSING_AND_DEALLOCATE;
00436         }
00437         else
00438         {
00439                 packetIdentifier = ( unsigned char ) packet->data[ 0 ];
00440                 incomingWorldId=packet->data[sizeof( unsigned char )];
00441                 packetDataOffset=sizeof( unsigned char )*2;
00442         }
00443 
00444         switch (packetIdentifier)
00445         {
00446         case ID_REPLICA_MANAGER_CONSTRUCTION:
00447                 if (incomingWorldId!=worldId)
00448                         return RR_CONTINUE_PROCESSING;
00449                 OnConstruction(packet->data, packet->length, packet->guid, packetDataOffset);
00450                 break;
00451         case ID_REPLICA_MANAGER_SERIALIZE:
00452                 if (incomingWorldId!=worldId)
00453                         return RR_CONTINUE_PROCESSING;
00454                 OnSerialize(packet->data, packet->length, packet->guid, timestamp, packetDataOffset);
00455                 break;
00456         case ID_REPLICA_MANAGER_3_LOCAL_CONSTRUCTION_REJECTED:
00457                 if (incomingWorldId!=worldId)
00458                         return RR_CONTINUE_PROCESSING;
00459                 OnLocalConstructionRejected(packet->data, packet->length, packet->guid, packetDataOffset);
00460                 break;
00461         case ID_REPLICA_MANAGER_3_LOCAL_CONSTRUCTION_ACCEPTED:
00462                 if (incomingWorldId!=worldId)
00463                         return RR_CONTINUE_PROCESSING;
00464                 OnLocalConstructionAccepted(packet->data, packet->length, packet->guid, packetDataOffset);
00465                 break;
00466         case ID_REPLICA_MANAGER_DOWNLOAD_STARTED:
00467                 if (incomingWorldId!=worldId)
00468                         return RR_CONTINUE_PROCESSING;
00469                 OnDownloadStarted(packet->data, packet->length, packet->guid, packetDataOffset);
00470                 break;
00471         case ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE:
00472                 if (incomingWorldId!=worldId)
00473                         return RR_CONTINUE_PROCESSING;
00474                 OnDownloadComplete(packet->data, packet->length, packet->guid, packetDataOffset);
00475                 break;
00476         case ID_REPLICA_MANAGER_3_SERIALIZE_CONSTRUCTION_EXISTING:
00477                 if (incomingWorldId!=worldId)
00478                         return RR_CONTINUE_PROCESSING;
00479                 OnConstructionExisting(packet->data, packet->length, packet->guid, packetDataOffset);
00480                 break;
00481         case ID_REPLICA_MANAGER_SCOPE_CHANGE:
00482                 {
00483                         if (incomingWorldId!=worldId)
00484                                 return RR_CONTINUE_PROCESSING;
00485 
00486                         Connection_RM3 *connection = GetConnectionByGUID(packet->guid);
00487                         if (connection && connection->isValidated==false)
00488                         {
00489                                 // This connection is now confirmed bidirectional
00490                                 connection->isValidated=true;
00491                                 // Reply back on validation
00492                                 connection->SendValidation(rakPeerInterface,worldId);
00493                         }
00494                 }
00495         }
00496 
00497         return RR_CONTINUE_PROCESSING;
00498 }
00499 
00500 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00501 
00502 void Connection_RM3::AutoConstructByQuery(ReplicaManager3 *replicaManager3)
00503 {
00504         ValidateLists(replicaManager3);
00505 
00506         ConstructionMode constructionMode = QueryConstructionMode();
00507 
00508         DataStructures::DefaultIndexType index;
00509         RM3ConstructionState constructionState;
00510         LastSerializationResult *lsr;
00511         index=0;
00512 
00513         constructedReplicasCulled.Clear(false,__FILE__,__LINE__);
00514         destroyedReplicasCulled.Clear(false,__FILE__,__LINE__);
00515 
00516         if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
00517         {
00518                 while (index < queryToConstructReplicaList.GetSize())
00519                 {
00520                         lsr=queryToConstructReplicaList[index];
00521                         constructionState=lsr->replica->QueryConstruction(this, replicaManager3);
00522                         if (constructionState==RM3CS_ALREADY_EXISTS_REMOTELY)
00523                         {
00524                                 OnReplicaAlreadyExists(index, replicaManager3);
00525 
00526                                 // Serialize construction data to this connection
00527                                 RakNet::BitStream bsOut;
00528                                 bsOut.Write((MessageID)ID_REPLICA_MANAGER_3_SERIALIZE_CONSTRUCTION_EXISTING);
00529                                 bsOut.Write(replicaManager3->GetWorldID());
00530                                 NetworkID networkId;
00531                                 networkId=lsr->replica->GetNetworkID();
00532                                 bsOut.Write(networkId);
00533                                 BitSize_t bitsWritten = bsOut.GetNumberOfBitsUsed();
00534                                 lsr->replica->SerializeConstructionExisting(&bsOut, this);
00535                                 if (bsOut.GetNumberOfBitsUsed()!=bitsWritten)
00536                                         replicaManager3->SendUnified(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,GetSystemAddress(), false);
00537 
00538                                 // Serialize first serialization to this connection.
00539                                 // This is done here, as it isn't done in PushConstruction
00540                                 SerializeParameters sp;
00541                                 RakNet::BitStream emptyBs;
00542                                 for (index=0; index < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; index++)
00543                                 {
00544                                         sp.lastSentBitstream[index]=&emptyBs;
00545                                         sp.pro[index]=replicaManager3->GetDefaultSendParameters();
00546                                 }
00547                                 sp.bitsWrittenSoFar=0;
00548                                 sp.destinationConnection=this;
00549                                 sp.messageTimestamp=0;
00550                                 sp.whenLastSerialized=0;
00551 
00552                                 RakNet::Replica3 *replica = lsr->replica;
00553 
00554                                 RM3SerializationResult res = replica->Serialize(&sp);
00555                                 if (res!=RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION &&
00556                                         res!=RM3SR_DO_NOT_SERIALIZE &&
00557                                         res!=RM3SR_SERIALIZED_UNIQUELY)
00558                                 {
00559                                         bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
00560                                         for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
00561                                         {
00562                                                 sp.bitsWrittenSoFar+=sp.outputBitstream[z].GetNumberOfBitsUsed();
00563                                                 allIndices[z]=true;
00564                                         }
00565                                         if (SendSerialize(replica, allIndices, sp.outputBitstream, sp.messageTimestamp, sp.pro, replicaManager3->GetRakPeerInterface(), replicaManager3->GetWorldID())==SSICR_SENT_DATA)
00566                                                 lsr->replica->whenLastSerialized=RakNet::GetTime();
00567                                 }
00568                         }
00569                         else if (constructionState==RM3CS_SEND_CONSTRUCTION)
00570                         {
00571                                 OnConstructToThisConnection(index, replicaManager3);
00572                                 constructedReplicasCulled.Push(lsr->replica,lsr->replica,__FILE__,__LINE__);
00573                         }
00574                         else if (constructionState==RM3CS_NEVER_CONSTRUCT)
00575                         {
00576                                 OnNeverConstruct(index, replicaManager3);
00577                         }
00578                         else//  if (constructionState==RM3CS_NO_ACTION)
00579                         {
00580                                 // Do nothing
00581                                 index++;
00582                         }
00583                 }
00584 
00585                 if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
00586                 {
00587                         RM3DestructionState destructionState;
00588                         index=0;
00589                         while (index < queryToDestructReplicaList.GetSize())
00590                         {
00591                                 lsr=queryToDestructReplicaList[index];
00592                                 destructionState=lsr->replica->QueryDestruction(this, replicaManager3);
00593                                 if (destructionState==RM3DS_SEND_DESTRUCTION)
00594                                 {
00595                                         OnSendDestructionFromQuery(index, replicaManager3);
00596                                         destroyedReplicasCulled.Push(lsr->replica,lsr->replica,__FILE__,__LINE__);
00597                                 }
00598                                 else if (destructionState==RM3DS_DO_NOT_QUERY_DESTRUCTION)
00599                                 {
00600                                         OnDoNotQueryDestruction(index, replicaManager3);
00601                                 }
00602                                 else//  if (destructionState==RM3CS_NO_ACTION)
00603                                 {
00604                                         // Do nothing
00605                                         index++;
00606                                 }
00607                         }
00608                 }
00609         }
00610         else if (constructionMode==QUERY_CONNECTION_FOR_REPLICA_LIST)
00611         {
00612                 QueryReplicaList(constructedReplicasCulled,destroyedReplicasCulled);
00613 
00614                 DataStructures::DefaultIndexType idx1, idx2;
00615 #ifdef _DEBUG
00616                 // The user should not construct a replica that already exists
00617                 for (idx2=0; idx2 < constructedReplicasCulled.GetSize(); idx2++)
00618                 {
00619                         RakAssert(constructedReplicaList.GetIndexOf(constructedReplicasCulled[idx2])==(DataStructures::DefaultIndexType)-1);
00620                 }
00621 
00622 #endif
00623 
00624                 // Create new
00625                 for (idx2=0; idx2 < constructedReplicasCulled.GetSize(); idx2++)
00626                         OnConstructToThisConnection(constructedReplicasCulled[idx2], replicaManager3);
00627 
00628                 bool exists;
00629                 for (idx2=0; idx2 < destroyedReplicasCulled.GetSize(); idx2++)
00630                 {
00631                         exists=false;
00632                         idx1=constructedReplicaList.GetIndexOf(destroyedReplicasCulled[idx2]);
00633                         RakAssert(idx1!=(DataStructures::DefaultIndexType)-1);
00634                         if (idx1!=(DataStructures::DefaultIndexType)-1)
00635                         {
00636                                 OnSendDestructionFromQuery(idx1,replicaManager3);
00637                         }
00638 
00639                         // If this assert hits, the user tried to destroy a replica that doesn't exist on the remote system
00640                         RakAssert(exists);
00641                 }
00642         }
00643 
00644         SendConstruction(constructedReplicasCulled,destroyedReplicasCulled,replicaManager3->defaultSendParameters,replicaManager3->rakPeerInterface,replicaManager3->worldId);
00645 
00646 }
00647 void ReplicaManager3::Update(void)
00648 {
00649         DataStructures::DefaultIndexType index,index2;
00650 
00651         for (index=0; index < connectionList.GetSize(); index++)
00652         {
00653                 if (connectionList[index]->isValidated==false)
00654                         continue;
00655                 connectionList[index]->AutoConstructByQuery(this);
00656         }
00657 
00658         if (autoSerializeInterval>0)
00659         {
00660                 RakNetTime time = RakNet::GetTime();
00661 
00662                 if (time - lastAutoSerializeOccurance > autoSerializeInterval)
00663                 {
00664                         for (index=0; index < userReplicaList.GetSize(); index++)
00665                         {
00666                                 userReplicaList[index]->forceSendUntilNextUpdate=false;
00667                                 userReplicaList[index]->OnUserReplicaPreSerializeTick();
00668                         }
00669 
00670 
00671                         DataStructures::DefaultIndexType index;
00672                         SerializeParameters sp;
00673                         sp.curTime=time;
00674                         Connection_RM3 *connection;
00675                         SendSerializeIfChangedResult ssicr;
00676                         sp.messageTimestamp=0;
00677                         for (int i=0; i < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; i++)
00678                                 sp.pro[i]=defaultSendParameters;
00679                         index2=0;
00680                         for (index=0; index < connectionList.GetSize(); index++)
00681                         {
00682                                 connection = connectionList[index];
00683                                 sp.bitsWrittenSoFar=0;
00684                                 index2=0;
00685                                 while (index2 < connection->queryToSerializeReplicaList.GetSize())
00686                                 {
00687                                         sp.destinationConnection=connection;
00688                                         sp.whenLastSerialized=connection->queryToSerializeReplicaList[index2]->replica->whenLastSerialized;
00689                                         ssicr=connection->SendSerializeIfChanged(index2, &sp, GetRakPeerInterface(), GetWorldID(), this);
00690                                         if (ssicr==SSICR_SENT_DATA)
00691                                         {
00692                                                 connection->queryToSerializeReplicaList[index2]->replica->whenLastSerialized=time;
00693                                                 index2++;
00694                                         }
00695                                         else if (ssicr==SSICR_NEVER_SERIALIZE)
00696                                         {
00697                                                 // Removed from the middle of the list
00698                                         }
00699                                         else
00700                                                 index2++;
00701                                 }
00702                         }
00703 
00704                         lastAutoSerializeOccurance=time;
00705                 }
00706         }
00707 }
00708 
00709 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00710 
00711 void ReplicaManager3::OnClosedConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
00712 {
00713         (void) lostConnectionReason;
00714         (void) systemAddress;
00715         if (autoDestroyConnections)
00716         {
00717                 Connection_RM3 *connection = PopConnection(rakNetGUID);
00718                 if (connection)
00719                         DeallocConnection(connection);
00720         }
00721 }
00722 
00723 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00724 
00725 void ReplicaManager3::OnNewConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
00726 {
00727         (void) isIncoming;
00728         if (autoCreateConnections)
00729         {
00730                 Connection_RM3 *connection = AllocConnection(systemAddress, rakNetGUID);
00731                 if (connection)
00732                         PushConnection(connection);
00733         }
00734 }
00735 
00736 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00737 
00738 void ReplicaManager3::OnRakPeerShutdown(void)
00739 {
00740         if (autoDestroyConnections)
00741         {
00742                 while (connectionList.GetSize())
00743                 {
00744                         Connection_RM3 *connection = PopConnection(connectionList.GetSize()-1);
00745                         if (connection)
00746                                 DeallocConnection(connection);
00747                 }
00748         }
00749 
00750         Clear();
00751 }
00752 
00753 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00754 
00755 void ReplicaManager3::OnConstructionExisting(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
00756 {
00757         Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
00758         if (connection==0)
00759         {
00760                 // Almost certainly a bug
00761                 RakAssert("Got OnConstruction but no connection yet" && 0);
00762                 return;
00763         }
00764 
00765         RakNet::BitStream bsIn(packetData,packetDataLength,false);
00766         bsIn.IgnoreBytes(packetDataOffset);
00767 
00768         if (networkIDManager==0)
00769                 networkIDManager=rakPeerInterface->GetNetworkIDManager();
00770         RakAssert(networkIDManager);
00771 
00772         NetworkID networkId;
00773         bsIn.Read(networkId);
00774         Replica3* existingReplica = networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
00775         if (existingReplica)
00776         {
00777                 existingReplica->DeserializeConstructionExisting(&bsIn, connection);
00778         }
00779 }
00780 
00781 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00782 
00783 void ReplicaManager3::OnConstruction(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
00784 {
00785         Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
00786         if (connection==0)
00787         {
00788                 // Almost certainly a bug
00789                 RakAssert("Got OnConstruction but no connection yet" && 0);
00790                 return;
00791         }
00792 
00793         RakNet::BitStream bsIn(packetData,packetDataLength,false);
00794         bsIn.IgnoreBytes(packetDataOffset);
00795         DataStructures::DefaultIndexType objectListSize, index, index2;
00796         BitSize_t bitOffset;
00797         Replica3 *replica;
00798         uint32_t allocationNumber=0;
00799         NetworkID networkId;
00800         RakNetGUID creatingSystemGuid;
00801 
00802         if (networkIDManager==0)
00803                 networkIDManager=rakPeerInterface->GetNetworkIDManager();
00804         RakAssert(networkIDManager);
00805 
00806         bsIn.Read(objectListSize);
00807         for (index=0; index < objectListSize; index++)
00808         {
00809                 bsIn.Read(bitOffset);
00810                 bsIn.Read(networkId);
00811                 Replica3* existingReplica = networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
00812                 if (existingReplica)
00813                 {
00814                         existingReplica->replicaManager=this;
00815 
00816                         // Network ID already in use
00817                         connection->OnDownloadExisting(existingReplica, this);
00818 
00819                         bsIn.SetReadOffset(bitOffset);
00820                         continue;
00821                 }
00822 
00823                 replica = connection->AllocReplica(&bsIn, this);
00824                 if (replica==0)
00825                 {
00826                         bsIn.SetReadOffset(bitOffset);
00827                         continue;
00828                 }
00829 
00830                 replica->SetNetworkIDManager(networkIDManager);
00831 
00832                 if (networkId==UNASSIGNED_NETWORK_ID)
00833                 {
00834                         if (networkIDManager->IsNetworkIDAuthority()==false)
00835                         {
00836                                 // Can't assign network ID
00837                                 replica->replicaManager=0;
00838                                 replica->DeallocReplica(connection);
00839                                 bsIn.SetReadOffset(bitOffset);
00840                                 continue;
00841                         }
00842 
00843                         bsIn.Read(allocationNumber);
00844                 }
00845                 else
00846                 {
00847 
00848                         replica->SetNetworkID(networkId);
00849                 }
00850 
00851                 replica->replicaManager=this;
00852                 bsIn.Read(creatingSystemGuid);
00853                 replica->creatingSystemGUID=creatingSystemGuid;
00854 
00855                 if (!replica->QueryRemoteConstruction(connection) ||
00856                         !replica->DeserializeConstruction(&bsIn, connection))
00857                 {
00858                         // Overtake this message to mean construction rejected
00859                         if (networkId==UNASSIGNED_NETWORK_ID)
00860                         {
00861                                 RakNet::BitStream bsOut;
00862                                 bsOut.Write((MessageID)ID_REPLICA_MANAGER_3_LOCAL_CONSTRUCTION_REJECTED);
00863                                 bsOut.Write(worldId);
00864                                 bsOut.Write(allocationNumber);
00865                                 replica->SerializeConstructionRequestRejected(&bsOut,connection);
00866                                 rakPeerInterface->Send(&bsOut, defaultSendParameters.priority, defaultSendParameters.reliability, defaultSendParameters.orderingChannel, connection->GetSystemAddress(), false, defaultSendParameters.sendReceipt);
00867                         }
00868 
00869                         replica->replicaManager=0;
00870                         replica->DeallocReplica(connection);
00871                         bsIn.SetReadOffset(bitOffset);
00872                         continue;
00873                 }
00874 
00875                 bsIn.SetReadOffset(bitOffset);
00876                 replica->PostDeserializeConstruction(connection);
00877 
00878                 if (networkId==UNASSIGNED_NETWORK_ID)
00879                 {
00880                         // Overtake this message to mean construction accepted
00881                         RakNet::BitStream bsOut;
00882                         bsOut.Write((MessageID)ID_REPLICA_MANAGER_3_LOCAL_CONSTRUCTION_ACCEPTED);
00883                         bsOut.Write(worldId);
00884                         bsOut.Write(allocationNumber);
00885                         bsOut.Write(replica->GetNetworkID());
00886                         replica->SerializeConstructionRequestAccepted(&bsOut,connection);
00887                         rakPeerInterface->Send(&bsOut, defaultSendParameters.priority, defaultSendParameters.reliability, defaultSendParameters.orderingChannel, connection->GetSystemAddress(), false, defaultSendParameters.sendReceipt);
00888                 }
00889                 bsIn.AlignReadToByteBoundary();
00890 
00891                 // Register the replica
00892                 ReferenceInternal(replica);
00893 
00894                 // Tell the connection(s) that this object exists since they just sent it to us
00895                 connection->OnDownloadFromThisSystem(replica, this);
00896 
00897                 for (index2=0; index2 < connectionList.GetSize(); index2++)
00898                 {
00899                         if (connectionList[index2]!=connection)
00900                                 connectionList[index2]->OnDownloadFromOtherSystem(replica, this);
00901                 }
00902         }
00903 
00904         // Destructions
00905         bool b = bsIn.Read(objectListSize);
00906         RakAssert(b);
00907         for (index=0; index < objectListSize; index++)
00908         {
00909                 bsIn.Read(networkId);
00910                 bsIn.Read(bitOffset);
00911                 replica = networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
00912                 if (replica==0)
00913                 {
00914                         // Unknown object
00915                         bsIn.SetReadOffset(bitOffset);
00916                         continue;
00917                 }
00918                 bsIn.Read(replica->deletingSystemGUID);
00919                 if (replica->DeserializeDestruction(&bsIn,connection))
00920                 {
00921                         // Make sure it wasn't deleted in DeserializeDestruction
00922                         if (networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId))
00923                         {
00924                                 replica->PreDestruction(connection);
00925 
00926                                 // Forward deletion by remote system
00927                                 BroadcastDestruction(replica,connection->GetSystemAddress());
00928                                 Dereference(replica);
00929                                 replica->replicaManager=0; // Prevent BroadcastDestruction from being called again
00930                                 replica->DeallocReplica(connection);
00931                         }
00932                 }
00933                 else
00934                 {
00935                         replica->PreDestruction(connection);
00936                         connection->OnDereference(replica, this);
00937                 }
00938 
00939                 bsIn.AlignReadToByteBoundary();
00940         }
00941 }
00942 
00943 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00944 
00945 void ReplicaManager3::OnSerialize(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, RakNetTime timestamp, unsigned char packetDataOffset)
00946 {
00947         Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
00948         if (connection==0)
00949                 return;
00950         if (networkIDManager==0)
00951                 networkIDManager=rakPeerInterface->GetNetworkIDManager();
00952         RakAssert(networkIDManager);
00953         RakNet::BitStream bsIn(packetData,packetDataLength,false);
00954         bsIn.IgnoreBytes(packetDataOffset);
00955 
00956         struct DeserializeParameters ds;
00957         ds.timeStamp=timestamp;
00958         ds.sourceConnection=connection;
00959 
00960         Replica3 *replica;
00961         NetworkID networkId;
00962         BitSize_t bitsUsed;
00963         bsIn.Read(networkId);
00964         replica = networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
00965         if (replica)
00966         {
00967                 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
00968                 {
00969                         bsIn.Read(ds.bitstreamWrittenTo[z]);
00970                         if (ds.bitstreamWrittenTo[z])
00971                         {
00972                                 bsIn.ReadCompressed(bitsUsed);
00973                                 bsIn.AlignReadToByteBoundary();
00974                                 bsIn.Read(ds.serializationBitstream[z], bitsUsed);
00975                         }
00976                 }
00977                 replica->Deserialize(&ds);
00978         }
00979 }
00980 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00981 
00982 void ReplicaManager3::OnDownloadStarted(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
00983 {
00984         Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
00985         if (connection==0)
00986                 return;
00987         RakNet::BitStream bsIn(packetData,packetDataLength,false);
00988         bsIn.IgnoreBytes(packetDataOffset);
00989         connection->DeserializeOnDownloadStarted(&bsIn);
00990 }
00991 
00992 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00993 
00994 void ReplicaManager3::OnDownloadComplete(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
00995 {
00996         Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
00997         if (connection==0)
00998                 return;
00999         RakNet::BitStream bsIn(packetData,packetDataLength,false);
01000         bsIn.IgnoreBytes(packetDataOffset);
01001         connection->DeserializeOnDownloadComplete(&bsIn);
01002 }
01003 
01004 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01005 
01006 void ReplicaManager3::OnLocalConstructionRejected(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
01007 {
01008         Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
01009         if (connection==0)
01010                 return;
01011         RakNet::BitStream bsIn(packetData,packetDataLength,false);
01012         bsIn.IgnoreBytes(packetDataOffset);
01013         uint32_t allocationNumber;
01014         bsIn.Read(allocationNumber);
01015         DataStructures::DefaultIndexType index;
01016         for (index=0; index < userReplicaList.GetSize(); index++)
01017         {
01018                 if (userReplicaList[index]->GetAllocationNumber()==allocationNumber)
01019                 {
01020                         userReplicaList[index]->DeserializeConstructionRequestRejected(&bsIn,connection);
01021                         break;
01022                 }
01023         }
01024 
01025 }
01026 
01027 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01028 
01029 void ReplicaManager3::OnLocalConstructionAccepted(unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset)
01030 {
01031         Connection_RM3 *connection = GetConnectionByGUID(senderGuid);
01032         if (connection==0)
01033                 return;
01034         RakNet::BitStream bsIn(packetData,packetDataLength,false);
01035         bsIn.IgnoreBytes(packetDataOffset);
01036         uint32_t allocationNumber;
01037         bsIn.Read(allocationNumber);
01038         NetworkID assignedNetworkId;
01039         bsIn.Read(assignedNetworkId);
01040         DataStructures::DefaultIndexType index;
01041         DataStructures::DefaultIndexType index2;
01042         Replica3 *replica;
01043         SerializeParameters sp;
01044         sp.whenLastSerialized=0;
01045         RakNet::BitStream emptyBs;
01046         for (index=0; index < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; index++)
01047                 sp.lastSentBitstream[index]=&emptyBs;
01048         RM3SerializationResult res;
01049         for (index=0; index < userReplicaList.GetSize(); index++)
01050         {
01051                 if (userReplicaList[index]->GetAllocationNumber()==allocationNumber)
01052                 {
01053                         replica=userReplicaList[index];
01054                         index2=connection->constructedReplicaList.GetIndexOf(replica);
01055                         if (index2!=(DataStructures::DefaultIndexType)-1)
01056                         {
01057                                 LastSerializationResult *lsr = connection->constructedReplicaList[index2];
01058 
01059                                 replica->SetNetworkID(assignedNetworkId);
01060                                 replica->DeserializeConstructionRequestAccepted(&bsIn,connection);
01061                                 sp.destinationConnection=connection;
01062 
01063                                 // Immediately serialize
01064                                 res = replica->Serialize(&sp);
01065                                 if (res!=RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION &&
01066                                         res!=RM3SR_DO_NOT_SERIALIZE &&
01067                                         res!=RM3SR_SERIALIZED_UNIQUELY)
01068                                 {
01069                                         sp.destinationConnection=connection;
01070                                         sp.messageTimestamp=0;
01071                                         for (int i=0; i < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; i++)
01072                                                 sp.pro[i]=defaultSendParameters;
01073                                         bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
01074                                         for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01075                                         {
01076                                                 allIndices[z]=true;
01077                                         }
01078                                         if (connection->SendSerialize(replica, allIndices, sp.outputBitstream, sp.messageTimestamp, sp.pro, rakPeerInterface, worldId)==SSICR_SENT_DATA)
01079                                                 lsr->replica->whenLastSerialized=RakNet::GetTime();
01080                                 }
01081 
01082                                 // Start serialization queries
01083                                 connection->queryToSerializeReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01084 
01085                                 return;
01086                         }
01087                 }
01088         }
01089 }
01090 
01091 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01092 
01093 Replica3* ReplicaManager3::GetReplicaByNetworkID(NetworkID networkId)
01094 {
01095         DataStructures::DefaultIndexType i;
01096         for (i=0; i < userReplicaList.GetSize(); i++)
01097         {
01098                 if (userReplicaList[i]->GetNetworkID()==networkId)
01099                         return userReplicaList[i];
01100         }
01101         return 0;
01102 }
01103 
01104 
01105 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01106 
01107 
01108 void ReplicaManager3::BroadcastDestructionList(DataStructures::Multilist<ML_STACK, Replica3*> &replicaList, SystemAddress exclusionAddress)
01109 {
01110         RakNet::BitStream bsOut;
01111         DataStructures::DefaultIndexType i,j;
01112 
01113         for (i=0; i < replicaList.GetSize(); i++)
01114         {
01115 
01116                 if (replicaList[i]->deletingSystemGUID==UNASSIGNED_RAKNET_GUID)
01117                         replicaList[i]->deletingSystemGUID=GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
01118         }
01119 
01120         for (j=0; j < connectionList.GetSize(); j++)
01121         {
01122                 if (connectionList[j]->GetSystemAddress()==exclusionAddress)
01123                         continue;
01124 
01125                 bsOut.Reset();
01126                 bsOut.Write((MessageID)ID_REPLICA_MANAGER_CONSTRUCTION);
01127                 bsOut.Write(worldId);
01128                 DataStructures::DefaultIndexType cnt=0;
01129                 bsOut.Write(cnt); // No construction
01130                 cnt=replicaList.GetSize();
01131                 bsOut.Write(cnt);
01132 
01133                 for (i=0; i < replicaList.GetSize(); i++)
01134                 {
01135                         if (connectionList[j]->HasReplicaConstructed(replicaList[i])==false)
01136                                 continue;
01137 
01138                         NetworkID networkId;
01139                         networkId=replicaList[i]->GetNetworkID();
01140                         bsOut.Write(networkId);
01141                         BitSize_t offsetStart, offsetEnd;
01142                         offsetStart=bsOut.GetWriteOffset();
01143                         bsOut.Write(offsetStart);
01144                         bsOut.Write(replicaList[i]->deletingSystemGUID);
01145                         replicaList[i]->SerializeDestruction(&bsOut, connectionList[j]);
01146                         bsOut.AlignWriteToByteBoundary();
01147                         offsetEnd=bsOut.GetWriteOffset();
01148                         bsOut.SetWriteOffset(offsetStart);
01149                         bsOut.Write(offsetEnd);
01150                         bsOut.SetWriteOffset(offsetEnd);
01151                 }
01152 
01153                 rakPeerInterface->Send(&bsOut,defaultSendParameters.priority,defaultSendParameters.reliability,defaultSendParameters.orderingChannel,connectionList[j]->GetSystemAddress(),false, defaultSendParameters.sendReceipt);
01154         }
01155 }
01156 
01157 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01158 
01159 
01160 void ReplicaManager3::BroadcastDestruction(Replica3 *replica, SystemAddress exclusionAddress)
01161 {
01162         DataStructures::Multilist<ML_STACK, Replica3*> replicaList;
01163         replicaList.Push(replica, __FILE__, __LINE__ );
01164         BroadcastDestructionList(replicaList,exclusionAddress);
01165 }
01166 
01167 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01168 
01169 
01170 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01171 
01172 Connection_RM3::Connection_RM3(SystemAddress _systemAddress, RakNetGUID _guid)
01173 : systemAddress(_systemAddress), guid(_guid)
01174 {
01175         isValidated=false;
01176         isFirstConstruction=true;
01177 }
01178 
01179 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01180 
01181 Connection_RM3::~Connection_RM3()
01182 {
01183         constructedReplicaList.ClearPointers(true,__FILE__,__LINE__);
01184         queryToConstructReplicaList.ClearPointers(true,__FILE__,__LINE__);
01185 }
01186 
01187 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01188 
01189 void Connection_RM3::GetConstructedReplicas(DataStructures::Multilist<ML_STACK, Replica3*> &objectsTheyDoHave)
01190 {
01191         objectsTheyDoHave.Clear(true,__FILE__,__LINE__);
01192         for (DataStructures::DefaultIndexType idx=0; idx < constructedReplicaList.GetSize(); idx++)
01193         {
01194                 objectsTheyDoHave.Push(constructedReplicaList[idx]->replica, __FILE__, __LINE__ );
01195         }
01196         objectsTheyDoHave.TagSorted();
01197 }
01198 
01199 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01200 
01201 bool Connection_RM3::HasReplicaConstructed(RakNet::Replica3 *replica)
01202 {
01203         return constructedReplicaList.GetIndexOf(replica)!=(DataStructures::DefaultIndexType)-1;
01204 }
01205 
01206 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01207 void Connection_RM3::SendSerializeHeader(RakNet::Replica3 *replica, RakNetTime timestamp, RakNet::BitStream *bs, unsigned char worldId)
01208 {
01209         bs->Reset();
01210 
01211         if (timestamp!=0)
01212         {
01213                 bs->Write((MessageID)ID_TIMESTAMP);
01214                 bs->Write(timestamp);
01215         }
01216         bs->Write((MessageID)ID_REPLICA_MANAGER_SERIALIZE);
01217         bs->Write(worldId);
01218         bs->Write(replica->GetNetworkID());
01219 }
01220 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01221 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)
01222 {
01223         bool channelHasData;
01224         BitSize_t sum=0;
01225         for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01226         {
01227                 if (indicesToSend[z])
01228                         sum+=serializationData[z].GetNumberOfBitsUsed();
01229         }
01230         if (sum==0)
01231                 return SSICR_DID_NOT_SEND_DATA;
01232 
01233         RakAssert(replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID);
01234 
01235         RakNet::BitStream out;
01236         BitSize_t bitsUsed;
01237 
01238         int channelIndex;
01239         PRO lastPro=sendParameters[0];
01240 
01241         for (channelIndex=0; channelIndex < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; channelIndex++)
01242         {
01243                 if (channelIndex==0)
01244                 {
01245                         SendSerializeHeader(replica, timestamp, &out, worldId);
01246                 }
01247                 else if (lastPro!=sendParameters[channelIndex])
01248                 {
01249                         // Write out remainder
01250                         for (int channelIndex2=channelIndex; channelIndex2 < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; channelIndex2++)
01251                                 out.Write(false);
01252 
01253                         // Send remainder
01254                         replica->OnSerializeTransmission(&out, systemAddress);
01255                         rakPeer->Send(&out,lastPro.priority,lastPro.reliability,lastPro.orderingChannel,systemAddress,false,lastPro.sendReceipt);
01256 
01257                         // Restart stream
01258                         SendSerializeHeader(replica, timestamp, &out, worldId);
01259 
01260                         for (int channelIndex2=0; channelIndex2 < channelIndex; channelIndex2++)
01261                                 out.Write(false);
01262                         lastPro=sendParameters[channelIndex];
01263                 }
01264 
01265                 bitsUsed=serializationData[channelIndex].GetNumberOfBitsUsed();
01266                 channelHasData = indicesToSend[channelIndex]==true && bitsUsed>0;
01267                 out.Write(channelHasData);
01268                 if (channelHasData)
01269                 {
01270                         out.WriteCompressed(bitsUsed);
01271                         out.AlignWriteToByteBoundary();
01272                         out.Write(serializationData[channelIndex]);
01273                         // Crap, forgot this line, was a huge bug in that I'd only send to the first 3 systems
01274                         serializationData[channelIndex].ResetReadPointer();
01275                 }
01276         }
01277         replica->OnSerializeTransmission(&out, systemAddress);
01278         rakPeer->Send(&out,lastPro.priority,lastPro.reliability,lastPro.orderingChannel,systemAddress,false,lastPro.sendReceipt);
01279         return SSICR_SENT_DATA;
01280 }
01281 
01282 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01283 
01284 SendSerializeIfChangedResult Connection_RM3::SendSerializeIfChanged(DataStructures::DefaultIndexType queryToSerializeIndex, SerializeParameters *sp, RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager)
01285 {
01286         RakNet::Replica3 *replica = queryToSerializeReplicaList[queryToSerializeIndex]->replica;
01287 
01288         if (replica->GetNetworkID()==UNASSIGNED_NETWORK_ID)
01289                 return SSICR_DID_NOT_SEND_DATA;
01290 
01291         RM3QuerySerializationResult rm3qsr = replica->QuerySerialization(this);
01292         if (rm3qsr==RM3QSR_NEVER_CALL_SERIALIZE)
01293         {
01294                 // Never again for this connection and replica pair
01295                 OnNeverSerialize(queryToSerializeIndex, replicaManager);
01296                 return SSICR_NEVER_SERIALIZE;
01297         }
01298 
01299         if (rm3qsr==RM3QSR_DO_NOT_CALL_SERIALIZE)
01300                 return SSICR_DID_NOT_SEND_DATA;
01301 
01302         if (replica->forceSendUntilNextUpdate)
01303         {
01304                 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01305                 {
01306                         if (replica->lastSentSerialization.indicesToSend[z])
01307                                 sp->bitsWrittenSoFar+=replica->lastSentSerialization.bitStream[z].GetNumberOfBitsUsed();
01308                 }
01309                 return SendSerialize(replica, replica->lastSentSerialization.indicesToSend, replica->lastSentSerialization.bitStream, sp->messageTimestamp, sp->pro, rakPeer, worldId);
01310         }
01311 
01312         for (int i=0; i < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; i++)
01313         {
01314                 sp->outputBitstream[i].Reset();
01315                 if (queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS)
01316                         sp->lastSentBitstream[i]=&queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[i];
01317                 else
01318                         sp->lastSentBitstream[i]=&replica->lastSentSerialization.bitStream[i];
01319         }
01320 
01321         RM3SerializationResult serializationResult = replica->Serialize(sp);
01322 
01323         if (serializationResult==RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION)
01324         {
01325                 // Never again for this connection and replica pair
01326                 OnNeverSerialize(queryToSerializeIndex, replicaManager);
01327                 return SSICR_NEVER_SERIALIZE;
01328         }
01329 
01330         if (serializationResult==RM3SR_DO_NOT_SERIALIZE)
01331         {
01332                 // Don't serialize this tick only
01333                 return SSICR_DID_NOT_SEND_DATA;
01334         }
01335 
01336         // This is necessary in case the user in the Serialize() function for some reason read the bitstream they also wrote
01337         // WIthout this code, the Write calls to another bitstream would not write the entire bitstream
01338         BitSize_t sum=0;
01339         for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01340         {
01341                 sp->outputBitstream[z].ResetReadPointer();
01342                 sum+=sp->outputBitstream[z].GetNumberOfBitsUsed();
01343         }
01344         
01345         if (sum==0)
01346         {
01347                 // Don't serialize this tick only
01348                 return SSICR_DID_NOT_SEND_DATA;
01349         }
01350 
01351         if (serializationResult==RM3SR_SERIALIZED_ALWAYS)
01352         {
01353                 bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
01354                 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01355                 {
01356                         sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
01357                         allIndices[z]=true;
01358 
01359                         queryToSerializeReplicaList[queryToSerializeIndex]->AllocBS();
01360                         queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].Reset();
01361                         queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].Write(&sp->outputBitstream[z]);
01362                         sp->outputBitstream[z].ResetReadPointer();
01363                 }
01364                 return SendSerialize(replica, allIndices, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId);
01365         }
01366 
01367         if (serializationResult==RM3SR_SERIALIZED_ALWAYS_IDENTICALLY)
01368         {
01369                 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01370                 {
01371                         replica->lastSentSerialization.indicesToSend[z]=sp->outputBitstream[z].GetNumberOfBitsUsed()>0;
01372                         sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
01373                         replica->lastSentSerialization.bitStream[z].Reset();
01374                         replica->lastSentSerialization.bitStream[z].Write(&sp->outputBitstream[z]);
01375                         sp->outputBitstream[z].ResetReadPointer();
01376                         replica->forceSendUntilNextUpdate=true;
01377                 }
01378                 return SendSerialize(replica, replica->lastSentSerialization.indicesToSend, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId);
01379         }
01380 
01381         bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
01382         if (serializationResult==RM3SR_BROADCAST_IDENTICALLY || serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION)
01383         {
01384                 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01385                 {
01386                         if (sp->outputBitstream[z].GetNumberOfBitsUsed() > 0 &&
01387                                 (serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION ||
01388                                 ((sp->outputBitstream[z].GetNumberOfBitsUsed()!=replica->lastSentSerialization.bitStream[z].GetNumberOfBitsUsed() ||
01389                                 memcmp(sp->outputBitstream[z].GetData(), replica->lastSentSerialization.bitStream[z].GetData(), sp->outputBitstream[z].GetNumberOfBytesUsed())!=0))))
01390                         {
01391                                 indicesToSend[z]=true;
01392                                 replica->lastSentSerialization.indicesToSend[z]=true;
01393                                 sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
01394                                 replica->lastSentSerialization.bitStream[z].Reset();
01395                                 replica->lastSentSerialization.bitStream[z].Write(&sp->outputBitstream[z]);
01396                                 sp->outputBitstream[z].ResetReadPointer();
01397                                 replica->forceSendUntilNextUpdate=true;
01398                         }
01399                         else
01400                         {
01401                                 indicesToSend[z]=false;
01402                                 replica->lastSentSerialization.indicesToSend[z]=false;
01403                         }
01404                 }
01405         }
01406         else
01407         {
01408                 queryToSerializeReplicaList[queryToSerializeIndex]->AllocBS();
01409 
01410                 // RM3SR_SERIALIZED_UNIQUELY
01411                 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01412                 {
01413                         if (sp->outputBitstream[z].GetNumberOfBitsUsed() > 0 &&
01414                                 (sp->outputBitstream[z].GetNumberOfBitsUsed()!=queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].GetNumberOfBitsUsed() ||
01415                                 memcmp(sp->outputBitstream[z].GetData(), queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].GetData(), sp->outputBitstream[z].GetNumberOfBytesUsed())!=0)
01416                                 )
01417                         {
01418                                 indicesToSend[z]=true;
01419                                 sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
01420                                 queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].Reset();
01421                                 queryToSerializeReplicaList[queryToSerializeIndex]->lastSerializationResultBS->bitStream[z].Write(&sp->outputBitstream[z]);
01422                                 sp->outputBitstream[z].ResetReadPointer();
01423                         }
01424                         else
01425                         {
01426                                 indicesToSend[z]=false;
01427                         }
01428                 }
01429         }
01430 
01431 
01432         if (serializationResult==RM3SR_BROADCAST_IDENTICALLY || serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION)
01433                 replica->forceSendUntilNextUpdate=true;
01434 
01435         // Send out the data
01436         return SendSerialize(replica, indicesToSend, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId);
01437 }
01438 
01439 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01440 void Connection_RM3::OnLocalReference(Replica3* replica3, ReplicaManager3 *replicaManager)
01441 {
01442         ConstructionMode constructionMode = QueryConstructionMode();
01443         RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
01444         (void) replicaManager;
01445 
01446         LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(__FILE__,__LINE__);
01447         lsr->replica=replica3;
01448         queryToConstructReplicaList.Push(lsr,replica3,__FILE__,__LINE__);
01449 }
01450 
01451 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01452 
01453 void Connection_RM3::OnDereference(Replica3* replica3, ReplicaManager3 *replicaManager)
01454 {
01455         ValidateLists(replicaManager);
01456 
01457         LastSerializationResult* lsr=0;
01458         DataStructures::DefaultIndexType idx;
01459         idx=constructedReplicaList.GetIndexOf(replica3);
01460         if (idx!=(DataStructures::DefaultIndexType)-1)
01461         {
01462                 lsr=constructedReplicaList[idx];
01463                 constructedReplicaList.RemoveAtIndex(idx,__FILE__,__LINE__);
01464         }
01465         
01466         for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
01467         {
01468                 if (queryToConstructReplicaList[idx]->replica==replica3)
01469                 {
01470                         lsr=queryToConstructReplicaList[idx];
01471                         queryToConstructReplicaList.RemoveAtIndex(idx,__FILE__,__LINE__);
01472                         break;
01473                 }
01474         }
01475 
01476         for (idx=0; idx < queryToSerializeReplicaList.GetSize(); idx++)
01477         {
01478                 if (queryToSerializeReplicaList[idx]->replica==replica3)
01479                 {
01480                         lsr=queryToSerializeReplicaList[idx];
01481                         queryToSerializeReplicaList.RemoveAtIndex(idx,__FILE__,__LINE__);
01482                         break;
01483                 }
01484         }
01485 
01486         for (idx=0; idx < queryToDestructReplicaList.GetSize(); idx++)
01487         {
01488                 if (queryToDestructReplicaList[idx]->replica==replica3)
01489                 {
01490                         lsr=queryToDestructReplicaList[idx];
01491                         queryToDestructReplicaList.RemoveAtIndex(idx,__FILE__,__LINE__);
01492                         break;
01493                 }
01494         }
01495 
01496         ValidateLists(replicaManager);
01497 
01498         if (lsr)
01499                 RakNet::OP_DELETE(lsr,__FILE__,__LINE__);
01500 
01501         ValidateLists(replicaManager);
01502 }
01503 
01504 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01505 
01506 void Connection_RM3::OnDownloadFromThisSystem(Replica3* replica3, ReplicaManager3 *replicaManager)
01507 {
01508         ValidateLists(replicaManager);
01509         LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(__FILE__,__LINE__);
01510         lsr->replica=replica3;
01511 
01512         ConstructionMode constructionMode = QueryConstructionMode();
01513         if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
01514         {
01515                 queryToConstructReplicaList.RemoveAtKey(replica3,false,__FILE__,__LINE__);
01516                 queryToDestructReplicaList.Push(lsr,replica3,__FILE__,__LINE__);
01517         }
01518 
01519         constructedReplicaList.Push(lsr,replica3,__FILE__,__LINE__);
01520         //assert(queryToSerializeReplicaList.GetIndexOf(replica3)==(DataStructures::DefaultIndexType)-1);
01521         queryToSerializeReplicaList.Push(lsr,replica3,__FILE__,__LINE__);
01522 
01523         ValidateLists(replicaManager);
01524 }
01525 
01526 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01527 
01528 void Connection_RM3::OnDownloadFromOtherSystem(Replica3* replica3, ReplicaManager3 *replicaManager)
01529 {
01530         ConstructionMode constructionMode = QueryConstructionMode();
01531         if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
01532         {
01533                 if (queryToConstructReplicaList.GetIndexOf(replica3)==(DataStructures::DefaultIndexType)-1)
01534                         OnLocalReference(replica3, replicaManager);
01535         }
01536 }
01537 
01538 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01539 
01540 void Connection_RM3::OnNeverConstruct(DataStructures::DefaultIndexType queryToConstructIdx, ReplicaManager3 *replicaManager)
01541 {
01542         ConstructionMode constructionMode = QueryConstructionMode();
01543         RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
01544 
01545         ValidateLists(replicaManager);
01546         LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
01547         queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx,__FILE__,__LINE__);
01548         RakNet::OP_DELETE(lsr,__FILE__,__LINE__);
01549         ValidateLists(replicaManager);
01550 }
01551 
01552 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01553 
01554 void Connection_RM3::OnConstructToThisConnection(DataStructures::DefaultIndexType queryToConstructIdx, ReplicaManager3 *replicaManager)
01555 {
01556         ConstructionMode constructionMode = QueryConstructionMode();
01557         RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
01558 
01559         ValidateLists(replicaManager);
01560         LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
01561         queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx,__FILE__,__LINE__);
01562         //assert(constructedReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
01563         constructedReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01564         //assert(queryToDestructReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
01565         queryToDestructReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01566         //assert(queryToSerializeReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
01567         if (lsr->replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID)
01568                 queryToSerializeReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01569         ValidateLists(replicaManager);
01570 }
01571 
01572 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01573 
01574 void Connection_RM3::OnConstructToThisConnection(Replica3 *replica, ReplicaManager3 *replicaManager)
01575 {
01576         RakAssert(QueryConstructionMode()==QUERY_CONNECTION_FOR_REPLICA_LIST);
01577         (void) replicaManager;
01578 
01579         LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(__FILE__,__LINE__);
01580         lsr->replica=replica;
01581         constructedReplicaList.Push(lsr,replica,__FILE__,__LINE__);
01582         if (lsr->replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID)
01583                 queryToSerializeReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01584 }
01585 
01586 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01587 
01588 void Connection_RM3::OnNeverSerialize(DataStructures::DefaultIndexType queryToSerializeIndex, ReplicaManager3 *replicaManager)
01589 {
01590         ValidateLists(replicaManager);
01591         queryToSerializeReplicaList.RemoveAtIndex(queryToSerializeIndex,__FILE__,__LINE__);
01592         ValidateLists(replicaManager);
01593 }
01594 
01595 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01596 
01597 void Connection_RM3::OnReplicaAlreadyExists(DataStructures::DefaultIndexType queryToConstructIdx, ReplicaManager3 *replicaManager)
01598 {
01599         ConstructionMode constructionMode = QueryConstructionMode();
01600         RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
01601 
01602         ValidateLists(replicaManager);
01603         LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
01604         queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx,__FILE__,__LINE__);
01605         //assert(constructedReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
01606         constructedReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01607         //assert(queryToDestructReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
01608         queryToDestructReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01609         //assert(queryToSerializeReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
01610         queryToSerializeReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01611         ValidateLists(replicaManager);
01612 }
01613 
01614 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01615 
01616 void Connection_RM3::OnDownloadExisting(Replica3* replica3, ReplicaManager3 *replicaManager)
01617 {
01618         ValidateLists(replicaManager);
01619 
01620         ConstructionMode constructionMode = QueryConstructionMode();
01621         if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
01622         {
01623                 DataStructures::DefaultIndexType idx;
01624                 for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
01625                 {
01626                         if (queryToConstructReplicaList[idx]->replica==replica3)
01627                         {
01628                                 OnConstructToThisConnection(idx, replicaManager);
01629                                 return;
01630                         }
01631                 }
01632         }       
01633         else
01634         {
01635                 OnConstructToThisConnection(replica3, replicaManager);
01636         }
01637 }
01638 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01639 
01640 void Connection_RM3::OnSendDestructionFromQuery(DataStructures::DefaultIndexType queryToDestructIdx, ReplicaManager3 *replicaManager)
01641 {
01642         ConstructionMode constructionMode = QueryConstructionMode();
01643         RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
01644 
01645         ValidateLists(replicaManager);
01646         LastSerializationResult* lsr = queryToDestructReplicaList[queryToDestructIdx];
01647         queryToDestructReplicaList.RemoveAtIndex(queryToDestructIdx,__FILE__,__LINE__);
01648         queryToSerializeReplicaList.RemoveAtKey(lsr->replica,false,__FILE__,__LINE__);
01649         constructedReplicaList.RemoveAtKey(lsr->replica,true,__FILE__,__LINE__);
01650         //assert(queryToConstructReplicaList.GetIndexOf(lsr->replica)==(DataStructures::DefaultIndexType)-1);
01651         queryToConstructReplicaList.Push(lsr,lsr->replica,__FILE__,__LINE__);
01652         ValidateLists(replicaManager);
01653 }
01654 
01655 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01656 
01657 void Connection_RM3::OnDoNotQueryDestruction(DataStructures::DefaultIndexType queryToDestructIdx, ReplicaManager3 *replicaManager)
01658 {
01659         ValidateLists(replicaManager);
01660         queryToDestructReplicaList.RemoveAtIndex(queryToDestructIdx,__FILE__,__LINE__);
01661         ValidateLists(replicaManager);
01662 }
01663 
01664 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01665 
01666 void Connection_RM3::ValidateLists(ReplicaManager3 *replicaManager) const
01667 {
01668         (void) replicaManager;
01669         /*
01670 #ifdef _DEBUG
01671         // Each object should exist only once in either constructedReplicaList or queryToConstructReplicaList
01672         // replicaPointer from LastSerializationResult should be same among all lists
01673         DataStructures::DefaultIndexType idx, idx2;
01674         for (idx=0; idx < constructedReplicaList.GetSize(); idx++)
01675         {
01676                 idx2=queryToConstructReplicaList.GetIndexOf(constructedReplicaList[idx]->replica);
01677                 if (idx2!=(DataStructures::DefaultIndexType)-1)
01678                 {
01679                         int a=5;
01680                         assert(a==0);
01681                         int *b=0;
01682                         *b=5;
01683                 }
01684         }
01685 
01686         for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
01687         {
01688                 idx2=constructedReplicaList.GetIndexOf(queryToConstructReplicaList[idx]->replica);
01689                 if (idx2!=(DataStructures::DefaultIndexType)-1)
01690                 {
01691                         int a=5;
01692                         assert(a==0);
01693                         int *b=0;
01694                         *b=5;
01695                 }
01696         }
01697 
01698         LastSerializationResult *lsr, *lsr2;
01699         for (idx=0; idx < constructedReplicaList.GetSize(); idx++)
01700         {
01701                 lsr=constructedReplicaList[idx];
01702 
01703                 idx2=queryToSerializeReplicaList.GetIndexOf(lsr->replica);
01704                 if (idx2!=(DataStructures::DefaultIndexType)-1)
01705                 {
01706                         lsr2=queryToSerializeReplicaList[idx2];
01707                         if (lsr2!=lsr)
01708                         {
01709                                 int a=5;
01710                                 assert(a==0);
01711                                 int *b=0;
01712                                 *b=5;
01713                         }
01714                 }
01715 
01716                 idx2=queryToDestructReplicaList.GetIndexOf(lsr->replica);
01717                 if (idx2!=(DataStructures::DefaultIndexType)-1)
01718                 {
01719                         lsr2=queryToDestructReplicaList[idx2];
01720                         if (lsr2!=lsr)
01721                         {
01722                                 int a=5;
01723                                 assert(a==0);
01724                                 int *b=0;
01725                                 *b=5;
01726                         }
01727                 }
01728         }
01729         for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
01730         {
01731                 lsr=queryToConstructReplicaList[idx];
01732 
01733                 idx2=queryToSerializeReplicaList.GetIndexOf(lsr->replica);
01734                 if (idx2!=(DataStructures::DefaultIndexType)-1)
01735                 {
01736                         lsr2=queryToSerializeReplicaList[idx2];
01737                         if (lsr2!=lsr)
01738                         {
01739                                 int a=5;
01740                                 assert(a==0);
01741                                 int *b=0;
01742                                 *b=5;
01743                         }
01744                 }
01745 
01746                 idx2=queryToDestructReplicaList.GetIndexOf(lsr->replica);
01747                 if (idx2!=(DataStructures::DefaultIndexType)-1)
01748                 {
01749                         lsr2=queryToDestructReplicaList[idx2];
01750                         if (lsr2!=lsr)
01751                         {
01752                                 int a=5;
01753                                 assert(a==0);
01754                                 int *b=0;
01755                                 *b=5;
01756                         }
01757                 }
01758         }
01759 
01760         // Verify pointer integrity
01761         for (idx=0; idx < constructedReplicaList.GetSize(); idx++)
01762         {
01763                 if (constructedReplicaList[idx]->replica->replicaManager!=replicaManager)
01764                 {
01765                         int a=5;
01766                         assert(a==0);
01767                         int *b=0;
01768                         *b=5;
01769                 }
01770         }
01771 
01772         // Verify pointer integrity
01773         for (idx=0; idx < queryToConstructReplicaList.GetSize(); idx++)
01774         {
01775                 if (queryToConstructReplicaList[idx]->replica->replicaManager!=replicaManager)
01776                 {
01777                         int a=5;
01778                         assert(a==0);
01779                         int *b=0;
01780                         *b=5;
01781                 }
01782         }
01783 #endif
01784         */
01785 }
01786 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01787 
01788 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)
01789 {
01790         if (newObjects.GetSize()==0 && deletedObjects.GetSize()==0)
01791                 return;
01792 
01793         // All construction and destruction takes place in the same network message
01794         // Otherwise, if objects rely on each other being created the same tick to be valid, this won't always be true
01795         //      DataStructures::Multilist<ML_STACK, LastSerializationResult* > serializedObjects;
01796         BitSize_t offsetStart, offsetEnd;
01797         DataStructures::DefaultIndexType newListIndex, oldListIndex;
01798         RakNet::BitStream bsOut;
01799         NetworkID networkId;
01800         if (isFirstConstruction)
01801         {
01802                 bsOut.Write((MessageID)ID_REPLICA_MANAGER_DOWNLOAD_STARTED);
01803                 bsOut.Write(worldId);
01804                 SerializeOnDownloadStarted(&bsOut);
01805                 rakPeer->Send(&bsOut,sendParameters.priority,sendParameters.reliability,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
01806         }
01807         
01808 
01809         //      LastSerializationResult* lsr;
01810         bsOut.Reset();
01811         bsOut.Write((MessageID)ID_REPLICA_MANAGER_CONSTRUCTION);
01812         bsOut.Write(worldId);
01813         bsOut.Write(newObjects.GetSize());
01814         // Construction
01815         for (newListIndex=0; newListIndex < newObjects.GetSize(); newListIndex++)
01816         {
01817                 offsetStart=bsOut.GetWriteOffset();
01818                 bsOut.Write(offsetStart);
01819                 networkId=newObjects[newListIndex]->GetNetworkID();
01820                 bsOut.Write(networkId);
01821                 newObjects[newListIndex]->WriteAllocationID(&bsOut);
01822                 if (networkId==UNASSIGNED_NETWORK_ID)
01823                         bsOut.Write(newObjects[newListIndex]->GetAllocationNumber());
01824                 bsOut.Write(newObjects[newListIndex]->creatingSystemGUID);
01825                 newObjects[newListIndex]->SerializeConstruction(&bsOut, this);
01826                 bsOut.AlignWriteToByteBoundary();
01827                 offsetEnd=bsOut.GetWriteOffset();
01828                 bsOut.SetWriteOffset(offsetStart);
01829                 bsOut.Write(offsetEnd);
01830                 bsOut.SetWriteOffset(offsetEnd);
01831                 //              lsr = Reference(newObjects[newListIndex],false);
01832                 //              serializedObjects.Push(newObjects[newListIndex]);
01833         }
01834 
01835         // Destruction
01836         DataStructures::DefaultIndexType listSize=deletedObjects.GetSize();
01837         bsOut.Write(listSize);
01838         for (oldListIndex=0; oldListIndex < deletedObjects.GetSize(); oldListIndex++)
01839         {
01840                 networkId=deletedObjects[oldListIndex]->GetNetworkID();
01841                 bsOut.Write(networkId);
01842                 offsetStart=bsOut.GetWriteOffset();
01843                 bsOut.Write(offsetStart);
01844                 deletedObjects[oldListIndex]->deletingSystemGUID=rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
01845                 bsOut.Write(deletedObjects[oldListIndex]->deletingSystemGUID);
01846                 deletedObjects[oldListIndex]->SerializeDestruction(&bsOut, this);
01847                 bsOut.AlignWriteToByteBoundary();
01848                 offsetEnd=bsOut.GetWriteOffset();
01849                 bsOut.SetWriteOffset(offsetStart);
01850                 bsOut.Write(offsetEnd);
01851                 bsOut.SetWriteOffset(offsetEnd);
01852         }
01853         rakPeer->Send(&bsOut,sendParameters.priority,sendParameters.reliability,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
01854 
01855         // Initial Download serialize to a new system
01856         // Immediately send serialize after construction if the replica object already has saved data
01857         // If the object was serialized identically, and does not change later on, then the new connection never gets the data
01858         SerializeParameters sp;
01859         sp.whenLastSerialized=0;
01860         RakNet::BitStream emptyBs;
01861         for (int index=0; index < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; index++)
01862         {
01863                 sp.lastSentBitstream[index]=&emptyBs;
01864                 sp.pro[index]=sendParameters;
01865         }
01866 
01867         sp.bitsWrittenSoFar=0;
01868         RakNetTime t = RakNet::GetTime();
01869         for (newListIndex=0; newListIndex < newObjects.GetSize(); newListIndex++)
01870         {
01871                 sp.destinationConnection=this;
01872                 sp.messageTimestamp=0;
01873                 RakNet::Replica3 *replica = newObjects[newListIndex];
01874                 // 8/22/09 Forgot ResetWritePointer
01875                 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01876                 {
01877                         sp.outputBitstream[z].ResetWritePointer();
01878                 }
01879 
01880                 RM3SerializationResult res = replica->Serialize(&sp);
01881                 if (replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID)
01882                 {
01883                         if (res!=RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION &&
01884                                 res!=RM3SR_DO_NOT_SERIALIZE &&
01885                                 res!=RM3SR_SERIALIZED_UNIQUELY)
01886                         {
01887                                 bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
01888                                 for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
01889                                 {
01890                                         sp.bitsWrittenSoFar+=sp.outputBitstream[z].GetNumberOfBitsUsed();
01891                                         allIndices[z]=true;
01892                                 }
01893                                 SendSerialize(replica, allIndices, sp.outputBitstream, sp.messageTimestamp, sp.pro, rakPeer, worldId);
01894                                 newObjects[newListIndex]->whenLastSerialized=t;
01895 
01896                         }
01897                 }
01898                 // else wait for construction request accepted before serializing
01899         }
01900 
01901         if (isFirstConstruction)
01902         {
01903                 bsOut.Reset();
01904                 bsOut.Write((MessageID)ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE);
01905                 bsOut.Write(worldId);
01906                 SerializeOnDownloadComplete(&bsOut);
01907                 rakPeer->Send(&bsOut,sendParameters.priority,sendParameters.reliability,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
01908         }
01909         
01910         isFirstConstruction=false;
01911 
01912 }
01913 
01914 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01915 
01916 void Connection_RM3::SendValidation(RakPeerInterface *rakPeer, unsigned char worldId)
01917 {
01918         // Hijack to mean sendValidation
01919         RakNet::BitStream bsOut;
01920         bsOut.Write((MessageID)ID_REPLICA_MANAGER_SCOPE_CHANGE);
01921         bsOut.Write(worldId);
01922         rakPeer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,systemAddress,false);
01923 }
01924 
01925 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01926 
01927 Replica3::Replica3()
01928 {
01929         creatingSystemGUID=UNASSIGNED_RAKNET_GUID;
01930         deletingSystemGUID=UNASSIGNED_RAKNET_GUID;
01931         replicaManager=0;
01932         forceSendUntilNextUpdate=false;
01933         whenLastSerialized=0;
01934 }
01935 
01936 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01937 
01938 Replica3::~Replica3()
01939 {
01940         if (replicaManager)
01941         {
01942                 replicaManager->Dereference(this);
01943         }
01944 }
01945 
01946 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01947 
01948 void Replica3::BroadcastDestruction(void)
01949 {
01950         if (replicaManager)
01951                 replicaManager->BroadcastDestruction(this,UNASSIGNED_SYSTEM_ADDRESS);
01952 }
01953 
01954 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01955 
01956 RakNetGUID Replica3::GetCreatingSystemGUID(void) const
01957 {
01958         return creatingSystemGUID;
01959 }
01960 
01961 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01962 
01963 RM3ConstructionState Replica3::QueryConstruction_ClientConstruction(RakNet::Connection_RM3 *destinationConnection)
01964 {
01965         (void) destinationConnection;
01966         if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
01967                 return RM3CS_SEND_CONSTRUCTION;
01968         // Send back to the owner client too, because they couldn't assign the network ID
01969         if (networkIDManager->IsNetworkIDAuthority())
01970                 return RM3CS_SEND_CONSTRUCTION;
01971         return RM3CS_NEVER_CONSTRUCT;
01972 }
01973 
01974 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01975 
01976 bool Replica3::QueryRemoteConstruction_ClientConstruction(RakNet::Connection_RM3 *sourceConnection)
01977 {
01978         (void) sourceConnection;
01979 
01980         // OK to create
01981         return true;
01982 }
01983 
01984 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01985 
01986 RM3ConstructionState Replica3::QueryConstruction_ServerConstruction(RakNet::Connection_RM3 *destinationConnection)
01987 {
01988         (void) destinationConnection;
01989 
01990         if (networkIDManager->IsNetworkIDAuthority())
01991                 return RM3CS_SEND_CONSTRUCTION;
01992         return RM3CS_NEVER_CONSTRUCT;
01993 }
01994 
01995 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01996 
01997 bool Replica3::QueryRemoteConstruction_ServerConstruction(RakNet::Connection_RM3 *sourceConnection)
01998 {
01999         (void) sourceConnection;
02000         if (networkIDManager->IsNetworkIDAuthority())
02001                 return false;
02002         return true;
02003 }
02004 
02005 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02006 
02007 RM3ConstructionState Replica3::QueryConstruction_PeerToPeer(RakNet::Connection_RM3 *destinationConnection)
02008 {
02009         (void) destinationConnection;
02010 
02011         // We send to all, others do nothing
02012         if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
02013                 return RM3CS_SEND_CONSTRUCTION;
02014         return RM3CS_NEVER_CONSTRUCT;
02015 }
02016 
02017 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02018 
02019 bool Replica3::QueryRemoteConstruction_PeerToPeer(RakNet::Connection_RM3 *sourceConnection)
02020 {
02021         (void) sourceConnection;
02022 
02023         return true;
02024 }
02025 
02026 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02027 
02028 RM3QuerySerializationResult Replica3::QuerySerialization_ClientSerializable(RakNet::Connection_RM3 *destinationConnection)
02029 {
02030         // Owner client sends to all
02031         if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
02032                 return RM3QSR_CALL_SERIALIZE;
02033         // Server sends to all but owner client
02034         if (networkIDManager->IsNetworkIDAuthority() && destinationConnection->GetRakNetGUID()!=creatingSystemGUID)
02035                 return RM3QSR_CALL_SERIALIZE;
02036         // Remote clients do not send
02037         return RM3QSR_NEVER_CALL_SERIALIZE;
02038 }
02039 
02040 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02041 
02042 RM3QuerySerializationResult Replica3::QuerySerialization_ServerSerializable(RakNet::Connection_RM3 *destinationConnection)
02043 {
02044         (void) destinationConnection;
02045         // Server sends to all
02046         if (networkIDManager->IsNetworkIDAuthority())
02047                 return RM3QSR_CALL_SERIALIZE;
02048 
02049         // Clients do not send
02050         return RM3QSR_NEVER_CALL_SERIALIZE;
02051 }
02052 
02053 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02054 
02055 RM3QuerySerializationResult Replica3::QuerySerialization_PeerToPeer(RakNet::Connection_RM3 *destinationConnection)
02056 {
02057         (void) destinationConnection;
02058 
02059         // Owner peer sends to all
02060         if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
02061                 return RM3QSR_CALL_SERIALIZE;
02062 
02063         // Remote peers do not send
02064         return RM3QSR_NEVER_CALL_SERIALIZE;
02065 }
02066 
02067 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02068 
02069 RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_Client(RakNet::Connection_RM3 *droppedConnection) const
02070 {
02071         (void) droppedConnection;
02072         return RM3AOPC_DELETE_REPLICA;
02073 }
02074 
02075 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02076 
02077 RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_Server(RakNet::Connection_RM3 *droppedConnection) const
02078 {
02079         (void) droppedConnection;
02080         return RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION;
02081 }
02082 
02083 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02084 
02085 RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_PeerToPeer(RakNet::Connection_RM3 *droppedConnection) const
02086 {
02087         (void) droppedConnection;
02088         return RM3AOPC_DELETE_REPLICA;
02089 }
02090 
02091 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
02092 
02093 #endif // _RAKNET_SUPPORT_*

Generated on Thu Sep 30 2010 01:27:27 for RakNet by  doxygen 1.7.1