peer.c 38 KB

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