peer.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 (peer -> host -> checksum != NULL)
  96. fragmentLength -= sizeof(enet_uint32);
  97. if (packet -> dataLength > fragmentLength)
  98. {
  99. enet_uint32 fragmentCount = ENET_HOST_TO_NET_32 ((packet -> dataLength + fragmentLength - 1) / fragmentLength),
  100. fragmentNumber,
  101. fragmentOffset;
  102. enet_uint8 commandNumber;
  103. enet_uint16 startSequenceNumber;
  104. ENetList fragments;
  105. ENetOutgoingCommand * fragment;
  106. if ((packet -> flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT)) == ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT &&
  107. channel -> outgoingUnreliableSequenceNumber < 0xFFFF)
  108. {
  109. commandNumber = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT;
  110. startSequenceNumber = ENET_HOST_TO_NET_16 (channel -> outgoingUnreliableSequenceNumber + 1);
  111. }
  112. else
  113. {
  114. commandNumber = ENET_PROTOCOL_COMMAND_SEND_FRAGMENT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  115. startSequenceNumber = ENET_HOST_TO_NET_16 (channel -> outgoingReliableSequenceNumber + 1);
  116. }
  117. enet_list_clear (& fragments);
  118. for (fragmentNumber = 0,
  119. fragmentOffset = 0;
  120. fragmentOffset < packet -> dataLength;
  121. ++ fragmentNumber,
  122. fragmentOffset += fragmentLength)
  123. {
  124. if (packet -> dataLength - fragmentOffset < fragmentLength)
  125. fragmentLength = packet -> dataLength - fragmentOffset;
  126. fragment = (ENetOutgoingCommand *) enet_malloc (sizeof (ENetOutgoingCommand));
  127. if (fragment == NULL)
  128. {
  129. while (! enet_list_empty (& fragments))
  130. {
  131. fragment = (ENetOutgoingCommand *) enet_list_remove (enet_list_begin (& fragments));
  132. enet_free (fragment);
  133. }
  134. return -1;
  135. }
  136. fragment -> fragmentOffset = fragmentOffset;
  137. fragment -> fragmentLength = fragmentLength;
  138. fragment -> packet = packet;
  139. fragment -> command.header.command = commandNumber;
  140. fragment -> command.header.channelID = channelID;
  141. fragment -> command.sendFragment.startSequenceNumber = startSequenceNumber;
  142. fragment -> command.sendFragment.dataLength = ENET_HOST_TO_NET_16 (fragmentLength);
  143. fragment -> command.sendFragment.fragmentCount = fragmentCount;
  144. fragment -> command.sendFragment.fragmentNumber = ENET_HOST_TO_NET_32 (fragmentNumber);
  145. fragment -> command.sendFragment.totalLength = ENET_HOST_TO_NET_32 (packet -> dataLength);
  146. fragment -> command.sendFragment.fragmentOffset = ENET_NET_TO_HOST_32 (fragmentOffset);
  147. enet_list_insert (enet_list_end (& fragments), fragment);
  148. }
  149. packet -> referenceCount += fragmentNumber;
  150. while (! enet_list_empty (& fragments))
  151. {
  152. fragment = (ENetOutgoingCommand *) enet_list_remove (enet_list_begin (& fragments));
  153. enet_peer_setup_outgoing_command (peer, fragment);
  154. }
  155. return 0;
  156. }
  157. command.header.channelID = channelID;
  158. if ((packet -> flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNSEQUENCED)) == ENET_PACKET_FLAG_UNSEQUENCED)
  159. {
  160. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  161. command.sendUnsequenced.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  162. }
  163. else
  164. if (packet -> flags & ENET_PACKET_FLAG_RELIABLE || channel -> outgoingUnreliableSequenceNumber >= 0xFFFF)
  165. {
  166. command.header.command = ENET_PROTOCOL_COMMAND_SEND_RELIABLE | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  167. command.sendReliable.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  168. }
  169. else
  170. {
  171. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE;
  172. command.sendUnreliable.dataLength = ENET_HOST_TO_NET_16 (packet -> dataLength);
  173. }
  174. if (enet_peer_queue_outgoing_command (peer, & command, packet, 0, packet -> dataLength) == NULL)
  175. return -1;
  176. return 0;
  177. }
  178. /** Attempts to dequeue any incoming queued packet.
  179. @param peer peer to dequeue packets from
  180. @param channelID holds the channel ID of the channel the packet was received on success
  181. @returns a pointer to the packet, or NULL if there are no available incoming queued packets
  182. */
  183. ENetPacket *
  184. enet_peer_receive (ENetPeer * peer, enet_uint8 * channelID)
  185. {
  186. ENetIncomingCommand * incomingCommand;
  187. ENetPacket * packet;
  188. if (enet_list_empty (& peer -> dispatchedCommands))
  189. return NULL;
  190. incomingCommand = (ENetIncomingCommand *) enet_list_remove (enet_list_begin (& peer -> dispatchedCommands));
  191. if (channelID != NULL)
  192. * channelID = incomingCommand -> command.header.channelID;
  193. packet = incomingCommand -> packet;
  194. -- packet -> referenceCount;
  195. if (incomingCommand -> fragments != NULL)
  196. enet_free (incomingCommand -> fragments);
  197. enet_free (incomingCommand);
  198. return packet;
  199. }
  200. static void
  201. enet_peer_reset_outgoing_commands (ENetList * queue)
  202. {
  203. ENetOutgoingCommand * outgoingCommand;
  204. while (! enet_list_empty (queue))
  205. {
  206. outgoingCommand = (ENetOutgoingCommand *) enet_list_remove (enet_list_begin (queue));
  207. if (outgoingCommand -> packet != NULL)
  208. {
  209. -- outgoingCommand -> packet -> referenceCount;
  210. if (outgoingCommand -> packet -> referenceCount == 0)
  211. enet_packet_destroy (outgoingCommand -> packet);
  212. }
  213. enet_free (outgoingCommand);
  214. }
  215. }
  216. static void
  217. enet_peer_remove_incoming_commands (ENetList * queue, ENetListIterator startCommand, ENetListIterator endCommand)
  218. {
  219. ENetListIterator currentCommand;
  220. for (currentCommand = startCommand; currentCommand != endCommand; )
  221. {
  222. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  223. currentCommand = enet_list_next (currentCommand);
  224. enet_list_remove (& incomingCommand -> incomingCommandList);
  225. if (incomingCommand -> packet != NULL)
  226. {
  227. -- incomingCommand -> packet -> referenceCount;
  228. if (incomingCommand -> packet -> referenceCount == 0)
  229. enet_packet_destroy (incomingCommand -> packet);
  230. }
  231. if (incomingCommand -> fragments != NULL)
  232. enet_free (incomingCommand -> fragments);
  233. enet_free (incomingCommand);
  234. }
  235. }
  236. static void
  237. enet_peer_reset_incoming_commands (ENetList * queue)
  238. {
  239. enet_peer_remove_incoming_commands(queue, enet_list_begin (queue), enet_list_end(queue));
  240. }
  241. void
  242. enet_peer_reset_queues (ENetPeer * peer)
  243. {
  244. ENetChannel * channel;
  245. if (peer -> needsDispatch)
  246. {
  247. enet_list_remove (& peer -> dispatchList);
  248. peer -> needsDispatch = 0;
  249. }
  250. while (! enet_list_empty (& peer -> acknowledgements))
  251. enet_free (enet_list_remove (enet_list_begin (& peer -> acknowledgements)));
  252. enet_peer_reset_outgoing_commands (& peer -> sentReliableCommands);
  253. enet_peer_reset_outgoing_commands (& peer -> sentUnreliableCommands);
  254. enet_peer_reset_outgoing_commands (& peer -> outgoingReliableCommands);
  255. enet_peer_reset_outgoing_commands (& peer -> outgoingUnreliableCommands);
  256. enet_peer_reset_incoming_commands (& peer -> dispatchedCommands);
  257. if (peer -> channels != NULL && peer -> channelCount > 0)
  258. {
  259. for (channel = peer -> channels;
  260. channel < & peer -> channels [peer -> channelCount];
  261. ++ channel)
  262. {
  263. enet_peer_reset_incoming_commands (& channel -> incomingReliableCommands);
  264. enet_peer_reset_incoming_commands (& channel -> incomingUnreliableCommands);
  265. }
  266. enet_free (peer -> channels);
  267. }
  268. peer -> channels = NULL;
  269. peer -> channelCount = 0;
  270. }
  271. /** Forcefully disconnects a peer.
  272. @param peer peer to forcefully disconnect
  273. @remarks The foreign host represented by the peer is not notified of the disconnection and will timeout
  274. on its connection to the local host.
  275. */
  276. void
  277. enet_peer_reset (ENetPeer * peer)
  278. {
  279. peer -> outgoingPeerID = ENET_PROTOCOL_MAXIMUM_PEER_ID;
  280. peer -> connectID = 0;
  281. peer -> state = ENET_PEER_STATE_DISCONNECTED;
  282. peer -> incomingBandwidth = 0;
  283. peer -> outgoingBandwidth = 0;
  284. peer -> incomingBandwidthThrottleEpoch = 0;
  285. peer -> outgoingBandwidthThrottleEpoch = 0;
  286. peer -> incomingDataTotal = 0;
  287. peer -> outgoingDataTotal = 0;
  288. peer -> lastSendTime = 0;
  289. peer -> lastReceiveTime = 0;
  290. peer -> nextTimeout = 0;
  291. peer -> earliestTimeout = 0;
  292. peer -> packetLossEpoch = 0;
  293. peer -> packetsSent = 0;
  294. peer -> packetsLost = 0;
  295. peer -> packetLoss = 0;
  296. peer -> packetLossVariance = 0;
  297. peer -> packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE;
  298. peer -> packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE;
  299. peer -> packetThrottleCounter = 0;
  300. peer -> packetThrottleEpoch = 0;
  301. peer -> packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION;
  302. peer -> packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION;
  303. peer -> packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL;
  304. peer -> lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  305. peer -> lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  306. peer -> lastRoundTripTimeVariance = 0;
  307. peer -> highestRoundTripTimeVariance = 0;
  308. peer -> roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  309. peer -> roundTripTimeVariance = 0;
  310. peer -> mtu = peer -> host -> mtu;
  311. peer -> reliableDataInTransit = 0;
  312. peer -> outgoingReliableSequenceNumber = 0;
  313. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  314. peer -> incomingUnsequencedGroup = 0;
  315. peer -> outgoingUnsequencedGroup = 0;
  316. peer -> eventData = 0;
  317. memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
  318. enet_peer_reset_queues (peer);
  319. }
  320. /** Sends a ping request to a peer.
  321. @param peer destination for the ping request
  322. @remarks ping requests factor into the mean round trip time as designated by the
  323. roundTripTime field in the ENetPeer structure. Enet automatically pings all connected
  324. peers at regular intervals, however, this function may be called to ensure more
  325. frequent ping requests.
  326. */
  327. void
  328. enet_peer_ping (ENetPeer * peer)
  329. {
  330. ENetProtocol command;
  331. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  332. return;
  333. command.header.command = ENET_PROTOCOL_COMMAND_PING | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  334. command.header.channelID = 0xFF;
  335. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  336. }
  337. /** Force an immediate disconnection from a peer.
  338. @param peer peer to disconnect
  339. @param data data describing the disconnection
  340. @remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not
  341. guarenteed to receive the disconnect notification, and is reset immediately upon
  342. return from this function.
  343. */
  344. void
  345. enet_peer_disconnect_now (ENetPeer * peer, enet_uint32 data)
  346. {
  347. ENetProtocol command;
  348. if (peer -> state == ENET_PEER_STATE_DISCONNECTED)
  349. return;
  350. if (peer -> state != ENET_PEER_STATE_ZOMBIE &&
  351. peer -> state != ENET_PEER_STATE_DISCONNECTING)
  352. {
  353. enet_peer_reset_queues (peer);
  354. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  355. command.header.channelID = 0xFF;
  356. command.disconnect.data = ENET_HOST_TO_NET_32 (data);
  357. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  358. enet_host_flush (peer -> host);
  359. }
  360. enet_peer_reset (peer);
  361. }
  362. /** Request a disconnection from a peer.
  363. @param peer peer to request a disconnection
  364. @param data data describing the disconnection
  365. @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  366. once the disconnection is complete.
  367. */
  368. void
  369. enet_peer_disconnect (ENetPeer * peer, enet_uint32 data)
  370. {
  371. ENetProtocol command;
  372. if (peer -> state == ENET_PEER_STATE_DISCONNECTING ||
  373. peer -> state == ENET_PEER_STATE_DISCONNECTED ||
  374. peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT ||
  375. peer -> state == ENET_PEER_STATE_ZOMBIE)
  376. return;
  377. enet_peer_reset_queues (peer);
  378. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
  379. command.header.channelID = 0xFF;
  380. command.disconnect.data = ENET_HOST_TO_NET_32 (data);
  381. if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
  382. command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  383. else
  384. command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  385. enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);
  386. if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
  387. peer -> state = ENET_PEER_STATE_DISCONNECTING;
  388. else
  389. {
  390. enet_host_flush (peer -> host);
  391. enet_peer_reset (peer);
  392. }
  393. }
  394. /** Request a disconnection from a peer, but only after all queued outgoing packets are sent.
  395. @param peer peer to request a disconnection
  396. @param data data describing the disconnection
  397. @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  398. once the disconnection is complete.
  399. */
  400. void
  401. enet_peer_disconnect_later (ENetPeer * peer, enet_uint32 data)
  402. {
  403. if ((peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER) &&
  404. ! (enet_list_empty (& peer -> outgoingReliableCommands) &&
  405. enet_list_empty (& peer -> outgoingUnreliableCommands) &&
  406. enet_list_empty (& peer -> sentReliableCommands)))
  407. {
  408. peer -> state = ENET_PEER_STATE_DISCONNECT_LATER;
  409. peer -> eventData = data;
  410. }
  411. else
  412. enet_peer_disconnect (peer, data);
  413. }
  414. ENetAcknowledgement *
  415. enet_peer_queue_acknowledgement (ENetPeer * peer, const ENetProtocol * command, enet_uint16 sentTime)
  416. {
  417. ENetAcknowledgement * acknowledgement;
  418. if (command -> header.channelID < peer -> channelCount)
  419. {
  420. ENetChannel * channel = & peer -> channels [command -> header.channelID];
  421. enet_uint16 reliableWindow = command -> header.reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE,
  422. currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  423. if (command -> header.reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
  424. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  425. if (reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1 && reliableWindow <= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS)
  426. return NULL;
  427. }
  428. acknowledgement = (ENetAcknowledgement *) enet_malloc (sizeof (ENetAcknowledgement));
  429. if (acknowledgement == NULL)
  430. return NULL;
  431. peer -> outgoingDataTotal += sizeof (ENetProtocolAcknowledge);
  432. acknowledgement -> sentTime = sentTime;
  433. acknowledgement -> command = * command;
  434. enet_list_insert (enet_list_end (& peer -> acknowledgements), acknowledgement);
  435. return acknowledgement;
  436. }
  437. void
  438. enet_peer_setup_outgoing_command (ENetPeer * peer, ENetOutgoingCommand * outgoingCommand)
  439. {
  440. ENetChannel * channel = & peer -> channels [outgoingCommand -> command.header.channelID];
  441. peer -> outgoingDataTotal += enet_protocol_command_size (outgoingCommand -> command.header.command) + outgoingCommand -> fragmentLength;
  442. if (outgoingCommand -> command.header.channelID == 0xFF)
  443. {
  444. ++ peer -> outgoingReliableSequenceNumber;
  445. outgoingCommand -> reliableSequenceNumber = peer -> outgoingReliableSequenceNumber;
  446. outgoingCommand -> unreliableSequenceNumber = 0;
  447. }
  448. else
  449. if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  450. {
  451. ++ channel -> outgoingReliableSequenceNumber;
  452. channel -> outgoingUnreliableSequenceNumber = 0;
  453. outgoingCommand -> reliableSequenceNumber = channel -> outgoingReliableSequenceNumber;
  454. outgoingCommand -> unreliableSequenceNumber = 0;
  455. }
  456. else
  457. if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED)
  458. {
  459. ++ peer -> outgoingUnsequencedGroup;
  460. outgoingCommand -> reliableSequenceNumber = 0;
  461. outgoingCommand -> unreliableSequenceNumber = 0;
  462. }
  463. else
  464. {
  465. if (outgoingCommand -> fragmentOffset == 0)
  466. ++ channel -> outgoingUnreliableSequenceNumber;
  467. outgoingCommand -> reliableSequenceNumber = channel -> outgoingReliableSequenceNumber;
  468. outgoingCommand -> unreliableSequenceNumber = channel -> outgoingUnreliableSequenceNumber;
  469. }
  470. outgoingCommand -> sendAttempts = 0;
  471. outgoingCommand -> sentTime = 0;
  472. outgoingCommand -> roundTripTimeout = 0;
  473. outgoingCommand -> roundTripTimeoutLimit = 0;
  474. outgoingCommand -> command.header.reliableSequenceNumber = ENET_HOST_TO_NET_16 (outgoingCommand -> reliableSequenceNumber);
  475. switch (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK)
  476. {
  477. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  478. outgoingCommand -> command.sendUnreliable.unreliableSequenceNumber = ENET_HOST_TO_NET_16 (outgoingCommand -> unreliableSequenceNumber);
  479. break;
  480. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  481. outgoingCommand -> command.sendUnsequenced.unsequencedGroup = ENET_HOST_TO_NET_16 (peer -> outgoingUnsequencedGroup);
  482. break;
  483. default:
  484. break;
  485. }
  486. if (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  487. enet_list_insert (enet_list_end (& peer -> outgoingReliableCommands), outgoingCommand);
  488. else
  489. enet_list_insert (enet_list_end (& peer -> outgoingUnreliableCommands), outgoingCommand);
  490. }
  491. ENetOutgoingCommand *
  492. enet_peer_queue_outgoing_command (ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet, enet_uint32 offset, enet_uint16 length)
  493. {
  494. ENetOutgoingCommand * outgoingCommand = (ENetOutgoingCommand *) enet_malloc (sizeof (ENetOutgoingCommand));
  495. if (outgoingCommand == NULL)
  496. return NULL;
  497. outgoingCommand -> command = * command;
  498. outgoingCommand -> fragmentOffset = offset;
  499. outgoingCommand -> fragmentLength = length;
  500. outgoingCommand -> packet = packet;
  501. if (packet != NULL)
  502. ++ packet -> referenceCount;
  503. enet_peer_setup_outgoing_command (peer, outgoingCommand);
  504. return outgoingCommand;
  505. }
  506. void
  507. enet_peer_dispatch_incoming_unreliable_commands (ENetPeer * peer, ENetChannel * channel)
  508. {
  509. ENetListIterator droppedCommand, startCommand, currentCommand;
  510. for (droppedCommand = startCommand = currentCommand = enet_list_begin (& channel -> incomingUnreliableCommands);
  511. currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
  512. currentCommand = enet_list_next (currentCommand))
  513. {
  514. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  515. if ((incomingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
  516. continue;
  517. else
  518. if (incomingCommand -> reliableSequenceNumber != channel -> incomingReliableSequenceNumber)
  519. break;
  520. else
  521. if (incomingCommand -> fragmentsRemaining <= 0)
  522. channel -> incomingUnreliableSequenceNumber = incomingCommand -> unreliableSequenceNumber;
  523. else
  524. if (startCommand == currentCommand)
  525. startCommand = enet_list_next (currentCommand);
  526. else
  527. {
  528. enet_list_move (enet_list_end (& peer -> dispatchedCommands), startCommand, enet_list_previous (currentCommand));
  529. if (! peer -> needsDispatch)
  530. {
  531. enet_list_insert (enet_list_end (& peer -> host -> dispatchQueue), & peer -> dispatchList);
  532. peer -> needsDispatch = 1;
  533. }
  534. droppedCommand = startCommand = enet_list_next (currentCommand);
  535. }
  536. }
  537. if (startCommand != currentCommand)
  538. {
  539. enet_list_move (enet_list_end (& peer -> dispatchedCommands), startCommand, enet_list_previous (currentCommand));
  540. if (! peer -> needsDispatch)
  541. {
  542. enet_list_insert (enet_list_end (& peer -> host -> dispatchQueue), & peer -> dispatchList);
  543. peer -> needsDispatch = 1;
  544. }
  545. droppedCommand = startCommand = enet_list_next (currentCommand);
  546. }
  547. enet_peer_remove_incoming_commands (& channel -> incomingUnreliableCommands, enet_list_begin (& channel -> incomingUnreliableCommands), droppedCommand);
  548. }
  549. void
  550. enet_peer_dispatch_incoming_reliable_commands (ENetPeer * peer, ENetChannel * channel)
  551. {
  552. ENetListIterator currentCommand;
  553. for (currentCommand = enet_list_begin (& channel -> incomingReliableCommands);
  554. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  555. currentCommand = enet_list_next (currentCommand))
  556. {
  557. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  558. if (incomingCommand -> fragmentsRemaining > 0 ||
  559. incomingCommand -> reliableSequenceNumber != (enet_uint16) (channel -> incomingReliableSequenceNumber + 1))
  560. break;
  561. channel -> incomingReliableSequenceNumber = incomingCommand -> reliableSequenceNumber;
  562. if (incomingCommand -> fragmentCount > 0)
  563. channel -> incomingReliableSequenceNumber += incomingCommand -> fragmentCount - 1;
  564. }
  565. if (currentCommand == enet_list_begin (& channel -> incomingReliableCommands))
  566. return;
  567. channel -> incomingUnreliableSequenceNumber = 0;
  568. enet_list_move (enet_list_end (& peer -> dispatchedCommands), enet_list_begin (& channel -> incomingReliableCommands), enet_list_previous (currentCommand));
  569. if (! peer -> needsDispatch)
  570. {
  571. enet_list_insert (enet_list_end (& peer -> host -> dispatchQueue), & peer -> dispatchList);
  572. peer -> needsDispatch = 1;
  573. }
  574. enet_peer_dispatch_incoming_unreliable_commands (peer, channel);
  575. }
  576. ENetIncomingCommand *
  577. enet_peer_queue_incoming_command (ENetPeer * peer, const ENetProtocol * command, ENetPacket * packet, enet_uint32 fragmentCount)
  578. {
  579. static ENetIncomingCommand dummyCommand;
  580. ENetChannel * channel = & peer -> channels [command -> header.channelID];
  581. enet_uint32 unreliableSequenceNumber = 0, reliableSequenceNumber;
  582. enet_uint16 reliableWindow, currentWindow;
  583. ENetIncomingCommand * incomingCommand;
  584. ENetListIterator currentCommand;
  585. if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
  586. goto freePacket;
  587. if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
  588. {
  589. reliableSequenceNumber = command -> header.reliableSequenceNumber;
  590. reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  591. currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  592. if (reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
  593. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  594. if (reliableWindow < currentWindow || reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
  595. goto freePacket;
  596. }
  597. switch (command -> header.command & ENET_PROTOCOL_COMMAND_MASK)
  598. {
  599. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  600. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  601. if (reliableSequenceNumber == channel -> incomingReliableSequenceNumber)
  602. goto freePacket;
  603. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  604. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  605. currentCommand = enet_list_previous (currentCommand))
  606. {
  607. incomingCommand = (ENetIncomingCommand *) currentCommand;
  608. if (reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
  609. {
  610. if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
  611. continue;
  612. }
  613. else
  614. if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
  615. break;
  616. if (incomingCommand -> reliableSequenceNumber <= reliableSequenceNumber)
  617. {
  618. if (incomingCommand -> reliableSequenceNumber < reliableSequenceNumber)
  619. break;
  620. goto freePacket;
  621. }
  622. }
  623. break;
  624. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  625. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
  626. unreliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendUnreliable.unreliableSequenceNumber);
  627. if (reliableSequenceNumber == channel -> incomingReliableSequenceNumber &&
  628. unreliableSequenceNumber <= channel -> incomingUnreliableSequenceNumber)
  629. goto freePacket;
  630. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingUnreliableCommands));
  631. currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
  632. currentCommand = enet_list_previous (currentCommand))
  633. {
  634. incomingCommand = (ENetIncomingCommand *) currentCommand;
  635. if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED)
  636. continue;
  637. if (reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
  638. {
  639. if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
  640. continue;
  641. }
  642. else
  643. if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
  644. break;
  645. if (incomingCommand -> reliableSequenceNumber < reliableSequenceNumber)
  646. break;
  647. if (incomingCommand -> reliableSequenceNumber > reliableSequenceNumber)
  648. continue;
  649. if (incomingCommand -> unreliableSequenceNumber <= unreliableSequenceNumber)
  650. {
  651. if (incomingCommand -> unreliableSequenceNumber < unreliableSequenceNumber)
  652. break;
  653. goto freePacket;
  654. }
  655. }
  656. break;
  657. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  658. currentCommand = enet_list_end (& channel -> incomingUnreliableCommands);
  659. break;
  660. default:
  661. goto freePacket;
  662. }
  663. incomingCommand = (ENetIncomingCommand *) enet_malloc (sizeof (ENetIncomingCommand));
  664. if (incomingCommand == NULL)
  665. goto notifyError;
  666. incomingCommand -> reliableSequenceNumber = command -> header.reliableSequenceNumber;
  667. incomingCommand -> unreliableSequenceNumber = unreliableSequenceNumber & 0xFFFF;
  668. incomingCommand -> command = * command;
  669. incomingCommand -> fragmentCount = fragmentCount;
  670. incomingCommand -> fragmentsRemaining = fragmentCount;
  671. incomingCommand -> packet = packet;
  672. incomingCommand -> fragments = NULL;
  673. if (fragmentCount > 0)
  674. {
  675. incomingCommand -> fragments = (enet_uint32 *) enet_malloc ((fragmentCount + 31) / 32 * sizeof (enet_uint32));
  676. if (incomingCommand -> fragments == NULL)
  677. {
  678. enet_free (incomingCommand);
  679. goto notifyError;
  680. }
  681. memset (incomingCommand -> fragments, 0, (fragmentCount + 31) / 32 * sizeof (enet_uint32));
  682. }
  683. if (packet != NULL)
  684. ++ packet -> referenceCount;
  685. enet_list_insert (enet_list_next (currentCommand), incomingCommand);
  686. switch (command -> header.command & ENET_PROTOCOL_COMMAND_MASK)
  687. {
  688. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  689. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  690. enet_peer_dispatch_incoming_reliable_commands (peer, channel);
  691. break;
  692. default:
  693. enet_peer_dispatch_incoming_unreliable_commands (peer, channel);
  694. break;
  695. }
  696. return incomingCommand;
  697. freePacket:
  698. if (fragmentCount > 0)
  699. goto notifyError;
  700. if (packet != NULL && packet -> referenceCount == 0)
  701. enet_packet_destroy (packet);
  702. return & dummyCommand;
  703. notifyError:
  704. if (packet != NULL && packet -> referenceCount == 0)
  705. enet_packet_destroy (packet);
  706. return NULL;
  707. }
  708. /** @} */