peer.c 23 KB

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