host.c 18 KB

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