Network.h 4.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 "Object.h"
  25. #include "VectorBuffer.h"
  26. class Peer;
  27. struct _ENetHost;
  28. typedef _ENetHost ENetHost;
  29. /// Network subsystem. Sends and receives packets using ENet
  30. class Network : public Object
  31. {
  32. OBJECT(Network);
  33. public:
  34. /// Construct. Initialize ENet
  35. Network(Context* context);
  36. /// Destruct. Deinitialize ENet
  37. ~Network();
  38. /// Set maximum connections on the server
  39. void SetServerMaxConnections(unsigned server);
  40. /// Set maximum connections on the client
  41. void SetClientMaxConnections(unsigned client);
  42. /// Set number of packet channels to use. Each has its own packet ordering
  43. void SetNumChannels(unsigned channels);
  44. /// Set incoming and outgoing data rate
  45. void SetDataRate(int dataInBps, int dataOutBps);
  46. /// Service incoming and outgoing packets. Called by HandleBeginFrame()
  47. void Update();
  48. /// Start server on a port. Return true if successful
  49. bool StartServer(unsigned short port);
  50. /// Connect to a server
  51. Peer* Connect(const std::string& address, unsigned short port);
  52. /// Broadcast a packet to all connected client peers
  53. void Broadcast(const VectorBuffer& packet, unsigned char channel, bool reliable, bool inOrder = true);
  54. /// Broadcast a packet to all connected client peers
  55. void Broadcast(const void* data, unsigned size, unsigned char channel, bool reliable, bool inOrder = true);
  56. /// Stop server and close all connections
  57. void StopServer();
  58. /// Stop server and close all connections
  59. void StopClient();
  60. /// Return whether server has been started
  61. bool IsServer() const { return serverHost_ != 0; }
  62. /// Return whether client has been started (is automatically started when a connection attempt is made)
  63. bool IsClient() const { return clientHost_ != 0; }
  64. /// Return number of networking peers
  65. unsigned GetNumPeers() const { return peers_.size(); }
  66. /// Return all networking peers
  67. const std::vector<SharedPtr<Peer> >& GetPeers() const { return peers_; }
  68. /// Return networking peer by index
  69. Peer* GetPeer(unsigned index) const;
  70. /// Return the first server peer
  71. Peer* GetServerPeer() const;
  72. /// Return maximum server connections
  73. unsigned GetServerMaxConnections() const { return serverMaxConnections_; }
  74. /// Return maximum client connections
  75. unsigned GetClientMaxConnections() const { return clientMaxConnections_; }
  76. /// Return number of packet channels
  77. unsigned GetNumChannels() const { return numChannels_; }
  78. /// Return incoming data rate
  79. int GetDataRateIn() const { return dataInBps_; }
  80. /// Return outgoing data rate
  81. int GetDataRateOut() const { return dataOutBps_; }
  82. private:
  83. /// Update a specific network host
  84. void Update(ENetHost* enetHost);
  85. /// Handle frame begin event
  86. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  87. /// Server host
  88. ENetHost* serverHost_;
  89. /// Client host
  90. ENetHost* clientHost_;
  91. /// Peers
  92. std::vector<SharedPtr<Peer> > peers_;
  93. /// Maximum server connections
  94. unsigned serverMaxConnections_;
  95. /// Maximum client connections
  96. unsigned clientMaxConnections_;
  97. /// Number of packet channels to use
  98. unsigned numChannels_;
  99. /// Incoming data rate
  100. int dataInBps_;
  101. /// Outgoing data rate
  102. int dataOutBps_;
  103. };