netEvent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/dnet.h"
  24. #include "console/simBase.h"
  25. #include "sim/netConnection.h"
  26. #include "core/stream/bitStream.h"
  27. #define DebugChecksum 0xF00DBAAD
  28. FreeListChunker<NetEventNote> NetConnection::mEventNoteChunker;
  29. NetEvent::~NetEvent()
  30. {
  31. }
  32. void NetEvent::notifyDelivered(NetConnection *, bool)
  33. {
  34. }
  35. void NetEvent::notifySent(NetConnection *)
  36. {
  37. }
  38. #ifdef TORQUE_DEBUG_NET
  39. const char *NetEvent::getDebugName()
  40. {
  41. return getClassName();
  42. }
  43. #endif
  44. void NetConnection::eventOnRemove()
  45. {
  46. while(mNotifyEventList)
  47. {
  48. NetEventNote *temp = mNotifyEventList;
  49. mNotifyEventList = temp->mNextEvent;
  50. temp->mEvent->notifyDelivered(this, true);
  51. temp->mEvent->decRef();
  52. mEventNoteChunker.free(temp);
  53. }
  54. while(mUnorderedSendEventQueueHead)
  55. {
  56. NetEventNote *temp = mUnorderedSendEventQueueHead;
  57. mUnorderedSendEventQueueHead = temp->mNextEvent;
  58. temp->mEvent->notifyDelivered(this, true);
  59. temp->mEvent->decRef();
  60. mEventNoteChunker.free(temp);
  61. }
  62. while(mSendEventQueueHead)
  63. {
  64. NetEventNote *temp = mSendEventQueueHead;
  65. mSendEventQueueHead = temp->mNextEvent;
  66. temp->mEvent->notifyDelivered(this, true);
  67. temp->mEvent->decRef();
  68. mEventNoteChunker.free(temp);
  69. }
  70. }
  71. void NetConnection::eventPacketDropped(PacketNotify *notify)
  72. {
  73. NetEventNote *walk = notify->eventList;
  74. NetEventNote **insertList = &mSendEventQueueHead;
  75. NetEventNote *temp;
  76. while(walk)
  77. {
  78. switch(walk->mEvent->mGuaranteeType)
  79. {
  80. // It was a guaranteed ordered packet, reinsert it back into
  81. // mSendEventQueueHead in the right place (based on seq numbers)
  82. case NetEvent::GuaranteedOrdered:
  83. //Con::printf("EVT %d: DROP - %d", getId(), walk->mSeqCount);
  84. while(*insertList && (*insertList)->mSeqCount < walk->mSeqCount)
  85. insertList = &((*insertList)->mNextEvent);
  86. temp = walk->mNextEvent;
  87. walk->mNextEvent = *insertList;
  88. if(!walk->mNextEvent)
  89. mSendEventQueueTail = walk;
  90. *insertList = walk;
  91. insertList = &(walk->mNextEvent);
  92. walk = temp;
  93. break;
  94. // It was a guaranteed packet, put it at the top of
  95. // mUnorderedSendEventQueueHead.
  96. case NetEvent::Guaranteed:
  97. temp = walk->mNextEvent;
  98. walk->mNextEvent = mUnorderedSendEventQueueHead;
  99. mUnorderedSendEventQueueHead = walk;
  100. if(!walk->mNextEvent)
  101. mUnorderedSendEventQueueTail = walk;
  102. walk = temp;
  103. break;
  104. // Or else it was an unguaranteed packet, notify that
  105. // it was _not_ delivered and blast it.
  106. case NetEvent::Unguaranteed:
  107. walk->mEvent->notifyDelivered(this, false);
  108. walk->mEvent->decRef();
  109. temp = walk->mNextEvent;
  110. mEventNoteChunker.free(walk);
  111. walk = temp;
  112. }
  113. }
  114. }
  115. void NetConnection::eventPacketReceived(PacketNotify *notify)
  116. {
  117. NetEventNote *walk = notify->eventList;
  118. NetEventNote **noteList = &mNotifyEventList;
  119. while(walk)
  120. {
  121. NetEventNote *next = walk->mNextEvent;
  122. if(walk->mEvent->mGuaranteeType != NetEvent::GuaranteedOrdered)
  123. {
  124. walk->mEvent->notifyDelivered(this, true);
  125. walk->mEvent->decRef();
  126. mEventNoteChunker.free(walk);
  127. walk = next;
  128. }
  129. else
  130. {
  131. while(*noteList && (*noteList)->mSeqCount < walk->mSeqCount)
  132. noteList = &((*noteList)->mNextEvent);
  133. walk->mNextEvent = *noteList;
  134. *noteList = walk;
  135. noteList = &walk->mNextEvent;
  136. walk = next;
  137. }
  138. }
  139. while(mNotifyEventList && mNotifyEventList->mSeqCount == mLastAckedEventSeq + 1)
  140. {
  141. mLastAckedEventSeq++;
  142. NetEventNote *next = mNotifyEventList->mNextEvent;
  143. //Con::printf("EVT %d: ACK - %d", getId(), mNotifyEventList->mSeqCount);
  144. mNotifyEventList->mEvent->notifyDelivered(this, true);
  145. mNotifyEventList->mEvent->decRef();
  146. mEventNoteChunker.free(mNotifyEventList);
  147. mNotifyEventList = next;
  148. }
  149. }
  150. void NetConnection::eventWritePacket(BitStream *bstream, PacketNotify *notify)
  151. {
  152. #ifdef TORQUE_DEBUG_NET
  153. bstream->writeInt(DebugChecksum, 32);
  154. #endif
  155. NetEventNote *packQueueHead = NULL, *packQueueTail = NULL;
  156. while(mUnorderedSendEventQueueHead)
  157. {
  158. if(bstream->isFull())
  159. break;
  160. // dequeue the first event
  161. NetEventNote *ev = mUnorderedSendEventQueueHead;
  162. mUnorderedSendEventQueueHead = ev->mNextEvent;
  163. #ifdef TORQUE_DEBUG_NET
  164. U32 start = bstream->getCurPos();
  165. #endif
  166. bstream->writeFlag(true);
  167. S32 classId = ev->mEvent->getClassId(getNetClassGroup());
  168. AssertFatal(classId>=0, "NetConnection::eventWritePacket - event not in group!");
  169. bstream->writeClassId(classId, NetClassTypeEvent, getNetClassGroup());
  170. #ifdef TORQUE_NET_STATS
  171. U32 beginSize = bstream->getBitPosition();
  172. #endif
  173. ev->mEvent->pack(this, bstream);
  174. #ifdef TORQUE_NET_STATS
  175. ev->mEvent->getClassRep()->updateNetStatPack(0, bstream->getBitPosition() - beginSize);
  176. #endif
  177. DEBUG_LOG(("PKLOG %d EVENT %d: %s", getId(), bstream->getBitPosition() - start, ev->mEvent->getDebugName()) );
  178. #ifdef TORQUE_DEBUG_NET
  179. bstream->writeInt(classId ^ DebugChecksum, 32);
  180. #endif
  181. // add this event onto the packet queue
  182. ev->mNextEvent = NULL;
  183. if(!packQueueHead)
  184. packQueueHead = ev;
  185. else
  186. packQueueTail->mNextEvent = ev;
  187. packQueueTail = ev;
  188. }
  189. bstream->writeFlag(false);
  190. S32 prevSeq = -2;
  191. while(mSendEventQueueHead)
  192. {
  193. if(bstream->isFull())
  194. break;
  195. // if the event window is full, stop processing
  196. if(mSendEventQueueHead->mSeqCount > mLastAckedEventSeq + 126)
  197. break;
  198. // dequeue the first event
  199. NetEventNote *ev = mSendEventQueueHead;
  200. mSendEventQueueHead = ev->mNextEvent;
  201. //Con::printf("EVT %d: SEND - %d", getId(), ev->mSeqCount);
  202. bstream->writeFlag(true);
  203. ev->mNextEvent = NULL;
  204. if(!packQueueHead)
  205. packQueueHead = ev;
  206. else
  207. packQueueTail->mNextEvent = ev;
  208. packQueueTail = ev;
  209. if(!bstream->writeFlag(ev->mSeqCount == prevSeq + 1))
  210. bstream->writeInt(ev->mSeqCount & 0x7F, 7);
  211. prevSeq = ev->mSeqCount;
  212. #ifdef TORQUE_DEBUG_NET
  213. U32 start = bstream->getCurPos();
  214. #endif
  215. S32 classId = ev->mEvent->getClassId(getNetClassGroup());
  216. bstream->writeClassId(classId, NetClassTypeEvent, getNetClassGroup());
  217. #ifdef TORQUE_NET_STATS
  218. U32 beginSize = bstream->getBitPosition();
  219. #endif
  220. ev->mEvent->pack(this, bstream);
  221. #ifdef TORQUE_NET_STATS
  222. ev->mEvent->getClassRep()->updateNetStatPack(0, bstream->getBitPosition() - beginSize);
  223. #endif
  224. DEBUG_LOG(("PKLOG %d EVENT %d: %s", getId(), bstream->getBitPosition() - start, ev->mEvent->getDebugName()) );
  225. #ifdef TORQUE_DEBUG_NET
  226. bstream->writeInt(classId ^ DebugChecksum, 32);
  227. #endif
  228. }
  229. for(NetEventNote *ev = packQueueHead; ev; ev = ev->mNextEvent)
  230. ev->mEvent->notifySent(this);
  231. notify->eventList = packQueueHead;
  232. bstream->writeFlag(false);
  233. }
  234. void NetConnection::eventReadPacket(BitStream *bstream)
  235. {
  236. #ifdef TORQUE_DEBUG_NET
  237. U32 sum = bstream->readInt(32);
  238. AssertISV(sum == DebugChecksum, "Invalid checksum.");
  239. #endif
  240. S32 prevSeq = -2;
  241. NetEventNote **waitInsert = &mWaitSeqEvents;
  242. bool unguaranteedPhase = true;
  243. while(true)
  244. {
  245. bool bit = bstream->readFlag();
  246. if(unguaranteedPhase && !bit)
  247. {
  248. unguaranteedPhase = false;
  249. bit = bstream->readFlag();
  250. }
  251. if(!unguaranteedPhase && !bit)
  252. break;
  253. S32 seq = -1;
  254. if(!unguaranteedPhase) // get the sequence
  255. {
  256. if(bstream->readFlag())
  257. seq = (prevSeq + 1) & 0x7f;
  258. else
  259. seq = bstream->readInt(7);
  260. prevSeq = seq;
  261. }
  262. S32 classId = bstream->readClassId(NetClassTypeEvent, getNetClassGroup());
  263. if(classId == -1)
  264. {
  265. setLastError("Invalid packet. (bad event class id)");
  266. return;
  267. }
  268. StrongRefPtr<NetEvent> evt = (NetEvent *) ConsoleObject::create(getNetClassGroup(), NetClassTypeEvent, classId);
  269. if(evt.isNull())
  270. {
  271. setLastError("Invalid packet. (bad ghost class id)");
  272. return;
  273. }
  274. AbstractClassRep *rep = evt->getClassRep();
  275. if((rep->mNetEventDir == NetEventDirServerToClient && !isConnectionToServer())
  276. || (rep->mNetEventDir == NetEventDirClientToServer && isConnectionToServer()) )
  277. {
  278. setLastError("Invalid Packet. (invalid direction)");
  279. return;
  280. }
  281. evt->mSourceId = getId();
  282. #ifdef TORQUE_NET_STATS
  283. U32 beginSize = bstream->getBitPosition();
  284. #endif
  285. evt->unpack(this, bstream);
  286. #ifdef TORQUE_NET_STATS
  287. evt->getClassRep()->updateNetStatUnpack(bstream->getBitPosition() - beginSize);
  288. #endif
  289. if(mErrorBuffer.isNotEmpty())
  290. return;
  291. #ifdef TORQUE_DEBUG_NET
  292. U32 checksum = bstream->readInt(32);
  293. AssertISV( (checksum ^ DebugChecksum) == (U32)classId,
  294. avar("unpack did not match pack for event of class %s.",
  295. evt->getClassName()) );
  296. #endif
  297. if(unguaranteedPhase)
  298. {
  299. evt->process(this);
  300. evt = NULL;
  301. if(mErrorBuffer.isNotEmpty())
  302. return;
  303. continue;
  304. }
  305. seq |= (mNextRecvEventSeq & ~0x7F);
  306. if(seq < mNextRecvEventSeq)
  307. seq += 128;
  308. NetEventNote *note = mEventNoteChunker.alloc();
  309. note->mEvent = evt;
  310. note->mEvent->incRef();
  311. note->mSeqCount = seq;
  312. //Con::printf("EVT %d: RECV - %d", getId(), evt->mSeqCount);
  313. while(*waitInsert && (*waitInsert)->mSeqCount < seq)
  314. waitInsert = &((*waitInsert)->mNextEvent);
  315. note->mNextEvent = *waitInsert;
  316. *waitInsert = note;
  317. waitInsert = &(note->mNextEvent);
  318. }
  319. while(mWaitSeqEvents && mWaitSeqEvents->mSeqCount == mNextRecvEventSeq)
  320. {
  321. mNextRecvEventSeq++;
  322. NetEventNote *temp = mWaitSeqEvents;
  323. mWaitSeqEvents = temp->mNextEvent;
  324. //Con::printf("EVT %d: PROCESS - %d", getId(), temp->mSeqCount);
  325. temp->mEvent->process(this);
  326. temp->mEvent->decRef();
  327. mEventNoteChunker.free(temp);
  328. if(mErrorBuffer.isNotEmpty())
  329. return;
  330. }
  331. }
  332. bool NetConnection::postNetEvent(NetEvent *theEvent)
  333. {
  334. if(!mSendingEvents)
  335. {
  336. theEvent->decRef();
  337. return false;
  338. }
  339. NetEventNote *event = mEventNoteChunker.alloc();
  340. event->mEvent = theEvent;
  341. theEvent->incRef();
  342. event->mNextEvent = NULL;
  343. if(theEvent->mGuaranteeType == NetEvent::GuaranteedOrdered)
  344. {
  345. event->mSeqCount = mNextSendEventSeq++;
  346. if(!mSendEventQueueHead)
  347. mSendEventQueueHead = event;
  348. else
  349. mSendEventQueueTail->mNextEvent = event;
  350. mSendEventQueueTail = event;
  351. }
  352. else
  353. {
  354. event->mSeqCount = InvalidSendEventSeq;
  355. if(!mUnorderedSendEventQueueHead)
  356. mUnorderedSendEventQueueHead = event;
  357. else
  358. mUnorderedSendEventQueueTail->mNextEvent = event;
  359. mUnorderedSendEventQueueTail = event;
  360. }
  361. return true;
  362. }
  363. void NetConnection::eventWriteStartBlock(ResizeBitStream *stream)
  364. {
  365. stream->write(mNextRecvEventSeq);
  366. for(NetEventNote *walk = mWaitSeqEvents; walk; walk = walk->mNextEvent)
  367. {
  368. stream->writeFlag(true);
  369. S32 classId = walk->mEvent->getClassId(getNetClassGroup());
  370. stream->writeClassId(classId, NetClassTypeEvent, getNetClassGroup());
  371. walk->mEvent->write(this, stream);
  372. stream->validate();
  373. }
  374. stream->writeFlag(false);
  375. }
  376. void NetConnection::eventReadStartBlock(BitStream *stream)
  377. {
  378. stream->read(&mNextRecvEventSeq);
  379. NetEventNote *lastEvent = NULL;
  380. while(stream->readFlag())
  381. {
  382. S32 classTag = stream->readClassId(NetClassTypeEvent, getNetClassGroup());
  383. NetEvent *evt = (NetEvent *) ConsoleObject::create(getNetClassGroup(), NetClassTypeEvent, classTag);
  384. evt->unpack(this, stream);
  385. NetEventNote *add = mEventNoteChunker.alloc();
  386. add->mEvent = evt;
  387. evt->incRef();
  388. add->mNextEvent = NULL;
  389. if(!lastEvent)
  390. mWaitSeqEvents = add;
  391. else
  392. lastEvent->mNextEvent = add;
  393. lastEvent = add;
  394. }
  395. }