Peer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include "Precompiled.h"
  24. #include "Log.h"
  25. #include "NetworkEvents.h"
  26. #include "Peer.h"
  27. #include "StringUtils.h"
  28. #include <enet/enet.h>
  29. #include "DebugNew.h"
  30. static const int MAX_IPADDRESS = 32;
  31. OBJECTTYPESTATIC(Peer);
  32. Peer::Peer(Context* context, ENetPeer* peer, PeerType type) :
  33. Object(context),
  34. peer_(peer),
  35. type_(type),
  36. connectionState_(CS_CONNECTING),
  37. port_(0),
  38. simulatedPacketLoss_(0.0f),
  39. simulatedLatency_(0),
  40. halfSimulatedLatency_(0),
  41. userData_(0)
  42. {
  43. }
  44. Peer::~Peer()
  45. {
  46. FlushPackets();
  47. OnDisconnect();
  48. }
  49. void Peer::Send(const VectorBuffer& packet, unsigned char channel, bool reliable, bool inOrder)
  50. {
  51. if ((!peer_) || (!packet.GetSize()))
  52. return;
  53. if ((simulatedPacketLoss_ > 0.0f) && (!reliable))
  54. {
  55. if (Random() < simulatedPacketLoss_)
  56. return;
  57. }
  58. ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetSize(), reliable ? ENET_PACKET_FLAG_RELIABLE :
  59. (inOrder ? 0 : ENET_PACKET_FLAG_UNSEQUENCED));
  60. if (!simulatedLatency_)
  61. enet_peer_send(peer_, channel, enetPacket);
  62. else
  63. {
  64. QueuedPacket packet;
  65. packet.packet_ = enetPacket;
  66. packet.channel_ = channel;
  67. sentPackets_.push_back(packet);
  68. }
  69. }
  70. void Peer::Send(const void* data, unsigned size, unsigned char channel, bool reliable, bool inOrder)
  71. {
  72. if ((!peer_) || (!data) || (!size))
  73. return;
  74. if ((simulatedPacketLoss_ != 0.0f) && (!reliable))
  75. {
  76. if (Random() < simulatedPacketLoss_)
  77. return;
  78. }
  79. ENetPacket* enetPacket = enet_packet_create(data, size, reliable ? ENET_PACKET_FLAG_RELIABLE : (inOrder ? 0 :
  80. ENET_PACKET_FLAG_UNSEQUENCED));
  81. if (!simulatedLatency_)
  82. enet_peer_send(peer_, channel, enetPacket);
  83. else
  84. {
  85. QueuedPacket packet;
  86. packet.packet_ = enetPacket;
  87. packet.channel_ = channel;
  88. sentPackets_.push_back(packet);
  89. }
  90. }
  91. bool Peer::Receive(VectorBuffer& packet, unsigned char channel)
  92. {
  93. bool received = false;
  94. bool reliable = false;
  95. if (channel == CHANNEL_ANY)
  96. {
  97. for (std::map<unsigned char, std::vector<QueuedPacket> >::iterator i = packets_.begin(); i != packets_.end(); ++i)
  98. {
  99. std::vector<QueuedPacket>& packetList = i->second;
  100. if ((!packetList.empty()) && (packetList.front().timer_.GetMSec(false) >= halfSimulatedLatency_))
  101. {
  102. ENetPacket* enetPacket = packetList.front().packet_;
  103. reliable = (enetPacket->flags & ENET_PACKET_FLAG_RELIABLE) != 0;
  104. packetList.erase(packetList.begin());
  105. packet.SetData(enetPacket->data, enetPacket->dataLength);
  106. enet_packet_destroy(enetPacket);
  107. received = true;
  108. break;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. std::vector<QueuedPacket>& packetList = packets_[channel];
  115. if ((!packetList.empty()) && (packetList.front().timer_.GetMSec(false) >= halfSimulatedLatency_))
  116. {
  117. ENetPacket* enetPacket = packetList.front().packet_;
  118. reliable = (enetPacket->flags & ENET_PACKET_FLAG_RELIABLE) != 0;
  119. packetList.erase(packetList.begin());
  120. packet.SetData(enetPacket->data, enetPacket->dataLength);
  121. enet_packet_destroy(enetPacket);
  122. received = true;
  123. }
  124. }
  125. if ((received) && (!reliable) && (simulatedPacketLoss_ > 0.0f))
  126. {
  127. if (Random() < simulatedPacketLoss_)
  128. {
  129. packet.Clear();
  130. received = false;
  131. }
  132. }
  133. return received;
  134. }
  135. void Peer::Update()
  136. {
  137. // Check send timer of packets with simulated latency and send as necessary
  138. for (std::vector<QueuedPacket>::iterator i = sentPackets_.begin(); i != sentPackets_.end();)
  139. {
  140. if (i->timer_.GetMSec(false) >= halfSimulatedLatency_)
  141. {
  142. enet_peer_send(peer_, i->channel_, i->packet_);
  143. i = sentPackets_.erase(i);
  144. }
  145. else
  146. ++i;
  147. }
  148. }
  149. void Peer::FlushPackets()
  150. {
  151. for (std::map<unsigned char, std::vector<QueuedPacket> >::iterator i = packets_.begin(); i != packets_.end(); ++i)
  152. {
  153. std::vector<QueuedPacket>& packetList = i->second;
  154. while (!packetList.empty())
  155. {
  156. ENetPacket* enetPacket = packetList.back().packet_;
  157. packetList.pop_back();
  158. enet_packet_destroy(enetPacket);
  159. }
  160. }
  161. while (!sentPackets_.empty())
  162. {
  163. ENetPacket* enetPacket = sentPackets_.back().packet_;
  164. sentPackets_.pop_back();
  165. enet_packet_destroy(enetPacket);
  166. }
  167. }
  168. void Peer::Disconnect()
  169. {
  170. if ((peer_) && (connectionState_ > CS_DISCONNECTING))
  171. {
  172. connectionState_ = CS_DISCONNECTING;
  173. enet_peer_disconnect(peer_, 0);
  174. }
  175. }
  176. void Peer::ForceDisconnect()
  177. {
  178. OnDisconnect();
  179. }
  180. void Peer::SetSimulatedPacketLoss(float loss)
  181. {
  182. simulatedPacketLoss_ = Clamp(loss, 0.0f, 1.0f);
  183. }
  184. void Peer::SetSimulatedLatency(unsigned latency)
  185. {
  186. simulatedLatency_ = latency;
  187. halfSimulatedLatency_ = latency >> 1;
  188. }
  189. void Peer::OnConnect()
  190. {
  191. if (peer_)
  192. {
  193. char ipBuffer[MAX_IPADDRESS];
  194. enet_address_get_host_ip(&peer_->address, ipBuffer, MAX_IPADDRESS);
  195. address_ = std::string(ipBuffer);
  196. port_ = peer_->address.port;
  197. connectionState_ = CS_CONNECTED;
  198. // Send event
  199. using namespace PeerConnected;
  200. VariantMap eventData;
  201. eventData[P_PEER] = (void*)this;
  202. SendEvent(E_PEERCONNECTED, eventData);
  203. LOGINFO(address_ + ":" + ToString(port_) + " connected");
  204. }
  205. }
  206. void Peer::OnDisconnect()
  207. {
  208. if (peer_)
  209. {
  210. enet_peer_reset(peer_);
  211. connectionState_ = CS_DISCONNECTED;
  212. peer_ = 0;
  213. // Send event
  214. using namespace PeerDisconnected;
  215. VariantMap eventData;
  216. eventData[P_PEER] = (void*)this;
  217. SendEvent(E_PEERDISCONNECTED, eventData);
  218. if (!address_.empty())
  219. LOGINFO(address_ + ":" + ToString(port_) + " disconnected");
  220. else
  221. LOGINFO("Disconnected");
  222. }
  223. }