host.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
  15. @param incomingBandwidth downstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth.
  16. @param outgoingBandwidth upstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth.
  17. @returns the host on success and NULL on failure
  18. @remarks ENet will strategically drop packets on specific sides of a connection between hosts
  19. to ensure the host's bandwidth is not overwhelmed. The bandwidth parameters also determine
  20. the window size of a connection which limits the amount of reliable packets that may be in transit
  21. at any given time.
  22. */
  23. ENetHost *
  24. enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelLimit, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
  25. {
  26. ENetHost * host;
  27. ENetPeer * currentPeer;
  28. if (peerCount > ENET_PROTOCOL_MAXIMUM_PEER_ID)
  29. return NULL;
  30. host = (ENetHost *) enet_malloc (sizeof (ENetHost));
  31. if (host == NULL)
  32. return NULL;
  33. memset (host, 0, sizeof (ENetHost));
  34. host -> peers = (ENetPeer *) enet_malloc (peerCount * sizeof (ENetPeer));
  35. if (host -> peers == NULL)
  36. {
  37. enet_free (host);
  38. return NULL;
  39. }
  40. memset (host -> peers, 0, peerCount * sizeof (ENetPeer));
  41. host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM);
  42. if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address) < 0))
  43. {
  44. if (host -> socket != ENET_SOCKET_NULL)
  45. enet_socket_destroy (host -> socket);
  46. enet_free (host -> peers);
  47. enet_free (host);
  48. return NULL;
  49. }
  50. enet_socket_set_option (host -> socket, ENET_SOCKOPT_NONBLOCK, 1);
  51. enet_socket_set_option (host -> socket, ENET_SOCKOPT_BROADCAST, 1);
  52. enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
  53. enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
  54. if (address != NULL && enet_socket_get_address (host -> socket, & host -> address) < 0)
  55. host -> address = * address;
  56. if (! channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  57. channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  58. else
  59. if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
  60. channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
  61. host -> randomSeed = (enet_uint32) (size_t) host;
  62. host -> randomSeed += enet_host_random_seed ();
  63. host -> randomSeed = (host -> randomSeed << 16) | (host -> randomSeed >> 16);
  64. host -> channelLimit = channelLimit;
  65. host -> incomingBandwidth = incomingBandwidth;
  66. host -> outgoingBandwidth = outgoingBandwidth;
  67. host -> bandwidthThrottleEpoch = 0;
  68. host -> recalculateBandwidthLimits = 0;
  69. host -> mtu = ENET_HOST_DEFAULT_MTU;
  70. host -> peerCount = peerCount;
  71. host -> commandCount = 0;
  72. host -> bufferCount = 0;
  73. host -> checksum = NULL;
  74. host -> receivedAddress.host = ENET_HOST_ANY;
  75. host -> receivedAddress.port = 0;
  76. host -> receivedData = NULL;
  77. host -> receivedDataLength = 0;
  78. host -> totalSentData = 0;
  79. host -> totalSentPackets = 0;
  80. host -> totalReceivedData = 0;
  81. host -> totalReceivedPackets = 0;
  82. host -> connectedPeers = 0;
  83. host -> bandwidthLimitedPeers = 0;
  84. host -> duplicatePeers = ENET_PROTOCOL_MAXIMUM_PEER_ID;
  85. host -> compressor.context = NULL;
  86. host -> compressor.compress = NULL;
  87. host -> compressor.decompress = NULL;
  88. host -> compressor.destroy = NULL;
  89. host -> intercept = NULL;
  90. enet_list_clear (& host -> dispatchQueue);
  91. for (currentPeer = host -> peers;
  92. currentPeer < & host -> peers [host -> peerCount];
  93. ++ currentPeer)
  94. {
  95. currentPeer -> host = host;
  96. currentPeer -> incomingPeerID = currentPeer - host -> peers;
  97. currentPeer -> outgoingSessionID = currentPeer -> incomingSessionID = 0xFF;
  98. currentPeer -> data = NULL;
  99. enet_list_clear (& currentPeer -> acknowledgements);
  100. enet_list_clear (& currentPeer -> sentReliableCommands);
  101. enet_list_clear (& currentPeer -> sentUnreliableCommands);
  102. enet_list_clear (& currentPeer -> outgoingReliableCommands);
  103. enet_list_clear (& currentPeer -> outgoingUnreliableCommands);
  104. enet_list_clear (& currentPeer -> dispatchedCommands);
  105. enet_peer_reset (currentPeer);
  106. }
  107. return host;
  108. }
  109. /** Destroys the host and all resources associated with it.
  110. @param host pointer to the host to destroy
  111. */
  112. void
  113. enet_host_destroy (ENetHost * host)
  114. {
  115. ENetPeer * currentPeer;
  116. if (host == NULL)
  117. return;
  118. enet_socket_destroy (host -> socket);
  119. for (currentPeer = host -> peers;
  120. currentPeer < & host -> peers [host -> peerCount];
  121. ++ currentPeer)
  122. {
  123. enet_peer_reset (currentPeer);
  124. }
  125. if (host -> compressor.context != NULL && host -> compressor.destroy)
  126. (* host -> compressor.destroy) (host -> compressor.context);
  127. enet_free (host -> peers);
  128. enet_free (host);
  129. }
  130. /** Initiates a connection to a foreign host.
  131. @param host host seeking the connection
  132. @param address destination for the connection
  133. @param channelCount number of channels to allocate
  134. @param data user data supplied to the receiving host
  135. @returns a peer representing the foreign host on success, NULL on failure
  136. @remarks The peer returned will have not completed the connection until enet_host_service()
  137. notifies of an ENET_EVENT_TYPE_CONNECT event for the peer.
  138. */
  139. ENetPeer *
  140. enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelCount, enet_uint32 data)
  141. {
  142. ENetPeer * currentPeer;
  143. ENetChannel * channel;
  144. ENetProtocol command;
  145. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
  146. channelCount = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
  147. else
  148. if (channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  149. channelCount = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  150. for (currentPeer = host -> peers;
  151. currentPeer < & host -> peers [host -> peerCount];
  152. ++ currentPeer)
  153. {
  154. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
  155. break;
  156. }
  157. if (currentPeer >= & host -> peers [host -> peerCount])
  158. return NULL;
  159. currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
  160. if (currentPeer -> channels == NULL)
  161. return NULL;
  162. currentPeer -> channelCount = channelCount;
  163. currentPeer -> state = ENET_PEER_STATE_CONNECTING;
  164. currentPeer -> address = * address;
  165. currentPeer -> connectID = ++ host -> randomSeed;
  166. if (host -> outgoingBandwidth == 0)
  167. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  168. else
  169. currentPeer -> windowSize = (host -> outgoingBandwidth /
  170. ENET_PEER_WINDOW_SIZE_SCALE) *
  171. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  172. if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  173. currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  174. else
  175. if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  176. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  177. for (channel = currentPeer -> channels;
  178. channel < & currentPeer -> channels [channelCount];
  179. ++ channel)
  180. {
  181. channel -> outgoingReliableSequenceNumber = 0;
  182. channel -> outgoingUnreliableSequenceNumber = 0;
  183. channel -> incomingReliableSequenceNumber = 0;
  184. channel -> incomingUnreliableSequenceNumber = 0;
  185. enet_list_clear (& channel -> incomingReliableCommands);
  186. enet_list_clear (& channel -> incomingUnreliableCommands);
  187. channel -> usedReliableWindows = 0;
  188. memset (channel -> reliableWindows, 0, sizeof (channel -> reliableWindows));
  189. }
  190. command.header.command = ENET_PROTOCOL_COMMAND_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  191. command.header.channelID = 0xFF;
  192. command.connect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
  193. command.connect.incomingSessionID = currentPeer -> incomingSessionID;
  194. command.connect.outgoingSessionID = currentPeer -> outgoingSessionID;
  195. command.connect.mtu = ENET_HOST_TO_NET_32 (currentPeer -> mtu);
  196. command.connect.windowSize = ENET_HOST_TO_NET_32 (currentPeer -> windowSize);
  197. command.connect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
  198. command.connect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
  199. command.connect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  200. command.connect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
  201. command.connect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
  202. command.connect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
  203. command.connect.connectID = currentPeer -> connectID;
  204. command.connect.data = ENET_HOST_TO_NET_32 (data);
  205. enet_peer_queue_outgoing_command (currentPeer, & command, NULL, 0, 0);
  206. return currentPeer;
  207. }
  208. /** Queues a packet to be sent to all peers associated with the host.
  209. @param host host on which to broadcast the packet
  210. @param channelID channel on which to broadcast
  211. @param packet packet to broadcast
  212. */
  213. void
  214. enet_host_broadcast (ENetHost * host, enet_uint8 channelID, ENetPacket * packet)
  215. {
  216. ENetPeer * currentPeer;
  217. for (currentPeer = host -> peers;
  218. currentPeer < & host -> peers [host -> peerCount];
  219. ++ currentPeer)
  220. {
  221. if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
  222. continue;
  223. enet_peer_send (currentPeer, channelID, packet);
  224. }
  225. if (packet -> referenceCount == 0)
  226. enet_packet_destroy (packet);
  227. }
  228. /** Sets the packet compressor the host should use to compress and decompress packets.
  229. @param host host to enable or disable compression for
  230. @param compressor callbacks for for the packet compressor; if NULL, then compression is disabled
  231. */
  232. void
  233. enet_host_compress (ENetHost * host, const ENetCompressor * compressor)
  234. {
  235. if (host -> compressor.context != NULL && host -> compressor.destroy)
  236. (* host -> compressor.destroy) (host -> compressor.context);
  237. if (compressor)
  238. host -> compressor = * compressor;
  239. else
  240. host -> compressor.context = NULL;
  241. }
  242. /** Limits the maximum allowed channels of future incoming connections.
  243. @param host host to limit
  244. @param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
  245. */
  246. void
  247. enet_host_channel_limit (ENetHost * host, size_t channelLimit)
  248. {
  249. if (! channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  250. channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  251. else
  252. if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
  253. channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
  254. host -> channelLimit = channelLimit;
  255. }
  256. /** Adjusts the bandwidth limits of a host.
  257. @param host host to adjust
  258. @param incomingBandwidth new incoming bandwidth
  259. @param outgoingBandwidth new outgoing bandwidth
  260. @remarks the incoming and outgoing bandwidth parameters are identical in function to those
  261. specified in enet_host_create().
  262. */
  263. void
  264. enet_host_bandwidth_limit (ENetHost * host, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
  265. {
  266. host -> incomingBandwidth = incomingBandwidth;
  267. host -> outgoingBandwidth = outgoingBandwidth;
  268. host -> recalculateBandwidthLimits = 1;
  269. }
  270. void
  271. enet_host_bandwidth_throttle (ENetHost * host)
  272. {
  273. enet_uint32 timeCurrent = enet_time_get (),
  274. elapsedTime = timeCurrent - host -> bandwidthThrottleEpoch,
  275. peersRemaining = (enet_uint32) host -> connectedPeers,
  276. dataTotal = ~0,
  277. bandwidth = ~0,
  278. throttle = 0,
  279. bandwidthLimit = 0;
  280. int needsAdjustment = host -> bandwidthLimitedPeers > 0 ? 1 : 0;
  281. ENetPeer * peer;
  282. ENetProtocol command;
  283. if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  284. return;
  285. host -> bandwidthThrottleEpoch = timeCurrent;
  286. if (peersRemaining == 0)
  287. return;
  288. if (host -> outgoingBandwidth != 0)
  289. {
  290. dataTotal = 0;
  291. bandwidth = (host -> outgoingBandwidth * elapsedTime) / 1000;
  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. continue;
  298. dataTotal += peer -> outgoingDataTotal;
  299. }
  300. }
  301. while (peersRemaining > 0 && needsAdjustment != 0)
  302. {
  303. needsAdjustment = 0;
  304. if (dataTotal <= bandwidth)
  305. throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
  306. else
  307. throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
  308. for (peer = host -> peers;
  309. peer < & host -> peers [host -> peerCount];
  310. ++ peer)
  311. {
  312. enet_uint32 peerBandwidth;
  313. if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) ||
  314. peer -> incomingBandwidth == 0 ||
  315. peer -> outgoingBandwidthThrottleEpoch == timeCurrent)
  316. continue;
  317. peerBandwidth = (peer -> incomingBandwidth * elapsedTime) / 1000;
  318. if ((throttle * peer -> outgoingDataTotal) / ENET_PEER_PACKET_THROTTLE_SCALE <= peerBandwidth)
  319. continue;
  320. peer -> packetThrottleLimit = (peerBandwidth *
  321. ENET_PEER_PACKET_THROTTLE_SCALE) / peer -> outgoingDataTotal;
  322. if (peer -> packetThrottleLimit == 0)
  323. peer -> packetThrottleLimit = 1;
  324. if (peer -> packetThrottle > peer -> packetThrottleLimit)
  325. peer -> packetThrottle = peer -> packetThrottleLimit;
  326. peer -> outgoingBandwidthThrottleEpoch = timeCurrent;
  327. peer -> incomingDataTotal = 0;
  328. peer -> outgoingDataTotal = 0;
  329. needsAdjustment = 1;
  330. -- peersRemaining;
  331. bandwidth -= peerBandwidth;
  332. dataTotal -= peerBandwidth;
  333. }
  334. }
  335. if (peersRemaining > 0)
  336. {
  337. if (dataTotal <= bandwidth)
  338. throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
  339. else
  340. throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
  341. for (peer = host -> peers;
  342. peer < & host -> peers [host -> peerCount];
  343. ++ peer)
  344. {
  345. if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) ||
  346. peer -> outgoingBandwidthThrottleEpoch == timeCurrent)
  347. continue;
  348. peer -> packetThrottleLimit = throttle;
  349. if (peer -> packetThrottle > peer -> packetThrottleLimit)
  350. peer -> packetThrottle = peer -> packetThrottleLimit;
  351. peer -> incomingDataTotal = 0;
  352. peer -> outgoingDataTotal = 0;
  353. }
  354. }
  355. if (host -> recalculateBandwidthLimits)
  356. {
  357. host -> recalculateBandwidthLimits = 0;
  358. peersRemaining = (enet_uint32) host -> connectedPeers;
  359. bandwidth = host -> incomingBandwidth;
  360. needsAdjustment = 1;
  361. if (bandwidth == 0)
  362. bandwidthLimit = 0;
  363. else
  364. while (peersRemaining > 0 && needsAdjustment != 0)
  365. {
  366. needsAdjustment = 0;
  367. bandwidthLimit = bandwidth / peersRemaining;
  368. for (peer = host -> peers;
  369. peer < & host -> peers [host -> peerCount];
  370. ++ peer)
  371. {
  372. if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) ||
  373. peer -> incomingBandwidthThrottleEpoch == timeCurrent)
  374. continue;
  375. if (peer -> outgoingBandwidth > 0 &&
  376. peer -> outgoingBandwidth >= bandwidthLimit)
  377. continue;
  378. peer -> incomingBandwidthThrottleEpoch = timeCurrent;
  379. needsAdjustment = 1;
  380. -- peersRemaining;
  381. bandwidth -= peer -> outgoingBandwidth;
  382. }
  383. }
  384. for (peer = host -> peers;
  385. peer < & host -> peers [host -> peerCount];
  386. ++ peer)
  387. {
  388. if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
  389. continue;
  390. command.header.command = ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  391. command.header.channelID = 0xFF;
  392. command.bandwidthLimit.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  393. if (peer -> incomingBandwidthThrottleEpoch == timeCurrent)
  394. command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (peer -> outgoingBandwidth);
  395. else
  396. command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (bandwidthLimit);
  397. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  398. }
  399. }
  400. }
  401. /** @} */