netConnection.h 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _NETCONNECTION_H_
  23. #define _NETCONNECTION_H_
  24. #ifndef _MPOINT3_H_
  25. #include "math/mPoint3.h"
  26. #endif
  27. #ifndef _NETOBJECT_H_
  28. #include "sim/netObject.h"
  29. #endif
  30. #ifndef _NETSTRINGTABLE_H_
  31. #include "sim/netStringTable.h"
  32. #endif
  33. #ifndef _DNET_H_
  34. #include "core/dnet.h"
  35. #endif
  36. #ifndef _H_CONNECTIONSTRINGTABLE
  37. #include "sim/connectionStringTable.h"
  38. #endif
  39. class NetConnection;
  40. class NetObject;
  41. class BitStream;
  42. class ResizeBitStream;
  43. class Stream;
  44. class Point3F;
  45. struct GhostInfo;
  46. struct SubPacketRef; // defined in NetConnection subclass
  47. //#define DEBUG_NET
  48. #ifdef TORQUE_DEBUG_NET
  49. #define DEBUG_LOG(x) if(mLogging){Con::printf x;}
  50. #else
  51. #define DEBUG_LOG(x)
  52. #endif
  53. DECLARE_SCOPE( NetAPI );
  54. //----------------------------------------------------------------------------
  55. class NetEvent;
  56. struct NetEventNote
  57. {
  58. NetEvent *mEvent;
  59. S32 mSeqCount;
  60. NetEventNote *mNextEvent;
  61. };
  62. /// An event to be sent over the network.
  63. ///
  64. /// @note Torque implements two methods of network data passing; this is one of them.
  65. /// See NetConnection for details of the other, which is referred to as ghosting.
  66. ///
  67. /// Torque's network layer lets you pass events to/from the server. There are three
  68. /// types of events:
  69. /// - <b>Unguaranteed events</b> are events which are sent once. If they don't
  70. /// make it through the link, they are not resent. This is good for quick,
  71. /// frequent status updates which are of transient interest, like position
  72. /// updates or voice communication.
  73. /// - <b>Guaranteed events</b> are events which are guaranteed to be
  74. /// delivered. If they don't make it through the link, they are sent as
  75. /// needed. This is good for important, one-time information,
  76. /// like which team a user wants to play on, or the current weather.
  77. /// - <b>GuaranteedOrdered events</b> are events which are guaranteed not
  78. /// only to be delivered, but to be delivered in order. This is good for
  79. /// information which is not only important, but also order-critical, like
  80. /// chat messages.
  81. ///
  82. /// There are 6 methods that you need to implement if you want to make a
  83. /// basic NetEvent subclass, and 2 macros you need to call.
  84. ///
  85. /// @code
  86. /// // A simple NetEvent to transmit a string over the network.
  87. /// // This is based on the code in netTest.cc
  88. /// class SimpleMessageEvent : public NetEvent
  89. /// {
  90. /// typedef NetEvent Parent;
  91. /// char *msg;
  92. /// public:
  93. /// SimpleMessageEvent(const char *message = NULL);
  94. /// ~SimpleMessageEvent();
  95. ///
  96. /// virtual void pack (NetConnection *conn, BitStream *bstream);
  97. /// virtual void write (NetConnection *conn, BitStream *bstream);
  98. /// virtual void unpack (NetConnection *conn, BitStream *bstream);
  99. /// virtual void process(NetConnection *conn);
  100. ///
  101. /// DECLARE_CONOBJECT(SimpleMessageEvent);
  102. /// };
  103. ///
  104. /// IMPLEMENT_CO_NETEVENT_V1(SimpleMessageEvent);
  105. /// @endcode
  106. ///
  107. /// Notice the two macros which we call. The first, DECLARE_CONOBJECT() is there
  108. /// because we're a ConsoleObject. The second, IMPLEMENT_CO_NETEVENT_V1(), is there
  109. /// to register this event type with Torque's networking layer, so that it can be
  110. /// properly transmitted over the wire. There are three macros which you might use:
  111. /// - <b>IMPLEMENT_CO_NETEVENT_V1</b>, which indicates an event which may be sent
  112. /// in either direction, from the client to the server, or from the server to the
  113. /// client.
  114. /// - <b>IMPLEMENT_CO_CLIENTEVENT_V1</b>, which indicates an event which may only
  115. /// be sent to the client.
  116. /// - <b>IMPLEMENT_CO_SERVEREVENT_V1</b>, which indicates an event which may only
  117. /// be sent to the server.
  118. ///
  119. /// Choosing the right macro is a good way to make your game more resistant to hacking; for instance,
  120. /// PathManager events are marked as CLIENTEVENTs, because they would cause the server to crash if
  121. /// a client sent them.
  122. ///
  123. /// @note Torque allows you to call NetConnection::setLastError() on the NetConnection passed to
  124. /// your NetEvent. You can cause the connection to abort if invalid data is received, specifying
  125. /// a reason to the user.
  126. ///
  127. /// Now, the 6 methods which we have above; the constructor and destructor need only do
  128. /// whatever book-keeping is needed for your specific implementation. In our case, we
  129. /// just need to allocate/deallocate the space for our string:
  130. ///
  131. /// @code
  132. /// SimpleMessageEvent::SimpleMessageEvent(const char *message = NULL)
  133. /// {
  134. /// // If we wanted to make this not be a GuaranteedOrdered event, we'd
  135. /// // put a line like this in the constructor:
  136. /// // mGuaranteeType = Guaranteed;
  137. /// // (or whatever type you wanted.)
  138. /// if(message)
  139. /// msg = dStrdup(message);
  140. /// else
  141. /// msg = NULL;
  142. /// }
  143. ///
  144. /// SimpleMessageEvent::~SimpleMessageEvent()
  145. /// {
  146. /// dFree(msg);
  147. /// }
  148. /// @endcode
  149. ///
  150. /// Simple as that! Now, onto pack(), write(), unpack(), process().
  151. ///
  152. /// <b>pack()</b> is responsible for packing the event over the wire:
  153. ///
  154. /// @code
  155. /// void SimpleMessageEvent::pack(NetConnection* conn, BitStream *bstream)
  156. /// {
  157. /// bstream->writeString(msg);
  158. /// }
  159. /// @endcode
  160. ///
  161. /// <b>unpack()</b> is responsible for unpacking the event on the other end:
  162. ///
  163. /// @code
  164. /// // The networking layer takes care of instantiating a new
  165. /// // SimpleMessageEvent, which saves us a bit of effort.
  166. /// void SimpleMessageEvent::unpack(NetConnection *conn, BitStream *bstream)
  167. /// {
  168. /// char buf[256];
  169. /// bstream->readString(buf);
  170. /// msg = dStrdup(buf);
  171. /// }
  172. /// @endcode
  173. ///
  174. /// <b>process()</b> is called when the network layer is finished with things.
  175. /// A typical case is that a GuaranteedOrdered event is unpacked and stored, but
  176. /// not processed until the events preceding it in the sequence have also been
  177. /// dealt with.
  178. ///
  179. /// @code
  180. /// // This just prints the event in the console. You might
  181. /// // want to do something more clever here -- BJG
  182. /// void SimpleMessageEvent::process(NetConnection *conn)
  183. /// {
  184. /// Con::printf("RMSG %d %s", mSourceId, msg);
  185. /// }
  186. /// @endcode
  187. ///
  188. /// <b>write()</b> is called if a demo recording is started, and the event has not yet been
  189. /// processed, but it has been unpacked. It should be identical in its output to the bitstream
  190. /// compared to pack(), but since it is called after unpack() some lookups may not need to be
  191. /// performed. In normal demo recording, whole network packets are recorded, meaning that most
  192. /// of the time write() will not be called.
  193. ///
  194. /// In our case, it's entirely identical to pack():
  195. ///
  196. /// @code
  197. /// virtual void write(NetConnection*, BitStream *bstream)
  198. /// {
  199. /// bstream->writeString(msg);
  200. /// }
  201. /// @endcode
  202. ///
  203. /// The NetEvent is sent over the wire in a straightforward way (assuming you have a
  204. /// handle to a NetConnection):
  205. ///
  206. /// @code
  207. /// NetConnection *conn; // We assume you have filled this in.
  208. ///
  209. /// con->postNetEvent(new SimpleMessageEvent("This is a test!"));
  210. /// @endcode
  211. ///
  212. /// @see GhostAlwaysObjectEvent for an example of dissimilar write()/pack() methods.
  213. ///
  214. /// Finally, for more advanced applications, notifySent() is called whenever the event is
  215. /// sent over the wire, in NetConnection::eventWritePacket(). notifyDelivered() is called
  216. /// when the packet is finally received or (in the case of Unguaranteed packets) dropped.
  217. ///
  218. /// @note IMPLEMENT_CO_NETEVENT_V1 and co. have sibling macros which allow you to specify a
  219. /// groupMask; see ConsoleObject for a further discussion of this.
  220. class NetEvent : public ConsoleObject
  221. {
  222. public:
  223. DECLARE_ABSTRACT_CLASS( NetEvent, ConsoleObject );
  224. DECLARE_INSCOPE( NetAPI );
  225. /// @name Implementation Details
  226. ///
  227. /// These are internal fields which you won't need to manipulate, except for mGuaranteeType.
  228. /// @{
  229. ///
  230. typedef ConsoleObject Parent;
  231. enum {
  232. GuaranteedOrdered = 0,
  233. Guaranteed = 1,
  234. Unguaranteed = 2
  235. } mGuaranteeType;
  236. NetConnectionId mSourceId;
  237. void incRef()
  238. {
  239. incRefCount();
  240. }
  241. void decRef()
  242. {
  243. decRefCount();
  244. }
  245. #ifdef TORQUE_DEBUG_NET
  246. virtual const char *getDebugName();
  247. #endif
  248. /// @}
  249. /// @name Things To Subclass
  250. /// @{
  251. ///
  252. NetEvent() { mGuaranteeType = GuaranteedOrdered; }
  253. virtual ~NetEvent();
  254. virtual void write(NetConnection *ps, BitStream *bstream) = 0;
  255. virtual void pack(NetConnection *ps, BitStream *bstream) = 0;
  256. virtual void unpack(NetConnection *ps, BitStream *bstream) = 0;
  257. virtual void process(NetConnection *ps) = 0;
  258. virtual void notifySent(NetConnection *ps);
  259. virtual void notifyDelivered(NetConnection *ps, bool madeit);
  260. /// @}
  261. };
  262. #define IMPLEMENT_CO_NETEVENT_V1(className) \
  263. IMPLEMENT_CLASS( className, NULL ) \
  264. END_IMPLEMENT_CLASS; \
  265. S32 className::_smTypeId; \
  266. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  267. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  268. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  269. ConcreteClassRep<className> className::dynClassRep( #className, "Type" #className, &_smTypeId, NetClassGroupGameMask, NetClassTypeEvent, NetEventDirAny, className::getParentStaticClassRep(), &Parent::__description)
  270. #define IMPLEMENT_CO_CLIENTEVENT_V1(className) \
  271. IMPLEMENT_CLASS( className, NULL ) \
  272. END_IMPLEMENT_CLASS; \
  273. S32 className::_smTypeId; \
  274. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  275. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  276. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  277. ConcreteClassRep<className> className::dynClassRep(#className, "Type" #className, &_smTypeId,NetClassGroupGameMask, NetClassTypeEvent, NetEventDirServerToClient, className::getParentStaticClassRep(), &Parent::__description)
  278. #define IMPLEMENT_CO_SERVEREVENT_V1(className) \
  279. IMPLEMENT_CLASS( className, NULL ) \
  280. END_IMPLEMENT_CLASS; \
  281. S32 className::_smTypeId; \
  282. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  283. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  284. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  285. ConcreteClassRep<className> className::dynClassRep(#className, "Type" #className, &_smTypeId, NetClassGroupGameMask, NetClassTypeEvent, NetEventDirClientToServer, className::getParentStaticClassRep(), &Parent::__description)
  286. #define IMPLEMENT_CO_NETEVENT(className,groupMask) \
  287. IMPLEMENT_CLASS( className, NULL ) \
  288. END_IMPLEMENT_CLASS; \
  289. S32 className::_smTypeId; \
  290. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  291. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  292. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  293. ConcreteClassRep<className> className::dynClassRep(#className, "Type" #className, &_smTypeId, groupMask, NetClassTypeEvent, NetEventDirAny, className::getParentStaticClassRep(), &Parent::__description)
  294. #define IMPLEMENT_CO_CLIENTEVENT(className,groupMask) \
  295. IMPLEMENT_CLASS( className, NULL ) \
  296. END_IMPLEMENT_CLASS; \
  297. S32 className::_smTypeId; \
  298. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  299. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  300. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  301. ConcreteClassRep<className> className::dynClassRep(#className, "Type" #className, &_smTypeId, groupMask, NetClassTypeEvent, NetEventDirServerToClient, className::getParentStaticClassRep(), &Parent::__description)
  302. #define IMPLEMENT_CO_SERVEREVENT(className,groupMask) \
  303. IMPLEMENT_CLASS( className, className, __scope, NULL ) \
  304. END_IMPLEMENT_CLASS; \
  305. S32 className::_smTypeId; \
  306. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  307. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  308. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  309. ConcreteClassRep<className> className::dynClassRep(#className, "Type" #className, &_smTypeId, groupMask, NetClassTypeEvent, NetEventDirClientToServer, className::getParentStaticClassRep(), &Parent::__description)
  310. //----------------------------------------------------------------------------
  311. /// Torque network connection.
  312. ///
  313. /// @section NetConnection_intro Introduction
  314. ///
  315. /// NetConnection is the glue that binds a networked Torque game together. It combines
  316. /// the low-level notify protocol implemented in ConnectionProtocol with a SimGroup to
  317. /// provide a powerful basis for implementing a multiplayer game protocol.
  318. ///
  319. /// On top of this basis it implements several distinct subsystems:
  320. /// - <b>Event manager</b>, which is responsible for transmitting NetEvents over the wire.
  321. /// It deals with ensuring that the various types of NetEvents are delivered appropriately,
  322. /// and with notifying the event of its delivery status.
  323. /// - <b>Move manager</b>, which is responsible for transferring a Move to the server 32
  324. /// times a second (on the client) and applying it to the control object (on the server).
  325. /// - <b>Ghost manager</b>, which is responsible for doing scoping calculations (on the server
  326. /// side) and transmitting most-recent ghost information to the client.
  327. /// - <b>File transfer</b>; it is often the case that clients will lack important files when
  328. /// connecting to a server which is running a mod or new map. This subsystem allows the
  329. /// server to transfer such files to the client.
  330. /// - <b>Networked String Table</b>; string data can easily soak up network bandwidth, so for
  331. /// efficiency, we implement a networked string table. We can then notify the connection
  332. /// of strings we will reference often, such as player names, and transmit only a tag,
  333. /// instead of the whole string.
  334. /// - <b>Demo Recording</b> is also implemented in NetConnection. A demo in Torque is a log
  335. /// of the network traffic between client and server; when a NetConnection records a demo,
  336. /// it simply logs this data to a file. When it plays a demo back, it replays the logged
  337. /// data.
  338. /// - The <b>Connection Database</b> is used to keep track of all the NetConnections; it can
  339. /// be iterated over (for instance, to send an event to all active connections), or queried
  340. /// by address.
  341. ///
  342. /// @section NetConnection_events On Events
  343. ///
  344. /// The Event Manager is exposed to the outside world via postNetEvent(), which accepts NetEvents.
  345. ///
  346. /// @see NetEvent for a more thorough explanation of how to use events.
  347. ///
  348. /// @section NetConnection_ghosting On Ghosting and Scoping
  349. ///
  350. /// Ghosting is the most complex, and most powerful, part of Torque's networking capabilities. It
  351. /// allows the information sent to clients to be very precisely matched to what they need, so that
  352. /// no excess bandwidth is wasted. The control object's onCameraScopeQuery() is called, to determine
  353. /// scoping information for the client; then objects which are in scope are then transmitted to the
  354. /// client, prioritized by the results of their getPriority() method.
  355. ///
  356. /// There is a cap on the maximum number of ghosts; ghost IDs are currently sent via a 12-bit field,
  357. /// ergo, there is a cap of 4096 objects ghosted per client. This can be easily raised; see the
  358. /// GhostConstants enum.
  359. ///
  360. /// Each object ghosted is assigned a ghost ID; the client is _only_ aware of the ghost ID. This acts
  361. /// to enhance game security, as it becomes difficult to map objects from one connection to another, or
  362. /// to reliably identify objects from ID alone. IDs are also reassigned based on need, making it hard
  363. /// to track objects that have fallen out of scope (as any object which the player shouldn't see would).
  364. ///
  365. /// resolveGhost() is used on the client side, and resolveObjectFromGhostIndex() on the server side, to
  366. /// turn ghost IDs into object references.
  367. ///
  368. /// The NetConnection is a SimGroup. On the client side, it contains all the objects which have been
  369. /// ghosted to that client. On the server side, it is empty; it can be used (typically in script) to
  370. /// hold objects related to the connection. For instance, you might place an observation camera in the
  371. /// NetConnnection. In both cases, when the connection is destroyed, so are the contained objects.
  372. ///
  373. /// @see NetObject, which is the superclass for ghostable objects, and ShapeBase, which is the base
  374. /// for player and vehicle classes.
  375. ///
  376. /// @nosubgrouping
  377. class NetConnection : public SimGroup, public ConnectionProtocol
  378. {
  379. friend class NetInterface;
  380. typedef SimGroup Parent;
  381. public:
  382. /// Structure to track ghost references in packets.
  383. ///
  384. /// Every packet we send out with an update from a ghost causes one of these to be
  385. /// allocated. mask is used to track what states were sent; that way if a packet is
  386. /// dropped, we can easily manipulate the stored states and figure out what if any data
  387. /// we need to resend.
  388. ///
  389. struct GhostRef
  390. {
  391. U32 mask; ///< States we transmitted.
  392. U32 ghostInfoFlags; ///< Flags from GhostInfo::Flags
  393. GhostInfo *ghost; ///< Reference to the GhostInfo we're from.
  394. GhostRef *nextRef; ///< Next GhostRef in this packet.
  395. GhostRef *nextUpdateChain; ///< Next update we sent for this ghost.
  396. };
  397. enum Constants
  398. {
  399. HashTableSize = 127,
  400. };
  401. void sendDisconnectPacket(const char *reason);
  402. virtual bool canRemoteCreate();
  403. virtual void onTimedOut();
  404. virtual void onConnectTimedOut();
  405. virtual void onDisconnect(const char *reason);
  406. virtual void onConnectionRejected(const char *reason);
  407. virtual void onConnectionEstablished(bool isInitiator);
  408. virtual void handleStartupError(const char *errorString);
  409. virtual void writeConnectRequest(BitStream *stream);
  410. virtual bool readConnectRequest(BitStream *stream, const char **errorString);
  411. virtual void writeConnectAccept(BitStream *stream);
  412. virtual bool readConnectAccept(BitStream *stream, const char **errorString);
  413. void connect(const NetAddress *address);
  414. //----------------------------------------------------------------
  415. /// @name Global Connection List
  416. /// @{
  417. private:
  418. ///
  419. NetConnection *mNextConnection; ///< Next item in list.
  420. NetConnection *mPrevConnection; ///< Previous item in list.
  421. static NetConnection *mConnectionList; ///< Head of list.
  422. public:
  423. static NetConnection *getConnectionList() { return mConnectionList; }
  424. NetConnection *getNext() { return mNextConnection; }
  425. /// @}
  426. //----------------------------------------------------------------
  427. enum NetConnectionFlags
  428. {
  429. ConnectionToServer = BIT(0),
  430. ConnectionToClient = BIT(1),
  431. LocalClientConnection = BIT(2),
  432. NetworkConnection = BIT(3),
  433. };
  434. private:
  435. BitSet32 mTypeFlags;
  436. U32 mNetClassGroup; ///< The NetClassGroup of this connection.
  437. /// @name Statistics
  438. /// @{
  439. /// Last time a packet was sent in milliseconds.
  440. /// @see Platform::getVirtualMilliseconds()
  441. U32 mLastUpdateTime;
  442. F32 mRoundTripTime;
  443. F32 mPacketLoss;
  444. U32 mSimulatedPing;
  445. F32 mSimulatedPacketLoss;
  446. /// @}
  447. /// @name State
  448. /// @{
  449. U32 mProtocolVersion;
  450. U32 mSendDelayCredit;
  451. U32 mConnectSequence;
  452. U32 mAddressDigest[4];
  453. bool mEstablished;
  454. bool mMissionPathsSent;
  455. struct NetRate
  456. {
  457. U32 updateDelay;
  458. S32 packetSize;
  459. bool changed;
  460. };
  461. NetRate mCurRate;
  462. NetRate mMaxRate;
  463. /// If we're doing a "short circuited" connection, this stores
  464. /// a pointer to the other side.
  465. SimObjectPtr<NetConnection> mRemoteConnection;
  466. NetAddress mNetAddress;
  467. /// @}
  468. /// @name Timeout Management
  469. /// @{
  470. U32 mPingSendCount;
  471. U32 mPingRetryCount;
  472. U32 mLastPingSendTime;
  473. /// @}
  474. /// @name Connection Table
  475. ///
  476. /// We store our connections on a hash table so we can
  477. /// quickly find them.
  478. /// @{
  479. NetConnection *mNextTableHash;
  480. static NetConnection *mHashTable[HashTableSize];
  481. /// @}
  482. protected:
  483. static SimObjectPtr<NetConnection> mServerConnection;
  484. static SimObjectPtr<NetConnection> mLocalClientConnection;
  485. static bool mFilesWereDownloaded;
  486. U32 mConnectSendCount;
  487. U32 mConnectLastSendTime;
  488. SimObjectPtr<NetConnection> getRemoteConnection() { return mRemoteConnection; }
  489. public:
  490. static NetConnection *getConnectionToServer() { return mServerConnection; }
  491. static NetConnection *getLocalClientConnection() { return mLocalClientConnection; }
  492. static void setLocalClientConnection(NetConnection *conn) { mLocalClientConnection = conn; }
  493. U32 getNetClassGroup() { return mNetClassGroup; }
  494. static bool filesWereDownloaded() { return mFilesWereDownloaded; }
  495. static String &getErrorBuffer() { return mErrorBuffer; }
  496. #ifdef TORQUE_DEBUG_NET
  497. bool mLogging;
  498. void setLogging(bool logging) { mLogging = logging; }
  499. #endif
  500. void setSimulatedNetParams(F32 packetLoss, U32 ping)
  501. { mSimulatedPacketLoss = packetLoss; mSimulatedPing = ping; }
  502. bool isConnectionToServer() { return mTypeFlags.test(ConnectionToServer); }
  503. bool isLocalConnection() { return !mRemoteConnection.isNull() ; }
  504. bool isNetworkConnection() { return mTypeFlags.test(NetworkConnection); }
  505. void setIsConnectionToServer() { mTypeFlags.set(ConnectionToServer); }
  506. void setIsLocalClientConnection() { mTypeFlags.set(LocalClientConnection); }
  507. void setNetworkConnection(bool net) { mTypeFlags.set(BitSet32(NetworkConnection), net); }
  508. virtual void setEstablished();
  509. /// Call this if the "connection" is local to this app. This short-circuits the protocol layer.
  510. void setRemoteConnectionObject(NetConnection *connection) { mRemoteConnection = connection; };
  511. void setSequence(U32 connectSequence);
  512. void setAddressDigest(U32 digest[4]);
  513. void getAddressDigest(U32 digest[4]);
  514. U32 getSequence();
  515. void setProtocolVersion(U32 protocolVersion) { mProtocolVersion = protocolVersion; }
  516. U32 getProtocolVersion() { return mProtocolVersion; }
  517. F32 getRoundTripTime() { return mRoundTripTime; }
  518. F32 getPacketLoss() { return( mPacketLoss ); }
  519. static String mErrorBuffer;
  520. static void setLastError(const char *fmt,...);
  521. void checkMaxRate();
  522. void handlePacket(BitStream *stream);
  523. void processRawPacket(BitStream *stream);
  524. void handleNotify(bool recvd);
  525. void handleConnectionEstablished();
  526. void keepAlive();
  527. const NetAddress *getNetAddress();
  528. void setNetAddress(const NetAddress *address);
  529. Net::Error sendPacket(BitStream *stream);
  530. private:
  531. void netAddressTableInsert();
  532. void netAddressTableRemove();
  533. public:
  534. /// Find a NetConnection, if any, with the specified address.
  535. static NetConnection *lookup(const NetAddress *remoteAddress);
  536. bool checkTimeout(U32 time); ///< returns true if the connection timed out
  537. void checkPacketSend(bool force);
  538. bool missionPathsSent() const { return mMissionPathsSent; }
  539. void setMissionPathsSent(const bool s) { mMissionPathsSent = s; }
  540. static void consoleInit();
  541. void onRemove();
  542. NetConnection();
  543. ~NetConnection();
  544. public:
  545. enum NetConnectionState
  546. {
  547. NotConnected,
  548. AwaitingChallengeResponse, ///< We've sent a challenge request, awaiting the response.
  549. AwaitingConnectRequest, ///< We've received a challenge request and sent a challenge response.
  550. AwaitingConnectResponse, ///< We've received a challenge response and sent a connect request.
  551. Connected, ///< We've accepted a connect request, or we've received a connect response accept.
  552. };
  553. U32 mConnectionSendCount; ///< number of connection messages we've sent.
  554. U32 mConnectionState; ///< State of the connection, from NetConnectionState.
  555. void setConnectionState(U32 state) { mConnectionState = state; }
  556. U32 getConnectionState() { return mConnectionState; }
  557. void setGhostFrom(bool ghostFrom); ///< Sets whether ghosts transmit from this side of the connection.
  558. void setGhostTo(bool ghostTo); ///< Sets whether ghosts are allowed from the other side of the connection.
  559. void setSendingEvents(bool sending); ///< Sets whether this side actually sends the events that are posted to it.
  560. void setTranslatesStrings(bool xl); ///< Sets whether this connection is capable of translating strings.
  561. void setNetClassGroup(U32 group); ///< Sets the group of NetClasses this connection traffics in.
  562. bool isEstablished() { return mEstablished; } ///< Is the connection established?
  563. DECLARE_CONOBJECT(NetConnection);
  564. DECLARE_INSCOPE( NetAPI );
  565. /// Structure to track packets and what we sent over them.
  566. ///
  567. /// We need to know what is sent in each packet, so that if a packet is
  568. /// dropped, we know what to resend. This is the structure we use to track
  569. /// this data.
  570. struct PacketNotify
  571. {
  572. bool rateChanged; ///< Did the rate change on this packet?
  573. bool maxRateChanged; ///< Did the max rate change on this packet?
  574. U32 sendTime; ///< Timestampe, when we sent this packet.
  575. NetEventNote *eventList; ///< Linked list of events sent over this packet.
  576. GhostRef *ghostList; ///< Linked list of ghost updates we sent in this packet.
  577. SubPacketRef *subList; ///< Defined by subclass - used as desired.
  578. PacketNotify *nextPacket; ///< Next packet sent.
  579. PacketNotify();
  580. };
  581. virtual PacketNotify *allocNotify();
  582. PacketNotify *mNotifyQueueHead; ///< Head of packet notify list.
  583. PacketNotify *mNotifyQueueTail; ///< Tail of packet notify list.
  584. protected:
  585. virtual void readPacket(BitStream *bstream);
  586. virtual void writePacket(BitStream *bstream, PacketNotify *note);
  587. virtual void packetReceived(PacketNotify *note);
  588. virtual void packetDropped(PacketNotify *note);
  589. virtual void connectionError(const char *errorString);
  590. //----------------------------------------------------------------
  591. /// @name Event Manager
  592. /// @{
  593. private:
  594. NetEventNote *mSendEventQueueHead;
  595. NetEventNote *mSendEventQueueTail;
  596. NetEventNote *mUnorderedSendEventQueueHead;
  597. NetEventNote *mUnorderedSendEventQueueTail;
  598. NetEventNote *mWaitSeqEvents;
  599. NetEventNote *mNotifyEventList;
  600. static FreeListChunker<NetEventNote> mEventNoteChunker;
  601. bool mSendingEvents;
  602. S32 mNextSendEventSeq;
  603. S32 mNextRecvEventSeq;
  604. S32 mLastAckedEventSeq;
  605. enum NetEventConstants {
  606. InvalidSendEventSeq = -1,
  607. FirstValidSendEventSeq = 0
  608. };
  609. void eventOnRemove();
  610. void eventPacketDropped(PacketNotify *notify);
  611. void eventPacketReceived(PacketNotify *notify);
  612. void eventWritePacket(BitStream *bstream, PacketNotify *notify);
  613. void eventReadPacket(BitStream *bstream);
  614. void eventWriteStartBlock(ResizeBitStream *stream);
  615. void eventReadStartBlock(BitStream *stream);
  616. public:
  617. /// Post an event to this connection.
  618. bool postNetEvent(NetEvent *event);
  619. /// @}
  620. //----------------------------------------------------------------
  621. /// @name Networked string table
  622. /// @{
  623. private:
  624. bool mTranslateStrings;
  625. ConnectionStringTable *mStringTable;
  626. public:
  627. void mapString(U32 netId, NetStringHandle &string)
  628. { mStringTable->mapString(netId, string); }
  629. U32 checkString(NetStringHandle &string, bool *isOnOtherSide = NULL)
  630. { if(mStringTable) return mStringTable->checkString(string, isOnOtherSide); else return 0; }
  631. U32 getNetSendId(NetStringHandle &string)
  632. { if(mStringTable) return mStringTable->getNetSendId(string); else return 0;}
  633. void confirmStringReceived(NetStringHandle &string, U32 index)
  634. { if(!isRemoved()) mStringTable->confirmStringReceived(string, index); }
  635. NetStringHandle translateRemoteStringId(U32 id) { return mStringTable->lookupString(id); }
  636. void validateSendString(const char *str);
  637. void packString(BitStream *stream, const char *str);
  638. void unpackString(BitStream *stream, char readBuffer[1024]);
  639. void packNetStringHandleU(BitStream *stream, NetStringHandle &h);
  640. NetStringHandle unpackNetStringHandleU(BitStream *stream);
  641. /// @}
  642. //----------------------------------------------------------------
  643. /// @name Ghost manager
  644. /// @{
  645. protected:
  646. enum GhostStates
  647. {
  648. GhostAlwaysDone,
  649. ReadyForNormalGhosts,
  650. EndGhosting,
  651. GhostAlwaysStarting,
  652. SendNextDownloadRequest,
  653. FileDownloadSizeMessage,
  654. NumConnectionMessages,
  655. };
  656. GhostInfo **mGhostArray; ///< Linked list of ghostInfos ghosted by this side of the connection
  657. U32 mGhostZeroUpdateIndex; ///< Index in mGhostArray of first ghost with 0 update mask.
  658. U32 mGhostFreeIndex; ///< Index in mGhostArray of first free ghost.
  659. U32 mGhostsActive; ///- Track actve ghosts on client side
  660. bool mGhosting; ///< Am I currently ghosting objects?
  661. bool mScoping; ///< am I currently scoping objects?
  662. U32 mGhostingSequence; ///< Sequence number describing this ghosting session.
  663. NetObject **mLocalGhosts; ///< Local ghost for remote object.
  664. ///
  665. /// mLocalGhosts pointer is NULL if mGhostTo is false
  666. GhostInfo *mGhostRefs; ///< Allocated array of ghostInfos. Null if ghostFrom is false.
  667. GhostInfo **mGhostLookupTable; ///< Table indexed by object id to GhostInfo. Null if ghostFrom is false.
  668. /// The object around which we are scoping this connection.
  669. ///
  670. /// This is usually the player object, or a related object, like a vehicle
  671. /// that the player is driving.
  672. SimObjectPtr<NetObject> mScopeObject;
  673. void clearGhostInfo();
  674. bool validateGhostArray();
  675. void ghostPacketDropped(PacketNotify *notify);
  676. void ghostPacketReceived(PacketNotify *notify);
  677. void ghostWritePacket(BitStream *bstream, PacketNotify *notify);
  678. void ghostReadPacket(BitStream *bstream);
  679. void freeGhostInfo(GhostInfo *);
  680. void ghostWriteStartBlock(ResizeBitStream *stream);
  681. void ghostReadStartBlock(BitStream *stream);
  682. virtual void ghostWriteExtra(NetObject *,BitStream *) {}
  683. virtual void ghostReadExtra(NetObject *,BitStream *, bool newGhost) {}
  684. virtual void ghostPreRead(NetObject *, bool newGhost) {}
  685. /// Called when 'EndGhosting' message is received from server.
  686. virtual void onEndGhosting() {}
  687. public:
  688. /// Some configuration values.
  689. enum GhostConstants
  690. {
  691. GhostIdBitSize = 12,
  692. MaxGhostCount = 1 << GhostIdBitSize, //4096,
  693. GhostLookupTableSize = 1 << GhostIdBitSize, //4096
  694. GhostIndexBitSize = 4 // number of bits GhostIdBitSize-3 fits into
  695. };
  696. U32 getGhostsActive() { return mGhostsActive;};
  697. /// Are we ghosting to someone?
  698. bool isGhostingTo() { return mLocalGhosts != NULL; };
  699. /// Are we ghosting from someone?
  700. bool isGhostingFrom() { return mGhostArray != NULL; };
  701. /// Called by onRemove, to shut down the ghost subsystem.
  702. void ghostOnRemove();
  703. /// Called when we're done with normal scoping.
  704. ///
  705. /// This gives subclasses a chance to shove things into scope, such as
  706. /// the results of a sensor network calculation, that would otherwise
  707. /// be awkward to add.
  708. virtual void doneScopingScene() { /* null */ }
  709. /// Set the object around which we are currently scoping network traffic.
  710. void setScopeObject(NetObject *object);
  711. /// Get the object around which we are currently scoping network traffic.
  712. NetObject *getScopeObject();
  713. /// Add an object to scope.
  714. void objectInScope(NetObject *object);
  715. /// Add an object to scope, marking that it should always be scoped to this connection.
  716. void objectLocalScopeAlways(NetObject *object);
  717. /// Mark an object that is being ghosted as not always needing to be scoped.
  718. ///
  719. /// This undoes objectLocalScopeAlways(), but doesn't immediately flush it from scope.
  720. ///
  721. /// Instead, the standard scoping mechanisms will clear it from scope when it is appropos
  722. /// to do so.
  723. void objectLocalClearAlways(NetObject *object);
  724. /// Get a NetObject* from a ghost ID (on client side).
  725. NetObject *resolveGhost(S32 id);
  726. /// Get a NetObject* from a ghost index (on the server side).
  727. NetObject *resolveObjectFromGhostIndex(S32 id);
  728. /// Get the ghost index corresponding to a given NetObject. This is only
  729. /// meaningful on the server side.
  730. S32 getGhostIndex(NetObject *object);
  731. /// Move a GhostInfo into the nonzero portion of the list (so that we know to update it).
  732. void ghostPushNonZero(GhostInfo *gi);
  733. /// Move a GhostInfo into the zero portion of the list (so that we know not to update it).
  734. void ghostPushToZero(GhostInfo *gi);
  735. /// Move a GhostInfo from the zero portion of the list to the free portion.
  736. void ghostPushZeroToFree(GhostInfo *gi);
  737. /// Move a GhostInfo from the free portion of the list to the zero portion.
  738. inline void ghostPushFreeToZero(GhostInfo *info);
  739. /// Stop all ghosting activity and inform the other side about this.
  740. ///
  741. /// Turns off ghosting.
  742. void resetGhosting();
  743. /// Activate ghosting, once it's enabled.
  744. void activateGhosting();
  745. /// Are we ghosting?
  746. bool isGhosting() { return mGhosting; }
  747. /// Begin to stop ghosting an object.
  748. void detachObject(GhostInfo *info);
  749. /// Mark an object to be always ghosted. Index is the ghost index of the object.
  750. void setGhostAlwaysObject(NetObject *object, U32 index);
  751. /// Send ghost connection handshake message.
  752. ///
  753. /// As part of the ghost connection process, extensive hand-shaking must be performed.
  754. ///
  755. /// This is done by passing ConnectionMessageEvents; this is a helper function
  756. /// to more effectively perform this task. Messages are dealt with by
  757. /// handleConnectionMessage().
  758. ///
  759. /// @param message One of GhostStates
  760. /// @param sequence A sequence number, if any.
  761. /// @param ghostCount A count of ghosts relating to this message.
  762. void sendConnectionMessage(U32 message, U32 sequence = 0, U32 ghostCount = 0);
  763. /// Handle message from sendConnectionMessage().
  764. ///
  765. /// This is called to handle messages sent via sendConnectionMessage.
  766. ///
  767. /// @param message One of GhostStates
  768. /// @param sequence A sequence number, if any.
  769. /// @param ghostCount A count of ghosts relating to this message.
  770. virtual void handleConnectionMessage(U32 message, U32 sequence, U32 ghostCount);
  771. /// Sends a signal to any object that needs to wait till everything has been ghosted
  772. /// before performing an operation.
  773. static Signal<void()> smGhostAlwaysDone;
  774. /// @}
  775. public:
  776. //----------------------------------------------------------------
  777. /// @name File transfer
  778. /// @{
  779. protected:
  780. /// List of files missing for this connection.
  781. ///
  782. /// The currently downloading file is always first in the list (ie, [0]).
  783. Vector<char *> mMissingFileList;
  784. /// Stream for currently uploading file (if any).
  785. Stream *mCurrentDownloadingFile;
  786. /// Storage for currently downloading file.
  787. void *mCurrentFileBuffer;
  788. /// Size of currently downloading file in bytes.
  789. U32 mCurrentFileBufferSize;
  790. /// Our position in the currently downloading file in bytes.
  791. U32 mCurrentFileBufferOffset;
  792. /// Number of files we have downloaded.
  793. U32 mNumDownloadedFiles;
  794. /// Error storage for file transfers.
  795. String mLastFileErrorBuffer;
  796. /// Structure to track ghost-always objects and their ghost indices.
  797. struct GhostSave {
  798. NetObject *ghost;
  799. U32 index;
  800. };
  801. /// List of objects to ghost-always.
  802. Vector<GhostSave> mGhostAlwaysSaveList;
  803. public:
  804. /// Start sending the specified file over the link.
  805. bool startSendingFile(const char *fileName);
  806. /// Called when we receive a FileChunkEvent.
  807. void chunkReceived(U8 *chunkData, U32 chunkLen);
  808. /// Get the next file...
  809. void sendNextFileDownloadRequest();
  810. /// Post the next FileChunkEvent.
  811. void sendFileChunk();
  812. /// Called when we finish downloading file data.
  813. virtual void fileDownloadSegmentComplete();
  814. /// This is part of the file transfer logic; basically, we call this
  815. /// every time we finish downloading new files. It attempts to load
  816. /// the GhostAlways objects; if they fail, it marks an error and we
  817. /// have chance to retry.
  818. void loadNextGhostAlwaysObject(bool hadNewFiles);
  819. /// @}
  820. //----------------------------------------------------------------
  821. /// @name Demo Recording
  822. /// @{
  823. private:
  824. Stream *mDemoWriteStream;
  825. Stream *mDemoReadStream;
  826. U32 mDemoNextBlockType;
  827. U32 mDemoNextBlockSize;
  828. U32 mDemoWriteStartTime;
  829. U32 mDemoReadStartTime;
  830. U32 mDemoLastWriteTime;
  831. U32 mDemoRealStartTime;
  832. public:
  833. enum DemoBlockTypes {
  834. BlockTypePacket,
  835. BlockTypeSendPacket,
  836. NetConnectionBlockTypeCount
  837. };
  838. enum DemoConstants {
  839. MaxNumBlockTypes = 16,
  840. MaxBlockSize = 0x1000,
  841. };
  842. bool isRecording()
  843. { return mDemoWriteStream != NULL; }
  844. bool isPlayingBack()
  845. { return mDemoReadStream != NULL; }
  846. U32 getNextBlockType() { return mDemoNextBlockType; }
  847. void recordBlock(U32 type, U32 size, void *data);
  848. virtual void handleRecordedBlock(U32 type, U32 size, void *data);
  849. bool processNextBlock();
  850. bool startDemoRecord(const char *fileName);
  851. bool replayDemoRecord(const char *fileName);
  852. void startDemoRead();
  853. void stopRecording();
  854. void stopDemoPlayback();
  855. virtual void writeDemoStartBlock(ResizeBitStream *stream);
  856. virtual bool readDemoStartBlock(BitStream *stream);
  857. virtual void demoPlaybackComplete();
  858. /// @}
  859. };
  860. //----------------------------------------------------------------------------
  861. /// Information about a ghosted object.
  862. ///
  863. /// @note If the size of this structure changes, the
  864. /// NetConnection::getGhostIndex function MUST be changed
  865. /// to reflect the new size.
  866. struct GhostInfo
  867. {
  868. public: // required for MSVC
  869. NetObject *obj; ///< The object being ghosted.
  870. U32 updateMask; ///< Flags indicating what state data needs to be transferred.
  871. U32 updateSkipCount; ///< How many updates have we skipped this guy?
  872. U32 flags; ///< Flags from GhostInfo::Flags
  873. F32 priority; ///< A float value indicating the priority of this object for
  874. /// updates.
  875. /// @name References
  876. ///
  877. /// The GhostInfo structure is used in several linked lists; these members are
  878. /// the implementation for this.
  879. /// @{
  880. NetConnection::GhostRef *updateChain; ///< List of references in NetConnections to us.
  881. GhostInfo *nextObjectRef; ///< Next ghosted object.
  882. GhostInfo *prevObjectRef; ///< Previous ghosted object.
  883. NetConnection *connection; ///< Connection that we're ghosting over.
  884. GhostInfo *nextLookupInfo; ///< GhostInfo references are stored in a hash; this is the bucket
  885. /// implementation.
  886. /// @}
  887. U32 index;
  888. U32 arrayIndex;
  889. /// Flags relating to the state of the object.
  890. enum Flags
  891. {
  892. Valid = BIT(0),
  893. InScope = BIT(1),
  894. ScopeAlways = BIT(2),
  895. NotYetGhosted = BIT(3),
  896. Ghosting = BIT(4),
  897. KillGhost = BIT(5),
  898. KillingGhost = BIT(6),
  899. ScopedEvent = BIT(7),
  900. ScopeLocalAlways = BIT(8),
  901. };
  902. };
  903. inline void NetConnection::ghostPushNonZero(GhostInfo *info)
  904. {
  905. AssertFatal(info->arrayIndex >= mGhostZeroUpdateIndex && info->arrayIndex < mGhostFreeIndex, "Out of range arrayIndex.");
  906. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  907. if(info->arrayIndex != mGhostZeroUpdateIndex)
  908. {
  909. mGhostArray[mGhostZeroUpdateIndex]->arrayIndex = info->arrayIndex;
  910. mGhostArray[info->arrayIndex] = mGhostArray[mGhostZeroUpdateIndex];
  911. mGhostArray[mGhostZeroUpdateIndex] = info;
  912. info->arrayIndex = mGhostZeroUpdateIndex;
  913. }
  914. mGhostZeroUpdateIndex++;
  915. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  916. }
  917. inline void NetConnection::ghostPushToZero(GhostInfo *info)
  918. {
  919. AssertFatal(info->arrayIndex < mGhostZeroUpdateIndex, "Out of range arrayIndex.");
  920. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  921. mGhostZeroUpdateIndex--;
  922. if(info->arrayIndex != mGhostZeroUpdateIndex)
  923. {
  924. mGhostArray[mGhostZeroUpdateIndex]->arrayIndex = info->arrayIndex;
  925. mGhostArray[info->arrayIndex] = mGhostArray[mGhostZeroUpdateIndex];
  926. mGhostArray[mGhostZeroUpdateIndex] = info;
  927. info->arrayIndex = mGhostZeroUpdateIndex;
  928. }
  929. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  930. }
  931. inline void NetConnection::ghostPushZeroToFree(GhostInfo *info)
  932. {
  933. AssertFatal(info->arrayIndex >= mGhostZeroUpdateIndex && info->arrayIndex < mGhostFreeIndex, "Out of range arrayIndex.");
  934. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  935. mGhostFreeIndex--;
  936. if(info->arrayIndex != mGhostFreeIndex)
  937. {
  938. mGhostArray[mGhostFreeIndex]->arrayIndex = info->arrayIndex;
  939. mGhostArray[info->arrayIndex] = mGhostArray[mGhostFreeIndex];
  940. mGhostArray[mGhostFreeIndex] = info;
  941. info->arrayIndex = mGhostFreeIndex;
  942. }
  943. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  944. }
  945. inline void NetConnection::ghostPushFreeToZero(GhostInfo *info)
  946. {
  947. AssertFatal(info->arrayIndex >= mGhostFreeIndex, "Out of range arrayIndex.");
  948. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  949. if(info->arrayIndex != mGhostFreeIndex)
  950. {
  951. mGhostArray[mGhostFreeIndex]->arrayIndex = info->arrayIndex;
  952. mGhostArray[info->arrayIndex] = mGhostArray[mGhostFreeIndex];
  953. mGhostArray[mGhostFreeIndex] = info;
  954. info->arrayIndex = mGhostFreeIndex;
  955. }
  956. mGhostFreeIndex++;
  957. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  958. }
  959. #endif