host.c 18 KB

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