host.c 14 KB

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