Client.h 7.8 KB

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