| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593 |
- /*
- * Copyright (c) 2014, Oculus VR, Inc.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- *
- */
- #include "NativeFeatureIncludes.h"
- #if _RAKNET_SUPPORT_ReplicaManager3==1
- #include "ReplicaManager3.h"
- #include "GetTime.h"
- #include "MessageIdentifiers.h"
- #include "RakPeerInterface.h"
- #include "NetworkIDManager.h"
- using namespace RakNet;
- // DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS(LastSerializationResult,Replica3*,replica);
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool PRO::operator==( const PRO& right ) const
- {
- return priority == right.priority && reliability == right.reliability && orderingChannel == right.orderingChannel && sendReceipt == right.sendReceipt;
- }
- bool PRO::operator!=( const PRO& right ) const
- {
- return priority != right.priority || reliability != right.reliability || orderingChannel != right.orderingChannel || sendReceipt != right.sendReceipt;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- int Connection_RM3::Replica3LSRComp( Replica3 * const &replica3, LastSerializationResult * const &data )
- {
- /*
- if (replica3->GetNetworkID() < data->replica->GetNetworkID())
- return -1;
- if (replica3->GetNetworkID() > data->replica->GetNetworkID())
- return 1;
- */
- // 7/28/2013 - If GetNetworkID chagned during runtime, the list would be out of order and lookup would always fail or go out of bounds
- // I remember before that I could not directly compare
- if (replica3->referenceIndex < data->replica->referenceIndex)
- return -1;
- if (replica3->referenceIndex > data->replica->referenceIndex)
- return 1;
- return 0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- LastSerializationResult::LastSerializationResult()
- {
- replica=0;
- lastSerializationResultBS=0;
- whenLastSerialized = RakNet::GetTime();
- }
- LastSerializationResult::~LastSerializationResult()
- {
- if (lastSerializationResultBS)
- RakNet::OP_DELETE(lastSerializationResultBS,_FILE_AND_LINE_);
- }
- void LastSerializationResult::AllocBS(void)
- {
- if (lastSerializationResultBS==0)
- {
- lastSerializationResultBS=RakNet::OP_NEW<LastSerializationResultBS>(_FILE_AND_LINE_);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- ReplicaManager3::ReplicaManager3()
- {
- defaultSendParameters.orderingChannel=0;
- defaultSendParameters.priority=HIGH_PRIORITY;
- defaultSendParameters.reliability=RELIABLE_ORDERED;
- defaultSendParameters.sendReceipt=0;
- autoSerializeInterval=30;
- lastAutoSerializeOccurance=0;
- autoCreateConnections=true;
- autoDestroyConnections=true;
- currentlyDeallocatingReplica=0;
- for (unsigned int i=0; i < 255; i++)
- worldsArray[i]=0;
- AddWorld(0);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- ReplicaManager3::~ReplicaManager3()
- {
- if (autoDestroyConnections)
- {
- for (unsigned int i=0; i < worldsList.Size(); i++)
- {
- RakAssert(worldsList[i]->connectionList.Size()==0);
- }
- }
- Clear(true);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::SetAutoManageConnections(bool autoCreate, bool autoDestroy)
- {
- autoCreateConnections=autoCreate;
- autoDestroyConnections=autoDestroy;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool ReplicaManager3::GetAutoCreateConnections(void) const
- {
- return autoCreateConnections;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool ReplicaManager3::GetAutoDestroyConnections(void) const
- {
- return autoDestroyConnections;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::AutoCreateConnectionList(
- DataStructures::List<RakNetGUID> &participantListIn,
- DataStructures::List<Connection_RM3*> &participantListOut,
- WorldId worldId)
- {
- for (unsigned int index=0; index < participantListIn.Size(); index++)
- {
- if (GetConnectionByGUID(participantListIn[index], worldId)==false)
- {
- Connection_RM3 *connection = AllocConnection(rakPeerInterface->GetSystemAddressFromGuid(participantListIn[index]), participantListIn[index]);
- if (connection)
- {
- PushConnection(connection);
- participantListOut.Push(connection, _FILE_AND_LINE_);
- }
- }
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool ReplicaManager3::PushConnection(RakNet::Connection_RM3 *newConnection, WorldId worldId)
- {
- if (newConnection==0)
- return false;
- if (GetConnectionByGUID(newConnection->GetRakNetGUID(), worldId))
- return false;
- // Was this intended?
- RakAssert(newConnection->GetRakNetGUID()!=rakPeerInterface->GetMyGUID());
-
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- unsigned int index = world->connectionList.GetIndexOf(newConnection);
- if (index==(unsigned int)-1)
- {
- world->connectionList.Push(newConnection,_FILE_AND_LINE_);
- // Send message to validate the connection
- newConnection->SendValidation(rakPeerInterface, worldId);
- Connection_RM3::ConstructionMode constructionMode = newConnection->QueryConstructionMode();
- if (constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
- {
- unsigned int pushIdx;
- for (pushIdx=0; pushIdx < world->userReplicaList.Size(); pushIdx++)
- newConnection->OnLocalReference(world->userReplicaList[pushIdx], this);
- }
- }
- return true;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::DeallocReplicaNoBroadcastDestruction(RakNet::Connection_RM3 *connection, RakNet::Replica3 *replica3)
- {
- currentlyDeallocatingReplica=replica3;
- replica3->DeallocReplica(connection);
- currentlyDeallocatingReplica=0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RakNet::Connection_RM3 * ReplicaManager3::PopConnection(unsigned int index, WorldId worldId)
- {
- DataStructures::List<Replica3*> replicaList;
- DataStructures::List<NetworkID> destructionList;
- DataStructures::List<Replica3*> broadcastList;
- RakNet::Connection_RM3 *connection;
- unsigned int index2;
- RM3ActionOnPopConnection action;
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- connection=world->connectionList[index];
- // Clear out downloadGroup
- connection->ClearDownloadGroup(rakPeerInterface);
- RakNetGUID guid = connection->GetRakNetGUID();
- // This might be wrong, I am relying on the variable creatingSystemGuid which is transmitted
- // automatically from the first system to reference the object. However, if an object changes
- // owners then it is not going to be returned here, and therefore QueryActionOnPopConnection()
- // will not be called for the new owner.
- GetReplicasCreatedByGuid(guid, replicaList);
- for (index2=0; index2 < replicaList.Size(); index2++)
- {
- action = replicaList[index2]->QueryActionOnPopConnection(connection);
- replicaList[index2]->OnPoppedConnection(connection);
- if (action==RM3AOPC_DELETE_REPLICA)
- {
- if (replicaList[index2]->GetNetworkIDManager())
- destructionList.Push( replicaList[index2]->GetNetworkID(), _FILE_AND_LINE_ );
- }
- else if (action==RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION)
- {
- if (replicaList[index2]->GetNetworkIDManager())
- destructionList.Push( replicaList[index2]->GetNetworkID(), _FILE_AND_LINE_ );
- broadcastList.Push( replicaList[index2], _FILE_AND_LINE_ );
- }
- else if (action==RM3AOPC_DO_NOTHING)
- {
- for (unsigned int index3 = 0; index3 < connection->queryToSerializeReplicaList.Size(); index3++)
- {
- LastSerializationResult *lsr = connection->queryToSerializeReplicaList[index3];
- lsr->whenLastSerialized=0;
- if (lsr->lastSerializationResultBS)
- {
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- lsr->lastSerializationResultBS->bitStream[z].Reset();
- }
- }
- }
- }
- BroadcastDestructionList(broadcastList, connection->GetSystemAddress());
- for (index2=0; index2 < destructionList.Size(); index2++)
- {
- // Do lookup in case DeallocReplica destroyed one of of the later Replica3 instances in the list
- Replica3* replicaToDestroy = world->networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(destructionList[index2]);
- if (replicaToDestroy)
- {
- replicaToDestroy->PreDestruction(connection);
- replicaToDestroy->DeallocReplica(connection);
- }
- }
- world->connectionList.RemoveAtIndex(index);
- return connection;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RakNet::Connection_RM3 * ReplicaManager3::PopConnection(RakNetGUID guid, WorldId worldId)
- {
- unsigned int index;
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- for (index=0; index < world->connectionList.Size(); index++)
- {
- if (world->connectionList[index]->GetRakNetGUID()==guid)
- {
- return PopConnection(index, worldId);
- }
- }
- return 0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::Reference(RakNet::Replica3 *replica3, WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- unsigned int index = ReferenceInternal(replica3, worldId);
- if (index!=(unsigned int)-1)
- {
- unsigned int pushIdx;
- for (pushIdx=0; pushIdx < world->connectionList.Size(); pushIdx++)
- {
- Connection_RM3::ConstructionMode constructionMode = world->connectionList[pushIdx]->QueryConstructionMode();
- if (constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==Connection_RM3::QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
- {
- world->connectionList[pushIdx]->OnLocalReference(replica3, this);
- }
- }
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- unsigned int ReplicaManager3::ReferenceInternal(RakNet::Replica3 *replica3, WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- unsigned int index;
- index = world->userReplicaList.GetIndexOf(replica3);
- if (index==(unsigned int)-1)
- {
- RakAssert(world->networkIDManager);
- replica3->SetNetworkIDManager(world->networkIDManager);
- // If it crashes on rakPeerInterface==0 then you didn't call RakPeerInterface::AttachPlugin()
- if (replica3->creatingSystemGUID==UNASSIGNED_RAKNET_GUID)
- replica3->creatingSystemGUID=rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
- replica3->replicaManager=this;
- if (replica3->referenceIndex==(uint32_t)-1)
- {
- replica3->referenceIndex=nextReferenceIndex++;
- }
- world->userReplicaList.Push(replica3,_FILE_AND_LINE_);
- return world->userReplicaList.Size()-1;
- }
- return (unsigned int) -1;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::Dereference(RakNet::Replica3 *replica3, WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- unsigned int index, index2;
- for (index=0; index < world->userReplicaList.Size(); index++)
- {
- if (world->userReplicaList[index]==replica3)
- {
- world->userReplicaList.RemoveAtIndex(index);
- break;
- }
- }
- // Remove from all connections
- for (index2=0; index2 < world->connectionList.Size(); index2++)
- {
- world->connectionList[index2]->OnDereference(replica3, this);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::DereferenceList(DataStructures::List<Replica3*> &replicaListIn, WorldId worldId)
- {
- unsigned int index;
- for (index=0; index < replicaListIn.Size(); index++)
- Dereference(replicaListIn[index], worldId);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::GetReplicasCreatedByMe(DataStructures::List<Replica3*> &replicaListOut, WorldId worldId)
- {
- //RakNetGUID myGuid = rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
- GetReplicasCreatedByGuid(rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS), replicaListOut, worldId);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::GetReferencedReplicaList(DataStructures::List<Replica3*> &replicaListOut, WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- replicaListOut=world->userReplicaList;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::GetReplicasCreatedByGuid(RakNetGUID guid, DataStructures::List<Replica3*> &replicaListOut, WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- replicaListOut.Clear(false,_FILE_AND_LINE_);
- unsigned int index;
- for (index=0; index < world->userReplicaList.Size(); index++)
- {
- if (world->userReplicaList[index]->creatingSystemGUID==guid)
- replicaListOut.Push(world->userReplicaList[index],_FILE_AND_LINE_);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- unsigned ReplicaManager3::GetReplicaCount(WorldId worldId) const
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- return world->userReplicaList.Size();
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Replica3 *ReplicaManager3::GetReplicaAtIndex(unsigned index, WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- return world->userReplicaList[index];
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- unsigned int ReplicaManager3::GetConnectionCount(WorldId worldId) const
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- return world->connectionList.Size();
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Connection_RM3* ReplicaManager3::GetConnectionAtIndex(unsigned index, WorldId worldId) const
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- return world->connectionList[index];
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Connection_RM3* ReplicaManager3::GetConnectionBySystemAddress(const SystemAddress &sa, WorldId worldId) const
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- unsigned int index;
- for (index=0; index < world->connectionList.Size(); index++)
- {
- if (world->connectionList[index]->GetSystemAddress()==sa)
- {
- return world->connectionList[index];
- }
- }
- return 0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Connection_RM3* ReplicaManager3::GetConnectionByGUID(RakNetGUID guid, WorldId worldId) const
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- unsigned int index;
- for (index=0; index < world->connectionList.Size(); index++)
- {
- if (world->connectionList[index]->GetRakNetGUID()==guid)
- {
- return world->connectionList[index];
- }
- }
- return 0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::SetDefaultOrderingChannel(char def)
- {
- defaultSendParameters.orderingChannel=def;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::SetDefaultPacketPriority(PacketPriority def)
- {
- defaultSendParameters.priority=def;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::SetDefaultPacketReliability(PacketReliability def)
- {
- defaultSendParameters.reliability=def;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::SetAutoSerializeInterval(RakNet::Time intervalMS)
- {
- autoSerializeInterval=intervalMS;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::GetConnectionsThatHaveReplicaConstructed(Replica3 *replica, DataStructures::List<Connection_RM3*> &connectionsThatHaveConstructedThisReplica, WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- connectionsThatHaveConstructedThisReplica.Clear(false,_FILE_AND_LINE_);
- unsigned int index;
- for (index=0; index < world->connectionList.Size(); index++)
- {
- if (world->connectionList[index]->HasReplicaConstructed(replica))
- connectionsThatHaveConstructedThisReplica.Push(world->connectionList[index],_FILE_AND_LINE_);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool ReplicaManager3::GetAllConnectionDownloadsCompleted(WorldId worldId) const
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- unsigned int index;
- for (index=0; index < world->connectionList.Size(); index++)
- {
- if (world->connectionList[index]->GetDownloadWasCompleted()==false)
- return false;
- }
- return true;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::Clear(bool deleteWorlds)
- {
- for (unsigned int i=0; i < worldsList.Size(); i++)
- {
- worldsList[i]->Clear(this);
- if (deleteWorlds)
- {
- worldsArray[worldsList[i]->worldId]=0;
- delete worldsList[i];
- }
- }
- if (deleteWorlds)
- worldsList.Clear(false, _FILE_AND_LINE_);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- ReplicaManager3::RM3World::RM3World()
- {
- networkIDManager=0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::RM3World::Clear(ReplicaManager3 *replicaManager3)
- {
- if (replicaManager3->GetAutoDestroyConnections())
- {
- for (unsigned int i=0; i < connectionList.Size(); i++)
- replicaManager3->DeallocConnection(connectionList[i]);
- }
- else
- {
- // Clear out downloadGroup even if not auto destroying the connection, since the packets need to go back to RakPeer
- for (unsigned int i=0; i < connectionList.Size(); i++)
- connectionList[i]->ClearDownloadGroup(replicaManager3->GetRakPeerInterface());
- }
-
- for (unsigned int i=0; i < userReplicaList.Size(); i++)
- {
- userReplicaList[i]->replicaManager=0;
- userReplicaList[i]->SetNetworkIDManager(0);
- }
- connectionList.Clear(true,_FILE_AND_LINE_);
- userReplicaList.Clear(true,_FILE_AND_LINE_);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- PRO ReplicaManager3::GetDefaultSendParameters(void) const
- {
- return defaultSendParameters;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::AddWorld(WorldId worldId)
- {
- RakAssert(worldsArray[worldId]==0 && "World already in use");
- RM3World *newWorld = RakNet::OP_NEW<RM3World>(_FILE_AND_LINE_);
- newWorld->worldId=worldId;
- worldsArray[worldId]=newWorld;
- worldsList.Push(newWorld,_FILE_AND_LINE_);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::RemoveWorld(WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- for (unsigned int i=0; i < worldsList.Size(); i++)
- {
- if (worldsList[i]==worldsArray[worldId])
- {
- RakNet::OP_DELETE(worldsList[i],_FILE_AND_LINE_);
- worldsList.RemoveAtIndexFast(i);
- break;
- }
- }
- worldsArray[worldId]=0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- NetworkIDManager *ReplicaManager3::GetNetworkIDManager(WorldId worldId) const
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- return world->networkIDManager;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::SetNetworkIDManager(NetworkIDManager *_networkIDManager, WorldId worldId)
- {
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- world->networkIDManager=_networkIDManager;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- PluginReceiveResult ReplicaManager3::OnReceive(Packet *packet)
- {
- if (packet->length<2)
- return RR_CONTINUE_PROCESSING;
- WorldId incomingWorldId;
- RakNet::Time timestamp=0;
- unsigned char packetIdentifier, packetDataOffset;
- if ( ( unsigned char ) packet->data[ 0 ] == ID_TIMESTAMP )
- {
- if ( packet->length > sizeof( unsigned char ) + sizeof( RakNet::Time ) )
- {
- packetIdentifier = ( unsigned char ) packet->data[ sizeof( unsigned char ) + sizeof( RakNet::Time ) ];
- // Required for proper endian swapping
- RakNet::BitStream tsBs(packet->data+sizeof(MessageID),packet->length-1,false);
- tsBs.Read(timestamp);
- // Next line assumes worldId is only 1 byte
- RakAssert(sizeof(WorldId)==1);
- incomingWorldId=packet->data[sizeof( unsigned char )*2 + sizeof( RakNet::Time )];
- packetDataOffset=sizeof( unsigned char )*3 + sizeof( RakNet::Time );
- }
- else
- return RR_STOP_PROCESSING_AND_DEALLOCATE;
- }
- else
- {
- packetIdentifier = ( unsigned char ) packet->data[ 0 ];
- // Next line assumes worldId is only 1 byte
- RakAssert(sizeof(WorldId)==1);
- incomingWorldId=packet->data[sizeof( unsigned char )];
- packetDataOffset=sizeof( unsigned char )*2;
- }
- if (worldsArray[incomingWorldId]==0)
- return RR_CONTINUE_PROCESSING;
- switch (packetIdentifier)
- {
- case ID_REPLICA_MANAGER_CONSTRUCTION:
- return OnConstruction(packet, packet->data, packet->length, packet->guid, packetDataOffset, incomingWorldId);
- case ID_REPLICA_MANAGER_SERIALIZE:
- return OnSerialize(packet, packet->data, packet->length, packet->guid, timestamp, packetDataOffset, incomingWorldId);
- case ID_REPLICA_MANAGER_DOWNLOAD_STARTED:
- if (packet->wasGeneratedLocally==false)
- {
- return OnDownloadStarted(packet, packet->data, packet->length, packet->guid, packetDataOffset, incomingWorldId);
- }
- else
- break;
- case ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE:
- if (packet->wasGeneratedLocally==false)
- {
- return OnDownloadComplete(packet, packet->data, packet->length, packet->guid, packetDataOffset, incomingWorldId);
- }
- else
- break;
- case ID_REPLICA_MANAGER_SCOPE_CHANGE:
- {
- Connection_RM3 *connection = GetConnectionByGUID(packet->guid, incomingWorldId);
- if (connection && connection->isValidated==false)
- {
- // This connection is now confirmed bidirectional
- connection->isValidated=true;
- // Reply back on validation
- connection->SendValidation(rakPeerInterface,incomingWorldId);
- }
- }
- }
- return RR_CONTINUE_PROCESSING;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::AutoConstructByQuery(ReplicaManager3 *replicaManager3, WorldId worldId)
- {
- ValidateLists(replicaManager3);
- ConstructionMode constructionMode = QueryConstructionMode();
- unsigned int index;
- RM3ConstructionState constructionState;
- LastSerializationResult *lsr;
- index=0;
- constructedReplicasCulled.Clear(false,_FILE_AND_LINE_);
- destroyedReplicasCulled.Clear(false,_FILE_AND_LINE_);
- if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
- {
- while (index < queryToConstructReplicaList.Size())
- {
- lsr=queryToConstructReplicaList[index];
- constructionState=lsr->replica->QueryConstruction(this, replicaManager3);
- if (constructionState==RM3CS_ALREADY_EXISTS_REMOTELY || constructionState==RM3CS_ALREADY_EXISTS_REMOTELY_DO_NOT_CONSTRUCT)
- {
- OnReplicaAlreadyExists(index, replicaManager3);
- if (constructionState==RM3CS_ALREADY_EXISTS_REMOTELY)
- constructedReplicasCulled.Push(lsr->replica,_FILE_AND_LINE_);
- /*
- if (constructionState==RM3CS_ALREADY_EXISTS_REMOTELY)
- {
- // Serialize construction data to this connection
- RakNet::BitStream bsOut;
- bsOut.Write((MessageID)ID_REPLICA_MANAGER_3_SERIALIZE_CONSTRUCTION_EXISTING);
- bsOut.Write(replicaManager3->GetWorldID());
- NetworkID networkId;
- networkId=lsr->replica->GetNetworkID();
- bsOut.Write(networkId);
- BitSize_t bitsWritten = bsOut.GetNumberOfBitsUsed();
- lsr->replica->SerializeConstructionExisting(&bsOut, this);
- if (bsOut.GetNumberOfBitsUsed()!=bitsWritten)
- replicaManager3->SendUnified(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,GetSystemAddress(), false);
- }
- // Serialize first serialization to this connection.
- // This is done here, as it isn't done in PushConstruction
- SerializeParameters sp;
- RakNet::BitStream emptyBs;
- for (index=0; index < (unsigned int) RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; index++)
- {
- sp.lastSentBitstream[index]=&emptyBs;
- sp.pro[index]=replicaManager3->GetDefaultSendParameters();
- }
- sp.bitsWrittenSoFar=0;
- sp.destinationConnection=this;
- sp.messageTimestamp=0;
- sp.whenLastSerialized=0;
- RakNet::Replica3 *replica = lsr->replica;
- RM3SerializationResult res = replica->Serialize(&sp);
- if (res!=RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION &&
- res!=RM3SR_DO_NOT_SERIALIZE &&
- res!=RM3SR_SERIALIZED_UNIQUELY)
- {
- bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- sp.bitsWrittenSoFar+=sp.outputBitstream[z].GetNumberOfBitsUsed();
- allIndices[z]=true;
- }
- if (SendSerialize(replica, allIndices, sp.outputBitstream, sp.messageTimestamp, sp.pro, replicaManager3->GetRakPeerInterface(), replicaManager3->GetWorldID())==SSICR_SENT_DATA)
- lsr->replica->whenLastSerialized=RakNet::GetTimeMS();
- }
- */
- }
- else if (constructionState==RM3CS_SEND_CONSTRUCTION)
- {
- OnConstructToThisConnection(index, replicaManager3);
- RakAssert(lsr->replica);
- constructedReplicasCulled.Push(lsr->replica,_FILE_AND_LINE_);
- }
- else if (constructionState==RM3CS_NEVER_CONSTRUCT)
- {
- OnNeverConstruct(index, replicaManager3);
- }
- else// if (constructionState==RM3CS_NO_ACTION)
- {
- // Do nothing
- index++;
- }
- }
- if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
- {
- RM3DestructionState destructionState;
- index=0;
- while (index < queryToDestructReplicaList.Size())
- {
- lsr=queryToDestructReplicaList[index];
- destructionState=lsr->replica->QueryDestruction(this, replicaManager3);
- if (destructionState==RM3DS_SEND_DESTRUCTION)
- {
- OnSendDestructionFromQuery(index, replicaManager3);
- destroyedReplicasCulled.Push(lsr->replica,_FILE_AND_LINE_);
- }
- else if (destructionState==RM3DS_DO_NOT_QUERY_DESTRUCTION)
- {
- OnDoNotQueryDestruction(index, replicaManager3);
- }
- else// if (destructionState==RM3CS_NO_ACTION)
- {
- // Do nothing
- index++;
- }
- }
- }
- }
- else if (constructionMode==QUERY_CONNECTION_FOR_REPLICA_LIST)
- {
- QueryReplicaList(constructedReplicasCulled,destroyedReplicasCulled);
- unsigned int idx1, idx2;
- // Create new
- for (idx2=0; idx2 < constructedReplicasCulled.Size(); idx2++)
- OnConstructToThisConnection(constructedReplicasCulled[idx2], replicaManager3);
- bool exists;
- for (idx2=0; idx2 < destroyedReplicasCulled.Size(); idx2++)
- {
- exists=false;
- bool objectExists;
- idx1=constructedReplicaList.GetIndexFromKey(destroyedReplicasCulled[idx2], &objectExists);
- if (objectExists)
- {
- constructedReplicaList.RemoveAtIndex(idx1);
- unsigned int j;
- for (j=0; j < queryToSerializeReplicaList.Size(); j++)
- {
- if (queryToSerializeReplicaList[j]->replica==destroyedReplicasCulled[idx2] )
- {
- queryToSerializeReplicaList.RemoveAtIndex(j);
- break;
- }
- }
- }
- }
- }
- SendConstruction(constructedReplicasCulled,destroyedReplicasCulled,replicaManager3->defaultSendParameters,replicaManager3->rakPeerInterface,worldId,replicaManager3);
- }
- void ReplicaManager3::Update(void)
- {
- unsigned int index,index2,index3;
- WorldId worldId;
- RM3World *world;
- RakNet::Time time = RakNet::GetTime();
- for (index3=0; index3 < worldsList.Size(); index3++)
- {
- world = worldsList[index3];
- worldId = world->worldId;
- for (index=0; index < world->connectionList.Size(); index++)
- {
- if (world->connectionList[index]->isValidated==false)
- continue;
- world->connectionList[index]->AutoConstructByQuery(this, worldId);
- }
- }
- if (time - lastAutoSerializeOccurance >= autoSerializeInterval)
- {
- for (index3=0; index3 < worldsList.Size(); index3++)
- {
- world = worldsList[index3];
- worldId = world->worldId;
- for (index=0; index < world->userReplicaList.Size(); index++)
- {
- world->userReplicaList[index]->forceSendUntilNextUpdate=false;
- world->userReplicaList[index]->OnUserReplicaPreSerializeTick();
- }
- unsigned int index;
- SerializeParameters sp;
- sp.curTime=time;
- Connection_RM3 *connection;
- SendSerializeIfChangedResult ssicr;
- LastSerializationResult *lsr;
- sp.messageTimestamp=0;
- for (int i=0; i < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; i++)
- sp.pro[i]=defaultSendParameters;
- index2=0;
- for (index=0; index < world->connectionList.Size(); index++)
- {
- connection = world->connectionList[index];
- sp.bitsWrittenSoFar=0;
- index2=0;
- sp.destinationConnection=connection;
- DataStructures::List<Replica3*> replicasToSerialize;
- replicasToSerialize.Clear(true, _FILE_AND_LINE_);
- if (connection->QuerySerializationList(replicasToSerialize))
- {
- // Update replica->lsr so we can lookup in the next block
- // lsr is per connection / per replica
- while (index2 < connection->queryToSerializeReplicaList.Size())
- {
- connection->queryToSerializeReplicaList[index2]->replica->lsr=connection->queryToSerializeReplicaList[index2];
- index2++;
- }
- // User is manually specifying list of replicas to serialize
- index2=0;
- while (index2 < replicasToSerialize.Size())
- {
- lsr=replicasToSerialize[index2]->lsr;
- RakAssert(lsr->replica==replicasToSerialize[index2]);
- sp.whenLastSerialized=lsr->whenLastSerialized;
- ssicr=connection->SendSerializeIfChanged(lsr, &sp, GetRakPeerInterface(), worldId, this, time);
- if (ssicr==SSICR_SENT_DATA)
- lsr->whenLastSerialized=time;
- index2++;
- }
- }
- else
- {
- while (index2 < connection->queryToSerializeReplicaList.Size())
- {
- lsr=connection->queryToSerializeReplicaList[index2];
- sp.destinationConnection=connection;
- sp.whenLastSerialized=lsr->whenLastSerialized;
- ssicr=connection->SendSerializeIfChanged(lsr, &sp, GetRakPeerInterface(), worldId, this, time);
- if (ssicr==SSICR_SENT_DATA)
- {
- lsr->whenLastSerialized=time;
- index2++;
- }
- else if (ssicr==SSICR_NEVER_SERIALIZE)
- {
- // Removed from the middle of the list
- }
- else
- index2++;
- }
- }
- }
- }
- lastAutoSerializeOccurance=time;
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
- {
- (void) lostConnectionReason;
- (void) systemAddress;
- if (autoDestroyConnections)
- {
- Connection_RM3 *connection = PopConnection(rakNetGUID);
- if (connection)
- DeallocConnection(connection);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
- {
- (void) isIncoming;
- if (autoCreateConnections)
- {
- Connection_RM3 *connection = AllocConnection(systemAddress, rakNetGUID);
- if (connection)
- PushConnection(connection);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::OnRakPeerShutdown(void)
- {
- if (autoDestroyConnections)
- {
- RM3World *world;
- unsigned int index3;
- for (index3=0; index3 < worldsList.Size(); index3++)
- {
- world = worldsList[index3];
- while (world->connectionList.Size())
- {
- Connection_RM3 *connection = PopConnection(world->connectionList.Size()-1, world->worldId);
- if (connection)
- DeallocConnection(connection);
- }
- }
- }
- Clear(false);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::OnDetach(void)
- {
- OnRakPeerShutdown();
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- PluginReceiveResult ReplicaManager3::OnConstruction(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId)
- {
- RM3World *world = worldsArray[worldId];
- Connection_RM3 *connection = GetConnectionByGUID(senderGuid, worldId);
- if (connection==0)
- {
- // Almost certainly a bug
- RakAssert("Got OnConstruction but no connection yet" && 0);
- return RR_CONTINUE_PROCESSING;
- }
- if (connection->groupConstructionAndSerialize)
- {
- connection->downloadGroup.Push(packet, __FILE__, __LINE__);
- return RR_STOP_PROCESSING;
- }
- RakNet::BitStream bsIn(packetData,packetDataLength,false);
- bsIn.IgnoreBytes(packetDataOffset);
- uint16_t constructionObjectListSize, destructionObjectListSize, index, index2;
- BitSize_t streamEnd, writeAllocationIDEnd;
- Replica3 *replica;
- NetworkID networkId;
- RakNetGUID creatingSystemGuid;
- bool actuallyCreateObject=false;
- DataStructures::List<bool> actuallyCreateObjectList;
- DataStructures::List<Replica3*> constructionTickStack;
- RakAssert(world->networkIDManager);
- bsIn.Read(constructionObjectListSize);
- for (index=0; index < constructionObjectListSize; index++)
- {
- bsIn.Read(streamEnd);
- bsIn.Read(networkId);
- Replica3* existingReplica = world->networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
- bsIn.Read(actuallyCreateObject);
- actuallyCreateObjectList.Push(actuallyCreateObject, _FILE_AND_LINE_);
- bsIn.AlignReadToByteBoundary();
- if (actuallyCreateObject)
- {
- bsIn.Read(creatingSystemGuid);
- bsIn.Read(writeAllocationIDEnd);
- //printf("OnConstruction: %i\n",networkId.guid.g); // Removeme
- if (existingReplica)
- {
- existingReplica->replicaManager=this;
- // Network ID already in use
- connection->OnDownloadExisting(existingReplica, this);
- constructionTickStack.Push(0, _FILE_AND_LINE_);
- bsIn.SetReadOffset(streamEnd);
- continue;
- }
- bsIn.AlignReadToByteBoundary();
- replica = connection->AllocReplica(&bsIn, this);
- if (replica==0)
- {
- constructionTickStack.Push(0, _FILE_AND_LINE_);
- bsIn.SetReadOffset(streamEnd);
- continue;
- }
- // Go past the bitStream written to with WriteAllocationID(). Necessary in case the user didn't read out the bitStream the same way it was written
- // bitOffset2 is already aligned
- bsIn.SetReadOffset(writeAllocationIDEnd);
- replica->SetNetworkIDManager(world->networkIDManager);
- replica->SetNetworkID(networkId);
- replica->replicaManager=this;
- replica->creatingSystemGUID=creatingSystemGuid;
- if (!replica->QueryRemoteConstruction(connection) ||
- !replica->DeserializeConstruction(&bsIn, connection))
- {
- DeallocReplicaNoBroadcastDestruction(connection, replica);
- bsIn.SetReadOffset(streamEnd);
- constructionTickStack.Push(0, _FILE_AND_LINE_);
- continue;
- }
- constructionTickStack.Push(replica, _FILE_AND_LINE_);
- // Register the replica
- ReferenceInternal(replica, worldId);
- }
- else
- {
- if (existingReplica)
- {
- existingReplica->DeserializeConstructionExisting(&bsIn, connection);
- constructionTickStack.Push(existingReplica, _FILE_AND_LINE_);
- }
- else
- {
- constructionTickStack.Push(0, _FILE_AND_LINE_);
- }
- }
- bsIn.SetReadOffset(streamEnd);
- bsIn.AlignReadToByteBoundary();
- }
- RakAssert(constructionTickStack.Size()==constructionObjectListSize);
- RakAssert(actuallyCreateObjectList.Size()==constructionObjectListSize);
- RakNet::BitStream empty;
- for (index=0; index < constructionObjectListSize; index++)
- {
- bool pdcWritten=false;
- bsIn.Read(pdcWritten);
- if (pdcWritten)
- {
- bsIn.AlignReadToByteBoundary();
- bsIn.Read(streamEnd);
- bsIn.Read(networkId);
- if (constructionTickStack[index]!=0)
- {
- bsIn.AlignReadToByteBoundary();
- if (actuallyCreateObjectList[index])
- constructionTickStack[index]->PostDeserializeConstruction(&bsIn, connection);
- else
- constructionTickStack[index]->PostDeserializeConstructionExisting(&bsIn, connection);
- }
- bsIn.SetReadOffset(streamEnd);
- }
- else
- {
- if (constructionTickStack[index]!=0)
- {
- if (actuallyCreateObjectList[index])
- constructionTickStack[index]->PostDeserializeConstruction(&empty, connection);
- else
- constructionTickStack[index]->PostDeserializeConstructionExisting(&empty, connection);
- }
- }
- }
- for (index=0; index < constructionObjectListSize; index++)
- {
- if (constructionTickStack[index]!=0)
- {
- if (actuallyCreateObjectList[index])
- {
- // Tell the connection(s) that this object exists since they just sent it to us
- connection->OnDownloadFromThisSystem(constructionTickStack[index], this);
- for (index2=0; index2 < world->connectionList.Size(); index2++)
- {
- if (world->connectionList[index2]!=connection)
- world->connectionList[index2]->OnDownloadFromOtherSystem(constructionTickStack[index], this);
- }
- }
- }
- }
- // Destructions
- bool b = bsIn.Read(destructionObjectListSize);
- (void) b;
- RakAssert(b);
- for (index=0; index < destructionObjectListSize; index++)
- {
- bsIn.Read(networkId);
- bsIn.Read(streamEnd);
- replica = world->networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
- if (replica==0)
- {
- // Unknown object
- bsIn.SetReadOffset(streamEnd);
- continue;
- }
- bsIn.Read(replica->deletingSystemGUID);
- if (replica->DeserializeDestruction(&bsIn,connection))
- {
- // Make sure it wasn't deleted in DeserializeDestruction
- if (world->networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId))
- {
- replica->PreDestruction(connection);
- // Forward deletion by remote system
- if (replica->QueryRelayDestruction(connection))
- BroadcastDestruction(replica,connection->GetSystemAddress());
- Dereference(replica);
- DeallocReplicaNoBroadcastDestruction(connection, replica);
- }
- }
- else
- {
- replica->PreDestruction(connection);
- connection->OnDereference(replica, this);
- }
- bsIn.AlignReadToByteBoundary();
- }
- return RR_CONTINUE_PROCESSING;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- PluginReceiveResult ReplicaManager3::OnSerialize(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, RakNet::Time timestamp, unsigned char packetDataOffset, WorldId worldId)
- {
- Connection_RM3 *connection = GetConnectionByGUID(senderGuid, worldId);
- if (connection==0)
- return RR_CONTINUE_PROCESSING;
- if (connection->groupConstructionAndSerialize)
- {
- connection->downloadGroup.Push(packet, __FILE__, __LINE__);
- return RR_STOP_PROCESSING;
- }
- RM3World *world = worldsArray[worldId];
- RakAssert(world->networkIDManager);
- RakNet::BitStream bsIn(packetData,packetDataLength,false);
- bsIn.IgnoreBytes(packetDataOffset);
- struct DeserializeParameters ds;
- ds.timeStamp=timestamp;
- ds.sourceConnection=connection;
- Replica3 *replica;
- NetworkID networkId;
- BitSize_t bitsUsed;
- bsIn.Read(networkId);
- //printf("OnSerialize: %i\n",networkId.guid.g); // Removeme
- replica = world->networkIDManager->GET_OBJECT_FROM_ID<Replica3*>(networkId);
- if (replica)
- {
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- bsIn.Read(ds.bitstreamWrittenTo[z]);
- if (ds.bitstreamWrittenTo[z])
- {
- bsIn.ReadCompressed(bitsUsed);
- bsIn.AlignReadToByteBoundary();
- bsIn.Read(ds.serializationBitstream[z], bitsUsed);
- }
- }
- replica->Deserialize(&ds);
- }
- return RR_CONTINUE_PROCESSING;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- PluginReceiveResult ReplicaManager3::OnDownloadStarted(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId)
- {
- Connection_RM3 *connection = GetConnectionByGUID(senderGuid, worldId);
- if (connection==0)
- return RR_CONTINUE_PROCESSING;
- if (connection->QueryGroupDownloadMessages() &&
- // ID_DOWNLOAD_STARTED will be processed twice, being processed the second time once ID_DOWNLOAD_COMPLETE arrives.
- // However, the second time groupConstructionAndSerialize will be set to true so it won't be processed a third time
- connection->groupConstructionAndSerialize==false
- )
- {
- // These messages will be held by the plugin and returned when the download is complete
- connection->groupConstructionAndSerialize=true;
- RakAssert(connection->downloadGroup.Size()==0);
- connection->downloadGroup.Push(packet, __FILE__, __LINE__);
- return RR_STOP_PROCESSING;
- }
- connection->groupConstructionAndSerialize=false;
- RakNet::BitStream bsIn(packetData,packetDataLength,false);
- bsIn.IgnoreBytes(packetDataOffset);
- connection->DeserializeOnDownloadStarted(&bsIn);
- return RR_CONTINUE_PROCESSING;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- PluginReceiveResult ReplicaManager3::OnDownloadComplete(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId)
- {
- Connection_RM3 *connection = GetConnectionByGUID(senderGuid, worldId);
- if (connection==0)
- return RR_CONTINUE_PROCESSING;
- if (connection->groupConstructionAndSerialize==true && connection->downloadGroup.Size()>0)
- {
- // Push back buffered packets in front of this one
- unsigned int i;
- for (i=0; i < connection->downloadGroup.Size(); i++)
- rakPeerInterface->PushBackPacket(connection->downloadGroup[i],false);
- // Push this one to be last too. It will be processed again, but the second time
- // groupConstructionAndSerialize will be false and downloadGroup will be empty, so it will go past this block
- connection->downloadGroup.Clear(__FILE__,__LINE__);
- rakPeerInterface->PushBackPacket(packet,false);
- return RR_STOP_PROCESSING;
- }
- RakNet::BitStream bsIn(packetData,packetDataLength,false);
- bsIn.IgnoreBytes(packetDataOffset);
- connection->gotDownloadComplete=true;
- connection->DeserializeOnDownloadComplete(&bsIn);
- return RR_CONTINUE_PROCESSING;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Replica3* ReplicaManager3::GetReplicaByNetworkID(NetworkID networkId, WorldId worldId)
- {
- RM3World *world = worldsArray[worldId];
- unsigned int i;
- for (i=0; i < world->userReplicaList.Size(); i++)
- {
- if (world->userReplicaList[i]->GetNetworkID()==networkId)
- return world->userReplicaList[i];
- }
- return 0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::BroadcastDestructionList(DataStructures::List<Replica3*> &replicaListSource, const SystemAddress &exclusionAddress, WorldId worldId)
- {
- RakNet::BitStream bsOut;
- unsigned int i,j;
- RakAssert(worldsArray[worldId]!=0 && "World not in use");
- RM3World *world = worldsArray[worldId];
- DataStructures::List<Replica3*> replicaList;
- for (i=0; i < replicaListSource.Size(); i++)
- {
- if (replicaListSource[i]==currentlyDeallocatingReplica)
- continue;
- replicaList.Push(replicaListSource[i], __FILE__, __LINE__);
- }
- if (replicaList.Size()==0)
- return;
- for (i=0; i < replicaList.Size(); i++)
- {
- if (replicaList[i]->deletingSystemGUID==UNASSIGNED_RAKNET_GUID)
- replicaList[i]->deletingSystemGUID=GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
- }
- for (j=0; j < world->connectionList.Size(); j++)
- {
- if (world->connectionList[j]->GetSystemAddress()==exclusionAddress)
- continue;
- bsOut.Reset();
- bsOut.Write((MessageID)ID_REPLICA_MANAGER_CONSTRUCTION);
- bsOut.Write(worldId);
- uint16_t cnt=0;
- bsOut.Write(cnt); // No construction
- cnt=(uint16_t) replicaList.Size();
- BitSize_t cntOffset=bsOut.GetWriteOffset();;
- bsOut.Write(cnt); // Overwritten at send call
- cnt=0;
- for (i=0; i < replicaList.Size(); i++)
- {
- if (world->connectionList[j]->HasReplicaConstructed(replicaList[i])==false)
- continue;
- cnt++;
- NetworkID networkId;
- networkId=replicaList[i]->GetNetworkID();
- bsOut.Write(networkId);
- BitSize_t offsetStart, offsetEnd;
- offsetStart=bsOut.GetWriteOffset();
- bsOut.Write(offsetStart);
- bsOut.Write(replicaList[i]->deletingSystemGUID);
- replicaList[i]->SerializeDestruction(&bsOut, world->connectionList[j]);
- bsOut.AlignWriteToByteBoundary();
- offsetEnd=bsOut.GetWriteOffset();
- bsOut.SetWriteOffset(offsetStart);
- bsOut.Write(offsetEnd);
- bsOut.SetWriteOffset(offsetEnd);
- }
- if (cnt>0)
- {
- BitSize_t curOffset=bsOut.GetWriteOffset();
- bsOut.SetWriteOffset(cntOffset);
- bsOut.Write(cnt);
- bsOut.SetWriteOffset(curOffset);
- rakPeerInterface->Send(&bsOut,defaultSendParameters.priority,defaultSendParameters.reliability,defaultSendParameters.orderingChannel,world->connectionList[j]->GetSystemAddress(),false, defaultSendParameters.sendReceipt);
- }
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void ReplicaManager3::BroadcastDestruction(Replica3 *replica, const SystemAddress &exclusionAddress)
- {
- DataStructures::List<Replica3*> replicaList;
- replicaList.Push(replica, _FILE_AND_LINE_ );
- BroadcastDestructionList(replicaList,exclusionAddress);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Connection_RM3::Connection_RM3(const SystemAddress &_systemAddress, RakNetGUID _guid)
- : systemAddress(_systemAddress), guid(_guid)
- {
- isValidated=false;
- isFirstConstruction=true;
- groupConstructionAndSerialize=false;
- gotDownloadComplete=false;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Connection_RM3::~Connection_RM3()
- {
- unsigned int i;
- for (i=0; i < constructedReplicaList.Size(); i++)
- RakNet::OP_DELETE(constructedReplicaList[i], _FILE_AND_LINE_);
- for (i=0; i < queryToConstructReplicaList.Size(); i++)
- RakNet::OP_DELETE(queryToConstructReplicaList[i], _FILE_AND_LINE_);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::GetConstructedReplicas(DataStructures::List<Replica3*> &objectsTheyDoHave)
- {
- objectsTheyDoHave.Clear(true,_FILE_AND_LINE_);
- for (unsigned int idx=0; idx < constructedReplicaList.Size(); idx++)
- {
- objectsTheyDoHave.Push(constructedReplicaList[idx]->replica, _FILE_AND_LINE_ );
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool Connection_RM3::HasReplicaConstructed(RakNet::Replica3 *replica)
- {
- bool objectExists;
- constructedReplicaList.GetIndexFromKey(replica, &objectExists);
- return objectExists;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::SendSerializeHeader(RakNet::Replica3 *replica, RakNet::Time timestamp, RakNet::BitStream *bs, WorldId worldId)
- {
- bs->Reset();
- if (timestamp!=0)
- {
- bs->Write((MessageID)ID_TIMESTAMP);
- bs->Write(timestamp);
- }
- bs->Write((MessageID)ID_REPLICA_MANAGER_SERIALIZE);
- bs->Write(worldId);
- bs->Write(replica->GetNetworkID());
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::ClearDownloadGroup(RakPeerInterface *rakPeerInterface)
- {
- unsigned int i;
- for (i=0; i < downloadGroup.Size(); i++)
- rakPeerInterface->DeallocatePacket(downloadGroup[i]);
- downloadGroup.Clear(__FILE__,__LINE__);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- SendSerializeIfChangedResult Connection_RM3::SendSerialize(RakNet::Replica3 *replica, bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::BitStream serializationData[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time timestamp, PRO sendParameters[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakPeerInterface *rakPeer, unsigned char worldId, RakNet::Time curTime)
- {
- bool channelHasData;
- BitSize_t sum=0;
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- if (indicesToSend[z])
- sum+=serializationData[z].GetNumberOfBitsUsed();
- }
- RakNet::BitStream out;
- BitSize_t bitsPerChannel[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
- if (sum==0)
- {
- memset(bitsPerChannel, 0, sizeof(bitsPerChannel));
- replica->OnSerializeTransmission(&out, this, bitsPerChannel, curTime);
- return SSICR_DID_NOT_SEND_DATA;
- }
- RakAssert(replica->GetNetworkID()!=UNASSIGNED_NETWORK_ID);
- BitSize_t bitsUsed;
- int channelIndex;
- PRO lastPro=sendParameters[0];
- for (channelIndex=0; channelIndex < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; channelIndex++)
- {
- if (channelIndex==0)
- {
- SendSerializeHeader(replica, timestamp, &out, worldId);
- }
- else if (lastPro!=sendParameters[channelIndex])
- {
- // Write out remainder
- for (int channelIndex2=channelIndex; channelIndex2 < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; channelIndex2++)
- {
- bitsPerChannel[channelIndex2]=0;
- out.Write(false);
- }
- // Send remainder
- replica->OnSerializeTransmission(&out, this, bitsPerChannel, curTime);
- rakPeer->Send(&out,lastPro.priority,lastPro.reliability,lastPro.orderingChannel,systemAddress,false,lastPro.sendReceipt);
- // If no data left to send, quit out
- bool anyData=false;
- for (int channelIndex2=channelIndex; channelIndex2 < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; channelIndex2++)
- {
- if (serializationData[channelIndex2].GetNumberOfBitsUsed()>0)
- {
- anyData=true;
- break;
- }
- }
- if (anyData==false)
- return SSICR_SENT_DATA;
- // Restart stream
- SendSerializeHeader(replica, timestamp, &out, worldId);
- for (int channelIndex2=0; channelIndex2 < channelIndex; channelIndex2++)
- {
- bitsPerChannel[channelIndex2]=0;
- out.Write(false);
- }
- lastPro=sendParameters[channelIndex];
- }
- bitsUsed=serializationData[channelIndex].GetNumberOfBitsUsed();
- channelHasData = indicesToSend[channelIndex]==true && bitsUsed>0;
- out.Write(channelHasData);
- if (channelHasData)
- {
- bitsPerChannel[channelIndex] = bitsUsed;
- out.WriteCompressed(bitsUsed);
- out.AlignWriteToByteBoundary();
- out.Write(serializationData[channelIndex]);
- // Crap, forgot this line, was a huge bug in that I'd only send to the first 3 systems
- serializationData[channelIndex].ResetReadPointer();
- }
- else
- {
- bitsPerChannel[channelIndex] = 0;
- }
- }
- replica->OnSerializeTransmission(&out, this, bitsPerChannel, curTime);
- rakPeer->Send(&out,lastPro.priority,lastPro.reliability,lastPro.orderingChannel,systemAddress,false,lastPro.sendReceipt);
- return SSICR_SENT_DATA;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- SendSerializeIfChangedResult Connection_RM3::SendSerializeIfChanged(LastSerializationResult *lsr, SerializeParameters *sp, RakNet::RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager, RakNet::Time curTime)
- {
- RakNet::Replica3 *replica = lsr->replica;
- if (replica->GetNetworkID()==UNASSIGNED_NETWORK_ID)
- return SSICR_DID_NOT_SEND_DATA;
- RM3QuerySerializationResult rm3qsr = replica->QuerySerialization(this);
- if (rm3qsr==RM3QSR_NEVER_CALL_SERIALIZE)
- {
- // Never again for this connection and replica pair
- OnNeverSerialize(lsr, replicaManager);
- return SSICR_NEVER_SERIALIZE;
- }
- if (rm3qsr==RM3QSR_DO_NOT_CALL_SERIALIZE)
- return SSICR_DID_NOT_SEND_DATA;
- if (replica->forceSendUntilNextUpdate)
- {
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- if (replica->lastSentSerialization.indicesToSend[z])
- sp->bitsWrittenSoFar+=replica->lastSentSerialization.bitStream[z].GetNumberOfBitsUsed();
- }
- return SendSerialize(replica, replica->lastSentSerialization.indicesToSend, replica->lastSentSerialization.bitStream, sp->messageTimestamp, sp->pro, rakPeer, worldId, curTime);
- }
- for (int i=0; i < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; i++)
- {
- sp->outputBitstream[i].Reset();
- if (lsr->lastSerializationResultBS)
- sp->lastSentBitstream[i]=&lsr->lastSerializationResultBS->bitStream[i];
- else
- sp->lastSentBitstream[i]=&replica->lastSentSerialization.bitStream[i];
- }
- RM3SerializationResult serializationResult = replica->Serialize(sp);
- if (serializationResult==RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION)
- {
- // Never again for this connection and replica pair
- OnNeverSerialize(lsr, replicaManager);
- return SSICR_NEVER_SERIALIZE;
- }
- if (serializationResult==RM3SR_DO_NOT_SERIALIZE)
- {
- // Don't serialize this tick only
- return SSICR_DID_NOT_SEND_DATA;
- }
- // This is necessary in case the user in the Serialize() function for some reason read the bitstream they also wrote
- // WIthout this code, the Write calls to another bitstream would not write the entire bitstream
- BitSize_t sum=0;
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- sp->outputBitstream[z].ResetReadPointer();
- sum+=sp->outputBitstream[z].GetNumberOfBitsUsed();
- }
- if (sum==0)
- {
- // Don't serialize this tick only
- return SSICR_DID_NOT_SEND_DATA;
- }
- if (serializationResult==RM3SR_SERIALIZED_ALWAYS)
- {
- bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
- allIndices[z]=true;
- lsr->AllocBS();
- lsr->lastSerializationResultBS->bitStream[z].Reset();
- lsr->lastSerializationResultBS->bitStream[z].Write(&sp->outputBitstream[z]);
- sp->outputBitstream[z].ResetReadPointer();
- }
- return SendSerialize(replica, allIndices, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId, curTime);
- }
- if (serializationResult==RM3SR_SERIALIZED_ALWAYS_IDENTICALLY)
- {
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- replica->lastSentSerialization.indicesToSend[z]=sp->outputBitstream[z].GetNumberOfBitsUsed()>0;
- sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
- replica->lastSentSerialization.bitStream[z].Reset();
- replica->lastSentSerialization.bitStream[z].Write(&sp->outputBitstream[z]);
- sp->outputBitstream[z].ResetReadPointer();
- replica->forceSendUntilNextUpdate=true;
- }
- return SendSerialize(replica, replica->lastSentSerialization.indicesToSend, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId, curTime);
- }
- bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
- if (serializationResult==RM3SR_BROADCAST_IDENTICALLY || serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION)
- {
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- if (sp->outputBitstream[z].GetNumberOfBitsUsed() > 0 &&
- (serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION ||
- ((sp->outputBitstream[z].GetNumberOfBitsUsed()!=replica->lastSentSerialization.bitStream[z].GetNumberOfBitsUsed() ||
- memcmp(sp->outputBitstream[z].GetData(), replica->lastSentSerialization.bitStream[z].GetData(), sp->outputBitstream[z].GetNumberOfBytesUsed())!=0))))
- {
- indicesToSend[z]=true;
- replica->lastSentSerialization.indicesToSend[z]=true;
- sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
- replica->lastSentSerialization.bitStream[z].Reset();
- replica->lastSentSerialization.bitStream[z].Write(&sp->outputBitstream[z]);
- sp->outputBitstream[z].ResetReadPointer();
- replica->forceSendUntilNextUpdate=true;
- }
- else
- {
- indicesToSend[z]=false;
- replica->lastSentSerialization.indicesToSend[z]=false;
- }
- }
- }
- else
- {
- lsr->AllocBS();
- // RM3SR_SERIALIZED_UNIQUELY
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- if (sp->outputBitstream[z].GetNumberOfBitsUsed() > 0 &&
- (sp->outputBitstream[z].GetNumberOfBitsUsed()!=lsr->lastSerializationResultBS->bitStream[z].GetNumberOfBitsUsed() ||
- memcmp(sp->outputBitstream[z].GetData(), lsr->lastSerializationResultBS->bitStream[z].GetData(), sp->outputBitstream[z].GetNumberOfBytesUsed())!=0)
- )
- {
- indicesToSend[z]=true;
- sp->bitsWrittenSoFar+=sp->outputBitstream[z].GetNumberOfBitsUsed();
- lsr->lastSerializationResultBS->bitStream[z].Reset();
- lsr->lastSerializationResultBS->bitStream[z].Write(&sp->outputBitstream[z]);
- sp->outputBitstream[z].ResetReadPointer();
- }
- else
- {
- indicesToSend[z]=false;
- }
- }
- }
- if (serializationResult==RM3SR_BROADCAST_IDENTICALLY || serializationResult==RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION)
- replica->forceSendUntilNextUpdate=true;
- // Send out the data
- return SendSerialize(replica, indicesToSend, sp->outputBitstream, sp->messageTimestamp, sp->pro, rakPeer, worldId, curTime);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnLocalReference(Replica3* replica3, ReplicaManager3 *replicaManager)
- {
- ConstructionMode constructionMode = QueryConstructionMode();
- RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION);
- RakAssert(replica3);
- (void) replicaManager;
- (void) constructionMode;
- #ifdef _DEBUG
- for (unsigned int i=0; i < queryToConstructReplicaList.Size(); i++)
- {
- if (queryToConstructReplicaList[i]->replica==replica3)
- {
- RakAssert("replica added twice to queryToConstructReplicaList" && 0);
- }
- }
- if (constructedReplicaList.HasData(replica3)==true)
- {
- RakAssert("replica added to queryToConstructReplicaList when already in constructedReplicaList" && 0);
- }
- #endif
- LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(_FILE_AND_LINE_);
- lsr->replica=replica3;
- queryToConstructReplicaList.Push(lsr,_FILE_AND_LINE_);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnDereference(Replica3* replica3, ReplicaManager3 *replicaManager)
- {
- ValidateLists(replicaManager);
- if (replica3->GetNetworkIDManager() == 0)
- return;
- LastSerializationResult* lsr=0;
- unsigned int idx;
- bool objectExists;
- idx=constructedReplicaList.GetIndexFromKey(replica3, &objectExists);
- if (objectExists)
- {
- lsr=constructedReplicaList[idx];
- constructedReplicaList.RemoveAtIndex(idx);
- }
- for (idx=0; idx < queryToConstructReplicaList.Size(); idx++)
- {
- if (queryToConstructReplicaList[idx]->replica==replica3)
- {
- lsr=queryToConstructReplicaList[idx];
- queryToConstructReplicaList.RemoveAtIndex(idx);
- break;
- }
- }
- for (idx=0; idx < queryToSerializeReplicaList.Size(); idx++)
- {
- if (queryToSerializeReplicaList[idx]->replica==replica3)
- {
- lsr=queryToSerializeReplicaList[idx];
- queryToSerializeReplicaList.RemoveAtIndex(idx);
- break;
- }
- }
- for (idx=0; idx < queryToDestructReplicaList.Size(); idx++)
- {
- if (queryToDestructReplicaList[idx]->replica==replica3)
- {
- lsr=queryToDestructReplicaList[idx];
- queryToDestructReplicaList.RemoveAtIndex(idx);
- break;
- }
- }
- ValidateLists(replicaManager);
- if (lsr)
- RakNet::OP_DELETE(lsr,_FILE_AND_LINE_);
- ValidateLists(replicaManager);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnDownloadFromThisSystem(Replica3* replica3, ReplicaManager3 *replicaManager)
- {
- RakAssert(replica3);
- ValidateLists(replicaManager);
- LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(_FILE_AND_LINE_);
- lsr->replica=replica3;
- ConstructionMode constructionMode = QueryConstructionMode();
- if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
- {
- unsigned int j;
- for (j=0; j < queryToConstructReplicaList.Size(); j++)
- {
- if (queryToConstructReplicaList[j]->replica==replica3 )
- {
- queryToConstructReplicaList.RemoveAtIndex(j);
- break;
- }
- }
- queryToDestructReplicaList.Push(lsr,_FILE_AND_LINE_);
- }
- if (constructedReplicaList.Insert(lsr->replica, lsr, true, _FILE_AND_LINE_) != (unsigned) -1)
- {
- //assert(queryToSerializeReplicaList.GetIndexOf(replica3)==(unsigned int)-1);
- queryToSerializeReplicaList.Push(lsr,_FILE_AND_LINE_);
- }
- ValidateLists(replicaManager);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnDownloadFromOtherSystem(Replica3* replica3, ReplicaManager3 *replicaManager)
- {
- ConstructionMode constructionMode = QueryConstructionMode();
- if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
- {
- unsigned int j;
- for (j=0; j < queryToConstructReplicaList.Size(); j++)
- {
- if (queryToConstructReplicaList[j]->replica==replica3 )
- {
- return;
- }
- }
- OnLocalReference(replica3, replicaManager);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnNeverConstruct(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager)
- {
- ConstructionMode constructionMode = QueryConstructionMode();
- RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION);
- (void) constructionMode;
- ValidateLists(replicaManager);
- LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
- queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx);
- RakNet::OP_DELETE(lsr,_FILE_AND_LINE_);
- ValidateLists(replicaManager);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnConstructToThisConnection(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager)
- {
- ConstructionMode constructionMode = QueryConstructionMode();
- RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION);
- (void) constructionMode;
- ValidateLists(replicaManager);
- LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
- queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx);
- //assert(constructedReplicaList.GetIndexOf(lsr->replica)==(unsigned int)-1);
- constructedReplicaList.Insert(lsr->replica,lsr,true,_FILE_AND_LINE_);
- //assert(queryToDestructReplicaList.GetIndexOf(lsr->replica)==(unsigned int)-1);
- queryToDestructReplicaList.Push(lsr,_FILE_AND_LINE_);
- //assert(queryToSerializeReplicaList.GetIndexOf(lsr->replica)==(unsigned int)-1);
- queryToSerializeReplicaList.Push(lsr,_FILE_AND_LINE_);
- ValidateLists(replicaManager);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnConstructToThisConnection(Replica3 *replica, ReplicaManager3 *replicaManager)
- {
- RakAssert(replica);
- RakAssert(QueryConstructionMode()==QUERY_CONNECTION_FOR_REPLICA_LIST);
- (void) replicaManager;
- LastSerializationResult* lsr=RakNet::OP_NEW<LastSerializationResult>(_FILE_AND_LINE_);
- lsr->replica=replica;
- constructedReplicaList.Insert(replica,lsr,true,_FILE_AND_LINE_);
- queryToSerializeReplicaList.Push(lsr,_FILE_AND_LINE_);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnNeverSerialize(LastSerializationResult *lsr, ReplicaManager3 *replicaManager)
- {
- ValidateLists(replicaManager);
- unsigned int j;
- for (j=0; j < queryToSerializeReplicaList.Size(); j++)
- {
- if (queryToSerializeReplicaList[j]==lsr )
- {
- queryToSerializeReplicaList.RemoveAtIndex(j);
- break;
- }
- }
- ValidateLists(replicaManager);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnReplicaAlreadyExists(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager)
- {
- ConstructionMode constructionMode = QueryConstructionMode();
- RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION);
- (void) constructionMode;
- ValidateLists(replicaManager);
- LastSerializationResult* lsr = queryToConstructReplicaList[queryToConstructIdx];
- queryToConstructReplicaList.RemoveAtIndex(queryToConstructIdx);
- //assert(constructedReplicaList.GetIndexOf(lsr->replica)==(unsigned int)-1);
- constructedReplicaList.Insert(lsr->replica,lsr,true,_FILE_AND_LINE_);
- //assert(queryToDestructReplicaList.GetIndexOf(lsr->replica)==(unsigned int)-1);
- queryToDestructReplicaList.Push(lsr,_FILE_AND_LINE_);
- //assert(queryToSerializeReplicaList.GetIndexOf(lsr->replica)==(unsigned int)-1);
- queryToSerializeReplicaList.Push(lsr,_FILE_AND_LINE_);
- ValidateLists(replicaManager);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnDownloadExisting(Replica3* replica3, ReplicaManager3 *replicaManager)
- {
- ValidateLists(replicaManager);
- ConstructionMode constructionMode = QueryConstructionMode();
- if (constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION)
- {
- unsigned int idx;
- for (idx=0; idx < queryToConstructReplicaList.Size(); idx++)
- {
- if (queryToConstructReplicaList[idx]->replica==replica3)
- {
- OnConstructToThisConnection(idx, replicaManager);
- return;
- }
- }
- }
- else
- {
- OnConstructToThisConnection(replica3, replicaManager);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnSendDestructionFromQuery(unsigned int queryToDestructIdx, ReplicaManager3 *replicaManager)
- {
- ConstructionMode constructionMode = QueryConstructionMode();
- RakAssert(constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION || constructionMode==QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION);
- (void) constructionMode;
- ValidateLists(replicaManager);
- LastSerializationResult* lsr = queryToDestructReplicaList[queryToDestructIdx];
- queryToDestructReplicaList.RemoveAtIndex(queryToDestructIdx);
- unsigned int j;
- for (j=0; j < queryToSerializeReplicaList.Size(); j++)
- {
- if (queryToSerializeReplicaList[j]->replica==lsr->replica )
- {
- queryToSerializeReplicaList.RemoveAtIndex(j);
- break;
- }
- }
- for (j=0; j < constructedReplicaList.Size(); j++)
- {
- if (constructedReplicaList[j]->replica==lsr->replica )
- {
- constructedReplicaList.RemoveAtIndex(j);
- break;
- }
- }
- //assert(queryToConstructReplicaList.GetIndexOf(lsr->replica)==(unsigned int)-1);
- queryToConstructReplicaList.Push(lsr,_FILE_AND_LINE_);
- ValidateLists(replicaManager);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::OnDoNotQueryDestruction(unsigned int queryToDestructIdx, ReplicaManager3 *replicaManager)
- {
- ValidateLists(replicaManager);
- queryToDestructReplicaList.RemoveAtIndex(queryToDestructIdx);
- ValidateLists(replicaManager);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::ValidateLists(ReplicaManager3 *replicaManager) const
- {
- (void) replicaManager;
- /*
- #ifdef _DEBUG
- // Each object should exist only once in either constructedReplicaList or queryToConstructReplicaList
- // replicaPointer from LastSerializationResult should be same among all lists
- unsigned int idx, idx2;
- for (idx=0; idx < constructedReplicaList.Size(); idx++)
- {
- idx2=queryToConstructReplicaList.GetIndexOf(constructedReplicaList[idx]->replica);
- if (idx2!=(unsigned int)-1)
- {
- int a=5;
- assert(a==0);
- int *b=0;
- *b=5;
- }
- }
- for (idx=0; idx < queryToConstructReplicaList.Size(); idx++)
- {
- idx2=constructedReplicaList.GetIndexOf(queryToConstructReplicaList[idx]->replica);
- if (idx2!=(unsigned int)-1)
- {
- int a=5;
- assert(a==0);
- int *b=0;
- *b=5;
- }
- }
- LastSerializationResult *lsr, *lsr2;
- for (idx=0; idx < constructedReplicaList.Size(); idx++)
- {
- lsr=constructedReplicaList[idx];
- idx2=queryToSerializeReplicaList.GetIndexOf(lsr->replica);
- if (idx2!=(unsigned int)-1)
- {
- lsr2=queryToSerializeReplicaList[idx2];
- if (lsr2!=lsr)
- {
- int a=5;
- assert(a==0);
- int *b=0;
- *b=5;
- }
- }
- idx2=queryToDestructReplicaList.GetIndexOf(lsr->replica);
- if (idx2!=(unsigned int)-1)
- {
- lsr2=queryToDestructReplicaList[idx2];
- if (lsr2!=lsr)
- {
- int a=5;
- assert(a==0);
- int *b=0;
- *b=5;
- }
- }
- }
- for (idx=0; idx < queryToConstructReplicaList.Size(); idx++)
- {
- lsr=queryToConstructReplicaList[idx];
- idx2=queryToSerializeReplicaList.GetIndexOf(lsr->replica);
- if (idx2!=(unsigned int)-1)
- {
- lsr2=queryToSerializeReplicaList[idx2];
- if (lsr2!=lsr)
- {
- int a=5;
- assert(a==0);
- int *b=0;
- *b=5;
- }
- }
- idx2=queryToDestructReplicaList.GetIndexOf(lsr->replica);
- if (idx2!=(unsigned int)-1)
- {
- lsr2=queryToDestructReplicaList[idx2];
- if (lsr2!=lsr)
- {
- int a=5;
- assert(a==0);
- int *b=0;
- *b=5;
- }
- }
- }
- // Verify pointer integrity
- for (idx=0; idx < constructedReplicaList.Size(); idx++)
- {
- if (constructedReplicaList[idx]->replica->replicaManager!=replicaManager)
- {
- int a=5;
- assert(a==0);
- int *b=0;
- *b=5;
- }
- }
- // Verify pointer integrity
- for (idx=0; idx < queryToConstructReplicaList.Size(); idx++)
- {
- if (queryToConstructReplicaList[idx]->replica->replicaManager!=replicaManager)
- {
- int a=5;
- assert(a==0);
- int *b=0;
- *b=5;
- }
- }
- #endif
- */
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::SendConstruction(DataStructures::List<Replica3*> &newObjects, DataStructures::List<Replica3*> &deletedObjects, PRO sendParameters, RakNet::RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager3)
- {
- if (newObjects.Size()==0 && deletedObjects.Size()==0)
- return;
- // All construction and destruction takes place in the same network message
- // Otherwise, if objects rely on each other being created the same tick to be valid, this won't always be true
- // DataStructures::List<LastSerializationResult* > serializedObjects;
- BitSize_t offsetStart, offsetStart2, offsetEnd;
- unsigned int newListIndex, oldListIndex;
- RakNet::BitStream bsOut;
- NetworkID networkId;
- if (isFirstConstruction)
- {
- bsOut.Write((MessageID)ID_REPLICA_MANAGER_DOWNLOAD_STARTED);
- bsOut.Write(worldId);
- SerializeOnDownloadStarted(&bsOut);
- rakPeer->Send(&bsOut,sendParameters.priority,RELIABLE_ORDERED,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
- }
- // LastSerializationResult* lsr;
- bsOut.Reset();
- bsOut.Write((MessageID)ID_REPLICA_MANAGER_CONSTRUCTION);
- bsOut.Write(worldId);
- uint16_t objectSize = (uint16_t) newObjects.Size();
- bsOut.Write(objectSize);
- // Construction
- for (newListIndex=0; newListIndex < newObjects.Size(); newListIndex++)
- {
- offsetStart=bsOut.GetWriteOffset();
- bsOut.Write(offsetStart); // overwritten to point to the end of the stream
- networkId=newObjects[newListIndex]->GetNetworkID();
- bsOut.Write(networkId);
- RM3ConstructionState cs = newObjects[newListIndex]->QueryConstruction(this, replicaManager3);
- bool actuallyCreateObject = cs==RM3CS_SEND_CONSTRUCTION;
- bsOut.Write(actuallyCreateObject);
- bsOut.AlignWriteToByteBoundary();
- if (actuallyCreateObject)
- {
- // Actually create the object
- bsOut.Write(newObjects[newListIndex]->creatingSystemGUID);
- offsetStart2=bsOut.GetWriteOffset();
- bsOut.Write(offsetStart2); // overwritten to point to after the call to WriteAllocationID
- bsOut.AlignWriteToByteBoundary(); // Give the user an aligned bitStream in case they use memcpy
- newObjects[newListIndex]->WriteAllocationID(this, &bsOut);
- bsOut.AlignWriteToByteBoundary(); // Give the user an aligned bitStream in case they use memcpy
- offsetEnd=bsOut.GetWriteOffset();
- bsOut.SetWriteOffset(offsetStart2);
- bsOut.Write(offsetEnd);
- bsOut.SetWriteOffset(offsetEnd);
- newObjects[newListIndex]->SerializeConstruction(&bsOut, this);
- }
- else
- {
- newObjects[newListIndex]->SerializeConstructionExisting(&bsOut, this);
- }
- bsOut.AlignWriteToByteBoundary();
- offsetEnd=bsOut.GetWriteOffset();
- bsOut.SetWriteOffset(offsetStart);
- bsOut.Write(offsetEnd);
- bsOut.SetWriteOffset(offsetEnd);
- }
- RakNet::BitStream bsOut2;
- for (newListIndex=0; newListIndex < newObjects.Size(); newListIndex++)
- {
- bsOut2.Reset();
- RM3ConstructionState cs = newObjects[newListIndex]->QueryConstruction(this, replicaManager3);
- if (cs==RM3CS_SEND_CONSTRUCTION)
- {
- newObjects[newListIndex]->PostSerializeConstruction(&bsOut2, this);
- }
- else
- {
- RakAssert(cs==RM3CS_ALREADY_EXISTS_REMOTELY);
- newObjects[newListIndex]->PostSerializeConstructionExisting(&bsOut2, this);
- }
- if (bsOut2.GetNumberOfBitsUsed()>0)
- {
- bsOut.Write(true);
- bsOut.AlignWriteToByteBoundary();
- offsetStart=bsOut.GetWriteOffset();
- bsOut.Write(offsetStart); // overwritten to point to the end of the stream
- networkId=newObjects[newListIndex]->GetNetworkID();
- bsOut.Write(networkId);
- bsOut.AlignWriteToByteBoundary(); // Give the user an aligned bitStream in case they use memcpy
- bsOut.Write(&bsOut2);
- bsOut.AlignWriteToByteBoundary(); // Give the user an aligned bitStream in case they use memcpy
- offsetEnd=bsOut.GetWriteOffset();
- bsOut.SetWriteOffset(offsetStart);
- bsOut.Write(offsetEnd);
- bsOut.SetWriteOffset(offsetEnd);
- }
- else
- bsOut.Write(false);
- }
- bsOut.AlignWriteToByteBoundary();
- // Destruction
- objectSize = (uint16_t) deletedObjects.Size();
- bsOut.Write(objectSize);
- for (oldListIndex=0; oldListIndex < deletedObjects.Size(); oldListIndex++)
- {
- networkId=deletedObjects[oldListIndex]->GetNetworkID();
- bsOut.Write(networkId);
- offsetStart=bsOut.GetWriteOffset();
- bsOut.Write(offsetStart);
- deletedObjects[oldListIndex]->deletingSystemGUID=rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
- bsOut.Write(deletedObjects[oldListIndex]->deletingSystemGUID);
- deletedObjects[oldListIndex]->SerializeDestruction(&bsOut, this);
- bsOut.AlignWriteToByteBoundary();
- offsetEnd=bsOut.GetWriteOffset();
- bsOut.SetWriteOffset(offsetStart);
- bsOut.Write(offsetEnd);
- bsOut.SetWriteOffset(offsetEnd);
- }
- rakPeer->Send(&bsOut,sendParameters.priority,RELIABLE_ORDERED,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
- // TODO - shouldn't this be part of construction?
- // Initial Download serialize to a new system
- // Immediately send serialize after construction if the replica object already has saved data
- // If the object was serialized identically, and does not change later on, then the new connection never gets the data
- SerializeParameters sp;
- sp.whenLastSerialized=0;
- RakNet::BitStream emptyBs;
- for (int index=0; index < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; index++)
- {
- sp.lastSentBitstream[index]=&emptyBs;
- sp.pro[index]=sendParameters;
- sp.pro[index].reliability=RELIABLE_ORDERED;
- }
- sp.bitsWrittenSoFar=0;
- // RakNet::Time t = RakNet::GetTimeMS();
- for (newListIndex=0; newListIndex < newObjects.Size(); newListIndex++)
- {
- sp.destinationConnection=this;
- sp.messageTimestamp=0;
- RakNet::Replica3 *replica = newObjects[newListIndex];
- // 8/22/09 Forgot ResetWritePointer
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- sp.outputBitstream[z].ResetWritePointer();
- }
- RM3SerializationResult res = replica->Serialize(&sp);
- if (res!=RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION &&
- res!=RM3SR_DO_NOT_SERIALIZE &&
- res!=RM3SR_SERIALIZED_UNIQUELY)
- {
- bool allIndices[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
- for (int z=0; z < RM3_NUM_OUTPUT_BITSTREAM_CHANNELS; z++)
- {
- sp.bitsWrittenSoFar+=sp.outputBitstream[z].GetNumberOfBitsUsed();
- allIndices[z]=true;
- }
- SendSerialize(replica, allIndices, sp.outputBitstream, sp.messageTimestamp, sp.pro, rakPeer, worldId, GetTime());
- /// newObjects[newListIndex]->whenLastSerialized=t;
- }
- // else wait for construction request accepted before serializing
- }
- if (isFirstConstruction)
- {
- bsOut.Reset();
- bsOut.Write((MessageID)ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE);
- bsOut.Write(worldId);
- SerializeOnDownloadComplete(&bsOut);
- rakPeer->Send(&bsOut,sendParameters.priority,RELIABLE_ORDERED,sendParameters.orderingChannel,systemAddress,false,sendParameters.sendReceipt);
- }
- isFirstConstruction=false;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Connection_RM3::SendValidation(RakNet::RakPeerInterface *rakPeer, WorldId worldId)
- {
- // Hijack to mean sendValidation
- RakNet::BitStream bsOut;
- bsOut.Write((MessageID)ID_REPLICA_MANAGER_SCOPE_CHANGE);
- bsOut.Write(worldId);
- rakPeer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,systemAddress,false);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Replica3::Replica3()
- {
- creatingSystemGUID=UNASSIGNED_RAKNET_GUID;
- deletingSystemGUID=UNASSIGNED_RAKNET_GUID;
- replicaManager=0;
- forceSendUntilNextUpdate=false;
- lsr=0;
- referenceIndex = (uint32_t)-1;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Replica3::~Replica3()
- {
- if (replicaManager)
- {
- replicaManager->Dereference(this);
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- void Replica3::BroadcastDestruction(void)
- {
- replicaManager->BroadcastDestruction(this,UNASSIGNED_SYSTEM_ADDRESS);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RakNetGUID Replica3::GetCreatingSystemGUID(void) const
- {
- return creatingSystemGUID;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3ConstructionState Replica3::QueryConstruction_ClientConstruction(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer)
- {
- (void) destinationConnection;
- if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
- return RM3CS_SEND_CONSTRUCTION;
- // Send back to the owner client too, because they couldn't assign the network ID
- if (isThisTheServer)
- return RM3CS_SEND_CONSTRUCTION;
- return RM3CS_NEVER_CONSTRUCT;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool Replica3::QueryRemoteConstruction_ClientConstruction(RakNet::Connection_RM3 *sourceConnection, bool isThisTheServer)
- {
- (void) sourceConnection;
- (void) isThisTheServer;
- // OK to create
- return true;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3ConstructionState Replica3::QueryConstruction_ServerConstruction(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer)
- {
- (void) destinationConnection;
- if (isThisTheServer)
- return RM3CS_SEND_CONSTRUCTION;
- return RM3CS_NEVER_CONSTRUCT;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool Replica3::QueryRemoteConstruction_ServerConstruction(RakNet::Connection_RM3 *sourceConnection, bool isThisTheServer)
- {
- (void) sourceConnection;
- if (isThisTheServer)
- return false;
- return true;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3ConstructionState Replica3::QueryConstruction_PeerToPeer(RakNet::Connection_RM3 *destinationConnection, Replica3P2PMode p2pMode)
- {
- (void) destinationConnection;
- if (p2pMode==R3P2PM_SINGLE_OWNER)
- {
- // We send to all, others do nothing
- if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
- return RM3CS_SEND_CONSTRUCTION;
- // RM3CS_NEVER_CONSTRUCT will not send the object, and will not Serialize() it
- return RM3CS_NEVER_CONSTRUCT;
- }
- else if (p2pMode==R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE)
- {
- return RM3CS_SEND_CONSTRUCTION;
- }
- else if (p2pMode==R3P2PM_STATIC_OBJECT_CURRENTLY_AUTHORITATIVE)
- {
- return RM3CS_ALREADY_EXISTS_REMOTELY;
- }
- else if (p2pMode==R3P2PM_STATIC_OBJECT_NOT_CURRENTLY_AUTHORITATIVE)
- {
- return RM3CS_ALREADY_EXISTS_REMOTELY_DO_NOT_CONSTRUCT;
- }
- else
- {
- RakAssert(p2pMode==R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE);
- // RM3CS_ALREADY_EXISTS_REMOTELY will not send the object, but WILL call QuerySerialization() and Serialize() on it.
- return RM3CS_ALREADY_EXISTS_REMOTELY;
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- bool Replica3::QueryRemoteConstruction_PeerToPeer(RakNet::Connection_RM3 *sourceConnection)
- {
- (void) sourceConnection;
- return true;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3QuerySerializationResult Replica3::QuerySerialization_ClientSerializable(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer)
- {
- // Owner client sends to all
- if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
- return RM3QSR_CALL_SERIALIZE;
- // Server sends to all but owner client
- if (isThisTheServer && destinationConnection->GetRakNetGUID()!=creatingSystemGUID)
- return RM3QSR_CALL_SERIALIZE;
- // Remote clients do not send
- return RM3QSR_NEVER_CALL_SERIALIZE;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3QuerySerializationResult Replica3::QuerySerialization_ServerSerializable(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer)
- {
- (void) destinationConnection;
- // Server sends to all
- if (isThisTheServer)
- return RM3QSR_CALL_SERIALIZE;
- // Clients do not send
- return RM3QSR_NEVER_CALL_SERIALIZE;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3QuerySerializationResult Replica3::QuerySerialization_PeerToPeer(RakNet::Connection_RM3 *destinationConnection, Replica3P2PMode p2pMode)
- {
- (void) destinationConnection;
- if (p2pMode==R3P2PM_SINGLE_OWNER)
- {
- // Owner peer sends to all
- if (creatingSystemGUID==replicaManager->GetRakPeerInterface()->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
- return RM3QSR_CALL_SERIALIZE;
- // Remote peers do not send
- return RM3QSR_NEVER_CALL_SERIALIZE;
- }
- else if (p2pMode==R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE)
- {
- return RM3QSR_CALL_SERIALIZE;
- }
- else if (p2pMode==R3P2PM_STATIC_OBJECT_CURRENTLY_AUTHORITATIVE)
- {
- return RM3QSR_CALL_SERIALIZE;
- }
- else if (p2pMode==R3P2PM_STATIC_OBJECT_NOT_CURRENTLY_AUTHORITATIVE)
- {
- return RM3QSR_DO_NOT_CALL_SERIALIZE;
- }
- else
- {
- RakAssert(p2pMode==R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE);
- return RM3QSR_DO_NOT_CALL_SERIALIZE;
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_Client(RakNet::Connection_RM3 *droppedConnection) const
- {
- (void) droppedConnection;
- return RM3AOPC_DELETE_REPLICA;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_Server(RakNet::Connection_RM3 *droppedConnection) const
- {
- (void) droppedConnection;
- return RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- RM3ActionOnPopConnection Replica3::QueryActionOnPopConnection_PeerToPeer(RakNet::Connection_RM3 *droppedConnection) const
- {
- (void) droppedConnection;
- return RM3AOPC_DELETE_REPLICA;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- #endif // _RAKNET_SUPPORT_*
|