host.c 17 KB

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