peer.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /**
  2. @file peer.c
  3. @brief ENet peer management functions
  4. */
  5. #include <string.h>
  6. #define ENET_BUILDING_LIB 1
  7. #include "enet/enet.h"
  8. /** @defgroup peer ENet peer functions
  9. @{
  10. */
  11. /** Configures throttle parameter for a peer.
  12. Unreliable packets are dropped by ENet in response to the varying conditions
  13. of the Internet connection to the peer. The throttle represents a probability
  14. that an unreliable packet should not be dropped and thus sent by ENet to the peer.
  15. The lowest mean round trip time from the sending of a reliable packet to the
  16. receipt of its acknowledgement is measured over an amount of time specified by
  17. the interval parameter in milliseconds. If a measured round trip time happens to
  18. be significantly less than the mean round trip time measured over the interval,
  19. then the throttle probability is increased to allow more traffic by an amount
  20. specified in the acceleration parameter, which is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE
  21. constant. If a measured round trip time happens to be significantly greater than
  22. the mean round trip time measured over the interval, then the throttle probability
  23. is decreased to limit traffic by an amount specified in the deceleration parameter, which
  24. is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE constant. When the throttle has
  25. a value of ENET_PEER_PACKET_THROTTLE_SCALE, on unreliable packets are dropped by
  26. ENet, and so 100% of all unreliable packets will be sent. When the throttle has a
  27. value of 0, all unreliable packets are dropped by ENet, and so 0% of all unreliable
  28. packets will be sent. Intermediate values for the throttle represent intermediate
  29. probabilities between 0% and 100% of unreliable packets being sent. The bandwidth
  30. limits of the local and foreign hosts are taken into account to determine a
  31. sensible limit for the throttle probability above which it should not raise even in
  32. the best of conditions.
  33. @param peer peer to configure
  34. @param interval interval, in milliseconds, over which to measure lowest mean RTT; the default value is ENET_PEER_PACKET_THROTTLE_INTERVAL.
  35. @param acceleration rate at which to increase the throttle probability as mean RTT declines
  36. @param deceleration rate at which to decrease the throttle probability as mean RTT increases
  37. */
  38. void
  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. return -1;
  94. fragmentLength = peer -> mtu - sizeof (ENetProtocolHeader) - sizeof (ENetProtocolSendFragment);
  95. if (packet -> dataLength > fragmentLength)
  96. {
  97. enet_uint16 startSequenceNumber = ENET_HOST_TO_NET_16 (channel -> outgoingReliableSequenceNumber + 1);
  98. enet_uint32 fragmentCount = ENET_HOST_TO_NET_32 ((packet -> dataLength + fragmentLength - 1) / fragmentLength),
  99. fragmentNumber,
  100. fragmentOffset;
  101. packet -> flags |= ENET_PACKET_FLAG_RELIABLE;
  102. packet -> flags &= ~ENET_PACKET_FLAG_UNSEQUENCED;
  103. for (fragmentNumber = 0,
  104. fragmentOffset = 0;
  105. fragmentOffset < packet -> dataLength;
  106. ++ fragmentNumber,
  107. fragmentOffset += fragmentLength)
  108. {
  109. if (packet -> dataLength - fragmentOffset < fragmentLength)
  110. fragmentLength = packet -> dataLength - fragmentOffset;
  111. command.header.command = ENET_PROTOCOL_COMMAND_SEND_FRAGMENT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  112. command.header.channelID = channelID;
  113. command.sendFragment.startSequenceNumber = startSequenceNumber;
  114. command.sendFragment.dataLength = ENET_HOST_TO_NET_16 (fragmentLength);
  115. command.sendFragment.fragmentCount = fragmentCount;
  116. command.sendFragment.fragmentNumber = ENET_HOST_TO_NET_32 (fragmentNumber);
  117. command.sendFragment.totalLength = ENET_HOST_TO_NET_32 (packet -> dataLength);
  118. command.sendFragment.fragmentOffset = ENET_NET_TO_HOST_32 (fragmentOffset);
  119. enet_peer_queue_outgoing_command (peer, & command, packet, fragmentOffset, fragmentLength);
  120. }
  121. return 0;
  122. }
  123. command.header.channelID = channelID;
  124. if (! (packet -> flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNSEQUENCED)) && channel -> outgoingUnreliableSequenceNumber >= 0xFFFF)
  125. packet -> flags |= ENET_PACKET_FLAG_RELIABLE;
  126. if (packet -> flags & ENET_PACKET_FLAG_RELIABLE)
  127. {
  128. command.header.command = ENET_PROTOCOL_COMMAND_SEND_RELIABLE | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  129. command.sendReliable.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  130. }
  131. else
  132. if (packet -> flags & ENET_PACKET_FLAG_UNSEQUENCED)
  133. {
  134. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  135. command.sendUnsequenced.unsequencedGroup = ENET_HOST_TO_NET_16 (peer -> outgoingUnsequencedGroup + 1);
  136. command.sendUnsequenced.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  137. }
  138. else
  139. {
  140. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE;
  141. command.sendUnreliable.unreliableSequenceNumber = ENET_HOST_TO_NET_16 (channel -> outgoingUnreliableSequenceNumber + 1);
  142. command.sendUnreliable.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  143. }
  144. enet_peer_queue_outgoing_command (peer, & command, packet, 0, packet -> dataLength);
  145. return 0;
  146. }
  147. /** Attempts to dequeue any incoming queued packet.
  148. @param peer peer to dequeue packets from
  149. @param channelID channel on which to receive
  150. @returns a pointer to the packet, or NULL if there are no available incoming queued packets
  151. */
  152. ENetPacket *
  153. enet_peer_receive (ENetPeer * peer, enet_uint8 channelID)
  154. {
  155. ENetChannel * channel = & peer -> channels [channelID];
  156. ENetIncomingCommand * incomingCommand = NULL;
  157. ENetPacket * packet;
  158. if (! enet_list_empty (& channel -> incomingUnreliableCommands))
  159. {
  160. incomingCommand = (ENetIncomingCommand *) enet_list_front (& channel -> incomingUnreliableCommands);
  161. if ((incomingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE)
  162. {
  163. if (incomingCommand -> reliableSequenceNumber != channel -> incomingReliableSequenceNumber)
  164. incomingCommand = NULL;
  165. else
  166. channel -> incomingUnreliableSequenceNumber = incomingCommand -> unreliableSequenceNumber;
  167. }
  168. }
  169. if (incomingCommand == NULL &&
  170. ! enet_list_empty (& channel -> incomingReliableCommands))
  171. {
  172. incomingCommand = (ENetIncomingCommand *) enet_list_front (& channel -> incomingReliableCommands);
  173. if (incomingCommand -> fragmentsRemaining > 0 ||
  174. incomingCommand -> reliableSequenceNumber != (enet_uint16) (channel -> incomingReliableSequenceNumber + 1))
  175. return NULL;
  176. channel -> incomingReliableSequenceNumber = incomingCommand -> reliableSequenceNumber;
  177. if (incomingCommand -> fragmentCount > 0)
  178. channel -> incomingReliableSequenceNumber += incomingCommand -> fragmentCount - 1;
  179. }
  180. if (incomingCommand == NULL)
  181. return NULL;
  182. enet_list_remove (& incomingCommand -> incomingCommandList);
  183. packet = incomingCommand -> packet;
  184. -- packet -> referenceCount;
  185. if (incomingCommand -> fragments != NULL)
  186. enet_free (incomingCommand -> fragments);
  187. enet_free (incomingCommand);
  188. return packet;
  189. }
  190. static void
  191. enet_peer_reset_outgoing_commands (ENetList * queue)
  192. {
  193. ENetOutgoingCommand * outgoingCommand;
  194. while (! enet_list_empty (queue))
  195. {
  196. outgoingCommand = (ENetOutgoingCommand *) enet_list_remove (enet_list_begin (queue));
  197. if (outgoingCommand -> packet != NULL)
  198. {
  199. -- outgoingCommand -> packet -> referenceCount;
  200. if (outgoingCommand -> packet -> referenceCount == 0)
  201. enet_packet_destroy (outgoingCommand -> packet);
  202. }
  203. enet_free (outgoingCommand);
  204. }
  205. }
  206. static void
  207. enet_peer_reset_incoming_commands (ENetList * queue)
  208. {
  209. ENetIncomingCommand * incomingCommand;
  210. while (! enet_list_empty (queue))
  211. {
  212. incomingCommand = (ENetIncomingCommand *) enet_list_remove (enet_list_begin (queue));
  213. if (incomingCommand -> packet != NULL)
  214. {
  215. -- incomingCommand -> packet -> referenceCount;
  216. if (incomingCommand -> packet -> referenceCount == 0)
  217. enet_packet_destroy (incomingCommand -> packet);
  218. }
  219. if (incomingCommand -> fragments != NULL)
  220. enet_free (incomingCommand -> fragments);
  221. enet_free (incomingCommand);
  222. }
  223. }
  224. void
  225. enet_peer_reset_queues (ENetPeer * peer)
  226. {
  227. ENetChannel * channel;
  228. while (! enet_list_empty (& peer -> acknowledgements))
  229. enet_free (enet_list_remove (enet_list_begin (& peer -> acknowledgements)));
  230. enet_peer_reset_outgoing_commands (& peer -> sentReliableCommands);
  231. enet_peer_reset_outgoing_commands (& peer -> sentUnreliableCommands);
  232. enet_peer_reset_outgoing_commands (& peer -> outgoingReliableCommands);
  233. enet_peer_reset_outgoing_commands (& peer -> outgoingUnreliableCommands);
  234. if (peer -> channels != NULL && peer -> channelCount > 0)
  235. {
  236. for (channel = peer -> channels;
  237. channel < & peer -> channels [peer -> channelCount];
  238. ++ channel)
  239. {
  240. enet_peer_reset_incoming_commands (& channel -> incomingReliableCommands);
  241. enet_peer_reset_incoming_commands (& channel -> incomingUnreliableCommands);
  242. }
  243. enet_free (peer -> channels);
  244. }
  245. peer -> channels = NULL;
  246. peer -> channelCount = 0;
  247. }
  248. /** Forcefully disconnects a peer.
  249. @param peer peer to forcefully disconnect
  250. @remarks The foreign host represented by the peer is not notified of the disconnection and will timeout
  251. on its connection to the local host.
  252. */
  253. void
  254. enet_peer_reset (ENetPeer * peer)
  255. {
  256. peer -> outgoingPeerID = ENET_PROTOCOL_MAXIMUM_PEER_ID;
  257. peer -> sessionID = 0;
  258. peer -> state = ENET_PEER_STATE_DISCONNECTED;
  259. peer -> incomingBandwidth = 0;
  260. peer -> outgoingBandwidth = 0;
  261. peer -> incomingBandwidthThrottleEpoch = 0;
  262. peer -> outgoingBandwidthThrottleEpoch = 0;
  263. peer -> incomingDataTotal = 0;
  264. peer -> outgoingDataTotal = 0;
  265. peer -> lastSendTime = 0;
  266. peer -> lastReceiveTime = 0;
  267. peer -> nextTimeout = 0;
  268. peer -> earliestTimeout = 0;
  269. peer -> packetLossEpoch = 0;
  270. peer -> packetsSent = 0;
  271. peer -> packetsLost = 0;
  272. peer -> packetLoss = 0;
  273. peer -> packetLossVariance = 0;
  274. peer -> packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE;
  275. peer -> packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE;
  276. peer -> packetThrottleCounter = 0;
  277. peer -> packetThrottleEpoch = 0;
  278. peer -> packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION;
  279. peer -> packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION;
  280. peer -> packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL;
  281. peer -> lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  282. peer -> lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  283. peer -> lastRoundTripTimeVariance = 0;
  284. peer -> highestRoundTripTimeVariance = 0;
  285. peer -> roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  286. peer -> roundTripTimeVariance = 0;
  287. peer -> mtu = peer -> host -> mtu;
  288. peer -> reliableDataInTransit = 0;
  289. peer -> outgoingReliableSequenceNumber = 0;
  290. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  291. peer -> incomingUnsequencedGroup = 0;
  292. peer -> outgoingUnsequencedGroup = 0;
  293. peer -> disconnectData = 0;
  294. memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
  295. enet_peer_reset_queues (peer);
  296. }
  297. /** Sends a ping request to a peer.
  298. @param peer destination for the ping request
  299. @remarks ping requests factor into the mean round trip time as designated by the
  300. roundTripTime field in the ENetPeer structure. Enet automatically pings all connected
  301. peers at regular intervals, however, this function may be called to ensure more
  302. frequent ping requests.
  303. */
  304. void
  305. enet_peer_ping (ENetPeer * peer)
  306. {
  307. ENetProtocol command;
  308. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  309. return;
  310. command.header.command = ENET_PROTOCOL_COMMAND_PING | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  311. command.header.channelID = 0xFF;
  312. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  313. }
  314. /** Force an immediate disconnection from a peer.
  315. @param peer peer to disconnect
  316. @param data data describing the disconnection
  317. @remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not
  318. guarenteed to receive the disconnect notification, and is reset immediately upon
  319. return from this function.
  320. */
  321. void
  322. enet_peer_disconnect_now (ENetPeer * peer, enet_uint32 data)
  323. {
  324. ENetProtocol command;
  325. if (peer -> state == ENET_PEER_STATE_DISCONNECTED)
  326. return;
  327. if (peer -> state != ENET_PEER_STATE_ZOMBIE &&
  328. peer -> state != ENET_PEER_STATE_DISCONNECTING)
  329. {
  330. enet_peer_reset_queues (peer);
  331. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  332. command.header.channelID = 0xFF;
  333. command.disconnect.data = ENET_HOST_TO_NET_32 (data);
  334. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  335. enet_host_flush (peer -> host);
  336. }
  337. enet_peer_reset (peer);
  338. }
  339. /** Request a disconnection from a peer.
  340. @param peer peer to request a disconnection
  341. @param data data describing the disconnection
  342. @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  343. once the disconnection is complete.
  344. */
  345. void
  346. enet_peer_disconnect (ENetPeer * peer, enet_uint32 data)
  347. {
  348. ENetProtocol command;
  349. if (peer -> state == ENET_PEER_STATE_DISCONNECTING ||
  350. peer -> state == ENET_PEER_STATE_DISCONNECTED ||
  351. peer -> state == ENET_PEER_STATE_ZOMBIE)
  352. return;
  353. enet_peer_reset_queues (peer);
  354. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
  355. command.header.channelID = 0xFF;
  356. command.disconnect.data = ENET_HOST_TO_NET_32 (data);
  357. if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
  358. command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  359. else
  360. command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  361. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  362. if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
  363. peer -> state = ENET_PEER_STATE_DISCONNECTING;
  364. else
  365. {
  366. enet_host_flush (peer -> host);
  367. enet_peer_reset (peer);
  368. }
  369. }
  370. /** Request a disconnection from a peer, but only after all queued outgoing packets are sent.
  371. @param peer peer to request a disconnection
  372. @param data data describing the disconnection
  373. @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  374. once the disconnection is complete.
  375. */
  376. void
  377. enet_peer_disconnect_later (ENetPeer * peer, enet_uint32 data)
  378. {
  379. if ((peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER) &&
  380. ! (enet_list_empty (& peer -> outgoingReliableCommands) &&
  381. enet_list_empty (& peer -> outgoingUnreliableCommands) &&
  382. enet_list_empty (& peer -> sentReliableCommands)))
  383. {
  384. peer -> state = ENET_PEER_STATE_DISCONNECT_LATER;
  385. peer -> disconnectData = data;
  386. }
  387. else
  388. enet_peer_disconnect (peer, data);
  389. }
  390. ENetAcknowledgement *
  391. enet_peer_queue_acknowledgement (ENetPeer * peer, const ENetProtocol * command, enet_uint16 sentTime)
  392. {
  393. ENetAcknowledgement * acknowledgement;
  394. peer -> outgoingDataTotal += sizeof (ENetProtocolAcknowledge);
  395. acknowledgement = (ENetAcknowledgement *) enet_malloc (sizeof (ENetAcknowledgement));
  396. acknowledgement -> sentTime = sentTime;
  397. acknowledgement -> command = * command;
  398. enet_list_insert (enet_list_end (& peer -> acknowledgements), acknowledgement);
  399. return acknowledgement;
  400. }
  401. ENetOutgoingCommand *
  402. enet_peer_queue_outgoing_command (ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet, enet_uint32 offset, enet_uint16 length)
  403. {
  404. ENetChannel * channel = & peer -> channels [command -> header.channelID];
  405. ENetOutgoingCommand * outgoingCommand;
  406. peer -> outgoingDataTotal += enet_protocol_command_size (command -> header.command) + length;
  407. outgoingCommand = (ENetOutgoingCommand *) enet_malloc (sizeof (ENetOutgoingCommand));
  408. if (command -> header.channelID == 0xFF)
  409. {
  410. ++ peer -> outgoingReliableSequenceNumber;
  411. outgoingCommand -> reliableSequenceNumber = peer -> outgoingReliableSequenceNumber;
  412. outgoingCommand -> unreliableSequenceNumber = 0;
  413. }
  414. else
  415. if (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  416. {
  417. ++ channel -> outgoingReliableSequenceNumber;
  418. outgoingCommand -> reliableSequenceNumber = channel -> outgoingReliableSequenceNumber;
  419. outgoingCommand -> unreliableSequenceNumber = 0;
  420. }
  421. else
  422. if (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED)
  423. {
  424. ++ peer -> outgoingUnsequencedGroup;
  425. outgoingCommand -> reliableSequenceNumber = 0;
  426. outgoingCommand -> unreliableSequenceNumber = 0;
  427. }
  428. else
  429. {
  430. ++ channel -> outgoingUnreliableSequenceNumber;
  431. outgoingCommand -> reliableSequenceNumber = channel -> outgoingReliableSequenceNumber;
  432. outgoingCommand -> unreliableSequenceNumber = channel -> outgoingUnreliableSequenceNumber;
  433. }
  434. outgoingCommand -> sendAttempts = 0;
  435. outgoingCommand -> sentTime = 0;
  436. outgoingCommand -> roundTripTimeout = 0;
  437. outgoingCommand -> roundTripTimeoutLimit = 0;
  438. outgoingCommand -> fragmentOffset = offset;
  439. outgoingCommand -> fragmentLength = length;
  440. outgoingCommand -> packet = packet;
  441. outgoingCommand -> command = * command;
  442. outgoingCommand -> command.header.reliableSequenceNumber = ENET_HOST_TO_NET_16 (outgoingCommand -> reliableSequenceNumber);
  443. if (packet != NULL)
  444. ++ packet -> referenceCount;
  445. if (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  446. enet_list_insert (enet_list_end (& peer -> outgoingReliableCommands), outgoingCommand);
  447. else
  448. enet_list_insert (enet_list_end (& peer -> outgoingUnreliableCommands), outgoingCommand);
  449. return outgoingCommand;
  450. }
  451. ENetIncomingCommand *
  452. enet_peer_queue_incoming_command (ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet, enet_uint32 fragmentCount)
  453. {
  454. ENetChannel * channel = & peer -> channels [command -> header.channelID];
  455. enet_uint32 unreliableSequenceNumber = 0, reliableSequenceNumber;
  456. ENetIncomingCommand * incomingCommand;
  457. ENetListIterator currentCommand;
  458. if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
  459. goto freePacket;
  460. if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
  461. {
  462. reliableSequenceNumber = command -> header.reliableSequenceNumber;
  463. if (channel -> incomingReliableSequenceNumber >= 0xF000 && reliableSequenceNumber < 0x1000)
  464. reliableSequenceNumber += 0x10000;
  465. if (reliableSequenceNumber < channel -> incomingReliableSequenceNumber ||
  466. (channel -> incomingReliableSequenceNumber < 0x1000 && (reliableSequenceNumber & 0xFFFF) >= 0xF000))
  467. goto freePacket;
  468. }
  469. switch (command -> header.command & ENET_PROTOCOL_COMMAND_MASK)
  470. {
  471. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  472. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  473. if (reliableSequenceNumber == channel -> incomingReliableSequenceNumber)
  474. goto freePacket;
  475. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  476. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  477. currentCommand = enet_list_previous (currentCommand))
  478. {
  479. incomingCommand = (ENetIncomingCommand *) currentCommand;
  480. if (reliableSequenceNumber >= 0x10000 && incomingCommand -> reliableSequenceNumber < 0xF000)
  481. reliableSequenceNumber -= 0x10000;
  482. if (incomingCommand -> reliableSequenceNumber <= reliableSequenceNumber)
  483. {
  484. if (incomingCommand -> reliableSequenceNumber < reliableSequenceNumber)
  485. break;
  486. goto freePacket;
  487. }
  488. }
  489. break;
  490. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  491. unreliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendUnreliable.unreliableSequenceNumber);
  492. if (channel -> incomingUnreliableSequenceNumber >= 0xF000 && unreliableSequenceNumber < 0x1000)
  493. unreliableSequenceNumber += 0x10000;
  494. if (unreliableSequenceNumber <= channel -> incomingUnreliableSequenceNumber ||
  495. (channel -> incomingUnreliableSequenceNumber < 0x1000 && (unreliableSequenceNumber & 0xFFFF) >= 0xF000))
  496. goto freePacket;
  497. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingUnreliableCommands));
  498. currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
  499. currentCommand = enet_list_previous (currentCommand))
  500. {
  501. incomingCommand = (ENetIncomingCommand *) currentCommand;
  502. if ((incomingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE)
  503. continue;
  504. if (unreliableSequenceNumber >= 0x10000 && incomingCommand -> unreliableSequenceNumber < 0xF000)
  505. unreliableSequenceNumber -= 0x10000;
  506. if (incomingCommand -> unreliableSequenceNumber <= unreliableSequenceNumber)
  507. {
  508. if (incomingCommand -> unreliableSequenceNumber < unreliableSequenceNumber)
  509. break;
  510. goto freePacket;
  511. }
  512. }
  513. break;
  514. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  515. currentCommand = enet_list_end (& channel -> incomingUnreliableCommands);
  516. break;
  517. default:
  518. goto freePacket;
  519. }
  520. incomingCommand = (ENetIncomingCommand *) enet_malloc (sizeof (ENetIncomingCommand));
  521. incomingCommand -> reliableSequenceNumber = command -> header.reliableSequenceNumber;
  522. incomingCommand -> unreliableSequenceNumber = unreliableSequenceNumber & 0xFFFF;
  523. incomingCommand -> command = * command;
  524. incomingCommand -> fragmentCount = fragmentCount;
  525. incomingCommand -> fragmentsRemaining = fragmentCount;
  526. incomingCommand -> packet = packet;
  527. incomingCommand -> fragments = NULL;
  528. if (fragmentCount > 0)
  529. {
  530. incomingCommand -> fragments = (enet_uint32 *) enet_malloc ((fragmentCount + 31) / 32 * sizeof (enet_uint32));
  531. memset (incomingCommand -> fragments, 0, (fragmentCount + 31) / 32 * sizeof (enet_uint32));
  532. }
  533. if (packet != NULL)
  534. ++ packet -> referenceCount;
  535. enet_list_insert (enet_list_next (currentCommand), incomingCommand);
  536. return incomingCommand;
  537. freePacket:
  538. if (packet != NULL)
  539. {
  540. if (packet -> referenceCount == 0)
  541. enet_packet_destroy (packet);
  542. }
  543. return NULL;
  544. }
  545. /** @} */