Connection.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Controls.h"
  25. #include "Peer.h"
  26. #include "RemoteEvent.h"
  27. #include "ReplicationState.h"
  28. #include "Timer.h"
  29. class Scene;
  30. static const int KEY_LENGTH = 256;
  31. /// Client's scene join state
  32. enum JoinState
  33. {
  34. JS_NOTINSCENE = 0,
  35. JS_PREPARESCENE,
  36. JS_LOADSCENE,
  37. JS_SENDFULLUPDATE,
  38. JS_WAITFORACK,
  39. JS_SENDDELTAS
  40. };
  41. /// Multiplayer protocol connection
  42. class Connection : public Object
  43. {
  44. OBJECT(Connection);
  45. public:
  46. /// Construct
  47. Connection(Context* context, Peer* peer);
  48. /// Destruct
  49. virtual ~Connection();
  50. /// Send a packet on the reliable channel
  51. void SendReliable(const VectorBuffer& packet);
  52. /// Send a packet on the unreliable channel
  53. void SendUnreliable(const VectorBuffer& packet);
  54. /// Attempt to receive a packet on the reliable channel. Return true if a packet was received
  55. bool ReceiveReliable(VectorBuffer& packet);
  56. /// Attempt to receive a packet on the unreliable channel. Return true if a packet was received
  57. bool ReceiveUnreliable(VectorBuffer& packet);
  58. /// Disconnect
  59. void Disconnect();
  60. /// Disconnect immediately
  61. void forceDisconnect();
  62. /// Set scene
  63. void SetScene(Scene* scene);
  64. /// Joined scene
  65. void JoinedScene();
  66. /// Left scene
  67. void LeftScene();
  68. /// Set login data
  69. void SetLoginData(const VariantMap& loginData);
  70. /// Set server challenge value
  71. void SetChallenge(unsigned challenge);
  72. /// Set client's join state
  73. void SetJoinState(JoinState state);
  74. /// Set remote frame number and last acked local frame number
  75. void SetFrameNumbers(unsigned short frameNumber, unsigned short frameAck);
  76. /// Set client controls
  77. void SetControls(const Controls& controls);
  78. /// Set client position for node relevancy calculations
  79. void SetPosition(const Vector3& position);
  80. /// Queue a remote event to be sent
  81. void addRemoteEvent(const RemoteEvent& remoteEvent);
  82. /// Check if a received remote event should be dispatched, or if it was dispatched already
  83. bool CheckRemoteEventFrame(const RemoteEvent& remoteEvent, unsigned short previousEventFrameNumber);
  84. /// Add unacked controls for possible client-side prediction / replay
  85. void AddUnackedControls(unsigned short frameNumber, const Controls& controls);
  86. /// Remove scene revisions the client has already received
  87. void PurgeAckedSceneState();
  88. //! Remove controls the server has already received
  89. void PurgeAckedControls();
  90. /// Remove events the remote end has already received
  91. void PurgeAckedRemoteEvents(unsigned short frameNumber);
  92. /// Remove all scene revision data
  93. void ClearSceneState();
  94. /// Remove all remote events
  95. void ClearRemoteEvents();
  96. /// Update the round trip time using a smoothed average
  97. void UpdateRoundTripTime(int netFps, unsigned short frameNumber);
  98. /// Return network peer
  99. Peer* GetPeer() const { return peer_; }
  100. /// Return scene the client has joined, if any
  101. Scene* GetScene() const { return scene_; }
  102. /// Return identity string (address:port)
  103. std::string GetIdentity() const;
  104. /// Return login data
  105. const VariantMap& GetLoginData() const { return loginData_; }
  106. /// Return challenge value
  107. unsigned GetChallenge() const { return challenge_; }
  108. /// Return whether connected
  109. bool IsConnected() const;
  110. /// Return whether a challenge has been assigned
  111. bool HasChallenge() const { return hasChallenge_; }
  112. /// Return the client's scene join state
  113. JoinState GetJoinState() const { return joinState_; }
  114. /// Return the remote frame number
  115. unsigned short GetFrameNumber() const { return frameNumber_; }
  116. /// Return the last acked local frame number
  117. unsigned short GetFrameAck() const { return frameNumber_; }
  118. /// Return the last processed remote event frame number
  119. unsigned short GetEventFrameNumber() const { return eventFrameNumber_; }
  120. /// Return the current estimated round trip time
  121. float GetRoundTripTime() const { return roundTripTime_; }
  122. /// Return client controls
  123. const Controls& GetControls() { return controls_; }
  124. /// Return client position
  125. const Vector3& GetPosition() { return position_; }
  126. /// Return unacked remote events
  127. std::vector<RemoteEvent>& GetUnackedRemoteEvents() { return remoteEvents_; }
  128. /// Return unacked controls
  129. std::vector<std::pair<unsigned short, Controls> >& GetUnackedControls() { return unackedControls_; }
  130. /// Return unacked scene revisions
  131. SceneReplicationState& GetSceneState() { return sceneState_; }
  132. private:
  133. /// Send a packet
  134. void Send(const VectorBuffer& packet, bool reliable);
  135. /// Return a packet
  136. bool Receive(VectorBuffer& packet, bool reliable);
  137. /// Network peer
  138. SharedPtr<Peer> peer_;
  139. /// Scene
  140. SharedPtr<Scene> scene_;
  141. /// Login data
  142. VariantMap loginData_;
  143. /// Challenge value
  144. unsigned challenge_;
  145. /// Challenge assigned flag
  146. bool hasChallenge_;
  147. /// Remote frame number
  148. unsigned short frameNumber_;
  149. /// Last acked local frame number
  150. unsigned short frameAck_;
  151. /// Last processed remote event frame number
  152. unsigned short eventFrameNumber_;
  153. /// Current estimated round trip time
  154. float roundTripTime_;
  155. /// Client's scene join state
  156. JoinState joinState_;
  157. /// Client controls
  158. Controls controls_;
  159. /// Client position
  160. Vector3 position_;
  161. /// Remote events currently being sent
  162. std::vector<RemoteEvent> remoteEvents_;
  163. //! Unacked controls
  164. std::vector<std::pair<unsigned short, Controls> > unackedControls_;
  165. /// Unacked scene revisions
  166. SceneReplicationState sceneState_;
  167. };