peer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /**
  2. @file peer.c
  3. @brief ENet peer management functions
  4. */
  5. #define ENET_BUILDING_LIB 1
  6. #include "enet/memory.h"
  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;
  46. command.header.channelID = 0xFF;
  47. command.header.flags = 0;
  48. command.header.commandLength = sizeof (ENetProtocolThrottleConfigure);
  49. command.throttleConfigure.packetThrottleInterval = ENET_HOST_TO_NET_32 (interval);
  50. command.throttleConfigure.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (acceleration);
  51. command.throttleConfigure.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (deceleration);
  52. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  53. }
  54. int
  55. enet_peer_throttle (ENetPeer * peer, enet_uint32 rtt)
  56. {
  57. if (peer -> lastRoundTripTime <= peer -> lastRoundTripTimeVariance)
  58. {
  59. peer -> packetThrottle = peer -> packetThrottleLimit;
  60. }
  61. else
  62. if (rtt < peer -> lastRoundTripTime)
  63. {
  64. peer -> packetThrottle += peer -> packetThrottleAcceleration;
  65. if (peer -> packetThrottle > peer -> packetThrottleLimit)
  66. peer -> packetThrottle = peer -> packetThrottleLimit;
  67. return 1;
  68. }
  69. else
  70. if (rtt > peer -> lastRoundTripTime + 2 * peer -> lastRoundTripTimeVariance)
  71. {
  72. if (peer -> packetThrottle > peer -> packetThrottleDeceleration)
  73. peer -> packetThrottle -= peer -> packetThrottleDeceleration;
  74. else
  75. peer -> packetThrottle = 0;
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. /** Queues a packet to be sent.
  81. @param peer destination for the packet
  82. @param channelID channel on which to send
  83. @param packet packet to send
  84. @retval 0 on success
  85. @retval < 0 on failure
  86. */
  87. int
  88. enet_peer_send (ENetPeer * peer, enet_uint8 channelID, ENetPacket * packet)
  89. {
  90. ENetChannel * channel = & peer -> channels [channelID];
  91. ENetProtocol command;
  92. size_t fragmentLength;
  93. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  94. return -1;
  95. fragmentLength = peer -> mtu - sizeof (ENetProtocolHeader) - sizeof (ENetProtocolSendFragment);
  96. if (packet -> dataLength > fragmentLength)
  97. {
  98. enet_uint32 fragmentCount = ENET_HOST_TO_NET_32 ((packet -> dataLength + fragmentLength - 1) / fragmentLength),
  99. startSequenceNumber = ENET_HOST_TO_NET_32 (channel -> outgoingReliableSequenceNumber + 1),
  100. fragmentNumber,
  101. fragmentOffset;
  102. packet -> flags |= ENET_PACKET_FLAG_RELIABLE;
  103. for (fragmentNumber = 0,
  104. fragmentOffset = 0;
  105. fragmentOffset < packet -> dataLength;
  106. ++ fragmentNumber,
  107. fragmentOffset += fragmentLength)
  108. {
  109. command.header.command = ENET_PROTOCOL_COMMAND_SEND_FRAGMENT;
  110. command.header.channelID = channelID;
  111. command.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE;
  112. command.header.commandLength = sizeof (ENetProtocolSendFragment);
  113. command.sendFragment.startSequenceNumber = startSequenceNumber;
  114. command.sendFragment.fragmentCount = fragmentCount;
  115. command.sendFragment.fragmentNumber = ENET_HOST_TO_NET_32 (fragmentNumber);
  116. command.sendFragment.totalLength = ENET_HOST_TO_NET_32 (packet -> dataLength);
  117. command.sendFragment.fragmentOffset = ENET_NET_TO_HOST_32 (fragmentOffset);
  118. if (packet -> dataLength - fragmentOffset < fragmentLength)
  119. fragmentLength = packet -> dataLength - fragmentOffset;
  120. enet_peer_queue_outgoing_command (peer, & command, packet, fragmentOffset, fragmentLength);
  121. }
  122. return 0;
  123. }
  124. command.header.channelID = channelID;
  125. if (packet -> flags & ENET_PACKET_FLAG_RELIABLE)
  126. {
  127. command.header.command = ENET_PROTOCOL_COMMAND_SEND_RELIABLE;
  128. command.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE;
  129. command.header.commandLength = sizeof (ENetProtocolSendReliable);
  130. }
  131. else
  132. {
  133. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE;
  134. command.header.flags = 0;
  135. command.header.commandLength = sizeof (ENetProtocolSendUnreliable);
  136. command.sendUnreliable.unreliableSequenceNumber = ENET_HOST_TO_NET_32 (channel -> outgoingUnreliableSequenceNumber + 1);
  137. }
  138. enet_peer_queue_outgoing_command (peer, & command, packet, 0, packet -> dataLength);
  139. return 0;
  140. }
  141. /** Attempts to dequeue any incoming queued packet.
  142. @param peer peer to dequeue packets from
  143. @param channelID channel on which to receive
  144. @returns a pointer to the packet, or NULL if there are no available incoming queued packets
  145. */
  146. ENetPacket *
  147. enet_peer_receive (ENetPeer * peer, enet_uint8 channelID)
  148. {
  149. ENetChannel * channel = & peer -> channels [channelID];
  150. ENetIncomingCommand * incomingCommand = NULL;
  151. ENetPacket * packet;
  152. if (enet_list_empty (& channel -> incomingUnreliableCommands) == 0)
  153. {
  154. incomingCommand = (ENetIncomingCommand *) enet_list_front (& channel -> incomingUnreliableCommands);
  155. if (incomingCommand -> reliableSequenceNumber > channel -> incomingReliableSequenceNumber)
  156. incomingCommand = NULL;
  157. else
  158. channel -> incomingUnreliableSequenceNumber = incomingCommand -> unreliableSequenceNumber;
  159. }
  160. if (incomingCommand == NULL &&
  161. enet_list_empty (& channel -> incomingReliableCommands) == 0)
  162. {
  163. do
  164. {
  165. incomingCommand = (ENetIncomingCommand *) enet_list_front (& channel -> incomingReliableCommands);
  166. if (incomingCommand -> fragmentsRemaining > 0 ||
  167. incomingCommand -> reliableSequenceNumber > channel -> incomingReliableSequenceNumber + 1)
  168. return NULL;
  169. if (incomingCommand -> reliableSequenceNumber <= channel -> incomingReliableSequenceNumber)
  170. {
  171. -- incomingCommand -> packet -> referenceCount;
  172. if (incomingCommand -> packet -> referenceCount == 0)
  173. enet_packet_destroy (incomingCommand -> packet);
  174. if (incomingCommand -> fragments != NULL)
  175. enet_free (incomingCommand -> fragments);
  176. enet_list_remove (& incomingCommand -> incomingCommandList);
  177. enet_free (incomingCommand);
  178. incomingCommand = NULL;
  179. }
  180. } while (incomingCommand == NULL &&
  181. enet_list_empty (& channel -> incomingReliableCommands) == 0);
  182. if (incomingCommand == NULL)
  183. return NULL;
  184. channel -> incomingReliableSequenceNumber = incomingCommand -> reliableSequenceNumber;
  185. if (incomingCommand -> fragmentCount > 0)
  186. channel -> incomingReliableSequenceNumber += incomingCommand -> fragmentCount - 1;
  187. }
  188. if (incomingCommand == NULL)
  189. return NULL;
  190. enet_list_remove (& incomingCommand -> incomingCommandList);
  191. packet = incomingCommand -> packet;
  192. -- packet -> referenceCount;
  193. if (incomingCommand -> fragments != NULL)
  194. enet_free (incomingCommand -> fragments);
  195. enet_free (incomingCommand);
  196. return packet;
  197. }
  198. static void
  199. enet_peer_reset_outgoing_commands (ENetList * queue)
  200. {
  201. ENetOutgoingCommand * outgoingCommand;
  202. while (enet_list_empty (queue) == 0)
  203. {
  204. outgoingCommand = (ENetOutgoingCommand *) enet_list_remove (enet_list_begin (queue));
  205. if (outgoingCommand -> packet != NULL)
  206. {
  207. -- outgoingCommand -> packet -> referenceCount;
  208. if (outgoingCommand -> packet -> referenceCount == 0)
  209. enet_packet_destroy (outgoingCommand -> packet);
  210. }
  211. enet_free (outgoingCommand);
  212. }
  213. }
  214. static void
  215. enet_peer_reset_incoming_commands (ENetList * queue)
  216. {
  217. ENetIncomingCommand * incomingCommand;
  218. while (enet_list_empty (queue) == 0)
  219. {
  220. incomingCommand = (ENetIncomingCommand *) enet_list_remove (enet_list_begin (queue));
  221. if (incomingCommand -> packet != NULL)
  222. {
  223. -- incomingCommand -> packet -> referenceCount;
  224. if (incomingCommand -> packet -> referenceCount == 0)
  225. enet_packet_destroy (incomingCommand -> packet);
  226. }
  227. enet_free (incomingCommand);
  228. }
  229. }
  230. void
  231. enet_peer_reset_queues (ENetPeer * peer)
  232. {
  233. ENetChannel * channel;
  234. while (enet_list_empty (& peer -> acknowledgements) == 0)
  235. enet_free (enet_list_remove (enet_list_begin (& peer -> acknowledgements)));
  236. enet_peer_reset_outgoing_commands (& peer -> sentReliableCommands);
  237. enet_peer_reset_outgoing_commands (& peer -> sentUnreliableCommands);
  238. enet_peer_reset_outgoing_commands (& peer -> outgoingReliableCommands);
  239. enet_peer_reset_outgoing_commands (& peer -> outgoingUnreliableCommands);
  240. if (peer -> channels != NULL && peer -> channelCount > 0)
  241. {
  242. for (channel = peer -> channels;
  243. channel < & peer -> channels [peer -> channelCount];
  244. ++ channel)
  245. {
  246. enet_peer_reset_incoming_commands (& channel -> incomingReliableCommands);
  247. enet_peer_reset_incoming_commands (& channel -> incomingUnreliableCommands);
  248. }
  249. enet_free (peer -> channels);
  250. }
  251. peer -> channels = NULL;
  252. peer -> channelCount = 0;
  253. }
  254. /** Forcefully disconnects a peer.
  255. @param peer peer to forcefully disconnect
  256. @remarks The foreign host represented by the peer is not notified of the disconnection and will timeout
  257. on its connection to the local host.
  258. */
  259. void
  260. enet_peer_reset (ENetPeer * peer)
  261. {
  262. peer -> outgoingPeerID = 0xFFFF;
  263. peer -> challenge = 0;
  264. peer -> address.host = ENET_HOST_ANY;
  265. peer -> address.port = 0;
  266. peer -> state = ENET_PEER_STATE_DISCONNECTED;
  267. peer -> incomingBandwidth = 0;
  268. peer -> outgoingBandwidth = 0;
  269. peer -> incomingBandwidthThrottleEpoch = 0;
  270. peer -> outgoingBandwidthThrottleEpoch = 0;
  271. peer -> incomingDataTotal = 0;
  272. peer -> outgoingDataTotal = 0;
  273. peer -> lastSendTime = 0;
  274. peer -> lastReceiveTime = 0;
  275. peer -> nextTimeout = 0;
  276. peer -> packetLossEpoch = 0;
  277. peer -> packetsSent = 0;
  278. peer -> packetsLost = 0;
  279. peer -> packetLoss = 0;
  280. peer -> packetLossVariance = 0;
  281. peer -> packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE;
  282. peer -> packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE;
  283. peer -> packetThrottleCounter = 0;
  284. peer -> packetThrottleEpoch = 0;
  285. peer -> packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION;
  286. peer -> packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION;
  287. peer -> packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL;
  288. peer -> lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  289. peer -> lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  290. peer -> lastRoundTripTimeVariance = 0;
  291. peer -> highestRoundTripTimeVariance = 0;
  292. peer -> roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  293. peer -> roundTripTimeVariance = 0;
  294. peer -> mtu = peer -> host -> mtu;
  295. peer -> reliableDataInTransit = 0;
  296. peer -> outgoingReliableSequenceNumber = 0;
  297. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  298. enet_peer_reset_queues (peer);
  299. }
  300. /** Sends a ping request to a peer.
  301. @param peer destination for the ping request
  302. @remarks ping requests factor into the mean round trip time as designated by the
  303. roundTripTime field in the ENetPeer structure. Enet automatically pings all connected
  304. peers at regular intervals, however, this function may be called to ensure more
  305. frequent ping requests.
  306. */
  307. void
  308. enet_peer_ping (ENetPeer * peer)
  309. {
  310. ENetProtocol command;
  311. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  312. return;
  313. command.header.command = ENET_PROTOCOL_COMMAND_PING;
  314. command.header.channelID = 0xFF;
  315. command.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE;
  316. command.header.commandLength = sizeof (ENetProtocolPing);
  317. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  318. }
  319. /** Force an immediate disconnection from a peer.
  320. @param peer peer to disconnect
  321. @remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not
  322. guarenteed to receive the disconnect notification, and is reset immediately upon
  323. return from this function.
  324. */
  325. void
  326. enet_peer_disconnect_now (ENetPeer * peer)
  327. {
  328. ENetProtocol command;
  329. if (peer -> state != ENET_PEER_STATE_DISCONNECTED)
  330. return;
  331. if (peer -> state != ENET_PEER_STATE_ZOMBIE &&
  332. peer -> state != ENET_PEER_STATE_DISCONNECTING)
  333. {
  334. enet_peer_reset_queues (peer);
  335. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
  336. command.header.channelID = 0xFF;
  337. command.header.flags = 0;
  338. command.header.commandLength = sizeof (ENetProtocolDisconnect);
  339. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  340. enet_host_flush (peer -> host);
  341. }
  342. enet_peer_reset (peer);
  343. }
  344. /** Request a disconnection from a peer.
  345. @param peer peer to request a disconnection
  346. @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  347. once the disconnection is complete.
  348. */
  349. void
  350. enet_peer_disconnect (ENetPeer * peer)
  351. {
  352. ENetProtocol command;
  353. if (peer -> state == ENET_PEER_STATE_DISCONNECTING ||
  354. peer -> state == ENET_PEER_STATE_DISCONNECTED ||
  355. peer -> state == ENET_PEER_STATE_ZOMBIE)
  356. return;
  357. enet_peer_reset_queues (peer);
  358. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
  359. command.header.channelID = 0xFF;
  360. command.header.flags = 0;
  361. command.header.commandLength = sizeof (ENetProtocolDisconnect);
  362. if (peer -> state == ENET_PEER_STATE_CONNECTED)
  363. command.header.flags |= ENET_PROTOCOL_FLAG_ACKNOWLEDGE;
  364. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  365. if (peer -> state == ENET_PEER_STATE_CONNECTED)
  366. peer -> state = ENET_PEER_STATE_DISCONNECTING;
  367. else
  368. {
  369. enet_host_flush (peer -> host);
  370. enet_peer_reset (peer);
  371. }
  372. }
  373. ENetAcknowledgement *
  374. enet_peer_queue_acknowledgement (ENetPeer * peer, const ENetProtocol * command, enet_uint32 sentTime)
  375. {
  376. ENetAcknowledgement * acknowledgement;
  377. peer -> outgoingDataTotal += sizeof (ENetProtocolAcknowledge);
  378. acknowledgement = (ENetAcknowledgement *) enet_malloc (sizeof (ENetAcknowledgement));
  379. acknowledgement -> sentTime = sentTime;
  380. acknowledgement -> command = * command;
  381. enet_list_insert (enet_list_end (& peer -> acknowledgements), acknowledgement);
  382. return acknowledgement;
  383. }
  384. ENetOutgoingCommand *
  385. enet_peer_queue_outgoing_command (ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet, enet_uint32 offset, enet_uint16 length)
  386. {
  387. ENetChannel * channel = & peer -> channels [command -> header.channelID];
  388. ENetOutgoingCommand * outgoingCommand;
  389. peer -> outgoingDataTotal += command -> header.commandLength + length;
  390. outgoingCommand = (ENetOutgoingCommand *) enet_malloc (sizeof (ENetOutgoingCommand));
  391. if (command -> header.channelID == 0xFF)
  392. {
  393. ++ peer -> outgoingReliableSequenceNumber;
  394. outgoingCommand -> reliableSequenceNumber = peer -> outgoingReliableSequenceNumber;
  395. outgoingCommand -> unreliableSequenceNumber = 0;
  396. }
  397. else
  398. if (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE)
  399. {
  400. ++ channel -> outgoingReliableSequenceNumber;
  401. outgoingCommand -> reliableSequenceNumber = channel -> outgoingReliableSequenceNumber;
  402. outgoingCommand -> unreliableSequenceNumber = 0;
  403. }
  404. else
  405. {
  406. ++ channel -> outgoingUnreliableSequenceNumber;
  407. outgoingCommand -> reliableSequenceNumber = channel -> outgoingReliableSequenceNumber;
  408. outgoingCommand -> unreliableSequenceNumber = channel -> outgoingUnreliableSequenceNumber;
  409. }
  410. outgoingCommand -> sentTime = 0;
  411. outgoingCommand -> roundTripTimeout = 0;
  412. outgoingCommand -> roundTripTimeoutLimit = 0;
  413. outgoingCommand -> fragmentOffset = offset;
  414. outgoingCommand -> fragmentLength = length;
  415. outgoingCommand -> packet = packet;
  416. outgoingCommand -> command = * command;
  417. outgoingCommand -> command.header.reliableSequenceNumber = ENET_HOST_TO_NET_32 (outgoingCommand -> reliableSequenceNumber);
  418. if (packet != NULL)
  419. ++ packet -> referenceCount;
  420. if (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE)
  421. enet_list_insert (enet_list_end (& peer -> outgoingReliableCommands), outgoingCommand);
  422. else
  423. enet_list_insert (enet_list_end (& peer -> outgoingUnreliableCommands), outgoingCommand);
  424. return outgoingCommand;
  425. }
  426. ENetIncomingCommand *
  427. enet_peer_queue_incoming_command (ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet, enet_uint32 fragmentCount)
  428. {
  429. ENetChannel * channel = & peer -> channels [command -> header.channelID];
  430. enet_uint32 unreliableSequenceNumber = 0;
  431. ENetIncomingCommand * incomingCommand;
  432. ENetListIterator currentCommand;
  433. if (command -> header.command == ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE)
  434. unreliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> sendUnreliable.unreliableSequenceNumber);
  435. if (unreliableSequenceNumber == 0)
  436. {
  437. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  438. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  439. currentCommand = enet_list_previous (currentCommand))
  440. {
  441. incomingCommand = (ENetIncomingCommand *) currentCommand;
  442. if (incomingCommand -> reliableSequenceNumber <= command -> header.reliableSequenceNumber)
  443. {
  444. if (incomingCommand -> reliableSequenceNumber < command -> header.reliableSequenceNumber)
  445. break;
  446. goto freePacket;
  447. }
  448. }
  449. }
  450. else
  451. {
  452. if (command -> header.reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
  453. goto freePacket;
  454. if (unreliableSequenceNumber <= channel -> incomingUnreliableSequenceNumber)
  455. goto freePacket;
  456. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingUnreliableCommands));
  457. currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
  458. currentCommand = enet_list_previous (currentCommand))
  459. {
  460. incomingCommand = (ENetIncomingCommand *) currentCommand;
  461. if (incomingCommand -> unreliableSequenceNumber <= unreliableSequenceNumber)
  462. {
  463. if (incomingCommand -> unreliableSequenceNumber < unreliableSequenceNumber)
  464. break;
  465. goto freePacket;
  466. }
  467. }
  468. }
  469. incomingCommand = (ENetIncomingCommand *) enet_malloc (sizeof (ENetIncomingCommand));
  470. incomingCommand -> reliableSequenceNumber = command -> header.reliableSequenceNumber;
  471. incomingCommand -> unreliableSequenceNumber = unreliableSequenceNumber;
  472. incomingCommand -> command = * command;
  473. incomingCommand -> fragmentCount = fragmentCount;
  474. incomingCommand -> fragmentsRemaining = fragmentCount;
  475. incomingCommand -> packet = packet;
  476. if (fragmentCount > 0)
  477. incomingCommand -> fragments = (enet_uint32 *) enet_calloc ((fragmentCount + 31) / 32, sizeof (enet_uint32));
  478. else
  479. incomingCommand -> fragments = NULL;
  480. if (packet != NULL)
  481. ++ packet -> referenceCount;
  482. enet_list_insert (enet_list_next (currentCommand), incomingCommand);
  483. return incomingCommand;
  484. freePacket:
  485. if (packet != NULL)
  486. {
  487. if (packet -> referenceCount == 0)
  488. enet_packet_destroy (packet);
  489. }
  490. return NULL;
  491. }
  492. /** @} */