host.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 *enet_host_create(const ENetAddress * address, size_t peerCount, size_t channelLimit,
  26. 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
  45. || (address != NULL && enet_socket_bind(host->socket, address) < 0))
  46. {
  47. if (host->socket != ENET_SOCKET_NULL)
  48. enet_socket_destroy(host->socket);
  49. enet_free(host->peers);
  50. enet_free(host);
  51. return NULL;
  52. }
  53. enet_socket_set_option(host->socket, ENET_SOCKOPT_NONBLOCK, 1);
  54. enet_socket_set_option(host->socket, ENET_SOCKOPT_BROADCAST, 1);
  55. enet_socket_set_option(host->socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
  56. enet_socket_set_option(host->socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
  57. if (address != NULL)
  58. host->address = *address;
  59. if (!channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  60. channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  61. else 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; currentPeer < &host->peers[host->peerCount]; ++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. void enet_enable_crc(ENetHost* host)
  105. {
  106. host->checksum = enet_crc32;
  107. }
  108. /** Destroys the host and all resources associated with it.
  109. @param host pointer to the host to destroy
  110. */
  111. void enet_host_destroy(ENetHost * host)
  112. {
  113. ENetPeer * currentPeer;
  114. enet_socket_destroy(host->socket);
  115. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++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 *enet_host_connect(ENetHost * host, const ENetAddress * address, size_t channelCount,
  134. 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 if (channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  142. channelCount = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  143. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer)
  144. {
  145. if (currentPeer->state == ENET_PEER_STATE_DISCONNECTED)
  146. break;
  147. }
  148. if (currentPeer >= &host->peers[host->peerCount])
  149. return NULL;
  150. currentPeer->channels = (ENetChannel *) enet_malloc(channelCount * sizeof(ENetChannel));
  151. if (currentPeer->channels == NULL)
  152. return NULL;
  153. currentPeer->channelCount = channelCount;
  154. currentPeer->state = ENET_PEER_STATE_CONNECTING;
  155. currentPeer->address = *address;
  156. currentPeer->connectID = ++host->randomSeed;
  157. if (host->outgoingBandwidth == 0)
  158. currentPeer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  159. else
  160. currentPeer->windowSize = (host->outgoingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE)
  161. * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  162. if (currentPeer->windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  163. currentPeer->windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  164. else if (currentPeer->windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  165. currentPeer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  166. for (channel = currentPeer->channels; channel < &currentPeer->channels[channelCount]; ++channel)
  167. {
  168. channel->outgoingReliableSequenceNumber = 0;
  169. channel->outgoingUnreliableSequenceNumber = 0;
  170. channel->incomingReliableSequenceNumber = 0;
  171. channel->incomingUnreliableSequenceNumber = 0;
  172. enet_list_clear(&channel->incomingReliableCommands);
  173. enet_list_clear(&channel->incomingUnreliableCommands);
  174. channel->usedReliableWindows = 0;
  175. memset(channel->reliableWindows, 0, sizeof(channel->reliableWindows));
  176. }
  177. command.header.command = ENET_PROTOCOL_COMMAND_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  178. command.header.channelID = 0xFF;
  179. command.connect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
  180. command.connect.incomingSessionID = currentPeer->incomingSessionID;
  181. command.connect.outgoingSessionID = currentPeer->outgoingSessionID;
  182. command.connect.mtu = ENET_HOST_TO_NET_32 (currentPeer -> mtu);
  183. command.connect.windowSize = ENET_HOST_TO_NET_32 (currentPeer -> windowSize);
  184. command.connect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
  185. command.connect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
  186. command.connect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  187. command.connect.packetThrottleInterval =
  188. ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
  189. command.connect.packetThrottleAcceleration =
  190. ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
  191. command.connect.packetThrottleDeceleration =
  192. ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
  193. command.connect.connectID = currentPeer->connectID;
  194. command.connect.data = ENET_HOST_TO_NET_32 (data);
  195. enet_peer_queue_outgoing_command(currentPeer, &command, NULL, 0, 0);
  196. return currentPeer;
  197. }
  198. /** Queues a packet to be sent to all peers associated with the host.
  199. @param host host on which to broadcast the packet
  200. @param channelID channel on which to broadcast
  201. @param packet packet to broadcast
  202. */
  203. void enet_host_broadcast(ENetHost * host, enet_uint8 channelID, ENetPacket * packet)
  204. {
  205. ENetPeer * currentPeer;
  206. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer)
  207. {
  208. if (currentPeer->state != ENET_PEER_STATE_CONNECTED)
  209. continue;
  210. enet_peer_send(currentPeer, channelID, packet);
  211. }
  212. if (packet->referenceCount == 0)
  213. enet_packet_destroy(packet);
  214. }
  215. /** Sets the packet compressor the host should use to compress and decompress packets.
  216. @param host host to enable or disable compression for
  217. @param compressor callbacks for for the packet compressor; if NULL, then compression is disabled
  218. */
  219. void enet_host_compress(ENetHost * host, const ENetCompressor * compressor)
  220. {
  221. if (host->compressor.context != NULL && host->compressor.destroy)
  222. (*host->compressor.destroy)(host->compressor.context);
  223. if (compressor)
  224. host->compressor = *compressor;
  225. else
  226. host->compressor.context = NULL;
  227. }
  228. /** Limits the maximum allowed channels of future incoming connections.
  229. @param host host to limit
  230. @param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
  231. */
  232. void enet_host_channel_limit(ENetHost * host, size_t channelLimit)
  233. {
  234. if (!channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  235. channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  236. else if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
  237. channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
  238. host->channelLimit = channelLimit;
  239. }
  240. /** Adjusts the bandwidth limits of a host.
  241. @param host host to adjust
  242. @param incomingBandwidth new incoming bandwidth
  243. @param outgoingBandwidth new outgoing bandwidth
  244. @remarks the incoming and outgoing bandwidth parameters are identical in function to those
  245. specified in enet_host_create().
  246. */
  247. void enet_host_bandwidth_limit(ENetHost * host, enet_uint32 incomingBandwidth,
  248. enet_uint32 outgoingBandwidth)
  249. {
  250. host->incomingBandwidth = incomingBandwidth;
  251. host->outgoingBandwidth = outgoingBandwidth;
  252. host->recalculateBandwidthLimits = 1;
  253. }
  254. void enet_host_bandwidth_throttle(ENetHost * host)
  255. {
  256. enet_uint32 timeCurrent = enet_time_get(), elapsedTime = timeCurrent
  257. - host->bandwidthThrottleEpoch, peersTotal = 0, dataTotal = 0, peersRemaining,
  258. bandwidth, throttle = 0, bandwidthLimit = 0;
  259. int needsAdjustment;
  260. ENetPeer * peer;
  261. ENetProtocol command;
  262. if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  263. return;
  264. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer)
  265. {
  266. if (peer->state != ENET_PEER_STATE_CONNECTED
  267. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)
  268. continue;
  269. ++peersTotal;
  270. dataTotal += peer->outgoingDataTotal;
  271. }
  272. if (peersTotal == 0)
  273. return;
  274. peersRemaining = peersTotal;
  275. needsAdjustment = 1;
  276. if (host->outgoingBandwidth == 0)
  277. bandwidth = ~0;
  278. else
  279. bandwidth = (host->outgoingBandwidth * elapsedTime) / 1000;
  280. while (peersRemaining > 0 && needsAdjustment != 0)
  281. {
  282. needsAdjustment = 0;
  283. if (dataTotal < bandwidth)
  284. throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
  285. else
  286. throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
  287. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer)
  288. {
  289. enet_uint32 peerBandwidth;
  290. if ((peer->state != ENET_PEER_STATE_CONNECTED
  291. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)
  292. || peer->incomingBandwidth == 0
  293. || peer->outgoingBandwidthThrottleEpoch == timeCurrent)
  294. continue;
  295. peerBandwidth = (peer->incomingBandwidth * elapsedTime) / 1000;
  296. if ((throttle * peer->outgoingDataTotal) / ENET_PEER_PACKET_THROTTLE_SCALE
  297. <= peerBandwidth)
  298. continue;
  299. peer->packetThrottleLimit = (peerBandwidth * ENET_PEER_PACKET_THROTTLE_SCALE)
  300. / peer->outgoingDataTotal;
  301. if (peer->packetThrottleLimit == 0)
  302. peer->packetThrottleLimit = 1;
  303. if (peer->packetThrottle > peer->packetThrottleLimit)
  304. peer->packetThrottle = peer->packetThrottleLimit;
  305. peer->outgoingBandwidthThrottleEpoch = timeCurrent;
  306. needsAdjustment = 1;
  307. --peersRemaining;
  308. bandwidth -= peerBandwidth;
  309. dataTotal -= peerBandwidth;
  310. }
  311. }
  312. if (peersRemaining > 0)
  313. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer)
  314. {
  315. if ((peer->state != ENET_PEER_STATE_CONNECTED
  316. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)
  317. || peer->outgoingBandwidthThrottleEpoch == timeCurrent)
  318. continue;
  319. peer->packetThrottleLimit = throttle;
  320. if (peer->packetThrottle > peer->packetThrottleLimit)
  321. peer->packetThrottle = peer->packetThrottleLimit;
  322. }
  323. if (host->recalculateBandwidthLimits)
  324. {
  325. host->recalculateBandwidthLimits = 0;
  326. peersRemaining = peersTotal;
  327. bandwidth = host->incomingBandwidth;
  328. needsAdjustment = 1;
  329. if (bandwidth == 0)
  330. bandwidthLimit = 0;
  331. else
  332. while (peersRemaining > 0 && needsAdjustment != 0)
  333. {
  334. needsAdjustment = 0;
  335. bandwidthLimit = bandwidth / peersRemaining;
  336. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer)
  337. {
  338. if ((peer->state != ENET_PEER_STATE_CONNECTED
  339. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)
  340. || peer->incomingBandwidthThrottleEpoch == timeCurrent)
  341. continue;
  342. if (peer->outgoingBandwidth > 0 && peer->outgoingBandwidth >= bandwidthLimit)
  343. continue;
  344. peer->incomingBandwidthThrottleEpoch = timeCurrent;
  345. needsAdjustment = 1;
  346. --peersRemaining;
  347. bandwidth -= peer->outgoingBandwidth;
  348. }
  349. }
  350. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer)
  351. {
  352. if (peer->state != ENET_PEER_STATE_CONNECTED
  353. && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)
  354. continue;
  355. command.header.command = ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT
  356. | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  357. command.header.channelID = 0xFF;
  358. command.bandwidthLimit.outgoingBandwidth =
  359. ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  360. if (peer->incomingBandwidthThrottleEpoch == timeCurrent)
  361. command.bandwidthLimit.incomingBandwidth =
  362. ENET_HOST_TO_NET_32 (peer -> outgoingBandwidth);
  363. else
  364. command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (bandwidthLimit);
  365. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  366. }
  367. }
  368. host->bandwidthThrottleEpoch = timeCurrent;
  369. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer)
  370. {
  371. peer->incomingDataTotal = 0;
  372. peer->outgoingDataTotal = 0;
  373. }
  374. }
  375. /** @} */