host.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. @file host.c
  3. @brief ENet host management functions
  4. */
  5. #define ENET_BUILDING_LIB 1
  6. #include "enet/memory.h"
  7. #include "enet/enet.h"
  8. /** @defgroup host ENet host functions
  9. @{
  10. */
  11. /** Creates a host for communicating to peers.
  12. @param address the address at which other peers may connect to this host. If NULL, then no peers may connect to the host.
  13. @param peerCount the maximum number of peers that should be allocated for the host.
  14. @param incomingBandwidth downstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth.
  15. @param outgoingBandwidth upstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth.
  16. @returns the host on success and NULL on failure
  17. @remarks ENet will strategically drop packets on specific sides of a connection between hosts
  18. to ensure the host's bandwidth is not overwhelmed. The bandwidth parameters also determine
  19. the window size of a connection which limits the amount of reliable packets that may be in transit
  20. at any given time.
  21. */
  22. ENetHost *
  23. enet_host_create (const ENetAddress * address, size_t peerCount, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
  24. {
  25. ENetHost * host = (ENetHost *) enet_malloc (sizeof (ENetHost));
  26. ENetPeer * currentPeer;
  27. host -> peers = (ENetPeer *) enet_calloc (peerCount, sizeof (ENetPeer));
  28. host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, address);
  29. if (host -> socket == ENET_SOCKET_NULL)
  30. {
  31. enet_free (host -> peers);
  32. enet_free (host);
  33. return NULL;
  34. }
  35. if (address != NULL)
  36. host -> address = * address;
  37. host -> incomingBandwidth = incomingBandwidth;
  38. host -> outgoingBandwidth = outgoingBandwidth;
  39. host -> bandwidthThrottleEpoch = 0;
  40. host -> recalculateBandwidthLimits = 0;
  41. host -> mtu = ENET_HOST_DEFAULT_MTU;
  42. host -> peerCount = peerCount;
  43. host -> lastServicedPeer = host -> peers;
  44. host -> commandCount = 0;
  45. host -> bufferCount = 0;
  46. host -> receivedAddress.host = ENET_HOST_ANY;
  47. host -> receivedAddress.port = 0;
  48. host -> receivedDataLength = 0;
  49. for (currentPeer = host -> peers;
  50. currentPeer < & host -> peers [host -> peerCount];
  51. ++ currentPeer)
  52. {
  53. currentPeer -> host = host;
  54. currentPeer -> incomingPeerID = currentPeer - host -> peers;
  55. currentPeer -> data = NULL;
  56. enet_list_clear (& currentPeer -> acknowledgements);
  57. enet_list_clear (& currentPeer -> sentReliableCommands);
  58. enet_list_clear (& currentPeer -> sentUnreliableCommands);
  59. enet_list_clear (& currentPeer -> outgoingReliableCommands);
  60. enet_list_clear (& currentPeer -> outgoingUnreliableCommands);
  61. enet_peer_reset (currentPeer);
  62. }
  63. return host;
  64. }
  65. /** Destroys the host and all resources associated with it.
  66. @param host pointer to the host to destroy
  67. */
  68. void
  69. enet_host_destroy (ENetHost * host)
  70. {
  71. ENetPeer * currentPeer;
  72. enet_socket_destroy (host -> socket);
  73. for (currentPeer = host -> peers;
  74. currentPeer < & host -> peers [host -> peerCount];
  75. ++ currentPeer)
  76. {
  77. enet_peer_reset (currentPeer);
  78. }
  79. enet_free (host -> peers);
  80. enet_free (host);
  81. }
  82. /** Initiates a connection to a foreign host.
  83. @param host host seeking the connection
  84. @param address destination for the connection
  85. @param channelCount number of channels to allocate
  86. @returns a peer representing the foreign host on success, NULL on failure
  87. @remarks The peer returned will have not completed the connection until enet_host_service()
  88. notifies of an ENET_EVENT_TYPE_CONNECT event for the peer.
  89. */
  90. ENetPeer *
  91. enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelCount)
  92. {
  93. ENetPeer * currentPeer;
  94. ENetChannel * channel;
  95. ENetProtocol command;
  96. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
  97. channelCount = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
  98. else
  99. if (channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  100. channelCount = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  101. for (currentPeer = host -> peers;
  102. currentPeer < & host -> peers [host -> peerCount];
  103. ++ currentPeer)
  104. {
  105. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
  106. break;
  107. }
  108. if (currentPeer >= & host -> peers [host -> peerCount])
  109. return NULL;
  110. currentPeer -> state = ENET_PEER_STATE_CONNECTING;
  111. currentPeer -> address = * address;
  112. currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
  113. currentPeer -> channelCount = channelCount;
  114. currentPeer -> challenge = (enet_uint32) rand ();
  115. if (host -> outgoingBandwidth == 0)
  116. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  117. else
  118. currentPeer -> windowSize = (host -> outgoingBandwidth /
  119. ENET_PEER_WINDOW_SIZE_SCALE) *
  120. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  121. if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  122. currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  123. else
  124. if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  125. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  126. for (channel = currentPeer -> channels;
  127. channel < & currentPeer -> channels [channelCount];
  128. ++ channel)
  129. {
  130. channel -> outgoingReliableSequenceNumber = 0;
  131. channel -> outgoingUnreliableSequenceNumber = 0;
  132. channel -> incomingReliableSequenceNumber = 0;
  133. channel -> incomingUnreliableSequenceNumber = 0;
  134. enet_list_clear (& channel -> incomingReliableCommands);
  135. enet_list_clear (& channel -> incomingUnreliableCommands);
  136. }
  137. command.header.command = ENET_PROTOCOL_COMMAND_CONNECT;
  138. command.header.channelID = 0xFF;
  139. command.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE;
  140. command.header.commandLength = sizeof (ENetProtocolConnect);
  141. command.connect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
  142. command.connect.mtu = ENET_HOST_TO_NET_16 (currentPeer -> mtu);
  143. command.connect.windowSize = ENET_HOST_TO_NET_32 (currentPeer -> windowSize);
  144. command.connect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
  145. command.connect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
  146. command.connect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  147. command.connect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
  148. command.connect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
  149. command.connect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
  150. enet_peer_queue_outgoing_command (currentPeer, & command, NULL, 0, 0);
  151. return currentPeer;
  152. }
  153. /** Queues a packet to be sent to all peers associated with the host.
  154. @param host host on which to broadcast the packet
  155. @param channelID channel on which to broadcast
  156. @param packet packet to broadcast
  157. */
  158. void
  159. enet_host_broadcast (ENetHost * host, enet_uint8 channelID, ENetPacket * packet)
  160. {
  161. ENetPeer * currentPeer;
  162. for (currentPeer = host -> peers;
  163. currentPeer < & host -> peers [host -> peerCount];
  164. ++ currentPeer)
  165. {
  166. if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
  167. continue;
  168. enet_peer_send (currentPeer, channelID, packet);
  169. }
  170. if (packet -> referenceCount == 0)
  171. enet_packet_destroy (packet);
  172. }
  173. /** Adjusts the bandwidth limits of a host.
  174. @param host host to adjust
  175. @param incomingBandwidth new incoming bandwidth
  176. @param outgoingBandwidth new outgoing bandwidth
  177. @remarks the incoming and outgoing bandwidth parameters are identical in function to those
  178. specified in enet_host_create().
  179. */
  180. void
  181. enet_host_bandwidth_limit (ENetHost * host, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
  182. {
  183. host -> incomingBandwidth = incomingBandwidth;
  184. host -> outgoingBandwidth = outgoingBandwidth;
  185. host -> recalculateBandwidthLimits = 1;
  186. }
  187. void
  188. enet_host_bandwidth_throttle (ENetHost * host)
  189. {
  190. enet_uint32 timeCurrent = enet_time_get (),
  191. elapsedTime = timeCurrent - host -> bandwidthThrottleEpoch,
  192. peersTotal = 0,
  193. dataTotal = 0,
  194. peersRemaining,
  195. bandwidth,
  196. throttle = 0,
  197. bandwidthLimit = 0;
  198. int needsAdjustment;
  199. ENetPeer * peer;
  200. ENetProtocol command;
  201. if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  202. return;
  203. for (peer = host -> peers;
  204. peer < & host -> peers [host -> peerCount];
  205. ++ peer)
  206. {
  207. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  208. continue;
  209. ++ peersTotal;
  210. dataTotal += peer -> outgoingDataTotal;
  211. }
  212. if (peersTotal == 0)
  213. return;
  214. peersRemaining = peersTotal;
  215. needsAdjustment = 1;
  216. if (host -> outgoingBandwidth == 0)
  217. bandwidth = ~0;
  218. else
  219. bandwidth = (host -> outgoingBandwidth * elapsedTime) / 1000;
  220. while (peersRemaining > 0 && needsAdjustment != 0)
  221. {
  222. needsAdjustment = 0;
  223. if (dataTotal < bandwidth)
  224. throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
  225. else
  226. throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
  227. for (peer = host -> peers;
  228. peer < & host -> peers [host -> peerCount];
  229. ++ peer)
  230. {
  231. enet_uint32 peerBandwidth;
  232. if (peer -> state != ENET_PEER_STATE_CONNECTED ||
  233. peer -> incomingBandwidth == 0 ||
  234. peer -> outgoingBandwidthThrottleEpoch == timeCurrent)
  235. continue;
  236. peerBandwidth = (peer -> incomingBandwidth * elapsedTime) / 1000;
  237. if ((throttle * peer -> outgoingDataTotal) / ENET_PEER_PACKET_THROTTLE_SCALE <= peerBandwidth)
  238. continue;
  239. peer -> packetThrottleLimit = (peerBandwidth *
  240. ENET_PEER_PACKET_THROTTLE_SCALE) / peer -> outgoingDataTotal;
  241. if (peer -> packetThrottleLimit == 0)
  242. peer -> packetThrottleLimit = 1;
  243. if (peer -> packetThrottle > peer -> packetThrottleLimit)
  244. peer -> packetThrottle = peer -> packetThrottleLimit;
  245. peer -> outgoingBandwidthThrottleEpoch = timeCurrent;
  246. needsAdjustment = 1;
  247. -- peersRemaining;
  248. bandwidth -= peerBandwidth;
  249. dataTotal -= peerBandwidth;
  250. }
  251. }
  252. if (peersRemaining > 0)
  253. for (peer = host -> peers;
  254. peer < & host -> peers [host -> peerCount];
  255. ++ peer)
  256. {
  257. if (peer -> state != ENET_PEER_STATE_CONNECTED ||
  258. peer -> outgoingBandwidthThrottleEpoch == timeCurrent)
  259. continue;
  260. peer -> packetThrottleLimit = throttle;
  261. if (peer -> packetThrottle > peer -> packetThrottleLimit)
  262. peer -> packetThrottle = peer -> packetThrottleLimit;
  263. }
  264. if (host -> recalculateBandwidthLimits)
  265. {
  266. host -> recalculateBandwidthLimits = 0;
  267. peersRemaining = peersTotal;
  268. bandwidth = host -> incomingBandwidth;
  269. needsAdjustment = 1;
  270. if (bandwidth == 0)
  271. bandwidthLimit = 0;
  272. else
  273. while (peersRemaining > 0 && needsAdjustment != 0)
  274. {
  275. needsAdjustment = 0;
  276. bandwidthLimit = bandwidth / peersRemaining;
  277. for (peer = host -> peers;
  278. peer < & host -> peers [host -> peerCount];
  279. ++ peer)
  280. {
  281. if (peer -> state != ENET_PEER_STATE_CONNECTED ||
  282. peer -> incomingBandwidthThrottleEpoch == timeCurrent)
  283. continue;
  284. if (peer -> outgoingBandwidth > 0 &&
  285. bandwidthLimit > peer -> outgoingBandwidth)
  286. continue;
  287. peer -> incomingBandwidthThrottleEpoch = timeCurrent;
  288. needsAdjustment = 1;
  289. -- peersRemaining;
  290. bandwidth -= peer -> outgoingBandwidth;
  291. }
  292. }
  293. for (peer = host -> peers;
  294. peer < & host -> peers [host -> peerCount];
  295. ++ peer)
  296. {
  297. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  298. continue;
  299. command.header.command = ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT;
  300. command.header.channelID = 0xFF;
  301. command.header.flags = 0;
  302. command.header.commandLength = sizeof (ENetProtocolBandwidthLimit);
  303. command.bandwidthLimit.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  304. if (peer -> incomingBandwidthThrottleEpoch == timeCurrent)
  305. command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (peer -> outgoingBandwidth);
  306. else
  307. command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (bandwidthLimit);
  308. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  309. }
  310. }
  311. host -> bandwidthThrottleEpoch = timeCurrent;
  312. for (peer = host -> peers;
  313. peer < & host -> peers [host -> peerCount];
  314. ++ peer)
  315. {
  316. peer -> incomingDataTotal = 0;
  317. peer -> outgoingDataTotal = 0;
  318. }
  319. }
  320. /** @} */