host.c 13 KB

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