connectionProtocol.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef _CONNECTION_PROTOCOL_H_
  23. #define _CONNECTION_PROTOCOL_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _EVENT_H_
  28. #include "platform/event.h"
  29. #endif
  30. class BitStream;
  31. class ResizeBitStream;
  32. /// The base class for Torque's networking protocol.
  33. ///
  34. /// This implements a sliding window connected message stream over an unreliable transport (UDP). It
  35. /// provides a simple notify protocol to allow subclasses to be aware of what packets were sent
  36. /// succesfully and which failed.
  37. ///
  38. /// Basically, a window size of 32 is provided, and each packet contains in the header a bitmask,
  39. /// acknowledging the receipt (or failure to receive) of the last 32 packets.
  40. ///
  41. /// @see NetConnection, @ref NetProtocol
  42. class ConnectionProtocol
  43. {
  44. protected:
  45. U32 mLastSeqRecvdAtSend[32];
  46. U32 mLastSeqRecvd;
  47. U32 mHighestAckedSeq;
  48. U32 mLastSendSeq;
  49. U32 mAckMask;
  50. U32 mConnectSequence;
  51. U32 mLastRecvAckAck;
  52. bool mConnectionEstablished;
  53. public:
  54. ConnectionProtocol();
  55. void buildSendPacketHeader(BitStream *bstream, S32 packetType = 0);
  56. void sendPingPacket();
  57. void sendAckPacket();
  58. void setConnectionEstablished() { mConnectionEstablished = true; }
  59. bool windowFull();
  60. bool connectionEstablished();
  61. void setConnectSequence(U32 connectSeq) { mConnectSequence = connectSeq; }
  62. virtual void writeDemoStartBlock(ResizeBitStream *stream);
  63. virtual bool readDemoStartBlock(BitStream *stream);
  64. virtual void processRawPacket(BitStream *bstream);
  65. virtual Net::Error sendPacket(BitStream *bstream) = 0;
  66. virtual void keepAlive() = 0;
  67. virtual void handleConnectionEstablished() = 0;
  68. virtual void handleNotify(bool recvd) = 0;
  69. virtual void handlePacket(BitStream *bstream) = 0;
  70. };
  71. #endif