Peer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "VectorBuffer.h"
  27. #include <list>
  28. /// Peer type
  29. enum PeerType
  30. {
  31. PEER_SERVER = 1,
  32. PEER_CLIENT
  33. };
  34. /// Peer's connection state
  35. enum ConnectionState
  36. {
  37. CS_DISCONNECTED = 0,
  38. CS_DISCONNECTING,
  39. CS_CONNECTING,
  40. CS_CONNECTED
  41. };
  42. static const unsigned char CHANNEL_ANY = 0xff;
  43. class VectorBuffer;
  44. struct _ENetHost;
  45. struct _ENetPacket;
  46. struct _ENetPeer;
  47. typedef _ENetHost ENetHost;
  48. typedef _ENetPacket ENetPacket;
  49. typedef _ENetPeer ENetPeer;
  50. /// Queued packet
  51. struct QueuedPacket
  52. {
  53. /// ENet packet
  54. ENetPacket* packet_;
  55. /// Channel number
  56. unsigned char channel_;
  57. /// Timer for latency simulation
  58. Timer timer_;
  59. };
  60. /// Networking peer (a remote host)
  61. class Peer : public Object
  62. {
  63. OBJECT(Peer);
  64. friend class Network;
  65. public:
  66. /// Construct
  67. Peer(Context* context, ENetPeer* peer, PeerType type);
  68. /// Destruct
  69. virtual ~Peer();
  70. /// Send a packet
  71. void Send(const VectorBuffer& packet, unsigned char channel, bool reliable, bool inOrder = true);
  72. /// Send a packet
  73. void Send(const void* data, unsigned size, unsigned char channel, bool reliable, bool inOrder = true);
  74. /// Attempt to receive a packet. Return true if a packet was received
  75. bool Receive(VectorBuffer& packet, unsigned char channel = CHANNEL_ANY);
  76. /// Send latency simulated packets
  77. void Update();
  78. /// Remove all queued packets
  79. void FlushPackets();
  80. /// Disconnect benevolently
  81. void Disconnect();
  82. /// Disconnect forcibly
  83. void ForceDisconnect();
  84. /// Set simulated packet loss between 0.0 - 1.0
  85. void SetSimulatedPacketLoss(float loss);
  86. /// Set simulated connection latency in milliseconds
  87. void SetSimulatedLatency(unsigned lag);
  88. /// Set user data for identifying the peer
  89. void SetUserData(void* userData);
  90. /// Return peer type
  91. PeerType GetPeerType() const { return type_; }
  92. /// Return connection state
  93. ConnectionState GetConnectionState() const { return connectionState_; }
  94. /// Return whether has received packets waiting
  95. bool HasPackets() const { return packets_.size() > 0; }
  96. /// Return number of received packets waiting
  97. unsigned GetNumPackets() const { return packets_.size(); }
  98. /// Return address of remote host
  99. const std::string& GetAddress()const { return address_; }
  100. /// Return port of remote host
  101. unsigned short GetPort() const { return port_; }
  102. /// Return simulated packet loss
  103. float GetSimulatedPacketLoss() const { return simulatedPacketLoss_; }
  104. /// Return simulated latency
  105. unsigned GetSimulatedLatency() const { return simulatedLatency_; }
  106. /// Return user data
  107. void* GetUserData() const { return userData_; }
  108. private:
  109. /// Handle connection
  110. void OnConnect();
  111. /// Handle disconnection
  112. void OnDisconnect();
  113. /// ENet peer
  114. ENetPeer* peer_;
  115. /// Peer type
  116. PeerType type_;
  117. /// Connection state
  118. ConnectionState connectionState_;
  119. /// Received packets per channel
  120. std::map<unsigned char, std::vector<QueuedPacket> > packets_;
  121. /// Latency-simulated packets waiting to be sent
  122. std::vector<QueuedPacket> sentPackets_;
  123. /// Remote host address
  124. std::string address_;
  125. /// Remote host port
  126. unsigned short port_;
  127. /// Simulated packet loss
  128. float simulatedPacketLoss_;
  129. /// Simulated latency
  130. unsigned simulatedLatency_;
  131. /// Half of simulated latency, to divide evenly between send and receive latency
  132. unsigned halfSimulatedLatency_;
  133. /// User data
  134. void* userData_;
  135. };