netConnection.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _MPOINT_H_
  25. #include "math/mPoint.h"
  26. #endif
  27. #ifndef _NETOBJECT_H_
  28. #include "network/netObject.h"
  29. #endif
  30. #ifndef _NETSTRINGTABLE_H_
  31. #include "network/netStringTable.h"
  32. #endif
  33. #ifndef _EVENT_H_
  34. #include "platform/event.h"
  35. #endif
  36. #ifndef _CONNECTION_PROTOCOL_H_
  37. #include "network/connectionProtocol.h"
  38. #endif
  39. #ifndef _H_CONNECTIONSTRINGTABLE
  40. #include "network/connectionStringTable.h"
  41. #endif
  42. //----------------------------------------------------------------------------
  43. // the sim connection encapsulates the packet stream,
  44. // ghost manager, event manager and playerPSC of the old tribes net code
  45. class NetConnection;
  46. class NetObject;
  47. class BitStream;
  48. class ResizeBitStream;
  49. class Stream;
  50. class Point3F;
  51. struct GhostInfo;
  52. struct SubPacketRef; // defined in NetConnection subclass
  53. //#define DEBUG_NET
  54. #ifdef TORQUE_DEBUG_NET
  55. #define DEBUG_LOG(x) if(mLogging){Con::printf x;}
  56. #else
  57. #define DEBUG_LOG(x)
  58. #endif
  59. //----------------------------------------------------------------------------
  60. class NetEvent;
  61. struct NetEventNote
  62. {
  63. NetEvent *mEvent;
  64. S32 mSeqCount;
  65. NetEventNote *mNextEvent;
  66. };
  67. /// An event to be sent over the network.
  68. ///
  69. /// @note Torque implements two methods of network data passing; this is one of them.
  70. /// See NetConnection for details of the other, which is referred to as ghosting.
  71. ///
  72. /// Torque's network layer lets you pass events to/from the server. There are three
  73. /// types of events:
  74. /// - <b>Unguaranteed events</b> are events which are sent once. If they don't
  75. /// make it through the link, they are not resent. This is good for quick,
  76. /// frequent status updates which are of transient interest, like position
  77. /// updates or voice communication.
  78. /// - <b>Guaranteed events</b> are events which are guaranteed to be
  79. /// delivered. If they don't make it through the link, they are sent as
  80. /// needed. This is good for important, one-time information,
  81. /// like which team a user wants to play on, or the current weather.
  82. /// - <b>GuaranteedOrdered events</b> are events which are guaranteed not
  83. /// only to be delivered, but to be delivered in order. This is good for
  84. /// information which is not only important, but also order-critical, like
  85. /// chat messages.
  86. ///
  87. /// There are 6 methods that you need to implement if you want to make a
  88. /// basic NetEvent subclass, and 2 macros you need to call.
  89. ///
  90. /// @code
  91. /// // A simple NetEvent to transmit a string over the network.
  92. /// // This is based on the code in netTest.cc
  93. /// class SimpleMessageEvent : public NetEvent
  94. /// {
  95. /// typedef NetEvent Parent;
  96. /// char *msg;
  97. /// public:
  98. /// SimpleMessageEvent(const char *message = NULL);
  99. /// ~SimpleMessageEvent();
  100. ///
  101. /// virtual void pack (NetConnection *conn, BitStream *bstream);
  102. /// virtual void write (NetConnection *conn, BitStream *bstream);
  103. /// virtual void unpack (NetConnection *conn, BitStream *bstream);
  104. /// virtual void process(NetConnection *conn);
  105. ///
  106. /// DECLARE_CONOBJECT(SimpleMessageEvent);
  107. /// };
  108. ///
  109. /// IMPLEMENT_CO_NETEVENT_V1(SimpleMessageEvent);
  110. /// @endcode
  111. ///
  112. /// Notice the two macros which we call. The first, DECLARE_CONOBJECT() is there
  113. /// because we're a ConsoleObject. The second, IMPLEMENT_CO_NETEVENT_V1(), is there
  114. /// to register this event type with Torque's networking layer, so that it can be
  115. /// properly transmitted over the wire. There are three macros which you might use:
  116. /// - <b>IMPLEMENT_CO_NETEVENT_V1</b>, which indicates an event which may be sent
  117. /// in either direction, from the client to the server, or from the server to the
  118. /// client.
  119. /// - <b>IMPLEMENT_CO_CLIENTEVENT_V1</b>, which indicates an event which may only
  120. /// be sent to the client.
  121. /// - <b>IMPLEMENT_CO_SERVEREVENT_V1</b>, which indicates an event which may only
  122. /// be sent to the server.
  123. ///
  124. /// Choosing the right macro is a good way to make your game more resistant to hacking; for instance,
  125. /// PathManager events are marked as CLIENTEVENTs, because they would cause the server to crash if
  126. /// a client sent them.
  127. ///
  128. /// @note Torque allows you to call NetConnection::setLastError() on the NetConnection passed to
  129. /// your NetEvent. You can cause the connection to abort if invalid data is received, specifying
  130. /// a reason to the user.
  131. ///
  132. /// Now, the 6 methods which we have above; the constructor and destructor need only do
  133. /// whatever book-keeping is needed for your specific implementation. In our case, we
  134. /// just need to allocate/deallocate the space for our string:
  135. ///
  136. /// @code
  137. /// SimpleMessageEvent::SimpleMessageEvent(const char *message = NULL)
  138. /// {
  139. /// // If we wanted to make this not be a GuaranteedOrdered event, we'd
  140. /// // put a line like this in the constructor:
  141. /// // mGuaranteeType = Guaranteed;
  142. /// // (or whatever type you wanted.)
  143. /// if(message)
  144. /// msg = dStrdup(message);
  145. /// else
  146. /// msg = NULL;
  147. /// }
  148. ///
  149. /// SimpleMessageEvent::~SimpleMessageEvent()
  150. /// {
  151. /// dFree(msg);
  152. /// }
  153. /// @endcode
  154. ///
  155. /// Simple as that! Now, onto pack(), write(), unpack(), process().
  156. ///
  157. /// <b>pack()</b> is responsible for packing the event over the wire:
  158. ///
  159. /// @code
  160. /// void SimpleMessageEvent::pack(NetConnection* conn, BitStream *bstream)
  161. /// {
  162. /// bstream->writeString(msg);
  163. /// }
  164. /// @endcode
  165. ///
  166. /// <b>unpack()</b> is responsible for unpacking the event on the other end:
  167. ///
  168. /// @code
  169. /// // The networking layer takes care of instantiating a new
  170. /// // SimpleMessageEvent, which saves us a bit of effort.
  171. /// void SimpleMessageEvent::unpack(NetConnection *conn, BitStream *bstream)
  172. /// {
  173. /// char buf[256];
  174. /// bstream->readString(buf);
  175. /// msg = dStrdup(buf);
  176. /// }
  177. /// @endcode
  178. ///
  179. /// <b>process()</b> is called when the network layer is finished with things.
  180. /// A typical case is that a GuaranteedOrdered event is unpacked and stored, but
  181. /// not processed until the events preceding it in the sequence have also been
  182. /// dealt with.
  183. ///
  184. /// @code
  185. /// // This just prints the event in the console. You might
  186. /// // want to do something more clever here -- BJG
  187. /// void SimpleMessageEvent::process(NetConnection *conn)
  188. /// {
  189. /// Con::printf("RMSG %d %s", mSourceId, msg);
  190. /// }
  191. /// @endcode
  192. ///
  193. /// <b>write()</b> is called if a demo recording is started, and the event has not yet been
  194. /// processed, but it has been unpacked. It should be identical in its output to the bitstream
  195. /// compared to pack(), but since it is called after unpack() some lookups may not need to be
  196. /// performed. In normal demo recording, whole network packets are recorded, meaning that most
  197. /// of the time write() will not be called.
  198. ///
  199. /// In our case, it's entirely identical to pack():
  200. ///
  201. /// @code
  202. /// virtual void write(NetConnection*, BitStream *bstream)
  203. /// {
  204. /// bstream->writeString(msg);
  205. /// }
  206. /// @endcode
  207. ///
  208. /// The NetEvent is sent over the wire in a straightforward way (assuming you have a
  209. /// handle to a NetConnection):
  210. ///
  211. /// @code
  212. /// NetConnection *conn; // We assume you have filled this in.
  213. ///
  214. /// con->postNetEvent(new SimpleMessageEvent("This is a test!"));
  215. /// @endcode
  216. ///
  217. /// @see GhostAlwaysObjectEvent for an example of dissimilar write()/pack() methods.
  218. ///
  219. /// Finally, for more advanced applications, notifySent() is called whenever the event is
  220. /// sent over the wire, in NetConnection::eventWritePacket(). notifyDelivered() is called
  221. /// when the packet is finally received or (in the case of Unguaranteed packets) dropped.
  222. ///
  223. /// @note IMPLEMENT_CO_NETEVENT_V1 and co. have sibling macros which allow you to specify a
  224. /// groupMask; see ConsoleObject for a further discussion of this.
  225. class NetEvent : public ConsoleObject
  226. {
  227. public:
  228. /// @name Implementation Details
  229. ///
  230. /// These are internal fields which you won't need to manipulate, except for mGuaranteeType.
  231. /// @{
  232. ///
  233. S32 mRefCount;
  234. typedef ConsoleObject Parent;
  235. enum {
  236. GuaranteedOrdered = 0,
  237. Guaranteed = 1,
  238. Unguaranteed = 2
  239. } mGuaranteeType;
  240. NetConnectionId mSourceId;
  241. void incRef()
  242. {
  243. mRefCount++;
  244. }
  245. void decRef()
  246. {
  247. mRefCount--;
  248. if(!mRefCount)
  249. delete this;
  250. }
  251. #ifdef TORQUE_DEBUG_NET
  252. virtual const char *getDebugName();
  253. #endif
  254. /// @}
  255. /// @name Things To Subclass
  256. /// @{
  257. ///
  258. NetEvent() { mGuaranteeType = GuaranteedOrdered; mRefCount = 0; }
  259. virtual ~NetEvent();
  260. virtual void write(NetConnection *ps, BitStream *bstream) = 0;
  261. virtual void pack(NetConnection *ps, BitStream *bstream) = 0;
  262. virtual void unpack(NetConnection *ps, BitStream *bstream) = 0;
  263. virtual void process(NetConnection *ps) = 0;
  264. virtual void notifySent(NetConnection *ps);
  265. virtual void notifyDelivered(NetConnection *ps, bool madeit);
  266. /// @}
  267. };
  268. #define IMPLEMENT_CO_NETEVENT_V1(className) \
  269. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  270. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  271. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  272. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  273. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  274. ConcreteClassRep<className> className::dynClassRep(#className,NetClassGroupGameMask, NetClassTypeEvent, NetEventDirAny, className::getParentStaticClassRep())
  275. #define IMPLEMENT_CO_CLIENTEVENT_V1(className) \
  276. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  277. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  278. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  279. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  280. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  281. ConcreteClassRep<className> className::dynClassRep(#className,NetClassGroupGameMask, NetClassTypeEvent, NetEventDirServerToClient, className::getParentStaticClassRep())
  282. #define IMPLEMENT_CO_SERVEREVENT_V1(className) \
  283. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  284. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  285. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  286. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  287. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  288. ConcreteClassRep<className> className::dynClassRep(#className,NetClassGroupGameMask, NetClassTypeEvent, NetEventDirClientToServer, className::getParentStaticClassRep())
  289. #define IMPLEMENT_CO_NETEVENT(className,groupMask) \
  290. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  291. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  292. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  293. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  294. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  295. ConcreteClassRep<className> className::dynClassRep(#className,groupMask, NetClassTypeEvent, NetEventDirAny, className::getParentStaticClassRep())
  296. #define IMPLEMENT_CO_CLIENTEVENT(className,groupMask) \
  297. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  298. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  299. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  300. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  301. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  302. ConcreteClassRep<className> className::dynClassRep(#className,groupMask, NetClassTypeEvent, NetEventDirServerToClient, className::getParentStaticClassRep())
  303. #define IMPLEMENT_CO_SERVEREVENT(className,groupMask) \
  304. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  305. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  306. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  307. AbstractClassRep* className::getContainerChildStaticClassRep() { return NULL; } \
  308. AbstractClassRep::WriteCustomTamlSchema className::getStaticWriteCustomTamlSchema() { return NULL; } \
  309. ConcreteClassRep<className> className::dynClassRep(#className,groupMask, NetClassTypeEvent, NetEventDirClientToServer, className::getParentStaticClassRep())
  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 10-bit field,
  357. /// ergo, there is a cap of 1024 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 ConnectionProtocol, public SimGroup
  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. U32 mLastUpdateTime;
  440. F32 mRoundTripTime;
  441. F32 mPacketLoss;
  442. U32 mSimulatedPing;
  443. F32 mSimulatedPacketLoss;
  444. /// @}
  445. /// @name State
  446. /// @{
  447. U32 mProtocolVersion;
  448. U32 mSendDelayCredit;
  449. U32 mConnectSequence;
  450. U32 mAddressDigest[4];
  451. bool mEstablished;
  452. bool mMissionPathsSent;
  453. struct NetRate
  454. {
  455. U32 updateDelay;
  456. S32 packetSize;
  457. bool changed;
  458. };
  459. NetRate mCurRate;
  460. NetRate mMaxRate;
  461. /// If we're doing a "short circuited" connection, this stores
  462. /// a pointer to the other side.
  463. SimObjectPtr<NetConnection> mRemoteConnection;
  464. NetAddress mNetAddress;
  465. /// @}
  466. /// @name Timeout Management
  467. /// @{
  468. U32 mPingSendCount;
  469. U32 mPingRetryCount;
  470. U32 mLastPingSendTime;
  471. /// @}
  472. /// @name Connection Table
  473. ///
  474. /// We store our connections on a hash table so we can
  475. /// quickly find them.
  476. /// @{
  477. NetConnection *mNextTableHash;
  478. static NetConnection *mHashTable[HashTableSize];
  479. /// @}
  480. protected:
  481. static SimObjectPtr<NetConnection> mServerConnection;
  482. static SimObjectPtr<NetConnection> mLocalClientConnection;
  483. static bool mFilesWereDownloaded;
  484. U32 mConnectSendCount;
  485. U32 mConnectLastSendTime;
  486. public:
  487. static NetConnection *getConnectionToServer() { return mServerConnection; }
  488. static NetConnection *getLocalClientConnection() { return mLocalClientConnection; }
  489. static void setLocalClientConnection(NetConnection *conn) { mLocalClientConnection = conn; }
  490. U32 getNetClassGroup() { return mNetClassGroup; }
  491. static bool filesWereDownloaded() { return mFilesWereDownloaded; }
  492. static char *getErrorBuffer() { return mErrorBuffer; }
  493. #ifdef TORQUE_DEBUG_NET
  494. bool mLogging;
  495. void setLogging(bool logging) { mLogging = logging; }
  496. #endif
  497. void setSimulatedNetParams(F32 packetLoss, U32 ping)
  498. { mSimulatedPacketLoss = packetLoss; mSimulatedPing = ping; }
  499. bool isConnectionToServer() { return mTypeFlags.test(ConnectionToServer); }
  500. bool isLocalConnection() { return !mRemoteConnection.isNull() ; }
  501. bool isNetworkConnection() { return mTypeFlags.test(NetworkConnection); }
  502. void setIsConnectionToServer() { mTypeFlags.set(ConnectionToServer); }
  503. void setIsLocalClientConnection() { mTypeFlags.set(LocalClientConnection); }
  504. void setNetworkConnection(bool net) { mTypeFlags.set(BitSet32(NetworkConnection), net); }
  505. virtual void setEstablished();
  506. /// Call this if the "connection" is local to this app. This short-circuits the protocol layer.
  507. void setRemoteConnectionObject(NetConnection *connection) { mRemoteConnection = connection; };
  508. void setSequence(U32 connectSequence);
  509. void setAddressDigest(U32 digest[4]);
  510. void getAddressDigest(U32 digest[4]);
  511. U32 getSequence();
  512. void setProtocolVersion(U32 protocolVersion) { mProtocolVersion = protocolVersion; }
  513. U32 getProtocolVersion() { return mProtocolVersion; }
  514. F32 getRoundTripTime() { return mRoundTripTime; }
  515. F32 getPacketLoss() { return( mPacketLoss ); }
  516. static char mErrorBuffer[256];
  517. static void setLastError(const char *fmt,...);
  518. void checkMaxRate();
  519. void handlePacket(BitStream *stream);
  520. void processRawPacket(BitStream *stream);
  521. void handleNotify(bool recvd);
  522. void handleConnectionEstablished();
  523. void keepAlive();
  524. const NetAddress *getNetAddress();
  525. void setNetAddress(const NetAddress *address);
  526. Net::Error sendPacket(BitStream *stream);
  527. private:
  528. void netAddressTableInsert();
  529. void netAddressTableRemove();
  530. public:
  531. /// Find a NetConnection, if any, with the specified address.
  532. static NetConnection *lookup(const NetAddress *remoteAddress);
  533. bool checkTimeout(U32 time); ///< returns true if the connection timed out
  534. void checkPacketSend(bool force);
  535. bool missionPathsSent() const { return mMissionPathsSent; }
  536. void setMissionPathsSent(const bool s) { mMissionPathsSent = s; }
  537. static void consoleInit();
  538. void onRemove();
  539. NetConnection();
  540. ~NetConnection();
  541. public:
  542. enum NetConnectionState
  543. {
  544. NotConnected,
  545. AwaitingChallengeResponse, ///< We've sent a challenge request, awaiting the response.
  546. AwaitingConnectRequest, ///< We've received a challenge request and sent a challenge response.
  547. AwaitingConnectResponse, ///< We've received a challenge response and sent a connect request.
  548. Connected, ///< We've accepted a connect request, or we've received a connect response accept.
  549. };
  550. U32 mConnectionSendCount; ///< number of connection messages we've sent.
  551. U32 mConnectionState; ///< State of the connection, from NetConnectionState.
  552. void setConnectionState(U32 state) { mConnectionState = state; }
  553. U32 getConnectionState() { return mConnectionState; }
  554. void setGhostFrom(bool ghostFrom); ///< Sets whether ghosts transmit from this side of the connection.
  555. void setGhostTo(bool ghostTo); ///< Sets whether ghosts are allowed from the other side of the connection.
  556. void setSendingEvents(bool sending); ///< Sets whether this side actually sends the events that are posted to it.
  557. void setTranslatesStrings(bool xl); ///< Sets whether this connection is capable of translating strings.
  558. void setNetClassGroup(U32 group); ///< Sets the group of NetClasses this connection traffics in.
  559. bool isEstablished() { return mEstablished; } ///< Is the connection established?
  560. DECLARE_CONOBJECT(NetConnection);
  561. /// Structure to track packets and what we sent over them.
  562. ///
  563. /// We need to know what is sent in each packet, so that if a packet is
  564. /// dropped, we know what to resend. This is the structure we use to track
  565. /// this data.
  566. struct PacketNotify
  567. {
  568. bool rateChanged; ///< Did the rate change on this packet?
  569. bool maxRateChanged; ///< Did the max rate change on this packet?
  570. U32 sendTime; ///< Timestampe, when we sent this packet.
  571. NetEventNote *eventList; ///< Linked list of events sent over this packet.
  572. GhostRef *ghostList; ///< Linked list of ghost updates we sent in this packet.
  573. SubPacketRef *subList; ///< Defined by subclass - used as desired.
  574. PacketNotify *nextPacket; ///< Next packet sent.
  575. PacketNotify();
  576. };
  577. virtual PacketNotify *allocNotify();
  578. PacketNotify *mNotifyQueueHead; ///< Head of packet notify list.
  579. PacketNotify *mNotifyQueueTail; ///< Tail of packet notify list.
  580. protected:
  581. virtual void readPacket(BitStream *bstream);
  582. virtual void writePacket(BitStream *bstream, PacketNotify *note);
  583. virtual void packetReceived(PacketNotify *note);
  584. virtual void packetDropped(PacketNotify *note);
  585. virtual void connectionError(const char *errorString);
  586. //----------------------------------------------------------------
  587. /// @name Event Manager
  588. /// @{
  589. private:
  590. NetEventNote *mSendEventQueueHead;
  591. NetEventNote *mSendEventQueueTail;
  592. NetEventNote *mUnorderedSendEventQueueHead;
  593. NetEventNote *mUnorderedSendEventQueueTail;
  594. NetEventNote *mWaitSeqEvents;
  595. NetEventNote *mNotifyEventList;
  596. static FreeListChunker<NetEventNote> mEventNoteChunker;
  597. bool mSendingEvents;
  598. S32 mNextSendEventSeq;
  599. S32 mNextRecvEventSeq;
  600. S32 mLastAckedEventSeq;
  601. enum NetEventConstants {
  602. InvalidSendEventSeq = -1,
  603. FirstValidSendEventSeq = 0
  604. };
  605. void eventOnRemove();
  606. void eventPacketDropped(PacketNotify *notify);
  607. void eventPacketReceived(PacketNotify *notify);
  608. void eventWritePacket(BitStream *bstream, PacketNotify *notify);
  609. void eventReadPacket(BitStream *bstream);
  610. void eventWriteStartBlock(ResizeBitStream *stream);
  611. void eventReadStartBlock(BitStream *stream);
  612. public:
  613. /// Post an event to this connection.
  614. bool postNetEvent(NetEvent *event);
  615. /// @}
  616. //----------------------------------------------------------------
  617. /// @name Networked string table
  618. /// @{
  619. private:
  620. bool mTranslateStrings;
  621. ConnectionStringTable *mStringTable;
  622. public:
  623. void mapString(U32 netId, NetStringHandle &string)
  624. { mStringTable->mapString(netId, string); }
  625. U32 checkString(NetStringHandle &string, bool *isOnOtherSide = NULL)
  626. { if(mStringTable) return mStringTable->checkString(string, isOnOtherSide); else return 0; }
  627. U32 getNetSendId(NetStringHandle &string)
  628. { if(mStringTable) return mStringTable->getNetSendId(string); else return 0;}
  629. void confirmStringReceived(NetStringHandle &string, U32 index)
  630. { if(!isRemoved()) mStringTable->confirmStringReceived(string, index); }
  631. NetStringHandle translateRemoteStringId(U32 id) { return mStringTable->lookupString(id); }
  632. void validateSendString(const char *str);
  633. void packString(BitStream *stream, const char *str);
  634. void unpackString(BitStream *stream, char readBuffer[1024]);
  635. void packNetStringHandleU(BitStream *stream, NetStringHandle &h);
  636. NetStringHandle unpackNetStringHandleU(BitStream *stream);
  637. /// @}
  638. //----------------------------------------------------------------
  639. /// @name Ghost manager
  640. /// @{
  641. protected:
  642. enum GhostStates
  643. {
  644. GhostAlwaysDone,
  645. ReadyForNormalGhosts,
  646. EndGhosting,
  647. GhostAlwaysStarting,
  648. SendNextDownloadRequest,
  649. FileDownloadSizeMessage,
  650. NumConnectionMessages,
  651. };
  652. GhostInfo **mGhostArray; ///< Linked list of ghostInfos ghosted by this side of the connection
  653. U32 mGhostZeroUpdateIndex; ///< Index in mGhostArray of first ghost with 0 update mask.
  654. U32 mGhostFreeIndex; ///< Index in mGhostArray of first free ghost.
  655. U32 mGhostsActive; ///- Track actve ghosts on client side
  656. bool mGhosting; ///< Am I currently ghosting objects?
  657. bool mScoping; ///< am I currently scoping objects?
  658. U32 mGhostingSequence; ///< Sequence number describing this ghosting session.
  659. NetObject **mLocalGhosts; ///< Local ghost for remote object.
  660. ///
  661. /// mLocalGhosts pointer is NULL if mGhostTo is false
  662. GhostInfo *mGhostRefs; ///< Allocated array of ghostInfos. Null if ghostFrom is false.
  663. GhostInfo **mGhostLookupTable; ///< Table indexed by object id to GhostInfo. Null if ghostFrom is false.
  664. /// The object around which we are scoping this connection.
  665. ///
  666. /// This is usually the player object, or a related object, like a vehicle
  667. /// that the player is driving.
  668. SimObjectPtr<NetObject> mScopeObject;
  669. void clearGhostInfo();
  670. bool validateGhostArray();
  671. void ghostPacketDropped(PacketNotify *notify);
  672. void ghostPacketReceived(PacketNotify *notify);
  673. void ghostWritePacket(BitStream *bstream, PacketNotify *notify);
  674. void ghostReadPacket(BitStream *bstream);
  675. void freeGhostInfo(GhostInfo *);
  676. void ghostWriteStartBlock(ResizeBitStream *stream);
  677. void ghostReadStartBlock(BitStream *stream);
  678. public:
  679. /// Some configuration values.
  680. enum GhostConstants
  681. {
  682. GhostIdBitSize = 12,
  683. MaxGhostCount = 1 << GhostIdBitSize, //4096,
  684. GhostLookupTableSize = 1 << GhostIdBitSize, //4096
  685. GhostIndexBitSize = 4 // number of bits GhostIdBitSize-3 fits into
  686. };
  687. U32 getGhostsActive() { return mGhostsActive;};
  688. /// Are we ghosting to someone?
  689. bool isGhostingTo() { return mLocalGhosts != NULL; };
  690. /// Are we ghosting from someone?
  691. bool isGhostingFrom() { return mGhostArray != NULL; };
  692. /// Called by onRemove, to shut down the ghost subsystem.
  693. void ghostOnRemove();
  694. /// Called when we're done with normal scoping.
  695. ///
  696. /// This gives subclasses a chance to shove things into scope, such as
  697. /// the results of a sensor network calculation, that would otherwise
  698. /// be awkward to add.
  699. virtual void doneScopingScene() { /* null */ }
  700. /// Set the object around which we are currently scoping network traffic.
  701. void setScopeObject(NetObject *object);
  702. /// Get the object aorund which we are currently scoping network traffic.
  703. NetObject *getScopeObject();
  704. /// Add an object to scope.
  705. void objectInScope(NetObject *object);
  706. /// Add an object to scope, marking that it should always be scoped to this connection.
  707. void objectLocalScopeAlways(NetObject *object);
  708. /// Mark an object that is being ghosted as not always needing to be scoped.
  709. ///
  710. /// This undoes objectLocalScopeAlways(), but doesn't immediately flush it from scope.
  711. ///
  712. /// Instead, the standard scoping mechanisms will clear it from scope when it is appropos
  713. /// to do so.
  714. void objectLocalClearAlways(NetObject *object);
  715. /// Get a NetObject* from a ghost ID (on client side).
  716. NetObject *resolveGhost(S32 id);
  717. /// Get a NetObject* from a ghost index (on the server side).
  718. NetObject *resolveObjectFromGhostIndex(S32 id);
  719. /// Get the ghost index corresponding to a given NetObject. This is only
  720. /// meaningful on the server side.
  721. S32 getGhostIndex(NetObject *object);
  722. /// Move a GhostInfo into the nonzero portion of the list (so that we know to update it).
  723. void ghostPushNonZero(GhostInfo *gi);
  724. /// Move a GhostInfo into the zero portion of the list (so that we know not to update it).
  725. void ghostPushToZero(GhostInfo *gi);
  726. /// Move a GhostInfo from the zero portion of the list to the free portion.
  727. void ghostPushZeroToFree(GhostInfo *gi);
  728. /// Move a GhostInfo from the free portion of the list to the zero portion.
  729. inline void ghostPushFreeToZero(GhostInfo *info);
  730. /// Stop all ghosting activity and inform the other side about this.
  731. ///
  732. /// Turns off ghosting.
  733. void resetGhosting();
  734. /// Activate ghosting, once it's enabled.
  735. void activateGhosting();
  736. /// Are we ghosting?
  737. bool isGhosting() { return mGhosting; }
  738. /// Begin to stop ghosting an object.
  739. void detachObject(GhostInfo *info);
  740. /// Mark an object to be always ghosted. Index is the ghost index of the object.
  741. void setGhostAlwaysObject(NetObject *object, U32 index);
  742. /// Send ghost connection handshake message.
  743. ///
  744. /// As part of the ghoost connection process, extensive hand-shaking must be performed.
  745. ///
  746. /// This is done by passing ConnectionMessageEvents; this is a helper function
  747. /// to more effectively perform this task. Messages are dealt with by
  748. /// handleConnectionMessage().
  749. ///
  750. /// @param message One of GhostStates
  751. /// @param sequence A sequence number, if any.
  752. /// @param ghostCount A count of ghosts relating to this message.
  753. void sendConnectionMessage(U32 message, U32 sequence = 0, U32 ghostCount = 0);
  754. /// Handle message from sendConnectionMessage().
  755. ///
  756. /// This is called to handle messages sent via sendConnectionMessage.
  757. ///
  758. /// @param message One of GhostStates
  759. /// @param sequence A sequence number, if any.
  760. /// @param ghostCount A count of ghosts relating to this message.
  761. virtual void handleConnectionMessage(U32 message, U32 sequence, U32 ghostCount);
  762. /// @}
  763. public:
  764. //----------------------------------------------------------------
  765. /// @name File transfer
  766. /// @{
  767. protected:
  768. /// List of files missing for this connection.
  769. ///
  770. /// The currently downloading file is always first in the list (ie, [0]).
  771. Vector<char *> mMissingFileList;
  772. /// Stream for currently uploading file (if any).
  773. Stream *mCurrentDownloadingFile;
  774. /// Storage for currently downloading file.
  775. void *mCurrentFileBuffer;
  776. /// Size of currently downloading file in bytes.
  777. U32 mCurrentFileBufferSize;
  778. /// Our position in the currently downloading file in bytes.
  779. U32 mCurrentFileBufferOffset;
  780. /// Number of files we have downloaded.
  781. U32 mNumDownloadedFiles;
  782. /// Error storage for file transfers.
  783. char mLastFileErrorBuffer[256];
  784. /// Structure to track ghost-always objects and their ghost indices.
  785. struct GhostSave {
  786. NetObject *ghost;
  787. U32 index;
  788. };
  789. /// List of objects to ghost-always.
  790. Vector<GhostSave> mGhostAlwaysSaveList;
  791. public:
  792. /// Start sending the specified file over the link.
  793. bool startSendingFile(const char *fileName);
  794. /// Called when we receive a FileChunkEvent.
  795. void chunkReceived(U8 *chunkData, U32 chunkLen);
  796. /// Get the next file...
  797. void sendNextFileDownloadRequest();
  798. /// Post the next FileChunkEvent.
  799. void sendFileChunk();
  800. /// Called when we finish downloading file data.
  801. virtual void fileDownloadSegmentComplete();
  802. /// This is part of the file transfer logic; basically, we call this
  803. /// every time we finish downloading new files. It attempts to load
  804. /// the GhostAlways objects; if they fail, it marks an error and we
  805. /// have chance to retry.
  806. void loadNextGhostAlwaysObject(bool hadNewFiles);
  807. /// @}
  808. //----------------------------------------------------------------
  809. /// @name Demo Recording
  810. /// @{
  811. private:
  812. Stream *mDemoWriteStream;
  813. Stream *mDemoReadStream;
  814. U32 mDemoNextBlockType;
  815. U32 mDemoNextBlockSize;
  816. U32 mDemoWriteStartTime;
  817. U32 mDemoReadStartTime;
  818. U32 mDemoLastWriteTime;
  819. U32 mDemoRealStartTime;
  820. public:
  821. enum DemoBlockTypes {
  822. BlockTypePacket,
  823. BlockTypeSendPacket,
  824. NetConnectionBlockTypeCount
  825. };
  826. enum DemoConstants {
  827. MaxNumBlockTypes = 16,
  828. MaxBlockSize = 0x1000,
  829. };
  830. bool isRecording()
  831. { return mDemoWriteStream != NULL; }
  832. bool isPlayingBack()
  833. { return mDemoReadStream != NULL; }
  834. U32 getNextBlockType() { return mDemoNextBlockType; }
  835. void recordBlock(U32 type, U32 size, void *data);
  836. virtual void handleRecordedBlock(U32 type, U32 size, void *data);
  837. bool processNextBlock();
  838. bool startDemoRecord(const char *fileName);
  839. bool replayDemoRecord(const char *fileName);
  840. void startDemoRead();
  841. void stopRecording();
  842. void stopDemoPlayback();
  843. virtual void writeDemoStartBlock(ResizeBitStream *stream);
  844. virtual bool readDemoStartBlock(BitStream *stream);
  845. virtual void demoPlaybackComplete();
  846. /// @}
  847. };
  848. //----------------------------------------------------------------------------
  849. /// Information about a ghosted object.
  850. ///
  851. /// @note If the size of this structure changes, the
  852. /// NetConnection::getGhostIndex function MUST be changed
  853. /// to reflect the new size.
  854. struct GhostInfo
  855. {
  856. public: // required for MSVC
  857. NetObject *obj; ///< The object being ghosted.
  858. U32 updateMask; ///< Flags indicating what state data needs to be transferred.
  859. U32 updateSkipCount; ///< How many updates have we skipped this guy?
  860. U32 flags; ///< Flags from GhostInfo::Flags
  861. F32 priority; ///< A float value indicating the priority of this object for
  862. /// updates.
  863. /// @name References
  864. ///
  865. /// The GhostInfo structure is used in several linked lists; these members are
  866. /// the implementation for this.
  867. /// @{
  868. NetConnection::GhostRef *updateChain; ///< List of references in NetConnections to us.
  869. GhostInfo *nextObjectRef; ///< Next ghosted object.
  870. GhostInfo *prevObjectRef; ///< Previous ghosted object.
  871. NetConnection *connection; ///< Connection that we're ghosting over.
  872. GhostInfo *nextLookupInfo; ///< GhostInfo references are stored in a hash; this is the bucket
  873. /// implementation.
  874. /// @}
  875. U32 index;
  876. U32 arrayIndex;
  877. /// Flags relating to the state of the object.
  878. enum Flags
  879. {
  880. Valid = BIT(0),
  881. InScope = BIT(1),
  882. ScopeAlways = BIT(2),
  883. NotYetGhosted = BIT(3),
  884. Ghosting = BIT(4),
  885. KillGhost = BIT(5),
  886. KillingGhost = BIT(6),
  887. ScopedEvent = BIT(7),
  888. ScopeLocalAlways = BIT(8),
  889. };
  890. };
  891. inline void NetConnection::ghostPushNonZero(GhostInfo *info)
  892. {
  893. AssertFatal(info->arrayIndex >= mGhostZeroUpdateIndex && info->arrayIndex < mGhostFreeIndex, "Out of range arrayIndex.");
  894. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  895. if(info->arrayIndex != mGhostZeroUpdateIndex)
  896. {
  897. mGhostArray[mGhostZeroUpdateIndex]->arrayIndex = info->arrayIndex;
  898. mGhostArray[info->arrayIndex] = mGhostArray[mGhostZeroUpdateIndex];
  899. mGhostArray[mGhostZeroUpdateIndex] = info;
  900. info->arrayIndex = mGhostZeroUpdateIndex;
  901. }
  902. mGhostZeroUpdateIndex++;
  903. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  904. }
  905. inline void NetConnection::ghostPushToZero(GhostInfo *info)
  906. {
  907. AssertFatal(info->arrayIndex < mGhostZeroUpdateIndex, "Out of range arrayIndex.");
  908. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  909. mGhostZeroUpdateIndex--;
  910. if(info->arrayIndex != mGhostZeroUpdateIndex)
  911. {
  912. mGhostArray[mGhostZeroUpdateIndex]->arrayIndex = info->arrayIndex;
  913. mGhostArray[info->arrayIndex] = mGhostArray[mGhostZeroUpdateIndex];
  914. mGhostArray[mGhostZeroUpdateIndex] = info;
  915. info->arrayIndex = mGhostZeroUpdateIndex;
  916. }
  917. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  918. }
  919. inline void NetConnection::ghostPushZeroToFree(GhostInfo *info)
  920. {
  921. AssertFatal(info->arrayIndex >= mGhostZeroUpdateIndex && info->arrayIndex < mGhostFreeIndex, "Out of range arrayIndex.");
  922. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  923. mGhostFreeIndex--;
  924. if(info->arrayIndex != mGhostFreeIndex)
  925. {
  926. mGhostArray[mGhostFreeIndex]->arrayIndex = info->arrayIndex;
  927. mGhostArray[info->arrayIndex] = mGhostArray[mGhostFreeIndex];
  928. mGhostArray[mGhostFreeIndex] = info;
  929. info->arrayIndex = mGhostFreeIndex;
  930. }
  931. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  932. }
  933. inline void NetConnection::ghostPushFreeToZero(GhostInfo *info)
  934. {
  935. AssertFatal(info->arrayIndex >= mGhostFreeIndex, "Out of range arrayIndex.");
  936. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  937. if(info->arrayIndex != mGhostFreeIndex)
  938. {
  939. mGhostArray[mGhostFreeIndex]->arrayIndex = info->arrayIndex;
  940. mGhostArray[info->arrayIndex] = mGhostArray[mGhostFreeIndex];
  941. mGhostArray[mGhostFreeIndex] = info;
  942. info->arrayIndex = mGhostFreeIndex;
  943. }
  944. mGhostFreeIndex++;
  945. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  946. }
  947. #endif