host.c 18 KB

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