connectionProtocol.cc 8.4 KB

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