connectionProtocol.cc 8.1 KB

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