peer.c 36 KB

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