host.c 14 KB

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