peer.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /**
  2. @file peer.c
  3. @brief ENet peer management functions
  4. */
  5. #include <string.h>
  6. #define ENET_BUILDING_LIB 1
  7. #include "enet/enet.h"
  8. /** @defgroup peer ENet peer functions
  9. @{
  10. */
  11. /** Configures throttle parameter for a peer.
  12. Unreliable packets are dropped by ENet in response to the varying conditions
  13. of the Internet connection to the peer. The throttle represents a probability
  14. that an unreliable packet should not be dropped and thus sent by ENet to the peer.
  15. The lowest mean round trip time from the sending of a reliable packet to the
  16. receipt of its acknowledgement is measured over an amount of time specified by
  17. the interval parameter in milliseconds. If a measured round trip time happens to
  18. be significantly less than the mean round trip time measured over the interval,
  19. then the throttle probability is increased to allow more traffic by an amount
  20. specified in the acceleration parameter, which is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE
  21. constant. If a measured round trip time happens to be significantly greater than
  22. the mean round trip time measured over the interval, then the throttle probability
  23. is decreased to limit traffic by an amount specified in the deceleration parameter, which
  24. is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE constant. When the throttle has
  25. a value of ENET_PEER_PACKET_THROTTLE_SCALE, on unreliable packets are dropped by
  26. ENet, and so 100% of all unreliable packets will be sent. When the throttle has a
  27. value of 0, all unreliable packets are dropped by ENet, and so 0% of all unreliable
  28. packets will be sent. Intermediate values for the throttle represent intermediate
  29. probabilities between 0% and 100% of unreliable packets being sent. The bandwidth
  30. limits of the local and foreign hosts are taken into account to determine a
  31. sensible limit for the throttle probability above which it should not raise even in
  32. the best of conditions.
  33. @param peer peer to configure
  34. @param interval interval, in milliseconds, over which to measure lowest mean RTT; the default value is ENET_PEER_PACKET_THROTTLE_INTERVAL.
  35. @param acceleration rate at which to increase the throttle probability as mean RTT declines
  36. @param deceleration rate at which to decrease the throttle probability as mean RTT increases
  37. */
  38. void enet_peer_throttle_configure(ENetPeer * peer, enet_uint32 interval, enet_uint32 acceleration,
  39. enet_uint32 deceleration)
  40. {
  41. ENetProtocol command;
  42. peer->packetThrottleInterval = interval;
  43. peer->packetThrottleAcceleration = acceleration;
  44. peer->packetThrottleDeceleration = deceleration;
  45. command.header.command = ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE
  46. | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  47. command.header.channelID = 0xFF;
  48. command.throttleConfigure.packetThrottleInterval = ENET_HOST_TO_NET_32 (interval);
  49. command.throttleConfigure.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (acceleration);
  50. command.throttleConfigure.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (deceleration);
  51. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  52. }
  53. int enet_peer_throttle(ENetPeer * peer, enet_uint32 rtt)
  54. {
  55. if (peer->lastRoundTripTime <= peer->lastRoundTripTimeVariance)
  56. {
  57. peer->packetThrottle = peer->packetThrottleLimit;
  58. }
  59. else if (rtt < peer->lastRoundTripTime)
  60. {
  61. peer->packetThrottle += peer->packetThrottleAcceleration;
  62. if (peer->packetThrottle > peer->packetThrottleLimit)
  63. peer->packetThrottle = peer->packetThrottleLimit;
  64. return 1;
  65. }
  66. else if (rtt > peer->lastRoundTripTime + 2 * peer->lastRoundTripTimeVariance)
  67. {
  68. if (peer->packetThrottle > peer->packetThrottleDeceleration)
  69. peer->packetThrottle -= peer->packetThrottleDeceleration;
  70. else
  71. peer->packetThrottle = 0;
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. /** Queues a packet to be sent.
  77. @param peer destination for the packet
  78. @param channelID channel on which to send
  79. @param packet packet to send
  80. @retval 0 on success
  81. @retval < 0 on failure
  82. */
  83. int enet_peer_send(ENetPeer * peer, enet_uint8 channelID, ENetPacket * packet)
  84. {
  85. ENetChannel * channel = &peer->channels[channelID];
  86. ENetProtocol command;
  87. size_t fragmentLength;
  88. if (peer->state != ENET_PEER_STATE_CONNECTED || channelID >= peer->channelCount
  89. || packet->dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE)
  90. return -1;
  91. fragmentLength = peer->mtu - sizeof(ENetProtocolHeader) - sizeof(ENetProtocolSendFragment);
  92. if (peer->host->checksum != NULL)
  93. fragmentLength -= sizeof(enet_uint32);
  94. if (packet->dataLength > fragmentLength)
  95. {
  96. enet_uint32 fragmentCount = (packet->dataLength + fragmentLength - 1) / fragmentLength,
  97. fragmentNumber, fragmentOffset;
  98. enet_uint8 commandNumber;
  99. enet_uint16 startSequenceNumber;
  100. ENetList fragments;
  101. ENetOutgoingCommand * fragment;
  102. if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT)
  103. return -1;
  104. if ((packet->flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT))
  105. == ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT
  106. && channel->outgoingUnreliableSequenceNumber < 0xFFFF)
  107. {
  108. commandNumber = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT;
  109. startSequenceNumber =
  110. ENET_HOST_TO_NET_16 (channel -> outgoingUnreliableSequenceNumber + 1);
  111. }
  112. else
  113. {
  114. commandNumber = ENET_PROTOCOL_COMMAND_SEND_FRAGMENT
  115. | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  116. startSequenceNumber =
  117. ENET_HOST_TO_NET_16 (channel -> outgoingReliableSequenceNumber + 1);
  118. }
  119. enet_list_clear(&fragments);
  120. for (fragmentNumber = 0, fragmentOffset = 0; fragmentOffset < packet->dataLength;
  121. ++fragmentNumber, fragmentOffset += fragmentLength)
  122. {
  123. if (packet->dataLength - fragmentOffset < fragmentLength)
  124. fragmentLength = packet->dataLength - fragmentOffset;
  125. fragment = (ENetOutgoingCommand *) enet_malloc(sizeof(ENetOutgoingCommand));
  126. if (fragment == NULL)
  127. {
  128. while (!enet_list_empty (& fragments))
  129. {
  130. fragment = (ENetOutgoingCommand *) enet_list_remove(
  131. enet_list_begin (& fragments));
  132. enet_free(fragment);
  133. }
  134. return -1;
  135. }
  136. fragment->fragmentOffset = fragmentOffset;
  137. fragment->fragmentLength = fragmentLength;
  138. fragment->packet = packet;
  139. fragment->command.header.command = commandNumber;
  140. fragment->command.header.channelID = channelID;
  141. fragment->command.sendFragment.startSequenceNumber = startSequenceNumber;
  142. fragment->command.sendFragment.dataLength = ENET_HOST_TO_NET_16 (fragmentLength);
  143. fragment->command.sendFragment.fragmentCount = ENET_HOST_TO_NET_32 (fragmentCount);
  144. fragment->command.sendFragment.fragmentNumber = ENET_HOST_TO_NET_32 (fragmentNumber);
  145. fragment->command.sendFragment.totalLength = ENET_HOST_TO_NET_32 (packet -> dataLength);
  146. fragment->command.sendFragment.fragmentOffset = ENET_NET_TO_HOST_32 (fragmentOffset);
  147. enet_list_insert(enet_list_end (& fragments), fragment);
  148. }
  149. packet->referenceCount += fragmentNumber;
  150. while (!enet_list_empty (& fragments))
  151. {
  152. fragment = (ENetOutgoingCommand *) enet_list_remove(enet_list_begin (& fragments));
  153. enet_peer_setup_outgoing_command(peer, fragment);
  154. }
  155. return 0;
  156. }
  157. command.header.channelID = channelID;
  158. if ((packet->flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNSEQUENCED))
  159. == ENET_PACKET_FLAG_UNSEQUENCED)
  160. {
  161. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED
  162. | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  163. command.sendUnsequenced.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  164. }
  165. else if (packet->flags & ENET_PACKET_FLAG_RELIABLE
  166. || channel->outgoingUnreliableSequenceNumber >= 0xFFFF)
  167. {
  168. command.header.command = ENET_PROTOCOL_COMMAND_SEND_RELIABLE
  169. | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  170. command.sendReliable.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  171. }
  172. else
  173. {
  174. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE;
  175. command.sendUnreliable.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  176. }
  177. if (enet_peer_queue_outgoing_command(peer, &command, packet, 0, packet->dataLength) == NULL)
  178. return -1;
  179. return 0;
  180. }
  181. /** Attempts to dequeue any incoming queued packet.
  182. @param peer peer to dequeue packets from
  183. @param channelID holds the channel ID of the channel the packet was received on success
  184. @returns a pointer to the packet, or NULL if there are no available incoming queued packets
  185. */
  186. ENetPacket *enet_peer_receive(ENetPeer * peer, enet_uint8 * channelID)
  187. {
  188. ENetIncomingCommand * incomingCommand;
  189. ENetPacket * packet;
  190. if (enet_list_empty (& peer -> dispatchedCommands))
  191. return NULL;
  192. incomingCommand = (ENetIncomingCommand *) enet_list_remove(
  193. enet_list_begin (& peer -> dispatchedCommands));
  194. if (channelID != NULL)
  195. *channelID = incomingCommand->command.header.channelID;
  196. packet = incomingCommand->packet;
  197. --packet->referenceCount;
  198. if (incomingCommand->fragments != NULL)
  199. enet_free(incomingCommand->fragments);
  200. enet_free(incomingCommand);
  201. return packet;
  202. }
  203. static void enet_peer_reset_outgoing_commands(ENetList * queue)
  204. {
  205. ENetOutgoingCommand * outgoingCommand;
  206. while (!enet_list_empty (queue))
  207. {
  208. outgoingCommand = (ENetOutgoingCommand *) enet_list_remove(enet_list_begin (queue));
  209. if (outgoingCommand->packet != NULL)
  210. {
  211. --outgoingCommand->packet->referenceCount;
  212. if (outgoingCommand->packet->referenceCount == 0)
  213. enet_packet_destroy(outgoingCommand->packet);
  214. }
  215. enet_free(outgoingCommand);
  216. }
  217. }
  218. static void enet_peer_remove_incoming_commands(ENetList * queue, ENetListIterator startCommand,
  219. ENetListIterator endCommand)
  220. {
  221. ENetListIterator currentCommand;
  222. for (currentCommand = startCommand; currentCommand != endCommand;)
  223. {
  224. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  225. currentCommand = enet_list_next (currentCommand);
  226. enet_list_remove(&incomingCommand->incomingCommandList);
  227. if (incomingCommand->packet != NULL)
  228. {
  229. --incomingCommand->packet->referenceCount;
  230. if (incomingCommand->packet->referenceCount == 0)
  231. enet_packet_destroy(incomingCommand->packet);
  232. }
  233. if (incomingCommand->fragments != NULL)
  234. enet_free(incomingCommand->fragments);
  235. enet_free(incomingCommand);
  236. }
  237. }
  238. static void enet_peer_reset_incoming_commands(ENetList * queue)
  239. {
  240. enet_peer_remove_incoming_commands(queue, enet_list_begin (queue), enet_list_end (queue));
  241. }
  242. void enet_peer_reset_queues(ENetPeer * peer)
  243. {
  244. ENetChannel * channel;
  245. if (peer->needsDispatch)
  246. {
  247. enet_list_remove(&peer->dispatchList);
  248. peer->needsDispatch = 0;
  249. }
  250. while (!enet_list_empty (& peer -> acknowledgements))
  251. enet_free(enet_list_remove(enet_list_begin (& peer -> acknowledgements)));
  252. enet_peer_reset_outgoing_commands(&peer->sentReliableCommands);
  253. enet_peer_reset_outgoing_commands(&peer->sentUnreliableCommands);
  254. enet_peer_reset_outgoing_commands(&peer->outgoingReliableCommands);
  255. enet_peer_reset_outgoing_commands(&peer->outgoingUnreliableCommands);
  256. enet_peer_reset_incoming_commands(&peer->dispatchedCommands);
  257. if (peer->channels != NULL && peer->channelCount > 0)
  258. {
  259. for (channel = peer->channels; channel < &peer->channels[peer->channelCount]; ++channel)
  260. {
  261. enet_peer_reset_incoming_commands(&channel->incomingReliableCommands);
  262. enet_peer_reset_incoming_commands(&channel->incomingUnreliableCommands);
  263. }
  264. enet_free(peer->channels);
  265. }
  266. peer->channels = NULL;
  267. peer->channelCount = 0;
  268. }
  269. /** Forcefully disconnects a peer.
  270. @param peer peer to forcefully disconnect
  271. @remarks The foreign host represented by the peer is not notified of the disconnection and will timeout
  272. on its connection to the local host.
  273. */
  274. void enet_peer_reset(ENetPeer * peer)
  275. {
  276. peer->outgoingPeerID = ENET_PROTOCOL_MAXIMUM_PEER_ID;
  277. peer->connectID = 0;
  278. peer->state = ENET_PEER_STATE_DISCONNECTED;
  279. peer->incomingBandwidth = 0;
  280. peer->outgoingBandwidth = 0;
  281. peer->incomingBandwidthThrottleEpoch = 0;
  282. peer->outgoingBandwidthThrottleEpoch = 0;
  283. peer->incomingDataTotal = 0;
  284. peer->outgoingDataTotal = 0;
  285. peer->lastSendTime = 0;
  286. peer->lastReceiveTime = 0;
  287. peer->nextTimeout = 0;
  288. peer->earliestTimeout = 0;
  289. peer->packetLossEpoch = 0;
  290. peer->packetsSent = 0;
  291. peer->packetsLost = 0;
  292. peer->packetLoss = 0;
  293. peer->packetLossVariance = 0;
  294. peer->packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE;
  295. peer->packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE;
  296. peer->packetThrottleCounter = 0;
  297. peer->packetThrottleEpoch = 0;
  298. peer->packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION;
  299. peer->packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION;
  300. peer->packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL;
  301. peer->pingInterval = ENET_PEER_PING_INTERVAL;
  302. peer->timeoutLimit = ENET_PEER_TIMEOUT_LIMIT;
  303. peer->timeoutMinimum = ENET_PEER_TIMEOUT_MINIMUM;
  304. peer->timeoutMaximum = ENET_PEER_TIMEOUT_MAXIMUM;
  305. peer->lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  306. peer->lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  307. peer->lastRoundTripTimeVariance = 0;
  308. peer->highestRoundTripTimeVariance = 0;
  309. peer->roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  310. peer->roundTripTimeVariance = 0;
  311. peer->mtu = peer->host->mtu;
  312. peer->reliableDataInTransit = 0;
  313. peer->outgoingReliableSequenceNumber = 0;
  314. peer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  315. peer->incomingUnsequencedGroup = 0;
  316. peer->outgoingUnsequencedGroup = 0;
  317. peer->eventData = 0;
  318. memset(peer->unsequencedWindow, 0, sizeof(peer->unsequencedWindow));
  319. enet_peer_reset_queues(peer);
  320. }
  321. /** Sends a ping request to a peer.
  322. @param peer destination for the ping request
  323. @remarks ping requests factor into the mean round trip time as designated by the
  324. roundTripTime field in the ENetPeer structure. Enet automatically pings all connected
  325. peers at regular intervals, however, this function may be called to ensure more
  326. frequent ping requests.
  327. */
  328. void enet_peer_ping(ENetPeer * peer)
  329. {
  330. ENetProtocol command;
  331. if (peer->state != ENET_PEER_STATE_CONNECTED)
  332. return;
  333. command.header.command = ENET_PROTOCOL_COMMAND_PING | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  334. command.header.channelID = 0xFF;
  335. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  336. }
  337. /** Sets the interval at which pings will be sent to a peer.
  338. Pings are used both to monitor the liveness of the connection and also to dynamically
  339. adjust the throttle during periods of low traffic so that the throttle has reasonable
  340. responsiveness during traffic spikes.
  341. @param peer the peer to adjust
  342. @param pingInterval the interval at which to send pings; defaults to ENET_PEER_PING_INTERVAL if 0
  343. */
  344. void enet_peer_ping_interval(ENetPeer * peer, enet_uint32 pingInterval)
  345. {
  346. peer->pingInterval = pingInterval ? pingInterval : ENET_PEER_PING_INTERVAL;
  347. }
  348. /** Sets the timeout parameters for a peer.
  349. The timeout parameter control how and when a peer will timeout from a failure to acknowledge
  350. reliable traffic. Timeout values use an exponential backoff mechanism, where if a reliable
  351. packet is not acknowledge within some multiple of the average RTT plus a variance tolerance,
  352. the timeout will be doubled until it reaches a set limit. If the timeout is thus at this
  353. limit and reliable packets have been sent but not acknowledged within a certain minimum time
  354. period, the peer will be disconnected. Alternatively, if reliable packets have been sent
  355. but not acknowledged for a certain maximum time period, the peer will be disconnected regardless
  356. of the current timeout limit value.
  357. @param peer the peer to adjust
  358. @param timeoutLimit the timeout limit; defaults to ENET_PEER_TIMEOUT_LIMIT if 0
  359. @param timeoutMinimum the timeout minimum; defaults to ENET_PEER_TIMEOUT_MINIMUM if 0
  360. @param timeoutMaximum the timeout maximum; defaults to ENET_PEER_TIMEOUT_MAXIMUM if 0
  361. */
  362. void enet_peer_timeout(ENetPeer * peer, enet_uint32 timeoutLimit, enet_uint32 timeoutMinimum,
  363. enet_uint32 timeoutMaximum)
  364. {
  365. peer->timeoutLimit = timeoutLimit ? timeoutLimit : ENET_PEER_TIMEOUT_LIMIT;
  366. peer->timeoutMinimum = timeoutMinimum ? timeoutMinimum : ENET_PEER_TIMEOUT_MINIMUM;
  367. peer->timeoutMaximum = timeoutMaximum ? timeoutMaximum : ENET_PEER_TIMEOUT_MAXIMUM;
  368. }
  369. /** Force an immediate disconnection from a peer.
  370. @param peer peer to disconnect
  371. @param data data describing the disconnection
  372. @remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not
  373. guarenteed to receive the disconnect notification, and is reset immediately upon
  374. return from this function.
  375. */
  376. void enet_peer_disconnect_now(ENetPeer * peer, enet_uint32 data)
  377. {
  378. ENetProtocol command;
  379. if (peer->state == ENET_PEER_STATE_DISCONNECTED)
  380. return;
  381. if (peer->state != ENET_PEER_STATE_ZOMBIE && peer->state != ENET_PEER_STATE_DISCONNECTING)
  382. {
  383. enet_peer_reset_queues(peer);
  384. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT
  385. | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  386. command.header.channelID = 0xFF;
  387. command.disconnect.data = ENET_HOST_TO_NET_32 (data);
  388. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  389. enet_host_flush(peer->host);
  390. }
  391. enet_peer_reset(peer);
  392. }
  393. /** Request a disconnection from a peer.
  394. @param peer peer to request a disconnection
  395. @param data data describing the disconnection
  396. @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  397. once the disconnection is complete.
  398. */
  399. void enet_peer_disconnect(ENetPeer * peer, enet_uint32 data)
  400. {
  401. ENetProtocol command;
  402. if (peer->state == ENET_PEER_STATE_DISCONNECTING || peer->state == ENET_PEER_STATE_DISCONNECTED
  403. || peer->state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT
  404. || peer->state == ENET_PEER_STATE_ZOMBIE)
  405. return;
  406. enet_peer_reset_queues(peer);
  407. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
  408. command.header.channelID = 0xFF;
  409. command.disconnect.data = ENET_HOST_TO_NET_32 (data);
  410. if (peer->state == ENET_PEER_STATE_CONNECTED || peer->state == ENET_PEER_STATE_DISCONNECT_LATER)
  411. command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  412. else
  413. command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  414. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  415. if (peer->state == ENET_PEER_STATE_CONNECTED || peer->state == ENET_PEER_STATE_DISCONNECT_LATER)
  416. peer->state = ENET_PEER_STATE_DISCONNECTING;
  417. else
  418. {
  419. enet_host_flush(peer->host);
  420. enet_peer_reset(peer);
  421. }
  422. }
  423. /** Request a disconnection from a peer, but only after all queued outgoing packets are sent.
  424. @param peer peer to request a disconnection
  425. @param data data describing the disconnection
  426. @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  427. once the disconnection is complete.
  428. */
  429. void enet_peer_disconnect_later(ENetPeer * peer, enet_uint32 data)
  430. {
  431. if ((peer->state == ENET_PEER_STATE_CONNECTED || peer->state == ENET_PEER_STATE_DISCONNECT_LATER)
  432. && !(enet_list_empty (& peer -> outgoingReliableCommands)
  433. && enet_list_empty (& peer -> outgoingUnreliableCommands)
  434. && enet_list_empty (& peer -> sentReliableCommands)))
  435. {
  436. peer->state = ENET_PEER_STATE_DISCONNECT_LATER;
  437. peer->eventData = data;
  438. }
  439. else
  440. enet_peer_disconnect(peer, data);
  441. }
  442. ENetAcknowledgement *enet_peer_queue_acknowledgement(ENetPeer * peer, const ENetProtocol * command, enet_uint16 sentTime)
  443. {
  444. ENetAcknowledgement * acknowledgement;
  445. if (command->header.channelID < peer->channelCount)
  446. {
  447. ENetChannel * channel = &peer->channels[command->header.channelID];
  448. enet_uint16 reliableWindow = command->header.reliableSequenceNumber
  449. / ENET_PEER_RELIABLE_WINDOW_SIZE, currentWindow =
  450. channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  451. if (command->header.reliableSequenceNumber < channel->incomingReliableSequenceNumber)
  452. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  453. if (reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1
  454. && reliableWindow <= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS)
  455. return NULL;
  456. }
  457. acknowledgement = (ENetAcknowledgement *) enet_malloc(sizeof(ENetAcknowledgement));
  458. if (acknowledgement == NULL)
  459. return NULL;
  460. peer->outgoingDataTotal += sizeof(ENetProtocolAcknowledge);
  461. acknowledgement->sentTime = sentTime;
  462. acknowledgement->command = *command;
  463. enet_list_insert(enet_list_end (& peer -> acknowledgements), acknowledgement);
  464. return acknowledgement;
  465. }
  466. void enet_peer_setup_outgoing_command(ENetPeer * peer, ENetOutgoingCommand * outgoingCommand)
  467. {
  468. ENetChannel * channel = &peer->channels[outgoingCommand->command.header.channelID];
  469. peer->outgoingDataTotal += enet_protocol_command_size(outgoingCommand->command.header.command)
  470. + outgoingCommand->fragmentLength;
  471. if (outgoingCommand->command.header.channelID == 0xFF)
  472. {
  473. ++peer->outgoingReliableSequenceNumber;
  474. outgoingCommand->reliableSequenceNumber = peer->outgoingReliableSequenceNumber;
  475. outgoingCommand->unreliableSequenceNumber = 0;
  476. }
  477. else if (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  478. {
  479. ++channel->outgoingReliableSequenceNumber;
  480. channel->outgoingUnreliableSequenceNumber = 0;
  481. outgoingCommand->reliableSequenceNumber = channel->outgoingReliableSequenceNumber;
  482. outgoingCommand->unreliableSequenceNumber = 0;
  483. }
  484. else if (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED)
  485. {
  486. ++peer->outgoingUnsequencedGroup;
  487. outgoingCommand->reliableSequenceNumber = 0;
  488. outgoingCommand->unreliableSequenceNumber = 0;
  489. }
  490. else
  491. {
  492. if (outgoingCommand->fragmentOffset == 0)
  493. ++channel->outgoingUnreliableSequenceNumber;
  494. outgoingCommand->reliableSequenceNumber = channel->outgoingReliableSequenceNumber;
  495. outgoingCommand->unreliableSequenceNumber = channel->outgoingUnreliableSequenceNumber;
  496. }
  497. outgoingCommand->sendAttempts = 0;
  498. outgoingCommand->sentTime = 0;
  499. outgoingCommand->roundTripTimeout = 0;
  500. outgoingCommand->roundTripTimeoutLimit = 0;
  501. outgoingCommand->command.header.reliableSequenceNumber =
  502. ENET_HOST_TO_NET_16 (outgoingCommand -> reliableSequenceNumber);
  503. switch (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK)
  504. {
  505. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  506. outgoingCommand->command.sendUnreliable.unreliableSequenceNumber =
  507. ENET_HOST_TO_NET_16 (outgoingCommand -> unreliableSequenceNumber);
  508. break;
  509. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  510. outgoingCommand->command.sendUnsequenced.unsequencedGroup =
  511. ENET_HOST_TO_NET_16 (peer -> outgoingUnsequencedGroup);
  512. break;
  513. default:
  514. break;
  515. }
  516. if (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  517. enet_list_insert(enet_list_end (& peer -> outgoingReliableCommands), outgoingCommand);
  518. else
  519. enet_list_insert(enet_list_end (& peer -> outgoingUnreliableCommands), outgoingCommand);
  520. }
  521. ENetOutgoingCommand *enet_peer_queue_outgoing_command(ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet,
  522. enet_uint32 offset, enet_uint16 length)
  523. {
  524. ENetOutgoingCommand * outgoingCommand = (ENetOutgoingCommand *) enet_malloc(
  525. sizeof(ENetOutgoingCommand));
  526. if (outgoingCommand == NULL)
  527. return NULL;
  528. outgoingCommand->command = *command;
  529. outgoingCommand->fragmentOffset = offset;
  530. outgoingCommand->fragmentLength = length;
  531. outgoingCommand->packet = packet;
  532. if (packet != NULL)
  533. ++packet->referenceCount;
  534. enet_peer_setup_outgoing_command(peer, outgoingCommand);
  535. return outgoingCommand;
  536. }
  537. void enet_peer_dispatch_incoming_unreliable_commands(ENetPeer * peer, ENetChannel * channel)
  538. {
  539. ENetListIterator droppedCommand, startCommand, currentCommand;
  540. for (droppedCommand = startCommand = currentCommand =
  541. enet_list_begin (& channel -> incomingUnreliableCommands);
  542. currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
  543. currentCommand = enet_list_next (currentCommand))
  544. {
  545. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  546. if ((incomingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK)
  547. == ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
  548. continue;
  549. if (incomingCommand->reliableSequenceNumber == channel->incomingReliableSequenceNumber)
  550. {
  551. if (incomingCommand->fragmentsRemaining <= 0)
  552. {
  553. channel->incomingUnreliableSequenceNumber =
  554. incomingCommand->unreliableSequenceNumber;
  555. continue;
  556. }
  557. if (startCommand != currentCommand)
  558. {
  559. enet_list_move(enet_list_end (& peer -> dispatchedCommands), startCommand,
  560. enet_list_previous (currentCommand));
  561. if (!peer->needsDispatch)
  562. {
  563. enet_list_insert(enet_list_end (& peer -> host -> dispatchQueue),
  564. &peer->dispatchList);
  565. peer->needsDispatch = 1;
  566. }
  567. droppedCommand = currentCommand;
  568. }
  569. else if (droppedCommand != currentCommand)
  570. droppedCommand = enet_list_previous (currentCommand);
  571. }
  572. else
  573. {
  574. enet_uint16 reliableWindow = incomingCommand->reliableSequenceNumber
  575. / ENET_PEER_RELIABLE_WINDOW_SIZE, currentWindow =
  576. channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  577. if (incomingCommand->reliableSequenceNumber < channel->incomingReliableSequenceNumber)
  578. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  579. if (reliableWindow >= currentWindow
  580. && reliableWindow < currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
  581. break;
  582. droppedCommand = enet_list_next (currentCommand);
  583. if (startCommand != currentCommand)
  584. {
  585. enet_list_move(enet_list_end (& peer -> dispatchedCommands), startCommand,
  586. enet_list_previous (currentCommand));
  587. if (!peer->needsDispatch)
  588. {
  589. enet_list_insert(enet_list_end (& peer -> host -> dispatchQueue),
  590. &peer->dispatchList);
  591. peer->needsDispatch = 1;
  592. }
  593. }
  594. }
  595. startCommand = enet_list_next (currentCommand);
  596. }
  597. if (startCommand != currentCommand)
  598. {
  599. enet_list_move(enet_list_end (& peer -> dispatchedCommands), startCommand,
  600. enet_list_previous (currentCommand));
  601. if (!peer->needsDispatch)
  602. {
  603. enet_list_insert(enet_list_end (& peer -> host -> dispatchQueue), &peer->dispatchList);
  604. peer->needsDispatch = 1;
  605. }
  606. droppedCommand = currentCommand;
  607. }
  608. enet_peer_remove_incoming_commands(&channel->incomingUnreliableCommands,
  609. enet_list_begin (& channel -> incomingUnreliableCommands), droppedCommand);
  610. }
  611. void enet_peer_dispatch_incoming_reliable_commands(ENetPeer * peer, ENetChannel * channel)
  612. {
  613. enet_uint16 oldReliableSequenceNumber = channel->incomingReliableSequenceNumber;
  614. ENetListIterator currentCommand;
  615. for (currentCommand = enet_list_begin (& channel -> incomingReliableCommands);
  616. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  617. currentCommand = enet_list_next (currentCommand))
  618. {
  619. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  620. if (incomingCommand->fragmentsRemaining > 0
  621. || incomingCommand->reliableSequenceNumber
  622. != (enet_uint16) (channel->incomingReliableSequenceNumber + 1))
  623. break;
  624. channel->incomingReliableSequenceNumber = incomingCommand->reliableSequenceNumber;
  625. if (incomingCommand->fragmentCount > 0)
  626. channel->incomingReliableSequenceNumber += incomingCommand->fragmentCount - 1;
  627. }
  628. if (currentCommand == enet_list_begin (& channel -> incomingReliableCommands))
  629. return;
  630. channel->incomingUnreliableSequenceNumber = 0;
  631. enet_list_move(enet_list_end (& peer -> dispatchedCommands),
  632. enet_list_begin (& channel -> incomingReliableCommands),
  633. enet_list_previous (currentCommand));
  634. if (!peer->needsDispatch)
  635. {
  636. enet_list_insert(enet_list_end (& peer -> host -> dispatchQueue), &peer->dispatchList);
  637. peer->needsDispatch = 1;
  638. }
  639. if (!enet_list_empty (& channel -> incomingUnreliableCommands))
  640. enet_peer_dispatch_incoming_unreliable_commands(peer, channel);
  641. }
  642. ENetIncomingCommand *enet_peer_queue_incoming_command(ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet,
  643. enet_uint32 fragmentCount)
  644. {
  645. static ENetIncomingCommand dummyCommand;
  646. ENetChannel * channel = &peer->channels[command->header.channelID];
  647. enet_uint32 unreliableSequenceNumber = 0, reliableSequenceNumber = 0;
  648. enet_uint16 reliableWindow, currentWindow;
  649. ENetIncomingCommand * incomingCommand;
  650. ENetListIterator currentCommand;
  651. if (peer->state == ENET_PEER_STATE_DISCONNECT_LATER)
  652. goto freePacket;
  653. if ((command->header.command & ENET_PROTOCOL_COMMAND_MASK)
  654. != ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
  655. {
  656. reliableSequenceNumber = command->header.reliableSequenceNumber;
  657. reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  658. currentWindow = channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  659. if (reliableSequenceNumber < channel->incomingReliableSequenceNumber)
  660. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  661. if (reliableWindow < currentWindow
  662. || reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
  663. goto freePacket;
  664. }
  665. switch (command->header.command & ENET_PROTOCOL_COMMAND_MASK)
  666. {
  667. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  668. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  669. if (reliableSequenceNumber == channel->incomingReliableSequenceNumber)
  670. goto freePacket;
  671. for (currentCommand =
  672. enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  673. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  674. currentCommand = enet_list_previous (currentCommand))
  675. {
  676. incomingCommand = (ENetIncomingCommand *) currentCommand;
  677. if (reliableSequenceNumber >= channel->incomingReliableSequenceNumber)
  678. {
  679. if (incomingCommand->reliableSequenceNumber
  680. < channel->incomingReliableSequenceNumber)
  681. continue;
  682. }
  683. else if (incomingCommand->reliableSequenceNumber
  684. >= channel->incomingReliableSequenceNumber)
  685. break;
  686. if (incomingCommand->reliableSequenceNumber <= reliableSequenceNumber)
  687. {
  688. if (incomingCommand->reliableSequenceNumber < reliableSequenceNumber)
  689. break;
  690. goto freePacket;
  691. }
  692. }
  693. break;
  694. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  695. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
  696. unreliableSequenceNumber =
  697. ENET_NET_TO_HOST_16 (command -> sendUnreliable.unreliableSequenceNumber);
  698. if (reliableSequenceNumber == channel->incomingReliableSequenceNumber
  699. && unreliableSequenceNumber <= channel->incomingUnreliableSequenceNumber)
  700. goto freePacket;
  701. for (currentCommand =
  702. enet_list_previous (enet_list_end (& channel -> incomingUnreliableCommands));
  703. currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
  704. currentCommand = enet_list_previous (currentCommand))
  705. {
  706. incomingCommand = (ENetIncomingCommand *) currentCommand;
  707. if ((command->header.command & ENET_PROTOCOL_COMMAND_MASK)
  708. == ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
  709. continue;
  710. if (reliableSequenceNumber >= channel->incomingReliableSequenceNumber)
  711. {
  712. if (incomingCommand->reliableSequenceNumber
  713. < channel->incomingReliableSequenceNumber)
  714. continue;
  715. }
  716. else if (incomingCommand->reliableSequenceNumber
  717. >= channel->incomingReliableSequenceNumber)
  718. break;
  719. if (incomingCommand->reliableSequenceNumber < reliableSequenceNumber)
  720. break;
  721. if (incomingCommand->reliableSequenceNumber > reliableSequenceNumber)
  722. continue;
  723. if (incomingCommand->unreliableSequenceNumber <= unreliableSequenceNumber)
  724. {
  725. if (incomingCommand->unreliableSequenceNumber < unreliableSequenceNumber)
  726. break;
  727. goto freePacket;
  728. }
  729. }
  730. break;
  731. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  732. currentCommand = enet_list_end (& channel -> incomingUnreliableCommands);
  733. break;
  734. default:
  735. goto freePacket;
  736. }
  737. incomingCommand = (ENetIncomingCommand *) enet_malloc(sizeof(ENetIncomingCommand));
  738. if (incomingCommand == NULL)
  739. goto notifyError;
  740. incomingCommand->reliableSequenceNumber = command->header.reliableSequenceNumber;
  741. incomingCommand->unreliableSequenceNumber = unreliableSequenceNumber & 0xFFFF;
  742. incomingCommand->command = *command;
  743. incomingCommand->fragmentCount = fragmentCount;
  744. incomingCommand->fragmentsRemaining = fragmentCount;
  745. incomingCommand->packet = packet;
  746. incomingCommand->fragments = NULL;
  747. if (fragmentCount > 0)
  748. {
  749. if (fragmentCount <= ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT)
  750. incomingCommand->fragments = (enet_uint32 *) enet_malloc(
  751. (fragmentCount + 31) / 32 * sizeof(enet_uint32));
  752. if (incomingCommand->fragments == NULL)
  753. {
  754. enet_free(incomingCommand);
  755. goto notifyError;
  756. }
  757. memset(incomingCommand->fragments, 0, (fragmentCount + 31) / 32 * sizeof(enet_uint32));
  758. }
  759. if (packet != NULL)
  760. ++packet->referenceCount;
  761. enet_list_insert(enet_list_next (currentCommand), incomingCommand);
  762. switch (command->header.command & ENET_PROTOCOL_COMMAND_MASK)
  763. {
  764. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  765. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  766. enet_peer_dispatch_incoming_reliable_commands(peer, channel);
  767. break;
  768. default:
  769. enet_peer_dispatch_incoming_unreliable_commands(peer, channel);
  770. break;
  771. }
  772. return incomingCommand;
  773. freePacket: if (fragmentCount > 0)
  774. goto notifyError;
  775. if (packet != NULL && packet->referenceCount == 0)
  776. enet_packet_destroy(packet);
  777. return &dummyCommand;
  778. notifyError: if (packet != NULL && packet->referenceCount == 0)
  779. enet_packet_destroy(packet);
  780. return NULL;
  781. }
  782. /** @} */