peer.c 37 KB

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