Peer.cpp 7.4 KB

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