host.c 15 KB

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