Connection.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "HashSet.h"
  26. #include "Object.h"
  27. #include "ReplicationState.h"
  28. #include "VectorBuffer.h"
  29. #include <kNetFwd.h>
  30. #include <kNet/SharedPtr.h>
  31. #ifdef SendMessage
  32. #undef SendMessage
  33. #endif
  34. class MemoryBuffer;
  35. class Node;
  36. class Scene;
  37. class Serializable;
  38. /// Queued remote event
  39. struct RemoteEvent
  40. {
  41. /// Receiver node ID (0 if not a remote node event)
  42. unsigned receiverID_;
  43. /// Event type
  44. StringHash eventType_;
  45. /// Event data
  46. VariantMap eventData_;
  47. /// In order -flag
  48. bool inOrder_;
  49. };
  50. /// Connection in a networked scene
  51. class Connection : public Object
  52. {
  53. OBJECT(Connection);
  54. public:
  55. /// Construct with context and kNet message connection pointers
  56. Connection(Context* context, bool isClient, kNet::SharedPtr<kNet::MessageConnection> connection);
  57. /// Destruct
  58. ~Connection();
  59. /// Send a message
  60. void SendMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
  61. /// Send a message
  62. void SendMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg);
  63. /// Send a message with content ID
  64. void SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
  65. /// Send a message with content ID
  66. void SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const VectorBuffer& msg);
  67. /// Send a remote event
  68. void SendRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData = VariantMap());
  69. /// Send a remote node event
  70. void SendRemoteEvent(Node* receiver, StringHash eventType, bool inOrder, const VariantMap& eventData = VariantMap());
  71. /// Assign scene. On the server, this will cause the client to load it
  72. void SetScene(Scene* newScene);
  73. /// Assign identity. Called by Network
  74. void SetIdentity(const VariantMap& identity);
  75. /// Set new controls. Moves the current controls as previous
  76. void SetControls(const Controls& newControls);
  77. /// Set the connection pending status. Called by Network
  78. void SetConnectPending(bool connectPending);
  79. /// Disconnect. If wait time is non-zero, will block while waiting for disconnect to finish
  80. void Disconnect(int waitMSec = 0);
  81. /// Send scene update messages. Called by Network
  82. void SendServerUpdate();
  83. /// Send latest controls from the client. Called by Network
  84. void SendClientUpdate();
  85. /// Send queued remote events. Called by Network
  86. void SendQueuedRemoteEvents();
  87. /// Process pending latest data for nodes and components
  88. void ProcessPendingLatestData();
  89. /// Process a LoadScene message from the server. Called by Network
  90. void ProcessLoadScene(int msgID, MemoryBuffer& msg);
  91. /// Process a SceneChecksumError message from the server. Called by Network
  92. void ProcessSceneChecksumError(int msgID, MemoryBuffer& msg);
  93. /// Process a scene update message from the server. Called by Network
  94. void ProcessSceneUpdate(int msgID, MemoryBuffer& msg);
  95. /// Process an Identity message from the client. Called by Network
  96. void ProcessIdentity(int msgID, MemoryBuffer& msg);
  97. /// Process a Controls message from the client. Called by Network
  98. void ProcessControls(int msgID, MemoryBuffer& msg);
  99. /// Process a SceneLoaded message from the client. Called by Network
  100. void ProcessSceneLoaded(int msgID, MemoryBuffer& msg);
  101. /// Process a remote event message from the client or server. Called by Network
  102. void ProcessRemoteEvent(int msgID, MemoryBuffer& msg);
  103. /// Return the kNet message connection
  104. kNet::MessageConnection* GetMessageConnection() const;
  105. /// Return client identity
  106. const VariantMap& GetIdentity() const { return identity_; }
  107. /// Return the scene used by this connection
  108. Scene* GetScene() const;
  109. /// Return the client controls of this connection
  110. const Controls& GetControls() const { return controls_; }
  111. /// Return the previous client controls of this connection
  112. const Controls& GetPreviousControls() const { return previousControls_; }
  113. /// Return whether is a client connection
  114. bool IsClient() const { return isClient_; }
  115. /// Return whether is fully connected
  116. bool IsConnected() const;
  117. /// Return whether connection is pending
  118. bool IsConnectPending() const { return connectPending_; }
  119. /// Return whether the scene is loaded and ready to receive updates from network
  120. bool IsSceneLoaded() const { return sceneLoaded_; }
  121. /// Return remote address
  122. String GetAddress() const;
  123. /// Return remote port
  124. unsigned short GetPort() const;
  125. /// Return an address:port string
  126. String ToString() const;
  127. private:
  128. /// Handle scene loaded event
  129. void HandleAsyncLoadFinished(StringHash eventType, VariantMap& eventData);
  130. /// Process a node for sending a network update. Recurses to process depended on node(s) first
  131. void ProcessNode(Node* node);
  132. /// Process a node that the client had not yet received
  133. void ProcessNewNode(Node* node);
  134. /// Process a node that the client has already received
  135. void ProcessExistingNode(Node* node);
  136. /// kNet message connection
  137. kNet::SharedPtr<kNet::MessageConnection> connection_;
  138. /// Identity map
  139. VariantMap identity_;
  140. /// Scene
  141. WeakPtr<Scene> scene_;
  142. /// Scene replication state (as last sent to the client)
  143. Map<unsigned, NodeReplicationState> sceneState_;
  144. /// Pending latest data for not yet received nodes
  145. Map<unsigned, PODVector<unsigned char> > nodeLatestData_;
  146. /// Pending latest data for not yet received components
  147. Map<unsigned, PODVector<unsigned char> > componentLatestData_;
  148. /// Queued remote events
  149. Vector<RemoteEvent> remoteEvents_;
  150. /// Delta update bits
  151. PODVector<unsigned char> deltaUpdateBits_;
  152. /// Node's changed user variables
  153. HashSet<ShortStringHash> changedVars_;
  154. /// Already processed nodes during a replication update
  155. HashSet<Node*> processedNodes_;
  156. /// Preallocated variants of correct type per networked object class
  157. Map<ShortStringHash, Vector<Variant> > classCurrentState_;
  158. /// Reused message buffer
  159. VectorBuffer msg_;
  160. /// Current controls
  161. Controls controls_;
  162. /// Previous controls
  163. Controls previousControls_;
  164. /// Update frame number
  165. unsigned frameNumber_;
  166. /// Client flag
  167. bool isClient_;
  168. /// Connection pending flag
  169. bool connectPending_;
  170. /// Scene loaded flag
  171. bool sceneLoaded_;
  172. };