Client.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "File.h"
  26. #include "Object.h"
  27. #include "Timer.h"
  28. #include "VectorBuffer.h"
  29. #include <map>
  30. #include <set>
  31. class Connection;
  32. class Network;
  33. class ResourceCache;
  34. class Scene;
  35. /// Downloadable package file description
  36. struct PackageInfo
  37. {
  38. /// File name
  39. std::string name_;
  40. /// File size
  41. unsigned size_;
  42. /// Data checksum
  43. unsigned checksum_;
  44. };
  45. /// Server scene description
  46. struct SceneInfo
  47. {
  48. /// Construct with default values
  49. SceneInfo() :
  50. numUsers_(0),
  51. netFps_(0)
  52. {
  53. }
  54. /// Scene name
  55. std::string name_;
  56. /// Current number of users in scene
  57. unsigned numUsers_;
  58. /// Network updates per second
  59. int netFps_;
  60. /// Scene file name
  61. std::string fileName_;
  62. /// Required package files
  63. std::vector<PackageInfo> requiredPackages_;
  64. };
  65. /// An ongoing download from the server
  66. struct FileTransfer
  67. {
  68. /// Construct with default values
  69. FileTransfer() :
  70. fragmentsReceived_(0),
  71. bytesReceived_(0),
  72. batchStart_(0),
  73. batchSize_(0),
  74. lastBatchSize_(0),
  75. lastDataRate_(0.0f)
  76. {
  77. }
  78. /// File used to store the download
  79. SharedPtr<File> file_;
  80. /// File name
  81. std::string fileName_;
  82. /// File size
  83. unsigned size_;
  84. /// Data checksum
  85. unsigned checksum_;
  86. /// Total number of fragments
  87. unsigned numFragments_;
  88. /// Fragments received so far
  89. unsigned fragmentsReceived_;
  90. /// Bytes received so far
  91. unsigned bytesReceived_;
  92. /// Timer for current batch of fragments
  93. Timer batchTimer_;
  94. /// Timer for the whole transfer
  95. Timer receiveTimer_;
  96. /// First fragment number in the current batch
  97. unsigned batchStart_;
  98. /// Number of fragments in the current batch
  99. unsigned batchSize_;
  100. /// Number of fragments in the previous batch
  101. unsigned lastBatchSize_;
  102. /// Download rate of the previous batch
  103. float lastDataRate_;
  104. };
  105. /// Multiplayer client subsystem
  106. class Client : public Object
  107. {
  108. OBJECT(Object);
  109. public:
  110. /// Construct
  111. Client(Context* context);
  112. /// Destruct
  113. virtual ~Client();
  114. /// Set scene to use. Will be cleared of any existing content
  115. void SetScene(Scene* scene);
  116. /// Set package download directory
  117. void SetDownloadDirectory(const std::string& path);
  118. /// Connect to a server
  119. bool Connect(const std::string& address, unsigned short port, const VariantMap& loginData = VariantMap());
  120. /// Disconnect from a server
  121. void Disconnect();
  122. /// Set client controls to be sent over the network
  123. void SetControls(const Controls& controls);
  124. /// Set client position to be sent over the network for node relevancy calculations
  125. void SetPosition(const Vector3& position);
  126. /// Send and receive packets and update the scene
  127. void Update(float timeStep);
  128. /// Return the scene
  129. Scene* GetScene() const { return scene_; }
  130. /// Return the server connection
  131. Connection* GetServerConnection() const { return serverConnection_; }
  132. /// Return whether connected
  133. bool IsConnected() const;
  134. /// Return whether scene join pending
  135. bool IsJoinPending() const;
  136. /// Return whether joined a scene
  137. bool IsJoined() const;
  138. /// Return network updates per second
  139. int GetNetFps() const { return netFps_; }
  140. /// Return client frame number
  141. unsigned short GetFrameNumber() const { return frameNumber_; }
  142. /// Return client controls
  143. const Controls& GetControls() const;
  144. /// Return client position
  145. const Vector3& GetPosition() const;
  146. /// Return scene info
  147. const SceneInfo& GetSceneInfo() const { return sceneInfo_; }
  148. /// Return number of ongoing file transfers
  149. unsigned GetNumFileTransfers() { return fileTransfers_.size(); }
  150. /// Return ongoing file transfers
  151. const std::map<StringHash, FileTransfer>& GetFileTransfers() { return fileTransfers_; }
  152. /// Return download directory
  153. const std::string& GetDownloadDirectory() const { return downloadDirectory_; }
  154. /// Return file transfer status as text
  155. std::string GetFileTransferStatus() const;
  156. private:
  157. /// Handle network peer disconnect event
  158. void HandlePeerDisconnected(StringHash eventType, VariantMap& eventData);
  159. /// Handle file transfer complete event
  160. void HandleFileTransferCompleted(StringHash eventType, VariantMap& eventData);
  161. /// Handle file transfer failed event
  162. void HandleFileTransferFailed(StringHash eventType, VariantMap& eventData);
  163. /// Handle async scene loading finished event
  164. void HandleAsyncLoadFinished(StringHash eventType, VariantMap& eventData);
  165. /// Handle a reliable network packet
  166. void HandleReliablePacket(VectorBuffer& packet);
  167. /// Handle a challenge packet
  168. void HandleChallenge(VectorBuffer& packet);
  169. /// Handle a scene info packet
  170. void HandleSceneInfo(VectorBuffer& packet);
  171. /// Handle a file transfer packet
  172. void HandleTransferData(VectorBuffer& packet);
  173. /// Handle a file transfer failed packet
  174. void HandleTransferFailed(VectorBuffer& packet);
  175. /// Handle a join reply packet
  176. void HandleJoinReply(VectorBuffer& packet);
  177. /// Handle a full scene update from the server
  178. void HandleFullUpdate(VectorBuffer& packet);
  179. /// Handle a server update packet
  180. void HandleServerUpdate(VectorBuffer& packet, bool initial = false);
  181. /// Check whether packages need to be downloaded to join the scene
  182. unsigned CheckPackages();
  183. /// Begin a file download
  184. bool RequestFile(const std::string& fileName, unsigned size, unsigned checksum);
  185. /// Begin setup of the client scene
  186. void SetupScene();
  187. /// Send join scene packet after scene setup is complete
  188. void SendJoinScene();
  189. /// Send join failed event
  190. void JoinFailed(const std::string& reason);
  191. /// Send a client update packet
  192. void SendClientUpdate();
  193. /// Read a server update block
  194. void ReadNetUpdateBlock(Deserializer& source, unsigned char msgID, std::set<unsigned>& updatedNodes, std::set<unsigned>& updatedComponents);
  195. /// Scene
  196. SharedPtr<Scene> scene_;
  197. /// Server connection
  198. SharedPtr<Connection> serverConnection_;
  199. /// Network updates per second
  200. int netFps_;
  201. /// Network update time accumulator
  202. float timeAcc_;
  203. /// Client framenumber
  204. unsigned short frameNumber_;
  205. /// Scene info
  206. SceneInfo sceneInfo_;
  207. /// Ongoing file downloads
  208. std::map<StringHash, FileTransfer> fileTransfers_;
  209. /// Directory to use for downloads
  210. std::string downloadDirectory_;
  211. /// Downloads required to join the scene
  212. std::set<std::string> pendingDownloads_;
  213. /// Login data to send to the server
  214. VariantMap pendingLoginData_;
  215. };