dnet.cpp 8.8 KB

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