host.c 17 KB

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