dnet.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "core/stream/bitStream.h"
  23. #include "core/dnet.h"
  24. #include "core/strings/stringFunctions.h"
  25. #include "console/consoleTypes.h"
  26. bool gLogToConsole = false;
  27. S32 gNetBitsReceived = 0;
  28. enum NetPacketType
  29. {
  30. DataPacket,
  31. PingPacket,
  32. AckPacket,
  33. InvalidPacketType,
  34. };
  35. static const char *packetTypeNames[] =
  36. {
  37. "DataPacket",
  38. "PingPacket",
  39. "AckPacket",
  40. };
  41. //-----------------------------------------------------------------
  42. //-----------------------------------------------------------------
  43. //-----------------------------------------------------------------
  44. ConsoleFunction(DNetSetLogging, void, 2, 2, "(bool enabled)"
  45. "@brief Enables logging of the connection protocols\n\n"
  46. "When enabled a lot of network debugging information is sent to the console.\n"
  47. "@param enabled True to enable, false to disable\n"
  48. "@ingroup Networking")
  49. {
  50. TORQUE_UNUSED(argc);
  51. gLogToConsole = dAtob(argv[1]);
  52. }
  53. ConnectionProtocol::ConnectionProtocol()
  54. {
  55. mLastSeqRecvd = 0;
  56. mHighestAckedSeq = 0;
  57. mLastSendSeq = 0; // start sending at 1
  58. mAckMask = 0;
  59. mLastRecvAckAck = 0;
  60. }
  61. void ConnectionProtocol::buildSendPacketHeader(BitStream *stream, S32 packetType)
  62. {
  63. S32 ackByteCount = ((mLastSeqRecvd - mLastRecvAckAck + 7) >> 3);
  64. AssertFatal(ackByteCount <= 4, "Too few ack bytes!");
  65. // S32 headerSize = 3 + ackByteCount;
  66. if(packetType == DataPacket)
  67. mLastSendSeq++;
  68. stream->writeFlag(true);
  69. stream->writeInt(mConnectSequence & 1, 1);
  70. stream->writeInt(mLastSendSeq & 0x1FF, 9);
  71. stream->writeInt(mLastSeqRecvd & 0x1FF, 9);
  72. stream->writeInt(packetType & 0x3, 2);
  73. stream->writeInt(ackByteCount & 0x7, 3);
  74. U32 bitmask = ~(0xFFFFFFFF << (ackByteCount*8));
  75. if(ackByteCount == 4)
  76. {
  77. // Performing a bit shift that is the same size as the variable being shifted
  78. // is undefined in the C/C++ standard. Handle that exception here when
  79. // ackByteCount*8 == 4*8 == 32
  80. bitmask = 0xFFFFFFFF;
  81. }
  82. stream->writeInt(mAckMask & bitmask, ackByteCount * 8);
  83. // if we're resending this header, we can't advance the
  84. // sequence recieved (in case this packet drops and the prev one
  85. // goes through)
  86. if(gLogToConsole)
  87. Con::printf("build hdr %d %d", mLastSendSeq, packetType);
  88. if(packetType == DataPacket)
  89. mLastSeqRecvdAtSend[mLastSendSeq & 0x1F] = mLastSeqRecvd;
  90. }
  91. void ConnectionProtocol::sendPingPacket()
  92. {
  93. U8 buffer[16];
  94. BitStream bs(buffer, 16);
  95. buildSendPacketHeader(&bs, PingPacket);
  96. if(gLogToConsole)
  97. Con::printf("send ping %d", mLastSendSeq);
  98. sendPacket(&bs);
  99. }
  100. void ConnectionProtocol::sendAckPacket()
  101. {
  102. U8 buffer[16];
  103. BitStream bs(buffer, 16);
  104. buildSendPacketHeader(&bs, AckPacket);
  105. if(gLogToConsole)
  106. Con::printf("send ack %d", mLastSendSeq);
  107. sendPacket(&bs);
  108. }
  109. // packets are read directly into the data portion of
  110. // connection notify packets... makes the events easier to post into
  111. // the system.
  112. void ConnectionProtocol::processRawPacket(BitStream *pstream)
  113. {
  114. // read in the packet header:
  115. // Fixed packet header: 3 bytes
  116. //
  117. // 1 bit game packet flag
  118. // 1 bit connect sequence
  119. // 9 bits packet seq number
  120. // 9 bits ackstart seq number
  121. // 2 bits packet type
  122. // 2 bits ack byte count
  123. //
  124. // type is:
  125. // 00 data packet
  126. // 01 ping packet
  127. // 02 ack packet
  128. // next 1-4 bytes are ack flags
  129. //
  130. // header len is 4-9 bytes
  131. // average case 4 byte header
  132. gNetBitsReceived = pstream->getStreamSize();
  133. pstream->readFlag(); // get rid of the game info packet bit
  134. U32 pkConnectSeqBit = pstream->readInt(1);
  135. U32 pkSequenceNumber = pstream->readInt(9);
  136. U32 pkHighestAck = pstream->readInt(9);
  137. U32 pkPacketType = pstream->readInt(2);
  138. S32 pkAckByteCount = pstream->readInt(3);
  139. // check connection sequence bit
  140. if(pkConnectSeqBit != (mConnectSequence & 1))
  141. return;
  142. if(pkAckByteCount > 4 || pkPacketType >= InvalidPacketType)
  143. return;
  144. S32 pkAckMask = pstream->readInt(8 * pkAckByteCount);
  145. // verify packet ordering and acking and stuff
  146. // check if the 9-bit sequence is within the packet window
  147. // (within 31 packets of the last received sequence number).
  148. pkSequenceNumber |= (mLastSeqRecvd & 0xFFFFFE00);
  149. // account for wrap around
  150. if(pkSequenceNumber < mLastSeqRecvd)
  151. pkSequenceNumber += 0x200;
  152. if(pkSequenceNumber > mLastSeqRecvd + 31)
  153. {
  154. // the sequence number is outside the window... must be out of order
  155. // discard.
  156. return;
  157. }
  158. pkHighestAck |= (mHighestAckedSeq & 0xFFFFFE00);
  159. // account for wrap around
  160. if(pkHighestAck < mHighestAckedSeq)
  161. pkHighestAck += 0x200;
  162. if(pkHighestAck > mLastSendSeq)
  163. {
  164. // the ack number is outside the window... must be an out of order
  165. // packet, discard.
  166. return;
  167. }
  168. if(gLogToConsole)
  169. {
  170. for(U32 i = mLastSeqRecvd+1; i < pkSequenceNumber; i++)
  171. Con::printf("Not recv %d", i);
  172. Con::printf("Recv %d %s", pkSequenceNumber, packetTypeNames[pkPacketType]);
  173. }
  174. // shift up the ack mask by the packet difference
  175. // this essentially nacks all the packets dropped
  176. mAckMask <<= pkSequenceNumber - mLastSeqRecvd;
  177. // if this packet is a data packet (i.e. not a ping packet or an ack packet), ack it
  178. if(pkPacketType == DataPacket)
  179. mAckMask |= 1;
  180. // do all the notifies...
  181. for(U32 i = mHighestAckedSeq+1; i <= pkHighestAck; i++)
  182. {
  183. bool packetTransmitSuccess = pkAckMask & (1 << (pkHighestAck - i));
  184. handleNotify(packetTransmitSuccess);
  185. if(gLogToConsole)
  186. Con::printf("Ack %d %d", i, packetTransmitSuccess);
  187. if(packetTransmitSuccess)
  188. {
  189. mLastRecvAckAck = mLastSeqRecvdAtSend[i & 0x1F];
  190. if(!mConnectionEstablished)
  191. {
  192. mConnectionEstablished = true;
  193. handleConnectionEstablished();
  194. }
  195. }
  196. }
  197. // the other side knows more about its window than we do.
  198. if(pkSequenceNumber - mLastRecvAckAck > 32)
  199. mLastRecvAckAck = pkSequenceNumber - 32;
  200. mHighestAckedSeq = pkHighestAck;
  201. // first things first...
  202. // ackback any pings or accept connects
  203. if(pkPacketType == PingPacket)
  204. {
  205. // send an ack to the other side
  206. // the ack will have the same packet sequence as our last sent packet
  207. // if the last packet we sent was the connection accepted packet
  208. // we must resend that packet
  209. sendAckPacket();
  210. }
  211. keepAlive(); // notification that the connection is ok
  212. // note: handlePacket() may delete the connection if an error occurs.
  213. if(mLastSeqRecvd != pkSequenceNumber)
  214. {
  215. mLastSeqRecvd = pkSequenceNumber;
  216. if(pkPacketType == DataPacket)
  217. handlePacket(pstream);
  218. }
  219. }
  220. bool ConnectionProtocol::windowFull()
  221. {
  222. return mLastSendSeq - mHighestAckedSeq >= 30;
  223. }
  224. void ConnectionProtocol::writeDemoStartBlock(ResizeBitStream *stream)
  225. {
  226. for(U32 i = 0; i < 32; i++)
  227. stream->write(mLastSeqRecvdAtSend[i]);
  228. stream->write(mLastSeqRecvd);
  229. stream->write(mHighestAckedSeq);
  230. stream->write(mLastSendSeq);
  231. stream->write(mAckMask);
  232. stream->write(mConnectSequence);
  233. stream->write(mLastRecvAckAck);
  234. stream->write(mConnectionEstablished);
  235. }
  236. bool ConnectionProtocol::readDemoStartBlock(BitStream *stream)
  237. {
  238. for(U32 i = 0; i < 32; i++)
  239. stream->read(&mLastSeqRecvdAtSend[i]);
  240. stream->read(&mLastSeqRecvd);
  241. stream->read(&mHighestAckedSeq);
  242. stream->read(&mLastSendSeq);
  243. stream->read(&mAckMask);
  244. stream->read(&mConnectSequence);
  245. stream->read(&mLastRecvAckAck);
  246. stream->read(&mConnectionEstablished);
  247. return true;
  248. }