Network.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "Connection.h"
  25. #include "Object.h"
  26. #include "VectorBuffer.h"
  27. #include <kNet/IMessageHandler.h>
  28. #include <kNet/INetworkServerListener.h>
  29. class MemoryBuffer;
  30. class Scene;
  31. /// Network subsystem. Manages joining to or hosting networked scenes.
  32. class Network : public Object, public kNet::IMessageHandler, public kNet::INetworkServerListener
  33. {
  34. OBJECT(Network);
  35. public:
  36. /// Construct
  37. Network(Context* context);
  38. /// Destruct
  39. ~Network();
  40. /// Handle a kNet message from either a client or the server
  41. virtual void HandleMessage(kNet::MessageConnection* source, kNet::message_id_t id, const char* data, size_t numBytes);
  42. /// Compute the content ID for a message
  43. virtual u32 ComputeContentID(kNet::message_id_t id, const char* data, size_t numBytes);
  44. /// Handle a new client connection
  45. virtual void NewConnectionEstablished(kNet::MessageConnection* connection);
  46. /// Handle a client disconnection
  47. virtual void ClientDisconnected(kNet::MessageConnection* connection);
  48. /// Connect to a server using UDP protocol. Return true if connection process successfully started
  49. bool Connect(const String& address, unsigned short port, Scene* scene, const VariantMap& identity = VariantMap());
  50. /// Disconnect the connection to the server. If wait time is non-zero, will block while waiting for disconnect to finish
  51. void Disconnect(int waitMSec = 0);
  52. /// Start a server on a port using UDP protocol. Return true if successful
  53. bool StartServer(unsigned short port);
  54. /// Stop the server
  55. void StopServer();
  56. /// Broadcast a message to all client connections
  57. void BroadcastMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg);
  58. /// Broadcast a message to all client connections
  59. void BroadcastMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
  60. /// Broadcast a message with content ID to all client connections
  61. void BroadcastMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const VectorBuffer& msg);
  62. /// Broadcast a message with content ID to all client connections
  63. void BroadcastMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
  64. /// Broadcast a remote event to all client connections
  65. void BroadcastRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData = VariantMap());
  66. /// Broadcast a remote event to all client connections in the specific scene
  67. void BroadcastRemoteEvent(Scene* scene, StringHash eventType, bool inOrder, const VariantMap& eventData = VariantMap());
  68. /// Broadcast a remote node event to all client connections in the scene with this node
  69. void BroadcastRemoteEvent(Node* receiver, StringHash eventType, bool inOrder, const VariantMap& eventData = VariantMap());
  70. /// Set network update FPS
  71. void SetUpdateFps(int fps);
  72. /// Update connections. Called by HandleBeginFrame
  73. void Update(float timeStep);
  74. /// Return network update FPS
  75. int GetUpdateFps() const { return updateFps_; }
  76. /// Return a client or server connection by kNet MessageConnection, or null if none exist
  77. Connection* GetConnection(kNet::MessageConnection* connection) const;
  78. /// Return the connection to the server. Null if not connected
  79. Connection* GetServerConnection() const;
  80. /// Return all client connections
  81. const Map<kNet::MessageConnection*, SharedPtr<Connection> > GetClientConnections() const { return clientConnections_; }
  82. /// Return whether the server is running
  83. bool IsServerRunning() const;
  84. private:
  85. /// Handle begin frame event
  86. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  87. /// Handle server connection
  88. void OnServerConnected();
  89. /// Handle server disconnection
  90. void OnServerDisconnected();
  91. /// kNet Network instance
  92. kNet::Network* network_;
  93. /// Client's server connection
  94. SharedPtr<Connection> serverConnection_;
  95. /// Server's client connections
  96. Map<kNet::MessageConnection*, SharedPtr<Connection> > clientConnections_;
  97. /// Network update FPS
  98. int updateFps_;
  99. /// Network time interval
  100. float updateInterval_;
  101. /// Network update time accumulator
  102. float updateAcc_;
  103. };