ReplicaManager3.cpp 95 KB

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