dnet.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. stream->writeInt(mAckMask & (~(0xFFFFFFFF << ackByteCount*8)), ackByteCount * 8);
  75. // if we're resending this header, we can't advance the
  76. // sequence recieved (in case this packet drops and the prev one
  77. // goes through)
  78. if(gLogToConsole)
  79. Con::printf("build hdr %d %d", mLastSendSeq, packetType);
  80. if(packetType == DataPacket)
  81. mLastSeqRecvdAtSend[mLastSendSeq & 0x1F] = mLastSeqRecvd;
  82. }
  83. void ConnectionProtocol::sendPingPacket()
  84. {
  85. U8 buffer[16];
  86. BitStream bs(buffer, 16);
  87. buildSendPacketHeader(&bs, PingPacket);
  88. if(gLogToConsole)
  89. Con::printf("send ping %d", mLastSendSeq);
  90. sendPacket(&bs);
  91. }
  92. void ConnectionProtocol::sendAckPacket()
  93. {
  94. U8 buffer[16];
  95. BitStream bs(buffer, 16);
  96. buildSendPacketHeader(&bs, AckPacket);
  97. if(gLogToConsole)
  98. Con::printf("send ack %d", mLastSendSeq);
  99. sendPacket(&bs);
  100. }
  101. // packets are read directly into the data portion of
  102. // connection notify packets... makes the events easier to post into
  103. // the system.
  104. void ConnectionProtocol::processRawPacket(BitStream *pstream)
  105. {
  106. // read in the packet header:
  107. // Fixed packet header: 3 bytes
  108. //
  109. // 1 bit game packet flag
  110. // 1 bit connect sequence
  111. // 9 bits packet seq number
  112. // 9 bits ackstart seq number
  113. // 2 bits packet type
  114. // 2 bits ack byte count
  115. //
  116. // type is:
  117. // 00 data packet
  118. // 01 ping packet
  119. // 02 ack packet
  120. // next 1-4 bytes are ack flags
  121. //
  122. // header len is 4-9 bytes
  123. // average case 4 byte header
  124. gNetBitsReceived = pstream->getStreamSize();
  125. pstream->readFlag(); // get rid of the game info packet bit
  126. U32 pkConnectSeqBit = pstream->readInt(1);
  127. U32 pkSequenceNumber = pstream->readInt(9);
  128. U32 pkHighestAck = pstream->readInt(9);
  129. U32 pkPacketType = pstream->readInt(2);
  130. S32 pkAckByteCount = pstream->readInt(3);
  131. // check connection sequence bit
  132. if(pkConnectSeqBit != (mConnectSequence & 1))
  133. return;
  134. if(pkAckByteCount > 4 || pkPacketType >= InvalidPacketType)
  135. return;
  136. S32 pkAckMask = pstream->readInt(8 * pkAckByteCount);
  137. // verify packet ordering and acking and stuff
  138. // check if the 9-bit sequence is within the packet window
  139. // (within 31 packets of the last received sequence number).
  140. pkSequenceNumber |= (mLastSeqRecvd & 0xFFFFFE00);
  141. // account for wrap around
  142. if(pkSequenceNumber < mLastSeqRecvd)
  143. pkSequenceNumber += 0x200;
  144. if(pkSequenceNumber > mLastSeqRecvd + 31)
  145. {
  146. // the sequence number is outside the window... must be out of order
  147. // discard.
  148. return;
  149. }
  150. pkHighestAck |= (mHighestAckedSeq & 0xFFFFFE00);
  151. // account for wrap around
  152. if(pkHighestAck < mHighestAckedSeq)
  153. pkHighestAck += 0x200;
  154. if(pkHighestAck > mLastSendSeq)
  155. {
  156. // the ack number is outside the window... must be an out of order
  157. // packet, discard.
  158. return;
  159. }
  160. if(gLogToConsole)
  161. {
  162. for(U32 i = mLastSeqRecvd+1; i < pkSequenceNumber; i++)
  163. Con::printf("Not recv %d", i);
  164. Con::printf("Recv %d %s", pkSequenceNumber, packetTypeNames[pkPacketType]);
  165. }
  166. // shift up the ack mask by the packet difference
  167. // this essentially nacks all the packets dropped
  168. mAckMask <<= pkSequenceNumber - mLastSeqRecvd;
  169. // if this packet is a data packet (i.e. not a ping packet or an ack packet), ack it
  170. if(pkPacketType == DataPacket)
  171. mAckMask |= 1;
  172. // do all the notifies...
  173. for(U32 i = mHighestAckedSeq+1; i <= pkHighestAck; i++)
  174. {
  175. bool packetTransmitSuccess = pkAckMask & (1 << (pkHighestAck - i));
  176. handleNotify(packetTransmitSuccess);
  177. if(gLogToConsole)
  178. Con::printf("Ack %d %d", i, packetTransmitSuccess);
  179. if(packetTransmitSuccess)
  180. {
  181. mLastRecvAckAck = mLastSeqRecvdAtSend[i & 0x1F];
  182. if(!mConnectionEstablished)
  183. {
  184. mConnectionEstablished = true;
  185. handleConnectionEstablished();
  186. }
  187. }
  188. }
  189. // the other side knows more about its window than we do.
  190. if(pkSequenceNumber - mLastRecvAckAck > 32)
  191. mLastRecvAckAck = pkSequenceNumber - 32;
  192. mHighestAckedSeq = pkHighestAck;
  193. // first things first...
  194. // ackback any pings or accept connects
  195. if(pkPacketType == PingPacket)
  196. {
  197. // send an ack to the other side
  198. // the ack will have the same packet sequence as our last sent packet
  199. // if the last packet we sent was the connection accepted packet
  200. // we must resend that packet
  201. sendAckPacket();
  202. }
  203. keepAlive(); // notification that the connection is ok
  204. // note: handlePacket() may delete the connection if an error occurs.
  205. if(mLastSeqRecvd != pkSequenceNumber)
  206. {
  207. mLastSeqRecvd = pkSequenceNumber;
  208. if(pkPacketType == DataPacket)
  209. handlePacket(pstream);
  210. }
  211. }
  212. bool ConnectionProtocol::windowFull()
  213. {
  214. return mLastSendSeq - mHighestAckedSeq >= 30;
  215. }
  216. void ConnectionProtocol::writeDemoStartBlock(ResizeBitStream *stream)
  217. {
  218. for(U32 i = 0; i < 32; i++)
  219. stream->write(mLastSeqRecvdAtSend[i]);
  220. stream->write(mLastSeqRecvd);
  221. stream->write(mHighestAckedSeq);
  222. stream->write(mLastSendSeq);
  223. stream->write(mAckMask);
  224. stream->write(mConnectSequence);
  225. stream->write(mLastRecvAckAck);
  226. stream->write(mConnectionEstablished);
  227. }
  228. bool ConnectionProtocol::readDemoStartBlock(BitStream *stream)
  229. {
  230. for(U32 i = 0; i < 32; i++)
  231. stream->read(&mLastSeqRecvdAtSend[i]);
  232. stream->read(&mLastSeqRecvd);
  233. stream->read(&mHighestAckedSeq);
  234. stream->read(&mLastSendSeq);
  235. stream->read(&mAckMask);
  236. stream->read(&mConnectSequence);
  237. stream->read(&mLastRecvAckAck);
  238. stream->read(&mConnectionEstablished);
  239. return true;
  240. }