host.c 17 KB

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