Server.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "Object.h"
  25. #include "Timer.h"
  26. #include <set>
  27. class Connection;
  28. class Network;
  29. class Scene;
  30. class VectorBuffer;
  31. /// Server-side ongoing download, with a timer for closing the file if unused
  32. struct ServerFileTransfer
  33. {
  34. SharedPtr<File> file_;
  35. Timer closeTimer_;
  36. };
  37. /// Multiplayer server subsystem
  38. class Server : public Object
  39. {
  40. OBJECT(Server);
  41. public:
  42. /// Construct with network subsystem pointer
  43. Server(Context* context);
  44. /// Destruct
  45. virtual ~Server();
  46. /// Set network updates (number of server frames) per second
  47. void SetNetFps(int fps);
  48. /// Set maximum unacked scene revisions stored per connection
  49. void SetMaxSceneRevisions(int revisions);
  50. /// Set delay for node becoming non-relevant, measured in server frames (prevent relevant/non-relevant state "flicker")
  51. void SetStayRelevantTime(int time);
  52. /// Add a scene to be provided over the network
  53. void AddScene(Scene* scene);
  54. /// Remove a scene
  55. void RemoveScene(Scene* scene);
  56. /// Start server in the specified port
  57. bool Start(unsigned short port);
  58. /// Stop server
  59. void Stop();
  60. /// Send and receive packets and update scene(s)
  61. void Update(float timeStep);
  62. /// Assign client to a scene
  63. bool SetClientScene(Connection* connection, Scene* scene);
  64. /// Return network updates per second
  65. int GetNetFps() const { return netFps_; }
  66. /// Return maximum unacked scene revisions stored per connection
  67. int GetMaxSceneRevisions() const { return maxSceneRevisions_; }
  68. /// Return delay for node becoming non-relevant
  69. int GetStayRelevantTime() const { return stayRelevantTime_; }
  70. /// Return whether is running on a port
  71. bool IsRunning() const;
  72. /// Return whether has a specific scene
  73. bool HasScene(Scene* scene) const;
  74. /// Return all scenes
  75. const std::vector<SharedPtr<Scene> >& GetScenes() const { return scenes_; }
  76. /// Return all connections
  77. const std::vector<SharedPtr<Connection> >& GetConnections() const { return connections_; }
  78. /// Return number of clients in scene
  79. unsigned GetNumUsersInScene(Scene* scene) const;
  80. private:
  81. /// Receive and handle packets from a connection
  82. void HandlePackets(Connection* connection);
  83. /// Handle peer connected event
  84. void HandlePeerConnected(StringHash eventType, VariantMap& eventData);
  85. /// Handle peer disconnected event
  86. void HandlePeerDisconnected(StringHash eventType, VariantMap& eventData);
  87. /// Handle a reliable client packet. Return true if should continue receiving packets
  88. bool HandleReliablePacket(Connection* connection, VectorBuffer& packet);
  89. /// Handle a login packet. Return true if client was authorized
  90. bool HandleLogin(Connection* connection, VectorBuffer& packet);
  91. /// Handle a file request packet
  92. void HandleRequestFile(Connection* connection, VectorBuffer& packet);
  93. /// Handle a join scene packet
  94. void HandleJoinScene(Connection* connection, VectorBuffer& packet);
  95. /// Handle a full update ack packet
  96. void HandleFullUpdateAck(Connection* connection, VectorBuffer& packet);
  97. /// Handle a client update packet. Return true if should continue receiving packets
  98. bool HandleClientUpdate(Connection* connection, VectorBuffer& packet);
  99. /// Send scene info to the client
  100. void SendSceneInfo(Connection* connection);
  101. /// Send full scene update to the client
  102. void SendFullUpdate(Connection* connection);
  103. /// Send a scene update to the client
  104. void SendServerUpdate(Connection* connection);
  105. /// Generate a challenge value for the client
  106. unsigned GenerateChallenge() const;
  107. /// Disconnect client either forcibly or benevolently
  108. void Disconnect(Connection* connection, bool forced, const std::string& logMessage);
  109. /// Generate a scene update. If connection has no stored scene revisions, a full update will be written
  110. void WriteNetUpdate(Connection* connection, Serializer& dest);
  111. /// Return relevant node IDs for the client
  112. void GetRelevantNodes(Connection* connection, std::set<unsigned>& dest) const;
  113. /// Scenes
  114. std::vector<SharedPtr<Scene> > scenes_;
  115. /// Client connections
  116. std::vector<SharedPtr<Connection> > connections_;
  117. /// Ongoing file downloads
  118. std::map<StringHash, ServerFileTransfer> fileTransfers_;
  119. /// Network updates per second
  120. int netFps_;
  121. /// Network update time accumulator
  122. float timeAcc_;
  123. /// Current server framenumber
  124. unsigned short frameNumber_;
  125. /// Maximum scene revisions to store per connection
  126. unsigned maxSceneRevisions_;
  127. /// Delay for node becoming non-relevant, in server frames
  128. int stayRelevantTime_;
  129. };