host.c 15 KB

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