netConnection.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. ConcreteClassRep<className> className::dynClassRep(#className,NetClassGroupGameMask, NetClassTypeEvent, NetEventDirAny, className::getParentStaticClassRep())
  273. #define IMPLEMENT_CO_CLIENTEVENT_V1(className) \
  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,NetClassGroupGameMask, NetClassTypeEvent, NetEventDirServerToClient, className::getParentStaticClassRep())
  278. #define IMPLEMENT_CO_SERVEREVENT_V1(className) \
  279. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  280. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  281. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  282. ConcreteClassRep<className> className::dynClassRep(#className,NetClassGroupGameMask, NetClassTypeEvent, NetEventDirClientToServer, className::getParentStaticClassRep())
  283. #define IMPLEMENT_CO_NETEVENT(className,groupMask) \
  284. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  285. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  286. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  287. ConcreteClassRep<className> className::dynClassRep(#className,groupMask, NetClassTypeEvent, NetEventDirAny, className::getParentStaticClassRep())
  288. #define IMPLEMENT_CO_CLIENTEVENT(className,groupMask) \
  289. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  290. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  291. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  292. ConcreteClassRep<className> className::dynClassRep(#className,groupMask, NetClassTypeEvent, NetEventDirServerToClient, className::getParentStaticClassRep())
  293. #define IMPLEMENT_CO_SERVEREVENT(className,groupMask) \
  294. AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
  295. AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
  296. AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
  297. ConcreteClassRep<className> className::dynClassRep(#className,groupMask, NetClassTypeEvent, NetEventDirClientToServer, className::getParentStaticClassRep())
  298. //----------------------------------------------------------------------------
  299. /// Torque network connection.
  300. ///
  301. /// @section NetConnection_intro Introduction
  302. ///
  303. /// NetConnection is the glue that binds a networked Torque game together. It combines
  304. /// the low-level notify protocol implemented in ConnectionProtocol with a SimGroup to
  305. /// provide a powerful basis for implementing a multiplayer game protocol.
  306. ///
  307. /// On top of this basis it implements several distinct subsystems:
  308. /// - <b>Event manager</b>, which is responsible for transmitting NetEvents over the wire.
  309. /// It deals with ensuring that the various types of NetEvents are delivered appropriately,
  310. /// and with notifying the event of its delivery status.
  311. /// - <b>Move manager</b>, which is responsible for transferring a Move to the server 32
  312. /// times a second (on the client) and applying it to the control object (on the server).
  313. /// - <b>Ghost manager</b>, which is responsible for doing scoping calculations (on the server
  314. /// side) and transmitting most-recent ghost information to the client.
  315. /// - <b>File transfer</b>; it is often the case that clients will lack important files when
  316. /// connecting to a server which is running a mod or new map. This subsystem allows the
  317. /// server to transfer such files to the client.
  318. /// - <b>Networked String Table</b>; string data can easily soak up network bandwidth, so for
  319. /// efficiency, we implement a networked string table. We can then notify the connection
  320. /// of strings we will reference often, such as player names, and transmit only a tag,
  321. /// instead of the whole string.
  322. /// - <b>Demo Recording</b> is also implemented in NetConnection. A demo in Torque is a log
  323. /// of the network traffic between client and server; when a NetConnection records a demo,
  324. /// it simply logs this data to a file. When it plays a demo back, it replays the logged
  325. /// data.
  326. /// - The <b>Connection Database</b> is used to keep track of all the NetConnections; it can
  327. /// be iterated over (for instance, to send an event to all active connections), or queried
  328. /// by address.
  329. ///
  330. /// @section NetConnection_events On Events
  331. ///
  332. /// The Event Manager is exposed to the outside world via postNetEvent(), which accepts NetEvents.
  333. ///
  334. /// @see NetEvent for a more thorough explanation of how to use events.
  335. ///
  336. /// @section NetConnection_ghosting On Ghosting and Scoping
  337. ///
  338. /// Ghosting is the most complex, and most powerful, part of Torque's networking capabilities. It
  339. /// allows the information sent to clients to be very precisely matched to what they need, so that
  340. /// no excess bandwidth is wasted. The control object's onCameraScopeQuery() is called, to determine
  341. /// scoping information for the client; then objects which are in scope are then transmitted to the
  342. /// client, prioritized by the results of their getPriority() method.
  343. ///
  344. /// There is a cap on the maximum number of ghosts; ghost IDs are currently sent via a 10-bit field,
  345. /// ergo, there is a cap of 1024 objects ghosted per client. This can be easily raised; see the
  346. /// GhostConstants enum.
  347. ///
  348. /// Each object ghosted is assigned a ghost ID; the client is _only_ aware of the ghost ID. This acts
  349. /// to enhance game security, as it becomes difficult to map objects from one connection to another, or
  350. /// to reliably identify objects from ID alone. IDs are also reassigned based on need, making it hard
  351. /// to track objects that have fallen out of scope (as any object which the player shouldn't see would).
  352. ///
  353. /// resolveGhost() is used on the client side, and resolveObjectFromGhostIndex() on the server side, to
  354. /// turn ghost IDs into object references.
  355. ///
  356. /// The NetConnection is a SimGroup. On the client side, it contains all the objects which have been
  357. /// ghosted to that client. On the server side, it is empty; it can be used (typically in script) to
  358. /// hold objects related to the connection. For instance, you might place an observation camera in the
  359. /// NetConnnection. In both cases, when the connection is destroyed, so are the contained objects.
  360. ///
  361. /// @see NetObject, which is the superclass for ghostable objects, and ShapeBase, which is the base
  362. /// for player and vehicle classes.
  363. ///
  364. /// @nosubgrouping
  365. class NetConnection : public ConnectionProtocol, public SimGroup
  366. {
  367. friend class NetInterface;
  368. typedef SimGroup Parent;
  369. public:
  370. /// Structure to track ghost references in packets.
  371. ///
  372. /// Every packet we send out with an update from a ghost causes one of these to be
  373. /// allocated. mask is used to track what states were sent; that way if a packet is
  374. /// dropped, we can easily manipulate the stored states and figure out what if any data
  375. /// we need to resend.
  376. ///
  377. struct GhostRef
  378. {
  379. U32 mask; ///< States we transmitted.
  380. U32 ghostInfoFlags; ///< Flags from GhostInfo::Flags
  381. GhostInfo *ghost; ///< Reference to the GhostInfo we're from.
  382. GhostRef *nextRef; ///< Next GhostRef in this packet.
  383. GhostRef *nextUpdateChain; ///< Next update we sent for this ghost.
  384. };
  385. enum Constants
  386. {
  387. HashTableSize = 127,
  388. };
  389. void sendDisconnectPacket(const char *reason);
  390. virtual bool canRemoteCreate();
  391. virtual void onTimedOut();
  392. virtual void onConnectTimedOut();
  393. virtual void onDisconnect(const char *reason);
  394. virtual void onConnectionRejected(const char *reason);
  395. virtual void onConnectionEstablished(bool isInitiator);
  396. virtual void handleStartupError(const char *errorString);
  397. virtual void writeConnectRequest(BitStream *stream);
  398. virtual bool readConnectRequest(BitStream *stream, const char **errorString);
  399. virtual void writeConnectAccept(BitStream *stream);
  400. virtual bool readConnectAccept(BitStream *stream, const char **errorString);
  401. void connect(const NetAddress *address);
  402. //----------------------------------------------------------------
  403. /// @name Global Connection List
  404. /// @{
  405. private:
  406. ///
  407. NetConnection *mNextConnection; ///< Next item in list.
  408. NetConnection *mPrevConnection; ///< Previous item in list.
  409. static NetConnection *mConnectionList; ///< Head of list.
  410. public:
  411. static NetConnection *getConnectionList() { return mConnectionList; }
  412. NetConnection *getNext() { return mNextConnection; }
  413. /// @}
  414. //----------------------------------------------------------------
  415. enum NetConnectionFlags
  416. {
  417. ConnectionToServer = BIT(0),
  418. ConnectionToClient = BIT(1),
  419. LocalClientConnection = BIT(2),
  420. NetworkConnection = BIT(3),
  421. };
  422. private:
  423. BitSet32 mTypeFlags;
  424. U32 mNetClassGroup; ///< The NetClassGroup of this connection.
  425. /// @name Statistics
  426. /// @{
  427. U32 mLastUpdateTime;
  428. F32 mRoundTripTime;
  429. F32 mPacketLoss;
  430. U32 mSimulatedPing;
  431. F32 mSimulatedPacketLoss;
  432. /// @}
  433. /// @name State
  434. /// @{
  435. U32 mProtocolVersion;
  436. U32 mSendDelayCredit;
  437. U32 mConnectSequence;
  438. U32 mAddressDigest[4];
  439. bool mEstablished;
  440. bool mMissionPathsSent;
  441. struct NetRate
  442. {
  443. U32 updateDelay;
  444. S32 packetSize;
  445. bool changed;
  446. };
  447. NetRate mCurRate;
  448. NetRate mMaxRate;
  449. /// If we're doing a "short circuited" connection, this stores
  450. /// a pointer to the other side.
  451. SimObjectPtr<NetConnection> mRemoteConnection;
  452. NetAddress mNetAddress;
  453. /// @}
  454. /// @name Timeout Management
  455. /// @{
  456. U32 mPingSendCount;
  457. U32 mPingRetryCount;
  458. U32 mLastPingSendTime;
  459. /// @}
  460. /// @name Connection Table
  461. ///
  462. /// We store our connections on a hash table so we can
  463. /// quickly find them.
  464. /// @{
  465. NetConnection *mNextTableHash;
  466. static NetConnection *mHashTable[HashTableSize];
  467. /// @}
  468. protected:
  469. static SimObjectPtr<NetConnection> mServerConnection;
  470. static SimObjectPtr<NetConnection> mLocalClientConnection;
  471. static bool mFilesWereDownloaded;
  472. U32 mConnectSendCount;
  473. U32 mConnectLastSendTime;
  474. public:
  475. static NetConnection *getConnectionToServer() { return mServerConnection; }
  476. static NetConnection *getLocalClientConnection() { return mLocalClientConnection; }
  477. static void setLocalClientConnection(NetConnection *conn) { mLocalClientConnection = conn; }
  478. U32 getNetClassGroup() { return mNetClassGroup; }
  479. static bool filesWereDownloaded() { return mFilesWereDownloaded; }
  480. static char *getErrorBuffer() { return mErrorBuffer; }
  481. #ifdef TORQUE_DEBUG_NET
  482. bool mLogging;
  483. void setLogging(bool logging) { mLogging = logging; }
  484. #endif
  485. void setSimulatedNetParams(F32 packetLoss, U32 ping)
  486. { mSimulatedPacketLoss = packetLoss; mSimulatedPing = ping; }
  487. bool isConnectionToServer() { return mTypeFlags.test(ConnectionToServer); }
  488. bool isLocalConnection() { return !mRemoteConnection.isNull() ; }
  489. bool isNetworkConnection() { return mTypeFlags.test(NetworkConnection); }
  490. void setIsConnectionToServer() { mTypeFlags.set(ConnectionToServer); }
  491. void setIsLocalClientConnection() { mTypeFlags.set(LocalClientConnection); }
  492. void setNetworkConnection(bool net) { mTypeFlags.set(BitSet32(NetworkConnection), net); }
  493. virtual void setEstablished();
  494. /// Call this if the "connection" is local to this app. This short-circuits the protocol layer.
  495. void setRemoteConnectionObject(NetConnection *connection) { mRemoteConnection = connection; };
  496. void setSequence(U32 connectSequence);
  497. void setAddressDigest(U32 digest[4]);
  498. void getAddressDigest(U32 digest[4]);
  499. U32 getSequence();
  500. void setProtocolVersion(U32 protocolVersion) { mProtocolVersion = protocolVersion; }
  501. U32 getProtocolVersion() { return mProtocolVersion; }
  502. F32 getRoundTripTime() { return mRoundTripTime; }
  503. F32 getPacketLoss() { return( mPacketLoss ); }
  504. static char mErrorBuffer[256];
  505. static void setLastError(const char *fmt,...);
  506. void checkMaxRate();
  507. void handlePacket(BitStream *stream);
  508. void processRawPacket(BitStream *stream);
  509. void handleNotify(bool recvd);
  510. void handleConnectionEstablished();
  511. void keepAlive();
  512. const NetAddress *getNetAddress();
  513. void setNetAddress(const NetAddress *address);
  514. Net::Error sendPacket(BitStream *stream);
  515. private:
  516. void netAddressTableInsert();
  517. void netAddressTableRemove();
  518. public:
  519. /// Find a NetConnection, if any, with the specified address.
  520. static NetConnection *lookup(const NetAddress *remoteAddress);
  521. bool checkTimeout(U32 time); ///< returns true if the connection timed out
  522. void checkPacketSend(bool force);
  523. bool missionPathsSent() const { return mMissionPathsSent; }
  524. void setMissionPathsSent(const bool s) { mMissionPathsSent = s; }
  525. static void consoleInit();
  526. void onRemove();
  527. NetConnection();
  528. ~NetConnection();
  529. public:
  530. enum NetConnectionState
  531. {
  532. NotConnected,
  533. AwaitingChallengeResponse, ///< We've sent a challenge request, awaiting the response.
  534. AwaitingConnectRequest, ///< We've received a challenge request and sent a challenge response.
  535. AwaitingConnectResponse, ///< We've received a challenge response and sent a connect request.
  536. Connected, ///< We've accepted a connect request, or we've received a connect response accept.
  537. };
  538. U32 mConnectionSendCount; ///< number of connection messages we've sent.
  539. U32 mConnectionState; ///< State of the connection, from NetConnectionState.
  540. void setConnectionState(U32 state) { mConnectionState = state; }
  541. U32 getConnectionState() { return mConnectionState; }
  542. void setGhostFrom(bool ghostFrom); ///< Sets whether ghosts transmit from this side of the connection.
  543. void setGhostTo(bool ghostTo); ///< Sets whether ghosts are allowed from the other side of the connection.
  544. void setSendingEvents(bool sending); ///< Sets whether this side actually sends the events that are posted to it.
  545. void setTranslatesStrings(bool xl); ///< Sets whether this connection is capable of translating strings.
  546. void setNetClassGroup(U32 group); ///< Sets the group of NetClasses this connection traffics in.
  547. bool isEstablished() { return mEstablished; } ///< Is the connection established?
  548. DECLARE_CONOBJECT(NetConnection);
  549. /// Structure to track packets and what we sent over them.
  550. ///
  551. /// We need to know what is sent in each packet, so that if a packet is
  552. /// dropped, we know what to resend. This is the structure we use to track
  553. /// this data.
  554. struct PacketNotify
  555. {
  556. bool rateChanged; ///< Did the rate change on this packet?
  557. bool maxRateChanged; ///< Did the max rate change on this packet?
  558. U32 sendTime; ///< Timestampe, when we sent this packet.
  559. NetEventNote *eventList; ///< Linked list of events sent over this packet.
  560. GhostRef *ghostList; ///< Linked list of ghost updates we sent in this packet.
  561. SubPacketRef *subList; ///< Defined by subclass - used as desired.
  562. PacketNotify *nextPacket; ///< Next packet sent.
  563. PacketNotify();
  564. };
  565. virtual PacketNotify *allocNotify();
  566. PacketNotify *mNotifyQueueHead; ///< Head of packet notify list.
  567. PacketNotify *mNotifyQueueTail; ///< Tail of packet notify list.
  568. protected:
  569. virtual void readPacket(BitStream *bstream);
  570. virtual void writePacket(BitStream *bstream, PacketNotify *note);
  571. virtual void packetReceived(PacketNotify *note);
  572. virtual void packetDropped(PacketNotify *note);
  573. virtual void connectionError(const char *errorString);
  574. //----------------------------------------------------------------
  575. /// @name Event Manager
  576. /// @{
  577. private:
  578. NetEventNote *mSendEventQueueHead;
  579. NetEventNote *mSendEventQueueTail;
  580. NetEventNote *mUnorderedSendEventQueueHead;
  581. NetEventNote *mUnorderedSendEventQueueTail;
  582. NetEventNote *mWaitSeqEvents;
  583. NetEventNote *mNotifyEventList;
  584. static FreeListChunker<NetEventNote> mEventNoteChunker;
  585. bool mSendingEvents;
  586. S32 mNextSendEventSeq;
  587. S32 mNextRecvEventSeq;
  588. S32 mLastAckedEventSeq;
  589. enum NetEventConstants {
  590. InvalidSendEventSeq = -1,
  591. FirstValidSendEventSeq = 0
  592. };
  593. void eventOnRemove();
  594. void eventPacketDropped(PacketNotify *notify);
  595. void eventPacketReceived(PacketNotify *notify);
  596. void eventWritePacket(BitStream *bstream, PacketNotify *notify);
  597. void eventReadPacket(BitStream *bstream);
  598. void eventWriteStartBlock(ResizeBitStream *stream);
  599. void eventReadStartBlock(BitStream *stream);
  600. public:
  601. /// Post an event to this connection.
  602. bool postNetEvent(NetEvent *event);
  603. /// @}
  604. //----------------------------------------------------------------
  605. /// @name Networked string table
  606. /// @{
  607. private:
  608. bool mTranslateStrings;
  609. ConnectionStringTable *mStringTable;
  610. public:
  611. void mapString(U32 netId, NetStringHandle &string)
  612. { mStringTable->mapString(netId, string); }
  613. U32 checkString(NetStringHandle &string, bool *isOnOtherSide = NULL)
  614. { if(mStringTable) return mStringTable->checkString(string, isOnOtherSide); else return 0; }
  615. U32 getNetSendId(NetStringHandle &string)
  616. { if(mStringTable) return mStringTable->getNetSendId(string); else return 0;}
  617. void confirmStringReceived(NetStringHandle &string, U32 index)
  618. { if(!isRemoved()) mStringTable->confirmStringReceived(string, index); }
  619. NetStringHandle translateRemoteStringId(U32 id) { return mStringTable->lookupString(id); }
  620. void validateSendString(const char *str);
  621. void packString(BitStream *stream, const char *str);
  622. void unpackString(BitStream *stream, char readBuffer[1024]);
  623. void packNetStringHandleU(BitStream *stream, NetStringHandle &h);
  624. NetStringHandle unpackNetStringHandleU(BitStream *stream);
  625. /// @}
  626. //----------------------------------------------------------------
  627. /// @name Ghost manager
  628. /// @{
  629. protected:
  630. enum GhostStates
  631. {
  632. GhostAlwaysDone,
  633. ReadyForNormalGhosts,
  634. EndGhosting,
  635. GhostAlwaysStarting,
  636. SendNextDownloadRequest,
  637. FileDownloadSizeMessage,
  638. NumConnectionMessages,
  639. };
  640. GhostInfo **mGhostArray; ///< Linked list of ghostInfos ghosted by this side of the connection
  641. U32 mGhostZeroUpdateIndex; ///< Index in mGhostArray of first ghost with 0 update mask.
  642. U32 mGhostFreeIndex; ///< Index in mGhostArray of first free ghost.
  643. U32 mGhostsActive; ///- Track actve ghosts on client side
  644. bool mGhosting; ///< Am I currently ghosting objects?
  645. bool mScoping; ///< am I currently scoping objects?
  646. U32 mGhostingSequence; ///< Sequence number describing this ghosting session.
  647. NetObject **mLocalGhosts; ///< Local ghost for remote object.
  648. ///
  649. /// mLocalGhosts pointer is NULL if mGhostTo is false
  650. GhostInfo *mGhostRefs; ///< Allocated array of ghostInfos. Null if ghostFrom is false.
  651. GhostInfo **mGhostLookupTable; ///< Table indexed by object id to GhostInfo. Null if ghostFrom is false.
  652. /// The object around which we are scoping this connection.
  653. ///
  654. /// This is usually the player object, or a related object, like a vehicle
  655. /// that the player is driving.
  656. SimObjectPtr<NetObject> mScopeObject;
  657. void clearGhostInfo();
  658. bool validateGhostArray();
  659. void ghostPacketDropped(PacketNotify *notify);
  660. void ghostPacketReceived(PacketNotify *notify);
  661. void ghostWritePacket(BitStream *bstream, PacketNotify *notify);
  662. void ghostReadPacket(BitStream *bstream);
  663. void freeGhostInfo(GhostInfo *);
  664. void ghostWriteStartBlock(ResizeBitStream *stream);
  665. void ghostReadStartBlock(BitStream *stream);
  666. public:
  667. /// Some configuration values.
  668. enum GhostConstants
  669. {
  670. GhostIdBitSize = 12,
  671. MaxGhostCount = 1 << GhostIdBitSize, //4096,
  672. GhostLookupTableSize = 1 << GhostIdBitSize, //4096
  673. GhostIndexBitSize = 4 // number of bits GhostIdBitSize-3 fits into
  674. };
  675. U32 getGhostsActive() { return mGhostsActive;};
  676. /// Are we ghosting to someone?
  677. bool isGhostingTo() { return mLocalGhosts != NULL; };
  678. /// Are we ghosting from someone?
  679. bool isGhostingFrom() { return mGhostArray != NULL; };
  680. /// Called by onRemove, to shut down the ghost subsystem.
  681. void ghostOnRemove();
  682. /// Called when we're done with normal scoping.
  683. ///
  684. /// This gives subclasses a chance to shove things into scope, such as
  685. /// the results of a sensor network calculation, that would otherwise
  686. /// be awkward to add.
  687. virtual void doneScopingScene() { /* null */ }
  688. /// Set the object around which we are currently scoping network traffic.
  689. void setScopeObject(NetObject *object);
  690. /// Get the object aorund which we are currently scoping network traffic.
  691. NetObject *getScopeObject();
  692. /// Add an object to scope.
  693. void objectInScope(NetObject *object);
  694. /// Add an object to scope, marking that it should always be scoped to this connection.
  695. void objectLocalScopeAlways(NetObject *object);
  696. /// Mark an object that is being ghosted as not always needing to be scoped.
  697. ///
  698. /// This undoes objectLocalScopeAlways(), but doesn't immediately flush it from scope.
  699. ///
  700. /// Instead, the standard scoping mechanisms will clear it from scope when it is appropos
  701. /// to do so.
  702. void objectLocalClearAlways(NetObject *object);
  703. /// Get a NetObject* from a ghost ID (on client side).
  704. NetObject *resolveGhost(S32 id);
  705. /// Get a NetObject* from a ghost index (on the server side).
  706. NetObject *resolveObjectFromGhostIndex(S32 id);
  707. /// Get the ghost index corresponding to a given NetObject. This is only
  708. /// meaningful on the server side.
  709. S32 getGhostIndex(NetObject *object);
  710. /// Move a GhostInfo into the nonzero portion of the list (so that we know to update it).
  711. void ghostPushNonZero(GhostInfo *gi);
  712. /// Move a GhostInfo into the zero portion of the list (so that we know not to update it).
  713. void ghostPushToZero(GhostInfo *gi);
  714. /// Move a GhostInfo from the zero portion of the list to the free portion.
  715. void ghostPushZeroToFree(GhostInfo *gi);
  716. /// Move a GhostInfo from the free portion of the list to the zero portion.
  717. inline void ghostPushFreeToZero(GhostInfo *info);
  718. /// Stop all ghosting activity and inform the other side about this.
  719. ///
  720. /// Turns off ghosting.
  721. void resetGhosting();
  722. /// Activate ghosting, once it's enabled.
  723. void activateGhosting();
  724. /// Are we ghosting?
  725. bool isGhosting() { return mGhosting; }
  726. /// Begin to stop ghosting an object.
  727. void detachObject(GhostInfo *info);
  728. /// Mark an object to be always ghosted. Index is the ghost index of the object.
  729. void setGhostAlwaysObject(NetObject *object, U32 index);
  730. /// Send ghost connection handshake message.
  731. ///
  732. /// As part of the ghoost connection process, extensive hand-shaking must be performed.
  733. ///
  734. /// This is done by passing ConnectionMessageEvents; this is a helper function
  735. /// to more effectively perform this task. Messages are dealt with by
  736. /// handleConnectionMessage().
  737. ///
  738. /// @param message One of GhostStates
  739. /// @param sequence A sequence number, if any.
  740. /// @param ghostCount A count of ghosts relating to this message.
  741. void sendConnectionMessage(U32 message, U32 sequence = 0, U32 ghostCount = 0);
  742. /// Handle message from sendConnectionMessage().
  743. ///
  744. /// This is called to handle messages sent via sendConnectionMessage.
  745. ///
  746. /// @param message One of GhostStates
  747. /// @param sequence A sequence number, if any.
  748. /// @param ghostCount A count of ghosts relating to this message.
  749. virtual void handleConnectionMessage(U32 message, U32 sequence, U32 ghostCount);
  750. /// @}
  751. public:
  752. //----------------------------------------------------------------
  753. /// @name File transfer
  754. /// @{
  755. protected:
  756. /// List of files missing for this connection.
  757. ///
  758. /// The currently downloading file is always first in the list (ie, [0]).
  759. Vector<char *> mMissingFileList;
  760. /// Stream for currently uploading file (if any).
  761. Stream *mCurrentDownloadingFile;
  762. /// Storage for currently downloading file.
  763. void *mCurrentFileBuffer;
  764. /// Size of currently downloading file in bytes.
  765. U32 mCurrentFileBufferSize;
  766. /// Our position in the currently downloading file in bytes.
  767. U32 mCurrentFileBufferOffset;
  768. /// Number of files we have downloaded.
  769. U32 mNumDownloadedFiles;
  770. /// Error storage for file transfers.
  771. char mLastFileErrorBuffer[256];
  772. /// Structure to track ghost-always objects and their ghost indices.
  773. struct GhostSave {
  774. NetObject *ghost;
  775. U32 index;
  776. };
  777. /// List of objects to ghost-always.
  778. Vector<GhostSave> mGhostAlwaysSaveList;
  779. public:
  780. /// Start sending the specified file over the link.
  781. bool startSendingFile(const char *fileName);
  782. /// Called when we receive a FileChunkEvent.
  783. void chunkReceived(U8 *chunkData, U32 chunkLen);
  784. /// Get the next file...
  785. void sendNextFileDownloadRequest();
  786. /// Post the next FileChunkEvent.
  787. void sendFileChunk();
  788. /// Called when we finish downloading file data.
  789. virtual void fileDownloadSegmentComplete();
  790. /// This is part of the file transfer logic; basically, we call this
  791. /// every time we finish downloading new files. It attempts to load
  792. /// the GhostAlways objects; if they fail, it marks an error and we
  793. /// have chance to retry.
  794. void loadNextGhostAlwaysObject(bool hadNewFiles);
  795. /// @}
  796. //----------------------------------------------------------------
  797. /// @name Demo Recording
  798. /// @{
  799. private:
  800. Stream *mDemoWriteStream;
  801. Stream *mDemoReadStream;
  802. U32 mDemoNextBlockType;
  803. U32 mDemoNextBlockSize;
  804. U32 mDemoWriteStartTime;
  805. U32 mDemoReadStartTime;
  806. U32 mDemoLastWriteTime;
  807. U32 mDemoRealStartTime;
  808. public:
  809. enum DemoBlockTypes {
  810. BlockTypePacket,
  811. BlockTypeSendPacket,
  812. NetConnectionBlockTypeCount
  813. };
  814. enum DemoConstants {
  815. MaxNumBlockTypes = 16,
  816. MaxBlockSize = 0x1000,
  817. };
  818. bool isRecording()
  819. { return mDemoWriteStream != NULL; }
  820. bool isPlayingBack()
  821. { return mDemoReadStream != NULL; }
  822. U32 getNextBlockType() { return mDemoNextBlockType; }
  823. void recordBlock(U32 type, U32 size, void *data);
  824. virtual void handleRecordedBlock(U32 type, U32 size, void *data);
  825. bool processNextBlock();
  826. bool startDemoRecord(const char *fileName);
  827. bool replayDemoRecord(const char *fileName);
  828. void startDemoRead();
  829. void stopRecording();
  830. void stopDemoPlayback();
  831. virtual void writeDemoStartBlock(ResizeBitStream *stream);
  832. virtual bool readDemoStartBlock(BitStream *stream);
  833. virtual void demoPlaybackComplete();
  834. /// @}
  835. };
  836. //----------------------------------------------------------------------------
  837. /// Information about a ghosted object.
  838. ///
  839. /// @note If the size of this structure changes, the
  840. /// NetConnection::getGhostIndex function MUST be changed
  841. /// to reflect the new size.
  842. struct GhostInfo
  843. {
  844. public: // required for MSVC
  845. NetObject *obj; ///< The object being ghosted.
  846. U32 updateMask; ///< Flags indicating what state data needs to be transferred.
  847. U32 updateSkipCount; ///< How many updates have we skipped this guy?
  848. U32 flags; ///< Flags from GhostInfo::Flags
  849. F32 priority; ///< A float value indicating the priority of this object for
  850. /// updates.
  851. /// @name References
  852. ///
  853. /// The GhostInfo structure is used in several linked lists; these members are
  854. /// the implementation for this.
  855. /// @{
  856. NetConnection::GhostRef *updateChain; ///< List of references in NetConnections to us.
  857. GhostInfo *nextObjectRef; ///< Next ghosted object.
  858. GhostInfo *prevObjectRef; ///< Previous ghosted object.
  859. NetConnection *connection; ///< Connection that we're ghosting over.
  860. GhostInfo *nextLookupInfo; ///< GhostInfo references are stored in a hash; this is the bucket
  861. /// implementation.
  862. /// @}
  863. U32 index;
  864. U32 arrayIndex;
  865. /// Flags relating to the state of the object.
  866. enum Flags
  867. {
  868. Valid = BIT(0),
  869. InScope = BIT(1),
  870. ScopeAlways = BIT(2),
  871. NotYetGhosted = BIT(3),
  872. Ghosting = BIT(4),
  873. KillGhost = BIT(5),
  874. KillingGhost = BIT(6),
  875. ScopedEvent = BIT(7),
  876. ScopeLocalAlways = BIT(8),
  877. };
  878. };
  879. inline void NetConnection::ghostPushNonZero(GhostInfo *info)
  880. {
  881. AssertFatal(info->arrayIndex >= mGhostZeroUpdateIndex && info->arrayIndex < mGhostFreeIndex, "Out of range arrayIndex.");
  882. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  883. if(info->arrayIndex != mGhostZeroUpdateIndex)
  884. {
  885. mGhostArray[mGhostZeroUpdateIndex]->arrayIndex = info->arrayIndex;
  886. mGhostArray[info->arrayIndex] = mGhostArray[mGhostZeroUpdateIndex];
  887. mGhostArray[mGhostZeroUpdateIndex] = info;
  888. info->arrayIndex = mGhostZeroUpdateIndex;
  889. }
  890. mGhostZeroUpdateIndex++;
  891. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  892. }
  893. inline void NetConnection::ghostPushToZero(GhostInfo *info)
  894. {
  895. AssertFatal(info->arrayIndex < mGhostZeroUpdateIndex, "Out of range arrayIndex.");
  896. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  897. mGhostZeroUpdateIndex--;
  898. if(info->arrayIndex != mGhostZeroUpdateIndex)
  899. {
  900. mGhostArray[mGhostZeroUpdateIndex]->arrayIndex = info->arrayIndex;
  901. mGhostArray[info->arrayIndex] = mGhostArray[mGhostZeroUpdateIndex];
  902. mGhostArray[mGhostZeroUpdateIndex] = info;
  903. info->arrayIndex = mGhostZeroUpdateIndex;
  904. }
  905. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  906. }
  907. inline void NetConnection::ghostPushZeroToFree(GhostInfo *info)
  908. {
  909. AssertFatal(info->arrayIndex >= mGhostZeroUpdateIndex && info->arrayIndex < mGhostFreeIndex, "Out of range arrayIndex.");
  910. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  911. mGhostFreeIndex--;
  912. if(info->arrayIndex != mGhostFreeIndex)
  913. {
  914. mGhostArray[mGhostFreeIndex]->arrayIndex = info->arrayIndex;
  915. mGhostArray[info->arrayIndex] = mGhostArray[mGhostFreeIndex];
  916. mGhostArray[mGhostFreeIndex] = info;
  917. info->arrayIndex = mGhostFreeIndex;
  918. }
  919. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  920. }
  921. inline void NetConnection::ghostPushFreeToZero(GhostInfo *info)
  922. {
  923. AssertFatal(info->arrayIndex >= mGhostFreeIndex, "Out of range arrayIndex.");
  924. AssertFatal(mGhostArray[info->arrayIndex] == info, "Invalid array object.");
  925. if(info->arrayIndex != mGhostFreeIndex)
  926. {
  927. mGhostArray[mGhostFreeIndex]->arrayIndex = info->arrayIndex;
  928. mGhostArray[info->arrayIndex] = mGhostArray[mGhostFreeIndex];
  929. mGhostArray[mGhostFreeIndex] = info;
  930. info->arrayIndex = mGhostFreeIndex;
  931. }
  932. mGhostFreeIndex++;
  933. //AssertFatal(validateGhostArray(), "Invalid ghost array!");
  934. }
  935. #endif