ReplicaManager3.h 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. /// \file
  11. /// \brief Contains the third iteration of the ReplicaManager class.
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_ReplicaManager3==1
  15. #ifndef __REPLICA_MANAGER_3
  16. #define __REPLICA_MANAGER_3
  17. #include "RakNetTypes.h"
  18. #include "RakNetTime.h"
  19. #include "BitStream.h"
  20. #include "PacketPriority.h"
  21. #include "PluginInterface2.h"
  22. #include "NetworkIDObject.h"
  23. #include "DS_OrderedList.h"
  24. #include "DS_Queue.h"
  25. /// \defgroup REPLICA_MANAGER_GROUP3 ReplicaManager3
  26. /// \brief Third implementation of object replication
  27. /// \details
  28. /// \ingroup PLUGINS_GROUP
  29. namespace RakNet
  30. {
  31. class Connection_RM3;
  32. class Replica3;
  33. /// \ingroup REPLICA_MANAGER_GROUP3
  34. /// Used for multiple worlds. World 0 is created automatically by default
  35. typedef uint8_t WorldId;
  36. /// \internal
  37. /// \ingroup REPLICA_MANAGER_GROUP3
  38. struct PRO
  39. {
  40. /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketPriority().
  41. PacketPriority priority;
  42. /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketReliability().
  43. PacketReliability reliability;
  44. /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultOrderingChannel().
  45. char orderingChannel;
  46. /// Passed to RakPeerInterface::Send(). Defaults to 0.
  47. uint32_t sendReceipt;
  48. bool operator==( const PRO& right ) const;
  49. bool operator!=( const PRO& right ) const;
  50. };
  51. /// \brief System to help automate game object construction, destruction, and serialization
  52. /// \details ReplicaManager3 tracks your game objects and automates the networking for replicating them across the network<BR>
  53. /// As objects are created, destroyed, or serialized differently, those changes are pushed out to other systems.<BR>
  54. /// To use:<BR>
  55. /// <OL>
  56. /// <LI>Derive from Connection_RM3 and implement Connection_RM3::AllocReplica(). This is a factory function where given a user-supplied identifier for a class (such as name) return an instance of that class. Should be able to return any networked object in your game.
  57. /// <LI>Derive from ReplicaManager3 and implement AllocConnection() and DeallocConnection() to return the class you created in step 1.
  58. /// <LI>Derive your networked game objects from Replica3. All pure virtuals have to be implemented, however defaults are provided for Replica3::QueryConstruction(), Replica3::QueryRemoteConstruction(), and Replica3::QuerySerialization() depending on your network architecture.
  59. /// <LI>When a new game object is created on the local system, pass it to ReplicaManager3::Reference().
  60. /// <LI>When a game object is destroyed on the local system, and you want other systems to know about it, call Replica3::BroadcastDestruction()
  61. /// </OL>
  62. /// <BR>
  63. /// At this point, all new connections will automatically download, get construction messages, get destruction messages, and update serialization automatically.
  64. /// \ingroup REPLICA_MANAGER_GROUP3
  65. class RAK_DLL_EXPORT ReplicaManager3 : public PluginInterface2
  66. {
  67. public:
  68. ReplicaManager3();
  69. virtual ~ReplicaManager3();
  70. /// \brief Implement to return a game specific derivation of Connection_RM3
  71. /// \details The connection object represents a remote system connected to you that is using the ReplicaManager3 system.<BR>
  72. /// It has functions to perform operations per-connection.<BR>
  73. /// AllocConnection() and DeallocConnection() are factory functions to create and destroy instances of the connection object.<BR>
  74. /// It is used if autoCreate is true via SetAutoManageConnections() (true by default). Otherwise, the function is not called, and you will have to call PushConnection() manually<BR>
  75. /// \note If you do not want a new network connection to immediately download game objects, SetAutoManageConnections() and PushConnection() are how you do this.
  76. /// \sa SetAutoManageConnections()
  77. /// \param[in] systemAddress Address of the system you are adding
  78. /// \param[in] rakNetGUID GUID of the system you are adding. See Packet::rakNetGUID or RakPeerInterface::GetGUIDFromSystemAddress()
  79. /// \return The new connection instance.
  80. virtual Connection_RM3* AllocConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID) const=0;
  81. /// \brief Implement to destroy a class instanced returned by AllocConnection()
  82. /// \details Most likely just implement as {delete connection;}<BR>
  83. /// It is used if autoDestroy is true via SetAutoManageConnections() (true by default). Otherwise, the function is not called and you would then be responsible for deleting your own connection objects.
  84. /// \param[in] connection The pointer instance to delete
  85. virtual void DeallocConnection(Connection_RM3 *connection) const=0;
  86. /// \brief Enable or disable automatically assigning connections to new instances of Connection_RM3
  87. /// \details ReplicaManager3 can automatically create and/or destroy Connection_RM3 as systems connect or disconnect from RakPeerInterface.<BR>
  88. /// By default this is on, to make the system easier to learn and setup.<BR>
  89. /// If you don't want all connections to take part in the game, or you want to delay when a connection downloads the game, set \a autoCreate to false.<BR>
  90. /// If you want to delay deleting a connection that has dropped, set \a autoDestroy to false. If you do this, then you must call PopConnection() to remove that connection from being internally tracked. You'll also have to delete the connection instance on your own.<BR>
  91. /// \param[in] autoCreate Automatically call ReplicaManager3::AllocConnection() for each new connection. Defaults to true. Also see AutoCreateConnectionList()
  92. /// \param[in] autoDestroy Automatically call ReplicaManager3::DeallocConnection() for each dropped connection. Defaults to true.
  93. void SetAutoManageConnections(bool autoCreate, bool autoDestroy);
  94. /// \return What was passed to the autoCreate parameter of SetAutoManageConnections()
  95. bool GetAutoCreateConnections(void) const;
  96. /// \return What was passed to the autoDestroy parameter of SetAutoManageConnections()
  97. bool GetAutoDestroyConnections(void) const;
  98. /// \brief Call AllocConnection() and PushConnection() for each connection in \a participantList
  99. /// \param[in] participantListIn The list of connections to allocate
  100. /// \param[in] participantListOut The connections allocated, if any
  101. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  102. void AutoCreateConnectionList(
  103. DataStructures::List<RakNetGUID> &participantListIn,
  104. DataStructures::List<Connection_RM3*> &participantListOut,
  105. WorldId worldId=0);
  106. /// \brief Track a new Connection_RM3 instance
  107. /// \details If \a autoCreate is false for SetAutoManageConnections(), then you need this function to add new instances of Connection_RM3 yourself.<BR>
  108. /// You don't need to track this pointer yourself, you can get it with GetConnectionAtIndex(), GetConnectionByGUID(), or GetConnectionBySystemAddress().<BR>
  109. /// \param[in] newConnection The new connection instance to track.
  110. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  111. bool PushConnection(RakNet::Connection_RM3 *newConnection, WorldId worldId=0);
  112. /// \brief Stop tracking a connection
  113. /// \details On call, for each replica returned by GetReplicasCreatedByGuid(), QueryActionOnPopConnection() will be called. Depending on the return value, this may delete the corresponding replica.<BR>
  114. /// If autoDestroy is true in the call to SetAutoManageConnections() (true by default) then this is called automatically when the connection is lost. In that case, the returned connection instance is deleted.<BR>
  115. /// \param[in] guid of the connection to get. Passed to ReplicaManager3::AllocConnection() originally.
  116. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  117. RakNet::Connection_RM3 * PopConnection(RakNetGUID guid, WorldId worldId=0);
  118. /// \brief Adds a replicated object to the system.
  119. /// \details Anytime you create a new object that derives from Replica3, and you want ReplicaManager3 to use it, pass it to Reference().<BR>
  120. /// Remote systems already connected will potentially download this object the next time ReplicaManager3::Update() is called, which happens every time you call RakPeerInterface::Receive().<BR>
  121. /// You can also call ReplicaManager3::Update() manually to send referenced objects right away
  122. /// \param[in] replica3 The object to start tracking
  123. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  124. void Reference(RakNet::Replica3 *replica3, WorldId worldId=0);
  125. /// \brief Removes a replicated object from the system.
  126. /// \details The object is not deallocated, it is up to the caller to do so.<BR>
  127. /// This is called automatically from the destructor of Replica3, so you don't need to call it manually unless you want to stop tracking an object before it is destroyed.
  128. /// \param[in] replica3 The object to stop tracking
  129. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  130. void Dereference(RakNet::Replica3 *replica3, WorldId worldId=0);
  131. /// \brief Removes multiple replicated objects from the system.
  132. /// \details Same as Dereference(), but for a list of objects.<BR>
  133. /// Useful with the lists returned by GetReplicasCreatedByGuid(), GetReplicasCreatedByMe(), or GetReferencedReplicaList().<BR>
  134. /// \param[in] replicaListIn List of objects
  135. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  136. void DereferenceList(DataStructures::List<Replica3*> &replicaListIn, WorldId worldId=0);
  137. /// \brief Returns all objects originally created by a particular system
  138. /// \details Originally created is defined as the value of Replica3::creatingSystemGUID, which is automatically assigned in ReplicaManager3::Reference().<BR>
  139. /// You do not have to be directly connected to that system to get the objects originally created by that system.<BR>
  140. /// \param[in] guid GUID of the system we are referring to. Originally passed as the \a guid parameter to ReplicaManager3::AllocConnection()
  141. /// \param[out] List of Replica3 instances to be returned
  142. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  143. void GetReplicasCreatedByGuid(RakNetGUID guid, DataStructures::List<Replica3*> &replicaListOut, WorldId worldId=0);
  144. /// \brief Returns all objects originally created by your system
  145. /// \details Calls GetReplicasCreatedByGuid() for your own system guid.
  146. /// \param[out] List of Replica3 instances to be returned
  147. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  148. void GetReplicasCreatedByMe(DataStructures::List<Replica3*> &replicaListOut, WorldId worldId=0);
  149. /// \brief Returns the entire list of Replicas that we know about.
  150. /// \details This is all Replica3 instances passed to Reference, as well as instances we downloaded and created via Connection_RM3::AllocReference()
  151. /// \param[out] List of Replica3 instances to be returned
  152. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  153. void GetReferencedReplicaList(DataStructures::List<Replica3*> &replicaListOut, WorldId worldId=0);
  154. /// \brief Returns the number of replicas known about
  155. /// \details Returns the size of the list that would be returned by GetReferencedReplicaList()
  156. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  157. /// \return How many replica objects are in the list of replica objects
  158. unsigned GetReplicaCount(WorldId worldId=0) const;
  159. /// \brief Returns a replica by index
  160. /// \details Returns one of the items in the list that would be returned by GetReferencedReplicaList()
  161. /// \param[in] index An index, from 0 to GetReplicaCount()-1.
  162. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  163. /// \return A Replica3 instance
  164. Replica3 *GetReplicaAtIndex(unsigned index, WorldId worldId=0);
  165. /// \brief Returns the number of connections
  166. /// \details Returns the number of connections added with ReplicaManager3::PushConnection(), minus the number removed with ReplicaManager3::PopConnection()
  167. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  168. /// \return The number of registered connections
  169. unsigned int GetConnectionCount(WorldId worldId=0) const;
  170. /// \brief Returns a connection pointer previously added with PushConnection()
  171. /// \param[in] index An index, from 0 to GetConnectionCount()-1.
  172. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  173. /// \return A Connection_RM3 pointer
  174. Connection_RM3* GetConnectionAtIndex(unsigned index, WorldId worldId=0) const;
  175. /// \brief Returns a connection pointer previously added with PushConnection()
  176. /// \param[in] sa The system address of the connection to return
  177. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  178. /// \return A Connection_RM3 pointer, or 0 if not found
  179. Connection_RM3* GetConnectionBySystemAddress(const SystemAddress &sa, WorldId worldId=0) const;
  180. /// \brief Returns a connection pointer previously added with PushConnection.()
  181. /// \param[in] guid The guid of the connection to return
  182. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  183. /// \return A Connection_RM3 pointer, or 0 if not found
  184. Connection_RM3* GetConnectionByGUID(RakNetGUID guid, WorldId worldId=0) const;
  185. /// \param[in] Default ordering channel to use for object creation, destruction, and serializations
  186. void SetDefaultOrderingChannel(char def);
  187. /// \param[in] Default packet priority to use for object creation, destruction, and serializations
  188. void SetDefaultPacketPriority(PacketPriority def);
  189. /// \param[in] Default packet reliability to use for object creation, destruction, and serializations
  190. void SetDefaultPacketReliability(PacketReliability def);
  191. /// \details Every \a intervalMS milliseconds, Connection_RM3::OnAutoserializeInterval() will be called.<BR>
  192. /// Defaults to 30.<BR>
  193. /// Pass with <0 to disable. Pass 0 to Serialize() every time RakPeer::Recieve() is called<BR>
  194. /// If you want to control the update interval with more granularity, use the return values from Replica3::Serialize().<BR>
  195. /// \param[in] intervalMS How frequently to autoserialize all objects. This controls the maximum number of game object updates per second.
  196. void SetAutoSerializeInterval(RakNet::Time intervalMS);
  197. /// \brief Return the connections that we think have an instance of the specified Replica3 instance
  198. /// \details This can be wrong, for example if that system locally deleted the outside the scope of ReplicaManager3, if QueryRemoteConstruction() returned false, or if DeserializeConstruction() returned false.
  199. /// \param[in] replica The replica to check against.
  200. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  201. /// \param[out] connectionsThatHaveConstructedThisReplica Populated with connection instances that we believe have \a replica allocated
  202. void GetConnectionsThatHaveReplicaConstructed(Replica3 *replica, DataStructures::List<Connection_RM3*> &connectionsThatHaveConstructedThisReplica, WorldId worldId=0);
  203. /// \brief Returns if GetDownloadWasCompleted() returns true for all connections
  204. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  205. /// \return True when all downloads have been completed
  206. bool GetAllConnectionDownloadsCompleted(WorldId worldId=0) const;
  207. /// \brief ReplicaManager3 can support multiple worlds, where each world has a separate NetworkIDManager, list of connections, replicas, etc
  208. /// A world with id 0 is created automatically. If you want multiple worlds, use this function, and ReplicaManager3::SetNetworkIDManager() to have a different NetworkIDManager instance per world
  209. /// \param[in] worldId A unique identifier for this world. User-defined
  210. void AddWorld(WorldId worldId);
  211. /// \brief Deallocate a world added with AddWorld, or the default world with id 0
  212. /// Deallocating a world will also stop tracking and updating all connections and replicas associated with that world.
  213. /// \param[in] worldId A \a worldId value previously added with AddWorld()
  214. void RemoveWorld(WorldId worldId);
  215. /// \brief Get one of the WorldId values added with AddWorld()
  216. /// \details WorldId 0 is created by default. Worlds will not necessarily be in the order added with AddWorld(). Edit RemoveWorld() changing RemoveAtIndexFast() to RemoveAtIndex() to preserve order.
  217. /// \param[in] index A value between 0 and GetWorldCount()-1
  218. /// \return One of the WorldId values added with AddWorld()
  219. WorldId GetWorldIdAtIndex(unsigned int index);
  220. /// \brief Returns the number of world id specifiers in memory, added with AddWorld() and removed with RemoveWorld()
  221. /// \return The number of worlds added
  222. unsigned int GetWorldCount(void) const;
  223. /// \details Sets the networkIDManager instance that this plugin relys upon.<BR>
  224. /// Uses whatever instance is attached to RakPeerInterface if unset.<BR>
  225. /// To support multiple worlds, you should set it to a different manager for each instance of the plugin
  226. /// \param[in] _networkIDManager The externally allocated NetworkIDManager instance for this plugin to use.
  227. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  228. void SetNetworkIDManager(NetworkIDManager *_networkIDManager, WorldId worldId=0);
  229. /// Returns what was passed to SetNetworkIDManager(), or the instance on RakPeerInterface if unset.
  230. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  231. NetworkIDManager *GetNetworkIDManager(WorldId worldId=0) const;
  232. /// \details Send a network command to destroy one or more Replica3 instances
  233. /// Usually you won't need this, but use Replica3::BroadcastDestruction() instead.
  234. /// The objects are unaffected locally
  235. /// \param[in] replicaList List of Replica3 objects to tell other systems to destroy.
  236. /// \param[in] exclusionAddress Which system to not send to. UNASSIGNED_SYSTEM_ADDRESS to send to all.
  237. /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld()
  238. void BroadcastDestructionList(DataStructures::List<Replica3*> &replicaListSource, const SystemAddress &exclusionAddress, WorldId worldId=0);
  239. /// \internal
  240. /// \details Tell other systems that have this replica to destroy this replica.<BR>
  241. /// You shouldn't need to call this, as it happens in the Replica3 destructor
  242. void BroadcastDestruction(Replica3 *replica, const SystemAddress &exclusionAddress);
  243. /// \internal
  244. /// \details Frees internal lists.<BR>
  245. /// \param[in] deleteWorlds True to also delete the worlds added with AddWorld()
  246. /// Externally allocated pointers are not deallocated
  247. void Clear(bool deleteWorlds=false);
  248. /// \internal
  249. PRO GetDefaultSendParameters(void) const;
  250. /// Call interfaces, send data
  251. virtual void Update(void);
  252. /// \internal
  253. struct RM3World
  254. {
  255. RM3World();
  256. void Clear(ReplicaManager3 *replicaManager3);
  257. DataStructures::List<Connection_RM3*> connectionList;
  258. DataStructures::List<Replica3*> userReplicaList;
  259. WorldId worldId;
  260. NetworkIDManager *networkIDManager;
  261. };
  262. protected:
  263. virtual PluginReceiveResult OnReceive(Packet *packet);
  264. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  265. virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
  266. virtual void OnRakPeerShutdown(void);
  267. virtual void OnDetach(void);
  268. PluginReceiveResult OnConstruction(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId);
  269. PluginReceiveResult OnSerialize(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, RakNet::Time timestamp, unsigned char packetDataOffset, WorldId worldId);
  270. PluginReceiveResult OnDownloadStarted(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId);
  271. PluginReceiveResult OnDownloadComplete(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId);
  272. void DeallocReplicaNoBroadcastDestruction(RakNet::Connection_RM3 *connection, RakNet::Replica3 *replica3);
  273. RakNet::Connection_RM3 * PopConnection(unsigned int index, WorldId worldId);
  274. Replica3* GetReplicaByNetworkID(NetworkID networkId, WorldId worldId);
  275. unsigned int ReferenceInternal(RakNet::Replica3 *replica3, WorldId worldId);
  276. PRO defaultSendParameters;
  277. RakNet::Time autoSerializeInterval;
  278. RakNet::Time lastAutoSerializeOccurance;
  279. bool autoCreateConnections, autoDestroyConnections;
  280. Replica3 *currentlyDeallocatingReplica;
  281. // Set on the first call to ReferenceInternal(), and should never be changed after that
  282. // Used to lookup in Replica3LSRComp. I don't want to rely on GetNetworkID() in case it changes at runtime
  283. uint32_t nextReferenceIndex;
  284. // For O(1) lookup
  285. RM3World *worldsArray[255];
  286. // For fast traversal
  287. DataStructures::List<RM3World *> worldsList;
  288. friend class Connection_RM3;
  289. };
  290. static const int RM3_NUM_OUTPUT_BITSTREAM_CHANNELS=16;
  291. /// \ingroup REPLICA_MANAGER_GROUP3
  292. struct LastSerializationResultBS
  293. {
  294. RakNet::BitStream bitStream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
  295. bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
  296. };
  297. /// Represents the serialized data for an object the last time it was sent. Used by Connection_RM3::OnAutoserializeInterval() and Connection_RM3::SendSerializeIfChanged()
  298. /// \ingroup REPLICA_MANAGER_GROUP3
  299. struct LastSerializationResult
  300. {
  301. LastSerializationResult();
  302. ~LastSerializationResult();
  303. /// The replica instance we serialized
  304. /// \note replica MUST be the first member of this struct because I cast from replica to LastSerializationResult in Update()
  305. RakNet::Replica3 *replica;
  306. //bool neverSerialize;
  307. // bool isConstructed;
  308. RakNet::Time whenLastSerialized;
  309. void AllocBS(void);
  310. LastSerializationResultBS* lastSerializationResultBS;
  311. };
  312. /// Parameters passed to Replica3::Serialize()
  313. /// \ingroup REPLICA_MANAGER_GROUP3
  314. struct SerializeParameters
  315. {
  316. /// Write your output for serialization here
  317. /// If nothing is written, the serialization will not occur
  318. /// Write to any or all of the NUM_OUTPUT_BITSTREAM_CHANNELS channels available. Channels can hold independent data
  319. RakNet::BitStream outputBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
  320. /// Last bitstream we sent for this replica to this system.
  321. /// Read, but DO NOT MODIFY
  322. RakNet::BitStream* lastSentBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
  323. /// Set to non-zero to transmit a timestamp with this message.
  324. /// Defaults to 0
  325. /// Use RakNet::GetTime() for this
  326. RakNet::Time messageTimestamp;
  327. /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketPriority().
  328. /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketReliability().
  329. /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultOrderingChannel().
  330. PRO pro[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
  331. /// Passed to RakPeerInterface::Send().
  332. RakNet::Connection_RM3 *destinationConnection;
  333. /// For prior serializations this tick, for the same connection, how many bits have we written so far?
  334. /// Use this to limit how many objects you send to update per-tick if desired
  335. BitSize_t bitsWrittenSoFar;
  336. /// When this object was last serialized to the connection
  337. /// 0 means never
  338. RakNet::Time whenLastSerialized;
  339. /// Current time, in milliseconds.
  340. /// curTime - whenLastSerialized is how long it has been since this object was last sent
  341. RakNet::Time curTime;
  342. };
  343. /// \ingroup REPLICA_MANAGER_GROUP3
  344. struct DeserializeParameters
  345. {
  346. RakNet::BitStream serializationBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
  347. bool bitstreamWrittenTo[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS];
  348. RakNet::Time timeStamp;
  349. RakNet::Connection_RM3 *sourceConnection;
  350. };
  351. /// \ingroup REPLICA_MANAGER_GROUP3
  352. enum SendSerializeIfChangedResult
  353. {
  354. SSICR_SENT_DATA,
  355. SSICR_DID_NOT_SEND_DATA,
  356. SSICR_NEVER_SERIALIZE,
  357. };
  358. /// \brief Each remote system is represented by Connection_RM3. Used to allocate Replica3 and track which instances have been allocated
  359. /// \details Important function: AllocReplica() - must be overridden to create an object given an identifier for that object, which you define for all objects in your game
  360. /// \ingroup REPLICA_MANAGER_GROUP3
  361. class RAK_DLL_EXPORT Connection_RM3
  362. {
  363. public:
  364. Connection_RM3(const SystemAddress &_systemAddress, RakNetGUID _guid);
  365. virtual ~Connection_RM3();
  366. /// \brief Class factory to create a Replica3 instance, given a user-defined identifier
  367. /// \details Identifier is returned by Replica3::WriteAllocationID() for what type of class to create.<BR>
  368. /// This is called when you download a replica from another system.<BR>
  369. /// See Replica3::Dealloc for the corresponding destruction message.<BR>
  370. /// Return 0 if unable to create the intended object. Note, in that case the other system will still think we have the object and will try to serialize object updates to us. Generally, you should not send objects the other system cannot create.<BR>
  371. /// \sa Replica3::WriteAllocationID().
  372. /// Sample implementation:<BR>
  373. /// {RakNet::RakString typeName; allocationIdBitstream->Read(typeName); if (typeName=="Soldier") return new Soldier; return 0;}<BR>
  374. /// \param[in] allocationIdBitstream user-defined bitstream uniquely identifying a game object type
  375. /// \param[in] replicaManager3 Instance of ReplicaManager3 that controls this connection
  376. /// \return The new replica instance
  377. virtual Replica3 *AllocReplica(RakNet::BitStream *allocationIdBitstream, ReplicaManager3 *replicaManager3)=0;
  378. /// \brief Get list of all replicas that are constructed for this connection
  379. /// \param[out] objectsTheyDoHave Destination list. Returned in sorted ascending order, sorted on the value of the Replica3 pointer.
  380. virtual void GetConstructedReplicas(DataStructures::List<Replica3*> &objectsTheyDoHave);
  381. /// Returns true if we think this remote connection has this replica constructed
  382. /// \param[in] replica3 Which replica we are querying
  383. /// \return True if constructed, false othewise
  384. bool HasReplicaConstructed(RakNet::Replica3 *replica);
  385. /// When a new connection connects, before sending any objects, SerializeOnDownloadStarted() is called
  386. /// \param[out] bitStream Passed to DeserializeOnDownloadStarted()
  387. virtual void SerializeOnDownloadStarted(RakNet::BitStream *bitStream) {(void) bitStream;}
  388. /// Receives whatever was written in SerializeOnDownloadStarted()
  389. /// \param[in] bitStream Written in SerializeOnDownloadStarted()
  390. virtual void DeserializeOnDownloadStarted(RakNet::BitStream *bitStream) {(void) bitStream;}
  391. /// When a new connection connects, after constructing and serialization all objects, SerializeOnDownloadComplete() is called
  392. /// \param[out] bitStream Passed to DeserializeOnDownloadComplete()
  393. virtual void SerializeOnDownloadComplete(RakNet::BitStream *bitStream) {(void) bitStream;}
  394. /// Receives whatever was written in DeserializeOnDownloadComplete()
  395. /// \param[in] bitStream Written in SerializeOnDownloadComplete()
  396. virtual void DeserializeOnDownloadComplete(RakNet::BitStream *bitStream) {(void) bitStream;}
  397. /// \return The system address passed to the constructor of this object
  398. SystemAddress GetSystemAddress(void) const {return systemAddress;}
  399. /// \return Returns the RakNetGUID passed to the constructor of this object
  400. RakNetGUID GetRakNetGUID(void) const {return guid;}
  401. /// \return True if ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE arrived for this connection
  402. bool GetDownloadWasCompleted(void) const {return gotDownloadComplete;}
  403. /// List of enumerations for how to get the list of valid objects for other systems
  404. enum ConstructionMode
  405. {
  406. /// For every object that does not exist on the remote system, call Replica3::QueryConstruction() every tick.
  407. /// Do not call Replica3::QueryDestruction()
  408. /// Do not call Connection_RM3::QueryReplicaList()
  409. QUERY_REPLICA_FOR_CONSTRUCTION,
  410. /// For every object that does not exist on the remote system, call Replica3::QueryConstruction() every tick. Based on the call, the object may be sent to the other system.
  411. /// For every object that does exist on the remote system, call Replica3::QueryDestruction() every tick. Based on the call, the object may be deleted on the other system.
  412. /// Do not call Connection_RM3::QueryReplicaList()
  413. QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION,
  414. /// Do not call Replica3::QueryConstruction() or Replica3::QueryDestruction()
  415. /// Call Connection_RM3::QueryReplicaList() to determine which objects exist on remote systems
  416. /// This can be faster than QUERY_REPLICA_FOR_CONSTRUCTION and QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION for large worlds
  417. /// See GridSectorizer.h under /Source for code that can help with this
  418. QUERY_CONNECTION_FOR_REPLICA_LIST
  419. };
  420. /// \brief Return whether or not downloads to our system should all be processed the same tick (call to RakPeer::Receive() )
  421. /// \details Normally the system will send ID_REPLICA_MANAGER_DOWNLOAD_STARTED, ID_REPLICA_MANAGER_CONSTRUCTION for all downloaded objects,
  422. /// ID_REPLICA_MANAGER_SERIALIZE for each downloaded object, and lastly ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE.
  423. /// This enables the application to show a downloading splash screen on ID_REPLICA_MANAGER_DOWNLOAD_STARTED, a progress bar, and to close the splash screen and activate all objects on ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE
  424. /// However, if the application was not set up for this then it would result in incomplete objects spread out over time, and cause problems
  425. /// If you return true from QueryGroupDownloadMessages(), then these messages will be returned all in one tick, returned only when the download is complete
  426. /// \note ID_REPLICA_MANAGER_DOWNLOAD_STARTED calls the callback DeserializeOnDownloadStarted()
  427. /// \note ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE calls the callback DeserializeOnDownloadComplete()
  428. virtual bool QueryGroupDownloadMessages(void) const {return false;}
  429. /// \brief Queries how to get the list of objects that exist on remote systems
  430. /// \details The default of calling QueryConstruction for every known object is easy to use, but not efficient, especially for large worlds where many objects are outside of the player's circle of influence.<BR>
  431. /// QueryDestruction is also not necessarily useful or efficient, as object destruction tends to happen in known cases, and can be accomplished by calling Replica3::BroadcastDestruction()
  432. /// QueryConstructionMode() allows you to specify more efficient algorithms than the default when overriden.
  433. /// \return How to get the list of objects that exist on the remote system. You should always return the same value for a given connection
  434. virtual ConstructionMode QueryConstructionMode(void) const {return QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION;}
  435. /// \brief Callback used when QueryConstructionMode() returns QUERY_CONNECTION_FOR_REPLICA_LIST
  436. /// \details This advantage of this callback is if that there are many objects that a particular connection does not have, then we do not have to iterate through those
  437. /// objects calling QueryConstruction() for each of them.<BR>
  438. ///<BR>
  439. /// See GridSectorizer in the Source directory as a method to find all objects within a certain radius in a fast way.<BR>
  440. ///<BR>
  441. /// \param[out] newReplicasToCreate Anything in this list will be created on the remote system
  442. /// \param[out] existingReplicasToDestroy Anything in this list will be destroyed on the remote system
  443. virtual void QueryReplicaList(
  444. DataStructures::List<Replica3*> &newReplicasToCreate,
  445. DataStructures::List<Replica3*> &existingReplicasToDestroy) {(void) newReplicasToCreate; (void) existingReplicasToDestroy;}
  446. /// \brief Override which replicas to serialize and in what order for a connection for a ReplicaManager3::Update() cycle
  447. /// \details By default, Connection_RM3 will iterate through queryToSerializeReplicaList and call QuerySerialization() on each Replica in that list
  448. /// queryToSerializeReplicaList is populated in the order in which ReplicaManager3::Reference() is called for those objects.
  449. /// If you write to to \a replicasToSerialize and return true, you can control in what order and for which replicas to call QuerySerialization()
  450. /// Example use case:
  451. /// We have more data to send then the bandwidth supports, so want to prioritize sends. For example enemies shooting are more important than animation effects
  452. /// When QuerySerializationList(), sort objects by priority, and write the list to \a replicasToSerialize, optionally skipping objects with a lower serialization frequency
  453. /// If you hit your bandwidth limit when checking SerializeParameters::bitsWrittenSoFar, you can return RM3SR_DO_NOT_SERIALIZE for all remaining items
  454. /// \note Only replicas written to replicasToSerialize are transmitted. Even if you returned RM3SR_SERIALIZED_ALWAYS a prior ReplicaManager3::Update() cycle, the replica will not be transmitted if it is not in replicasToSerialize
  455. /// \note If you do not know what objects are candidates for serialization, you can use queryToSerializeReplicaList as a source for your filtering or sorting operations
  456. /// \param[in] replicasToSerialize List of replicas to call QuerySerialization() on
  457. /// \return Return true to use replicasToSerialize (replicasToSerialize may be empty if desired). Otherwise return false.
  458. virtual bool QuerySerializationList(DataStructures::List<Replica3*> &replicasToSerialize) {(void) replicasToSerialize; return false;}
  459. /// \internal This is used internally - however, you can also call it manually to send a data update for a remote replica.<BR>
  460. /// \brief Sends over a serialization update for \a replica.<BR>
  461. /// NetworkID::GetNetworkID() is written automatically, serializationData is the object data.<BR>
  462. /// \param[in] replica Which replica to serialize
  463. /// \param[in] serializationData Serialized object data
  464. /// \param[in] timestamp 0 means no timestamp. Otherwise message is prepended with ID_TIMESTAMP
  465. /// \param[in] sendParameters Parameters on how to send
  466. /// \param[in] rakPeer Instance of RakPeerInterface to send on
  467. /// \param[in] worldId Which world, see ReplicaManager3::AddWorld()
  468. /// \param[in] curTime The current time
  469. virtual SendSerializeIfChangedResult 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], RakNet::RakPeerInterface *rakPeer, unsigned char worldId, RakNet::Time curTime);
  470. /// \internal
  471. /// \details Calls Connection_RM3::SendSerialize() if Replica3::Serialize() returns a different result than what is contained in \a lastSerializationResult.<BR>
  472. /// Used by autoserialization in Connection_RM3::OnAutoserializeInterval()
  473. /// \param[in] lsr Item in the queryToSerializeReplicaList
  474. /// \param[in] sp Controlling parameters over the serialization
  475. /// \param[in] rakPeer Instance of RakPeerInterface to send on
  476. /// \param[in] worldId Which world, see ReplicaManager3::AddWorld()
  477. /// \param[in] curTime The current time
  478. virtual SendSerializeIfChangedResult SendSerializeIfChanged(LastSerializationResult *lsr, SerializeParameters *sp, RakNet::RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager, RakNet::Time curTime);
  479. /// \internal
  480. /// \brief Given a list of objects that were created and destroyed, serialize and send them to another system.
  481. /// \param[in] newObjects Objects to serialize construction
  482. /// \param[in] deletedObjects Objects to serialize destruction
  483. /// \param[in] sendParameters Controlling parameters over the serialization
  484. /// \param[in] rakPeer Instance of RakPeerInterface to send on
  485. /// \param[in] worldId Which world, see ReplicaManager3::AddWorld()
  486. /// \param[in] replicaManager3 ReplicaManager3 instance
  487. virtual void SendConstruction(DataStructures::List<Replica3*> &newObjects, DataStructures::List<Replica3*> &deletedObjects, PRO sendParameters, RakNet::RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager3);
  488. /// \internal
  489. void SendValidation(RakNet::RakPeerInterface *rakPeer, WorldId worldId);
  490. /// \internal
  491. void AutoConstructByQuery(ReplicaManager3 *replicaManager3, WorldId worldId);
  492. // Internal - does the other system have this connection too? Validated means we can now use it
  493. bool isValidated;
  494. // Internal - Used to see if we should send download started
  495. bool isFirstConstruction;
  496. static int Replica3LSRComp( Replica3 * const &replica3, LastSerializationResult * const &data );
  497. // Internal
  498. void ClearDownloadGroup(RakPeerInterface *rakPeerInterface);
  499. protected:
  500. SystemAddress systemAddress;
  501. RakNetGUID guid;
  502. /*
  503. Operations:
  504. Locally reference a new replica:
  505. Add to queryToConstructReplicaList for all objects
  506. Add all objects to queryToConstructReplicaList
  507. Download:
  508. Add to constructedReplicaList for connection that send the object to us
  509. Add to queryToSerializeReplicaList for connection that send the object to us
  510. Add to queryToConstructReplicaList for all other connections
  511. Never construct for this connection:
  512. Remove from queryToConstructReplicaList
  513. Construct to this connection
  514. Remove from queryToConstructReplicaList
  515. Add to constructedReplicaList for this connection
  516. Add to queryToSerializeReplicaList for this connection
  517. Serialize:
  518. Iterate through queryToSerializeReplicaList
  519. Never serialize for this connection
  520. Remove from queryToSerializeReplicaList
  521. Reference (this system has this object already)
  522. Remove from queryToConstructReplicaList
  523. Add to constructedReplicaList for this connection
  524. Add to queryToSerializeReplicaList for this connection
  525. Downloaded an existing object
  526. if replica is in queryToConstructReplicaList, OnConstructToThisConnection()
  527. else ignore
  528. Send destruction from query
  529. Remove from queryToDestructReplicaList
  530. Remove from queryToSerializeReplicaList
  531. Remove from constructedReplicaList
  532. Add to queryToConstructReplicaList
  533. Do not query destruction again
  534. Remove from queryToDestructReplicaList
  535. */
  536. void OnLocalReference(Replica3* replica3, ReplicaManager3 *replicaManager);
  537. void OnDereference(Replica3* replica3, ReplicaManager3 *replicaManager);
  538. void OnDownloadFromThisSystem(Replica3* replica3, ReplicaManager3 *replicaManager);
  539. void OnDownloadFromOtherSystem(Replica3* replica3, ReplicaManager3 *replicaManager);
  540. void OnNeverConstruct(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager);
  541. void OnConstructToThisConnection(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager);
  542. void OnConstructToThisConnection(Replica3 *replica, ReplicaManager3 *replicaManager);
  543. void OnNeverSerialize(LastSerializationResult *lsr, ReplicaManager3 *replicaManager);
  544. void OnReplicaAlreadyExists(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager);
  545. void OnDownloadExisting(Replica3* replica3, ReplicaManager3 *replicaManager);
  546. void OnSendDestructionFromQuery(unsigned int queryToDestructIdx, ReplicaManager3 *replicaManager);
  547. void OnDoNotQueryDestruction(unsigned int queryToDestructIdx, ReplicaManager3 *replicaManager);
  548. void ValidateLists(ReplicaManager3 *replicaManager) const;
  549. void SendSerializeHeader(RakNet::Replica3 *replica, RakNet::Time timestamp, RakNet::BitStream *bs, WorldId worldId);
  550. // The list of objects that our local system and this remote system both have
  551. // Either we sent this object to them, or they sent this object to us
  552. // A given Replica can be either in queryToConstructReplicaList or constructedReplicaList but not both at the same time
  553. DataStructures::OrderedList<Replica3*, LastSerializationResult*, Connection_RM3::Replica3LSRComp> constructedReplicaList;
  554. // Objects that we have, but this system does not, and we will query each tick to see if it should be sent to them
  555. // If we do send it to them, the replica is moved to constructedReplicaList
  556. // A given Replica can be either in queryToConstructReplicaList or constructedReplicaList but not both at the same time
  557. DataStructures::List<LastSerializationResult*> queryToConstructReplicaList;
  558. // Objects that this system has constructed are added at the same time to queryToSerializeReplicaList
  559. // This list is used to serialize all objects that this system has to this connection
  560. DataStructures::List<LastSerializationResult*> queryToSerializeReplicaList;
  561. // Objects that are constructed on this system are also queried if they should be destroyed to this system
  562. DataStructures::List<LastSerializationResult*> queryToDestructReplicaList;
  563. // Working lists
  564. DataStructures::List<Replica3*> constructedReplicasCulled, destroyedReplicasCulled;
  565. // This is used if QueryGroupDownloadMessages() returns true when ID_REPLICA_MANAGER_DOWNLOAD_STARTED arrives
  566. // Packets will be gathered and not returned until ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE arrives
  567. bool groupConstructionAndSerialize;
  568. DataStructures::Queue<Packet*> downloadGroup;
  569. // Stores if we got download complete for this connection
  570. bool gotDownloadComplete;
  571. friend class ReplicaManager3;
  572. private:
  573. Connection_RM3() {};
  574. ConstructionMode constructionMode;
  575. };
  576. /// \brief Return codes for Connection_RM3::GetConstructionState() and Replica3::QueryConstruction()
  577. /// \details Indicates what state the object should be in for the remote system
  578. /// \ingroup REPLICA_MANAGER_GROUP3
  579. enum RM3ConstructionState
  580. {
  581. /// This object should exist on the remote system. Send a construction message if necessary
  582. /// If the NetworkID is already in use, it will not do anything
  583. /// If it is not in use, it will create the object, and then call DeserializeConstruction
  584. RM3CS_SEND_CONSTRUCTION,
  585. /// This object should exist on the remote system.
  586. /// The other system already has the object, and the object will never be deleted.
  587. /// This is true of objects that are loaded with the level, for example.
  588. /// Treat it as if it existed, without sending a construction message.
  589. /// Will call Serialize() and SerializeConstructionExisting() to the object on the remote system
  590. RM3CS_ALREADY_EXISTS_REMOTELY,
  591. /// Same as RM3CS_ALREADY_EXISTS_REMOTELY but does not call SerializeConstructionExisting()
  592. RM3CS_ALREADY_EXISTS_REMOTELY_DO_NOT_CONSTRUCT,
  593. /// This object will never be sent to the target system
  594. /// This object will never be serialized from this system to the target system
  595. RM3CS_NEVER_CONSTRUCT,
  596. /// Don't do anything this tick. Will query again next tick
  597. RM3CS_NO_ACTION,
  598. /// Max enum
  599. RM3CS_MAX,
  600. };
  601. /// If this object already exists for this system, should it be removed?
  602. /// \ingroup REPLICA_MANAGER_GROUP3
  603. enum RM3DestructionState
  604. {
  605. /// This object should not exist on the remote system. Send a destruction message if necessary.
  606. RM3DS_SEND_DESTRUCTION,
  607. /// This object will never be destroyed by a per-tick query. Don't call again
  608. RM3DS_DO_NOT_QUERY_DESTRUCTION,
  609. /// Don't do anything this tick. Will query again next tick
  610. RM3DS_NO_ACTION,
  611. /// Max enum
  612. RM3DS_MAX,
  613. };
  614. /// Return codes when constructing an object
  615. /// \ingroup REPLICA_MANAGER_GROUP3
  616. enum RM3SerializationResult
  617. {
  618. /// This object serializes identically no matter who we send to
  619. /// We also send it to every connection (broadcast).
  620. /// Efficient for memory, speed, and bandwidth but only if the object is always broadcast identically.
  621. RM3SR_BROADCAST_IDENTICALLY,
  622. /// Same as RM3SR_BROADCAST_IDENTICALLY, but assume the object needs to be serialized, do not check with a memcmp
  623. /// Assume the object changed, and serialize it
  624. /// Use this if you know exactly when your object needs to change. Can be faster than RM3SR_BROADCAST_IDENTICALLY.
  625. /// An example of this is if every member variable has an accessor, changing a member sets a flag, and you check that flag in Replica3::QuerySerialization()
  626. /// The opposite of this is RM3SR_DO_NOT_SERIALIZE, in case the object did not change
  627. RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION,
  628. /// Either this object serializes differently depending on who we send to or we send it to some systems and not others.
  629. /// Inefficient for memory and speed, but efficient for bandwidth
  630. /// However, if you don't know what to return, return this
  631. RM3SR_SERIALIZED_UNIQUELY,
  632. /// Do not compare against last sent value. Just send even if the data is the same as the last tick
  633. /// If the data is always changing anyway, or you want to send unreliably, this is a good method of serialization
  634. /// Can send unique data per connection if desired. If same data is sent to all connections, use RM3SR_SERIALIZED_ALWAYS_IDENTICALLY for even better performance
  635. /// Efficient for memory and speed, but not necessarily bandwidth
  636. RM3SR_SERIALIZED_ALWAYS,
  637. /// \deprecated, use RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION
  638. RM3SR_SERIALIZED_ALWAYS_IDENTICALLY,
  639. /// Do not serialize this object this tick, for this connection. Will query again next autoserialize timer
  640. RM3SR_DO_NOT_SERIALIZE,
  641. /// Never serialize this object for this connection
  642. /// Useful for objects that are downloaded, and never change again
  643. /// Efficient
  644. RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION,
  645. /// Max enum
  646. RM3SR_MAX,
  647. };
  648. /// First pass at topology to see if an object should be serialized
  649. /// \ingroup REPLICA_MANAGER_GROUP3
  650. enum RM3QuerySerializationResult
  651. {
  652. /// Call Serialize() to see if this object should be serializable for this connection
  653. RM3QSR_CALL_SERIALIZE,
  654. /// Do not call Serialize() this tick to see if this object should be serializable for this connection
  655. RM3QSR_DO_NOT_CALL_SERIALIZE,
  656. /// Never call Serialize() for this object and connection. This system will not serialize this object for this topology
  657. RM3QSR_NEVER_CALL_SERIALIZE,
  658. /// Max enum
  659. RM3QSR_MAX,
  660. };
  661. /// \ingroup REPLICA_MANAGER_GROUP3
  662. enum RM3ActionOnPopConnection
  663. {
  664. RM3AOPC_DO_NOTHING,
  665. RM3AOPC_DELETE_REPLICA,
  666. RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION,
  667. RM3AOPC_MAX,
  668. };
  669. /// \ingroup REPLICA_MANAGER_GROUP3
  670. /// Used for Replica3::QueryConstruction_PeerToPeer() and Replica3::QuerySerialization_PeerToPeer() to describe how the object replicates between hosts
  671. enum Replica3P2PMode
  672. {
  673. /// The Replica3 instance is constructed and serialized by one system only.
  674. /// Example: Your avatar. No other player serializes or can create your avatar.
  675. R3P2PM_SINGLE_OWNER,
  676. /// The Replica3 instance is constructed and/or serialized by different systems
  677. /// This system is currently in charge of construction and/or serialization
  678. /// Example: A pickup. When an avatar holds it, that avatar controls it. When it is on the ground, the host controls it.
  679. R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE,
  680. /// The Replica3 instance is constructed and/or serialized by different systems
  681. /// Another system is in charge of construction and/or serialization, but this system may be in charge at a later time
  682. /// Example: A pickup held by another player. That player sends creation of that object to new connections, and serializes it until it is dropped.
  683. R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE,
  684. /// The Replica3 instance is a static object (already exists on the remote system).
  685. /// This system is currently in charge of construction and/or serialization
  686. R3P2PM_STATIC_OBJECT_CURRENTLY_AUTHORITATIVE,
  687. /// The Replica3 instance is a static object (already exists on the remote system).
  688. /// Another system is in charge of construction and/or serialization, but this system may be in charge at a later time
  689. R3P2PM_STATIC_OBJECT_NOT_CURRENTLY_AUTHORITATIVE,
  690. };
  691. /// \brief Base class for your replicated objects for the ReplicaManager3 system.
  692. /// \details To use, derive your class, or a member of your class, from Replica3.<BR>
  693. /// \ingroup REPLICA_MANAGER_GROUP3
  694. class RAK_DLL_EXPORT Replica3 : public NetworkIDObject
  695. {
  696. public:
  697. Replica3();
  698. /// Before deleting a local instance of Replica3, call Replica3::BroadcastDestruction() for the deletion notification to go out on the network.
  699. /// It is not necessary to call ReplicaManager3::Dereference(), as this happens automatically in the destructor
  700. virtual ~Replica3();
  701. /// \brief Write a unique identifer that can be read on a remote system to create an object of this same class.
  702. /// \details The value written to \a allocationIdBitstream will be passed to Connection_RM3::AllocReplica().<BR>
  703. /// Sample implementation:<BR>
  704. /// {allocationIdBitstream->Write(RakNet::RakString("Soldier");}<BR>
  705. /// \param[out] allocationIdBitstream Bitstream for the user to write to, to identify this class
  706. virtual void WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const=0;
  707. /// \brief Ask if this object, which does not exist on \a destinationConnection should (now) be sent to that system.
  708. /// \details If ReplicaManager3::QueryConstructionMode() returns QUERY_CONNECTION_FOR_REPLICA_LIST or QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION (default),
  709. /// then QueyrConstruction() is called once per tick from ReplicaManager3::Update() to determine if an object should exist on a given system.<BR>
  710. /// Based on the return value, a network message may be sent to the other system to create the object.<BR>
  711. /// If QueryConstructionMode() is overriden to return QUERY_CONNECTION_FOR_REPLICA_LIST, this function is unused.<BR>
  712. /// \note Defaults are provided: QueryConstruction_PeerToPeer(), QueryConstruction_ServerConstruction(), QueryConstruction_ClientConstruction(). Return one of these functions for a working default for the relevant topology.
  713. /// \param[in] destinationConnection Which system we will send to
  714. /// \param[in] replicaManager3 Plugin instance for this Replica3
  715. /// \return What action to take
  716. virtual RM3ConstructionState QueryConstruction(RakNet::Connection_RM3 *destinationConnection, ReplicaManager3 *replicaManager3)=0;
  717. /// \brief Ask if this object, which does exist on \a destinationConnection should be removed from the remote system
  718. /// \details If ReplicaManager3::QueryConstructionMode() returns QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION (default),
  719. /// then QueryDestruction() is called once per tick from ReplicaManager3::Update() to determine if an object that exists on a remote system should be destroyed for a given system.<BR>
  720. /// Based on the return value, a network message may be sent to the other system to destroy the object.<BR>
  721. /// Note that you can also destroy objects with BroadcastDestruction(), so this function is not useful unless you plan to delete objects for only a particular connection.<BR>
  722. /// If QueryConstructionMode() is overriden to return QUERY_CONNECTION_FOR_REPLICA_LIST, this function is unused.<BR>
  723. /// \param[in] destinationConnection Which system we will send to
  724. /// \param[in] replicaManager3 Plugin instance for this Replica3
  725. /// \return What action to take. Only RM3CS_SEND_DESTRUCTION does anything at this time.
  726. virtual RM3DestructionState QueryDestruction(RakNet::Connection_RM3 *destinationConnection, ReplicaManager3 *replicaManager3) {(void) destinationConnection; (void) replicaManager3; return RM3DS_DO_NOT_QUERY_DESTRUCTION;}
  727. /// \brief We're about to call DeserializeConstruction() on this Replica3. If QueryRemoteConstruction() returns false, this object is deleted instead.
  728. /// \details By default, QueryRemoteConstruction_ServerConstruction() does not allow clients to create objects. The client will get Replica3::DeserializeConstructionRequestRejected().<BR>
  729. /// If you want the client to be able to potentially create objects for client/server, override accordingly.<BR>
  730. /// Other variants of QueryRemoteConstruction_* just return true.
  731. /// \note Defaults are provided: QueryRemoteConstruction_PeerToPeer(), QueryRemoteConstruction_ServerConstruction(), QueryRemoteConstruction_ClientConstruction(). Return one of these functions for a working default for the relevant topology.
  732. /// \param[in] sourceConnection Which system sent us the object creation request message.
  733. /// \return True to allow the object to pass onto DeserializeConstruction() (where it may also be rejected), false to immediately reject the remote construction request
  734. virtual bool QueryRemoteConstruction(RakNet::Connection_RM3 *sourceConnection)=0;
  735. /// \brief We got a message from a connection to destroy this replica
  736. /// Return true to automatically relay the destruction message to all our other connections
  737. /// For a client in client/server, it does not matter what this funtion returns
  738. /// For a server in client/server, this should normally return true
  739. /// For a peer in peer to peer, you can normally return false since the original destroying peer would have told all other peers about the destruction
  740. /// If a system gets a destruction command for an object that was already destroyed, the destruction message is ignored
  741. virtual bool QueryRelayDestruction(Connection_RM3 *sourceConnection) const {(void) sourceConnection; return true;}
  742. /// \brief Write data to be sent only when the object is constructed on a remote system.
  743. /// \details SerializeConstruction is used to write out data that you need to create this object in the context of your game, such as health, score, name. Use it for data you only need to send when the object is created.<BR>
  744. /// After SerializeConstruction() is called, Serialize() will be called immediately thereafter. However, they are sent in different messages, so Serialize() may arrive a later frame than SerializeConstruction()
  745. /// For that reason, the object should be valid after a call to DeserializeConstruction() for at least a short time.<BR>
  746. /// \note The object's NetworkID and allocation id are handled by the system automatically, you do not need to write these values to \a constructionBitstream
  747. /// \param[out] constructionBitstream Destination bitstream to write your data to
  748. /// \param[in] destinationConnection System that will receive this network message.
  749. virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection)=0;
  750. /// \brief Read data written by Replica3::SerializeConstruction()
  751. /// \details Reads whatever data was written to \a constructionBitstream in Replica3::SerializeConstruction()
  752. /// \param[out] constructionBitstream Bitstream written to in Replica3::SerializeConstruction()
  753. /// \param[in] sourceConnection System that sent us this network message.
  754. /// \return true to accept construction of the object. false to reject, in which case the object will be deleted via Replica3::DeallocReplica()
  755. virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection)=0;
  756. /// Same as SerializeConstruction(), but for an object that already exists on the remote system.
  757. /// Used if you return RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction
  758. virtual void SerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;};
  759. /// Same as DeserializeConstruction(), but for an object that already exists on the remote system.
  760. /// Used if you return RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction
  761. virtual void DeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;};
  762. /// \brief Write extra data to send with the object deletion event, if desired
  763. /// \details Replica3::SerializeDestruction() will be called to write any object destruction specific data you want to send with this event.
  764. /// \a destructionBitstream can be read in DeserializeDestruction()
  765. /// \param[out] destructionBitstream Bitstream for you to write to
  766. /// \param[in] destinationConnection System that will receive this network message.
  767. virtual void SerializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *destinationConnection)=0;
  768. /// \brief Read data written by Replica3::SerializeDestruction()
  769. /// \details Return true to delete the object. BroadcastDestruction() will be called automatically, followed by ReplicaManager3::Dereference.<BR>
  770. /// Return false to not delete it. If you delete it at a later point, you are responsible for calling BroadcastDestruction() yourself.
  771. virtual bool DeserializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *sourceConnection)=0;
  772. /// \brief The system is asking what to do with this replica when the connection is dropped
  773. /// \details Return QueryActionOnPopConnection_Client, QueryActionOnPopConnection_Server, or QueryActionOnPopConnection_PeerToPeer
  774. virtual RakNet::RM3ActionOnPopConnection QueryActionOnPopConnection(RakNet::Connection_RM3 *droppedConnection) const=0;
  775. /// Notification called for each of our replicas when a connection is popped
  776. virtual void OnPoppedConnection(RakNet::Connection_RM3 *droppedConnection) {(void) droppedConnection;}
  777. /// \brief Override with {delete this;}
  778. /// \details
  779. /// <OL>
  780. /// <LI>Got a remote message to delete this object which passed DeserializeDestruction(), OR
  781. /// <LI>ReplicaManager3::SetAutoManageConnections() was called autoDestroy true (which is the default setting), and a remote system that owns this object disconnected) OR
  782. /// <\OL>
  783. /// <BR>
  784. /// Override with {delete this;} to actually delete the object (and any other processing you wish).<BR>
  785. /// If you don't want to delete the object, just do nothing, however, the system will not know this. You may wish to call Dereference() if the object should no longer be networked, but remain in memory. You are responsible for deleting it yoruself later.<BR>
  786. /// destructionBitstream may be 0 if the object was deleted locally
  787. virtual void DeallocReplica(RakNet::Connection_RM3 *sourceConnection)=0;
  788. /// \brief Implement with QuerySerialization_ClientSerializable(), QuerySerialization_ServerSerializable(), or QuerySerialization_PeerToPeer()
  789. /// \details QuerySerialization() is a first pass query to check if a given object should serializable to a given system. The intent is that the user implements with one of the defaults for client, server, or peer to peer.<BR>
  790. /// Without this function, a careless implementation would serialize an object anytime it changed to all systems. This would give you feedback loops as the sender gets the same message back from the recipient it just sent to.<BR>
  791. /// If more than one system can serialize the same object then you will need to override to return true, and control the serialization result from Replica3::Serialize(). Be careful not to send back the same data to the system that just sent to you!
  792. /// \return True to allow calling Replica3::Serialize() for this connection, false to not call.
  793. virtual RakNet::RM3QuerySerializationResult QuerySerialization(RakNet::Connection_RM3 *destinationConnection)=0;
  794. /// \brief Called for each replica owned by the user, once per Serialization tick, before Serialize() is called.
  795. /// If you want to do some kind of operation on the Replica objects that you own, just before Serialization(), then overload this function
  796. virtual void OnUserReplicaPreSerializeTick(void) {}
  797. /// \brief Serialize our class to a bitstream
  798. /// \details User should implement this function to write the contents of this class to SerializationParamters::serializationBitstream.<BR>
  799. /// If data only needs to be written once, you can write it to SerializeConstruction() instead for efficiency.<BR>
  800. /// Transmitted over the network if it changed from the last time we called Serialize().<BR>
  801. /// Called every time the time interval to ReplicaManager3::SetAutoSerializeInterval() elapses and ReplicaManager3::Update is subsequently called.
  802. /// \param[in/out] serializeParameters Parameters controlling the serialization, including destination bitstream to write to
  803. /// \return Whether to serialize, and if so, how to optimize the results
  804. virtual RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters)=0;
  805. /// \brief Called when the class is actually transmitted via Serialize()
  806. /// \details Use to track how much bandwidth this class it taking
  807. virtual void OnSerializeTransmission(RakNet::BitStream *bitStream, RakNet::Connection_RM3 *destinationConnection, BitSize_t bitsPerChannel[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time curTime) {(void) bitStream; (void) destinationConnection; (void) bitsPerChannel; (void) curTime;}
  808. /// \brief Read what was written in Serialize()
  809. /// \details Reads the contents of the class from SerializationParamters::serializationBitstream.<BR>
  810. /// Called whenever Serialize() is called with different data from the last send.
  811. /// \param[in] serializationBitstream Bitstream passed to Serialize()
  812. /// \param[in] timeStamp 0 if unused, else contains the time the message originated on the remote system
  813. /// \param[in] sourceConnection Which system sent to us
  814. virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters)=0;
  815. /// \brief Called after SerializeConstruction completes for all objects in a given update tick.<BR>
  816. /// Writes to PostDeserializeConstruction(), which is called after all objects are created for a given Construction tick().
  817. /// Override to send data to PostDeserializeConstruction(), such as the NetworkID of other objects to resolve pointers to
  818. virtual void PostSerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;}
  819. /// Called after DeserializeConstruction completes for all objects in a given update tick.<BR>
  820. /// This is used to resolve dependency chains, where two objects would refer to each other in DeserializeConstruction, yet one had not been constructed yet
  821. /// In PostDeserializeConstruction(), you know that all objects have already been created, so can resolve NetworkIDs to pointers safely.
  822. /// You can also use it to trigger some sort of event when you know the object has completed deserialization.
  823. /// \param[in] constructionBitstream BitStream written in PostSerializeConstruction()
  824. /// \param[in] sourceConnection System that sent us this network message.
  825. virtual void PostDeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;}
  826. /// Same as PostSerializeConstruction(), but for objects that returned RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction
  827. virtual void PostSerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;}
  828. /// Same as PostDeserializeConstruction(), but for objects that returned RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction
  829. virtual void PostDeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;}
  830. /// Called after DeserializeDestruction completes for the object successfully, but obviously before the object is deleted.<BR>
  831. /// Override to trigger some sort of event when you know the object has completed destruction.
  832. /// \param[in] sourceConnection System that sent us this network message.
  833. virtual void PreDestruction(RakNet::Connection_RM3 *sourceConnection) {(void) sourceConnection;}
  834. /// \brief Default call for QueryConstruction().
  835. /// \details Both the client and the server is allowed to create this object. The network topology is client/server
  836. /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction()
  837. /// \param[in] isThisTheServer True if this system is the server, false if not.
  838. virtual RM3ConstructionState QueryConstruction_ClientConstruction(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer);
  839. /// Default call for QueryRemoteConstruction().
  840. /// \details Both the client and the server is allowed to create this object. The network topology is client/server
  841. /// The code means on the client or the server, allow creation of Replica3 instances
  842. /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction()
  843. /// \param[in] isThisTheServer True if this system is the server, false if not.
  844. virtual bool QueryRemoteConstruction_ClientConstruction(RakNet::Connection_RM3 *sourceConnection, bool isThisTheServer);
  845. /// \brief Default call for QueryConstruction().
  846. /// \details Only the server is allowed to create this object. The network topology is client/server
  847. /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction()
  848. /// \param[in] isThisTheServer True if this system is the server, false if not.
  849. virtual RM3ConstructionState QueryConstruction_ServerConstruction(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer);
  850. /// \brief Default call for QueryRemoteConstruction(). Allow the server to create this object, but not the client.
  851. /// \details Only the server is allowed to create this object. The network topology is client/server
  852. /// The code means if this is the server, and I got a command to create a Replica3 to ignore it. If this is the client, to allow it.
  853. /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction()
  854. /// \param[in] isThisTheServer True if this system is the server, false if not.
  855. virtual bool QueryRemoteConstruction_ServerConstruction(RakNet::Connection_RM3 *sourceConnection, bool isThisTheServer);
  856. /// \brief Default call for QueryConstruction().
  857. /// \details All clients are allowed to create all objects. The object is not relayed when remotely created
  858. /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction()
  859. /// \param[in] p2pMode If controlled only by this system ever, pass R3P2PM_SINGLE_OWNER. Otherwise pass R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE or R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE
  860. virtual RM3ConstructionState QueryConstruction_PeerToPeer(RakNet::Connection_RM3 *destinationConnection, Replica3P2PMode p2pMode=R3P2PM_SINGLE_OWNER);
  861. /// \brief Default call for QueryRemoteConstruction().
  862. /// \details All clients are allowed to create all objects. The object is not relayed when remotely created
  863. /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction()
  864. virtual bool QueryRemoteConstruction_PeerToPeer(RakNet::Connection_RM3 *sourceConnection);
  865. /// \brief Default call for QuerySerialization().
  866. /// \details Use if the values you are serializing are generated by the client that owns the object. The serialization will be relayed through the server to the other clients.
  867. /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction()
  868. /// \param[in] isThisTheServer True if this system is the server, false if not.
  869. virtual RakNet::RM3QuerySerializationResult QuerySerialization_ClientSerializable(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer);
  870. /// \brief Default call for QuerySerialization().
  871. /// \details Use if the values you are serializing are generated only by the server. The serialization will be sent to all clients, but the clients will not send back to the server.
  872. /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction()
  873. /// \param[in] isThisTheServer True if this system is the server, false if not.
  874. virtual RakNet::RM3QuerySerializationResult QuerySerialization_ServerSerializable(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer);
  875. /// \brief Default call for QuerySerialization().
  876. /// \details Use if the values you are serializing are on a peer to peer network. The peer that owns the object will send to all. Remote peers will not send.
  877. /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction()
  878. /// \param[in] p2pMode If controlled only by this system ever, pass R3P2PM_SINGLE_OWNER. Otherwise pass R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE or R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE
  879. virtual RakNet::RM3QuerySerializationResult QuerySerialization_PeerToPeer(RakNet::Connection_RM3 *destinationConnection, Replica3P2PMode p2pMode=R3P2PM_SINGLE_OWNER);
  880. /// Default: If we are a client, and the connection is lost, delete the server's objects
  881. virtual RM3ActionOnPopConnection QueryActionOnPopConnection_Client(RakNet::Connection_RM3 *droppedConnection) const;
  882. /// Default: If we are a server, and the connection is lost, delete the client's objects and broadcast the destruction
  883. virtual RM3ActionOnPopConnection QueryActionOnPopConnection_Server(RakNet::Connection_RM3 *droppedConnection) const;
  884. /// Default: If we are a peer, and the connection is lost, delete the peer's objects
  885. virtual RM3ActionOnPopConnection QueryActionOnPopConnection_PeerToPeer(RakNet::Connection_RM3 *droppedConnection) const;
  886. /// Call to send a network message to delete this object on other systems.<BR>
  887. /// Call it before deleting the object
  888. virtual void BroadcastDestruction(void);
  889. /// creatingSystemGUID is set the first time Reference() is called, or if we get the object from another system
  890. /// \return System that originally created this object
  891. RakNetGUID GetCreatingSystemGUID(void) const;
  892. /// \return If ReplicaManager3::Reference() was called on this object.
  893. bool WasReferenced(void) const {return replicaManager!=0;}
  894. /// GUID of the system that first called Reference() on this object.
  895. /// Transmitted automatically when the object is constructed
  896. RakNetGUID creatingSystemGUID;
  897. /// GUID of the system that caused the item to send a deletion command over the network
  898. RakNetGUID deletingSystemGUID;
  899. /// \internal
  900. /// ReplicaManager3 plugin associated with this object
  901. ReplicaManager3 *replicaManager;
  902. LastSerializationResultBS lastSentSerialization;
  903. bool forceSendUntilNextUpdate;
  904. LastSerializationResult *lsr;
  905. uint32_t referenceIndex;
  906. };
  907. /// \brief Use Replica3 through composition instead of inheritance by containing an instance of this templated class
  908. /// Calls to parent class for all functions
  909. /// Parent class must still define and functions though!
  910. /// \pre Parent class must call SetCompositeOwner() on this object
  911. template <class parent_type>
  912. class RAK_DLL_EXPORT Replica3Composite : public Replica3
  913. {
  914. protected:
  915. parent_type *r3CompositeOwner;
  916. public:
  917. void SetCompositeOwner(parent_type *p) {r3CompositeOwner=p;}
  918. parent_type* GetCompositeOwner(void) const {return r3CompositeOwner;};
  919. virtual void WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const {r3CompositeOwner->WriteAllocationID(destinationConnection, allocationIdBitstream);}
  920. virtual RakNet::RM3ConstructionState QueryConstruction(RakNet::Connection_RM3 *destinationConnection, RakNet::ReplicaManager3 *replicaManager3) {return r3CompositeOwner->QueryConstruction(destinationConnection, replicaManager3);}
  921. virtual RakNet::RM3DestructionState QueryDestruction(RakNet::Connection_RM3 *destinationConnection, RakNet::ReplicaManager3 *replicaManager3) {return r3CompositeOwner->QueryDestruction(destinationConnection, replicaManager3);}
  922. virtual bool QueryRemoteConstruction(RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->QueryRemoteConstruction(sourceConnection);}
  923. virtual bool QueryRelayDestruction(RakNet::Connection_RM3 *sourceConnection) const {return r3CompositeOwner->QueryRelayDestruction(sourceConnection);}
  924. virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeConstruction(constructionBitstream, destinationConnection);}
  925. virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->DeserializeConstruction(constructionBitstream, sourceConnection);}
  926. virtual void SerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeConstructionExisting(constructionBitstream, destinationConnection);}
  927. virtual void DeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->DeserializeConstructionExisting(constructionBitstream, sourceConnection);}
  928. virtual void SerializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeDestruction(destructionBitstream, destinationConnection);}
  929. virtual bool DeserializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->DeserializeDestruction(destructionBitstream, sourceConnection);}
  930. virtual RakNet::RM3ActionOnPopConnection QueryActionOnPopConnection(RakNet::Connection_RM3 *droppedConnection) const {return r3CompositeOwner->QueryActionOnPopConnection(droppedConnection);}
  931. virtual void OnPoppedConnection(RakNet::Connection_RM3 *droppedConnection) {r3CompositeOwner->OnPoppedConnection(droppedConnection);}
  932. virtual void DeallocReplica(RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->DeallocReplica(sourceConnection);}
  933. virtual RakNet::RM3QuerySerializationResult QuerySerialization(RakNet::Connection_RM3 *destinationConnection) {return r3CompositeOwner->QuerySerialization(destinationConnection);}
  934. virtual void OnUserReplicaPreSerializeTick(void) {r3CompositeOwner->OnUserReplicaPreSerializeTick();}
  935. virtual RakNet::RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters) {return r3CompositeOwner->Serialize(serializeParameters);}
  936. virtual void OnSerializeTransmission(RakNet::BitStream *bitStream, RakNet::Connection_RM3 *destinationConnection, RakNet::BitSize_t bitsPerChannel[RakNet::RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time curTime) {r3CompositeOwner->OnSerializeTransmission(bitStream, destinationConnection, bitsPerChannel, curTime);}
  937. virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters) {r3CompositeOwner->Deserialize(deserializeParameters);}
  938. virtual void PostSerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->PostSerializeConstruction(constructionBitstream, destinationConnection);}
  939. virtual void PostDeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PostDeserializeConstruction(constructionBitstream, sourceConnection);}
  940. virtual void PostSerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->PostSerializeConstructionExisting(constructionBitstream, destinationConnection);}
  941. virtual void PostDeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PostDeserializeConstructionExisting(constructionBitstream, sourceConnection);}
  942. virtual void PreDestruction(RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PreDestruction(sourceConnection);}
  943. };
  944. } // namespace RakNet
  945. #endif
  946. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号