Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

Session.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #ifndef BZR_NET_SESSION_H
  19. #define BZR_NET_SESSION_H
  20. #include "net/Address.h"
  21. #include "net/BlobAssembler.h"
  22. #include "net/ChecksumXorGenerator.h"
  23. #include "Noncopyable.h"
  24. #include <chrono>
  25. #include <map>
  26. #include <set>
  27. typedef chrono::high_resolution_clock net_clock;
  28. typedef net_clock::time_point net_time_point;
  29. class BinReader;
  30. struct Packet;
  31. struct PacketHeader;
  32. class SessionManager;
  33. class Session : Noncopyable
  34. {
  35. public:
  36. Session(SessionManager& manager,
  37. Address address,
  38. string accountName,
  39. string accountKey);
  40. Session(SessionManager& manager,
  41. Address address,
  42. uint64_t cookie);
  43. ~Session();
  44. void handle(const Packet& packet);
  45. void tick(net_time_point now);
  46. void sendBlob(BlobPtr blob);
  47. Address baseAddress() const;
  48. Address sendAddress() const;
  49. Address recvAddress() const;
  50. net_time_point nextTick() const;
  51. BlobAssembler& blobAssembler();
  52. private:
  53. typedef chrono::duration<uint16_t, ratio<1, 2>> SessionDuration;
  54. enum class State
  55. {
  56. kLogon,
  57. kReferred,
  58. kConnectResponse,
  59. kConnected
  60. };
  61. void sendLogon();
  62. void sendReferred();
  63. void sendConnectResponse();
  64. void handleBlobFragments(BinReader& reader);
  65. void handleServerSwitch(BinReader& reader);
  66. void handleRequestRetransmit(BinReader& reader);
  67. void handleRejectRetransmit(BinReader& reader);
  68. void handleAckSequence(BinReader& reader);
  69. void handleReferral(BinReader& reader);
  70. void handleConnect(BinReader& reader, const PacketHeader& header);
  71. void handleTimeSync(BinReader& reader);
  72. void handleEchoResponse(BinReader& reader);
  73. void handleFlow(BinReader& reader);
  74. void advanceServerSequence();
  75. SessionManager& manager_;
  76. const Address address_;
  77. State state_;
  78. // the time at which this session was created
  79. net_time_point sessionBegin_;
  80. // the time at which the next logon, referred, connect response or ping packet should be sent
  81. net_time_point nextPeriodic_;
  82. // the number of times the periodic packet for this state has been sent without a response
  83. size_t numPeriodicSent_;
  84. //
  85. // State::kLogon
  86. //
  87. string accountName_;
  88. string accountKey_;
  89. //
  90. // State::kReferred, State::kConnectResponse
  91. //
  92. uint64_t cookie_;
  93. //
  94. // State::kConnectResponse, State::kConnected
  95. //
  96. // the latest server sequence acknowledged by the client
  97. uint32_t serverSequence_;
  98. // the latest contiguous sequence received by the client, not necessarily acknowledged yet
  99. // serverLeadingSequence_ >= serverSequence_
  100. uint32_t serverLeadingSequence_;
  101. // the latest client sequence acknowledged by the server
  102. uint32_t clientSequence_;
  103. // the latest contiguous sequence sent by the client, not necessarily acknowledged yet
  104. // clientLeadingSequence_ >= clientSequence_
  105. uint32_t clientLeadingSequence_;
  106. // the value the server should use in the packet header's net id field
  107. uint16_t serverNetId_;
  108. // the value the client should use in the packet header's net id field
  109. uint16_t clientNetId_;
  110. // the value both the server and client should use in the packet header's iteration field
  111. uint16_t iteration_;
  112. // the rng used to generate xor values for server packets
  113. ChecksumXorGenerator serverXorGen_;
  114. // the rng used to generate xor values for client packets
  115. ChecksumXorGenerator clientXorGen_;
  116. // the time specified in the last connect packet or time sync header
  117. double beginTime_;
  118. // the local time at which beginTime_ was set
  119. net_time_point beginLocalTime_;
  120. // stores the set of all sequences > serverSequence_ sent by the server
  121. set<uint32_t> serverPackets_;
  122. // stores all packets with sequence > clientSequence_ sent by the client
  123. map<uint32_t, unique_ptr<Packet>> clientPackets_;
  124. net_time_point nextRequestMissing_;
  125. uint16_t lastFlowTime_;
  126. uint32_t lastFlowBytes_;
  127. BlobAssembler blobAssembler_;
  128. uint64_t blobId_;
  129. };
  130. #endif