protocol.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. /**
  2. @file protocol.c
  3. @brief ENet protocol functions
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #define ENET_BUILDING_LIB 1
  8. #include "enet/utility.h"
  9. #include "enet/memory.h"
  10. #include "enet/time.h"
  11. #include "enet/enet.h"
  12. static enet_uint32 timeCurrent;
  13. static int
  14. enet_protocol_dispatch_incoming_commands (ENetHost * host, ENetEvent * event)
  15. {
  16. ENetPeer * currentPeer = host -> lastServicedPeer;
  17. ENetChannel * channel;
  18. do
  19. {
  20. ++ currentPeer;
  21. if (currentPeer >= & host -> peers [host -> peerCount])
  22. currentPeer = host -> peers;
  23. if (currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
  24. {
  25. host -> recalculateBandwidthLimits = 1;
  26. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  27. event -> peer = currentPeer;
  28. enet_peer_reset (currentPeer);
  29. host -> lastServicedPeer = currentPeer;
  30. return 1;
  31. }
  32. if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
  33. continue;
  34. for (channel = currentPeer -> channels;
  35. channel < & currentPeer -> channels [currentPeer -> channelCount];
  36. ++ channel)
  37. {
  38. if (enet_list_empty (& channel -> incomingReliableCommands) &&
  39. enet_list_empty (& channel -> incomingUnreliableCommands))
  40. continue;
  41. event -> packet = enet_peer_receive (currentPeer, channel - currentPeer -> channels);
  42. if (event -> packet == NULL)
  43. continue;
  44. event -> type = ENET_EVENT_TYPE_RECEIVE;
  45. event -> peer = currentPeer;
  46. event -> channelID = (enet_uint8) (channel - currentPeer -> channels);
  47. host -> lastServicedPeer = currentPeer;
  48. return 1;
  49. }
  50. } while (currentPeer != host -> lastServicedPeer);
  51. return 0;
  52. }
  53. static void
  54. enet_protocol_remove_sent_unreliable_commands (ENetPeer * peer)
  55. {
  56. ENetOutgoingCommand * outgoingCommand;
  57. while (enet_list_empty (& peer -> sentUnreliableCommands) == 0)
  58. {
  59. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentUnreliableCommands);
  60. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  61. if (outgoingCommand -> packet != NULL)
  62. {
  63. -- outgoingCommand -> packet -> referenceCount;
  64. if (outgoingCommand -> packet -> referenceCount == 0)
  65. enet_packet_destroy (outgoingCommand -> packet);
  66. }
  67. enet_free (outgoingCommand);
  68. }
  69. }
  70. static ENetProtocolCommand
  71. enet_protocol_remove_sent_reliable_command (ENetPeer * peer, enet_uint32 reliableSequenceNumber, enet_uint8 channelID)
  72. {
  73. ENetOutgoingCommand * outgoingCommand;
  74. ENetListIterator currentCommand;
  75. ENetProtocolCommand commandNumber;
  76. for (currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  77. currentCommand != enet_list_end (& peer -> sentReliableCommands);
  78. currentCommand = enet_list_next (currentCommand))
  79. {
  80. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  81. if (outgoingCommand -> reliableSequenceNumber == reliableSequenceNumber &&
  82. outgoingCommand -> command.header.channelID == channelID)
  83. break;
  84. }
  85. if (currentCommand == enet_list_end (& peer -> sentReliableCommands))
  86. return ENET_PROTOCOL_COMMAND_NONE;
  87. commandNumber = outgoingCommand -> command.header.command;
  88. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  89. if (outgoingCommand -> packet != NULL)
  90. {
  91. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  92. -- outgoingCommand -> packet -> referenceCount;
  93. if (outgoingCommand -> packet -> referenceCount == 0)
  94. enet_packet_destroy (outgoingCommand -> packet);
  95. }
  96. enet_free (outgoingCommand);
  97. if (enet_list_empty (& peer -> sentReliableCommands))
  98. return commandNumber;
  99. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentReliableCommands);
  100. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  101. return commandNumber;
  102. }
  103. static ENetPeer *
  104. enet_protocol_handle_connect (ENetHost * host, const ENetProtocolHeader * header, const ENetProtocol * command)
  105. {
  106. enet_uint16 mtu;
  107. enet_uint32 windowSize;
  108. ENetChannel * channel;
  109. size_t channelCount;
  110. ENetPeer * currentPeer;
  111. ENetProtocol verifyCommand;
  112. if (command -> header.commandLength < sizeof (ENetProtocolConnect))
  113. return NULL;
  114. channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount);
  115. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT ||
  116. channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  117. return NULL;
  118. for (currentPeer = host -> peers;
  119. currentPeer < & host -> peers [host -> peerCount];
  120. ++ currentPeer)
  121. {
  122. if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
  123. currentPeer -> address.host == host -> receivedAddress.host &&
  124. currentPeer -> address.port == host -> receivedAddress.port &&
  125. currentPeer -> challenge == header -> challenge)
  126. return NULL;
  127. }
  128. for (currentPeer = host -> peers;
  129. currentPeer < & host -> peers [host -> peerCount];
  130. ++ currentPeer)
  131. {
  132. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
  133. break;
  134. }
  135. if (currentPeer >= & host -> peers [host -> peerCount])
  136. return NULL;
  137. currentPeer -> state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
  138. currentPeer -> challenge = header -> challenge;
  139. currentPeer -> address = host -> receivedAddress;
  140. currentPeer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
  141. currentPeer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.incomingBandwidth);
  142. currentPeer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.outgoingBandwidth);
  143. currentPeer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleInterval);
  144. currentPeer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleAcceleration);
  145. currentPeer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleDeceleration);
  146. currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
  147. currentPeer -> channelCount = channelCount;
  148. for (channel = currentPeer -> channels;
  149. channel < & currentPeer -> channels [channelCount];
  150. ++ channel)
  151. {
  152. channel -> outgoingReliableSequenceNumber = 0;
  153. channel -> outgoingUnreliableSequenceNumber = 0;
  154. channel -> incomingReliableSequenceNumber = 0;
  155. channel -> incomingUnreliableSequenceNumber = 0;
  156. enet_list_clear (& channel -> incomingReliableCommands);
  157. enet_list_clear (& channel -> incomingUnreliableCommands);
  158. }
  159. mtu = ENET_NET_TO_HOST_16 (command -> connect.mtu);
  160. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  161. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  162. else
  163. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  164. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  165. currentPeer -> mtu = mtu;
  166. if (host -> outgoingBandwidth == 0 &&
  167. currentPeer -> incomingBandwidth == 0)
  168. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  169. else
  170. currentPeer -> windowSize = (ENET_MIN (host -> outgoingBandwidth, currentPeer -> incomingBandwidth) /
  171. ENET_PEER_WINDOW_SIZE_SCALE) *
  172. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  173. if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  174. currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  175. else
  176. if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  177. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  178. if (host -> incomingBandwidth == 0)
  179. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  180. else
  181. windowSize = (host -> incomingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE) *
  182. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  183. if (windowSize > ENET_NET_TO_HOST_32 (command -> connect.windowSize))
  184. windowSize = ENET_NET_TO_HOST_32 (command -> connect.windowSize);
  185. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  186. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  187. else
  188. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  189. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  190. verifyCommand.header.command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT;
  191. verifyCommand.header.channelID = 0xFF;
  192. verifyCommand.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE;
  193. verifyCommand.header.commandLength = sizeof (ENetProtocolVerifyConnect);
  194. verifyCommand.verifyConnect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
  195. verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_16 (currentPeer -> mtu);
  196. verifyCommand.verifyConnect.windowSize = ENET_HOST_TO_NET_32 (windowSize);
  197. verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
  198. verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
  199. verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  200. verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
  201. verifyCommand.verifyConnect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
  202. verifyCommand.verifyConnect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
  203. enet_peer_queue_outgoing_command (currentPeer, & verifyCommand, NULL, 0, 0);
  204. return currentPeer;
  205. }
  206. static void
  207. enet_protocol_handle_send_reliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  208. {
  209. ENetPacket * packet;
  210. if (command -> header.commandLength <= sizeof (ENetProtocolSendReliable) ||
  211. command -> header.channelID >= peer -> channelCount ||
  212. peer -> state != ENET_PEER_STATE_CONNECTED)
  213. return;
  214. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendReliable),
  215. command -> header.commandLength - sizeof (ENetProtocolSendReliable),
  216. ENET_PACKET_FLAG_RELIABLE);
  217. enet_peer_queue_incoming_command (peer, command, packet, 0);
  218. }
  219. static void
  220. enet_protocol_handle_send_unreliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  221. {
  222. ENetPacket * packet;
  223. if (command -> header.commandLength <= sizeof (ENetProtocolSendUnreliable) ||
  224. command -> header.channelID >= peer -> channelCount ||
  225. peer -> state != ENET_PEER_STATE_CONNECTED)
  226. return;
  227. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable),
  228. command -> header.commandLength - sizeof (ENetProtocolSendUnreliable),
  229. 0);
  230. enet_peer_queue_incoming_command (peer, command, packet, 0);
  231. }
  232. static void
  233. enet_protocol_handle_send_fragment (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  234. {
  235. enet_uint32 fragmentNumber,
  236. fragmentCount,
  237. fragmentOffset,
  238. fragmentLength,
  239. startSequenceNumber,
  240. totalLength;
  241. ENetChannel * channel;
  242. ENetListIterator currentCommand;
  243. ENetIncomingCommand * startCommand;
  244. if (command -> header.commandLength <= sizeof (ENetProtocolSendFragment) ||
  245. command -> header.channelID >= peer -> channelCount ||
  246. peer -> state != ENET_PEER_STATE_CONNECTED)
  247. return;
  248. startSequenceNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.startSequenceNumber);
  249. fragmentNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentNumber);
  250. fragmentCount = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentCount);
  251. fragmentOffset = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentOffset);
  252. totalLength = ENET_NET_TO_HOST_32 (command -> sendFragment.totalLength);
  253. fragmentLength = command -> header.commandLength - sizeof (ENetProtocolSendFragment);
  254. if (fragmentOffset >= totalLength ||
  255. fragmentOffset + fragmentLength > totalLength ||
  256. fragmentNumber >= fragmentCount)
  257. return;
  258. channel = & peer -> channels [command -> header.channelID];
  259. if (startSequenceNumber <= channel -> incomingReliableSequenceNumber)
  260. return;
  261. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  262. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  263. currentCommand = enet_list_previous (currentCommand))
  264. {
  265. startCommand = (ENetIncomingCommand *) currentCommand;
  266. if (startCommand -> command.header.command == ENET_PROTOCOL_COMMAND_SEND_FRAGMENT &&
  267. startCommand -> command.sendFragment.startSequenceNumber == startSequenceNumber)
  268. break;
  269. }
  270. if (currentCommand == enet_list_end (& channel -> incomingReliableCommands))
  271. {
  272. ENetProtocol hostCommand = * command;
  273. hostCommand.sendFragment.startSequenceNumber = startSequenceNumber;
  274. hostCommand.sendFragment.fragmentNumber = fragmentNumber;
  275. hostCommand.sendFragment.fragmentCount = fragmentCount;
  276. hostCommand.sendFragment.fragmentOffset = fragmentOffset;
  277. hostCommand.sendFragment.totalLength = totalLength;
  278. startCommand = enet_peer_queue_incoming_command (peer,
  279. & hostCommand,
  280. enet_packet_create (NULL, totalLength, ENET_PACKET_FLAG_RELIABLE),
  281. fragmentCount);
  282. }
  283. else
  284. if (totalLength != startCommand -> packet -> dataLength ||
  285. fragmentCount != startCommand -> fragmentCount)
  286. return;
  287. if ((startCommand -> fragments [fragmentNumber / 32] & (1 << fragmentNumber)) == 0)
  288. -- startCommand -> fragmentsRemaining;
  289. startCommand -> fragments [fragmentNumber / 32] |= (1 << fragmentNumber);
  290. if (fragmentOffset + fragmentLength > startCommand -> packet -> dataLength)
  291. fragmentLength = startCommand -> packet -> dataLength - fragmentOffset;
  292. memcpy (startCommand -> packet -> data + fragmentOffset,
  293. (enet_uint8 *) command + sizeof (ENetProtocolSendFragment),
  294. fragmentLength);
  295. }
  296. static void
  297. enet_protocol_handle_ping (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  298. {
  299. if (command -> header.commandLength < sizeof (ENetProtocolPing))
  300. return;
  301. }
  302. static void
  303. enet_protocol_handle_bandwidth_limit (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  304. {
  305. if (command -> header.commandLength < sizeof (ENetProtocolBandwidthLimit))
  306. return;
  307. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
  308. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
  309. if (peer -> incomingBandwidth == 0 &&
  310. host -> outgoingBandwidth == 0)
  311. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  312. else
  313. peer -> windowSize = (ENET_MIN (peer -> incomingBandwidth, host -> outgoingBandwidth) /
  314. ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  315. if (peer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  316. peer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  317. else
  318. if (peer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  319. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  320. }
  321. static void
  322. enet_protocol_handle_throttle_configure (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  323. {
  324. if (command -> header.commandLength < sizeof (ENetProtocolThrottleConfigure))
  325. return;
  326. peer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleInterval);
  327. peer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleAcceleration);
  328. peer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleDeceleration);
  329. }
  330. static void
  331. enet_protocol_handle_disconnect (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  332. {
  333. if (command -> header.commandLength < sizeof (ENetProtocolDisconnect))
  334. return;
  335. enet_peer_reset_queues (peer);
  336. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  337. enet_peer_reset (peer);
  338. else
  339. if (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE)
  340. peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT;
  341. else
  342. peer -> state = ENET_PEER_STATE_ZOMBIE;
  343. }
  344. static int
  345. enet_protocol_handle_acknowledge (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  346. {
  347. enet_uint32 roundTripTime,
  348. receivedSentTime,
  349. receivedReliableSequenceNumber;
  350. ENetProtocolCommand commandNumber;
  351. if (command -> header.commandLength < sizeof (ENetProtocolAcknowledge))
  352. return 0;
  353. receivedSentTime = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedSentTime);
  354. if (ENET_TIME_LESS (timeCurrent, receivedSentTime))
  355. return 0;
  356. peer -> lastReceiveTime = timeCurrent;
  357. roundTripTime = ENET_TIME_DIFFERENCE (timeCurrent, receivedSentTime);
  358. enet_peer_throttle (peer, roundTripTime);
  359. peer -> roundTripTimeVariance -= peer -> roundTripTimeVariance / 4;
  360. if (roundTripTime >= peer -> roundTripTime)
  361. {
  362. peer -> roundTripTime += (roundTripTime - peer -> roundTripTime) / 8;
  363. peer -> roundTripTimeVariance += (roundTripTime - peer -> roundTripTime) / 4;
  364. }
  365. else
  366. {
  367. peer -> roundTripTime -= (peer -> roundTripTime - roundTripTime) / 8;
  368. peer -> roundTripTimeVariance += (peer -> roundTripTime - roundTripTime) / 4;
  369. }
  370. if (peer -> roundTripTime < peer -> lowestRoundTripTime)
  371. peer -> lowestRoundTripTime = peer -> roundTripTime;
  372. if (peer -> roundTripTimeVariance > peer -> highestRoundTripTimeVariance)
  373. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  374. if (peer -> packetThrottleEpoch == 0 ||
  375. ENET_TIME_DIFFERENCE(timeCurrent, peer -> packetThrottleEpoch) >= peer -> packetThrottleInterval)
  376. {
  377. peer -> lastRoundTripTime = peer -> lowestRoundTripTime;
  378. peer -> lastRoundTripTimeVariance = peer -> highestRoundTripTimeVariance;
  379. peer -> lowestRoundTripTime = peer -> roundTripTime;
  380. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  381. peer -> packetThrottleEpoch = timeCurrent;
  382. }
  383. receivedReliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedReliableSequenceNumber);
  384. commandNumber = enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command -> header.channelID);
  385. switch (peer -> state)
  386. {
  387. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  388. if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT)
  389. return 0;
  390. host -> recalculateBandwidthLimits = 1;
  391. peer -> state = ENET_PEER_STATE_CONNECTED;
  392. event -> type = ENET_EVENT_TYPE_CONNECT;
  393. event -> peer = peer;
  394. return 1;
  395. case ENET_PEER_STATE_DISCONNECTING:
  396. if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
  397. return 0;
  398. host -> recalculateBandwidthLimits = 1;
  399. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  400. event -> peer = peer;
  401. enet_peer_reset (peer);
  402. return 1;
  403. default:
  404. break;
  405. }
  406. return 0;
  407. }
  408. static void
  409. enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  410. {
  411. enet_uint16 mtu;
  412. enet_uint32 windowSize;
  413. if (command -> header.commandLength < sizeof (ENetProtocolVerifyConnect) ||
  414. peer -> state != ENET_PEER_STATE_CONNECTING)
  415. return;
  416. if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount ||
  417. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
  418. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
  419. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration)
  420. {
  421. peer -> state = ENET_PEER_STATE_ZOMBIE;
  422. return;
  423. }
  424. peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
  425. mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu);
  426. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  427. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  428. else
  429. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  430. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  431. if (mtu < peer -> mtu)
  432. peer -> mtu = mtu;
  433. windowSize = ENET_NET_TO_HOST_32 (command -> verifyConnect.windowSize);
  434. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  435. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  436. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  437. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  438. if (windowSize < peer -> windowSize)
  439. peer -> windowSize = windowSize;
  440. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.incomingBandwidth);
  441. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.outgoingBandwidth);
  442. host -> recalculateBandwidthLimits = 1;
  443. peer -> state = ENET_PEER_STATE_CONNECTED;
  444. event -> type = ENET_EVENT_TYPE_CONNECT;
  445. event -> peer = peer;
  446. }
  447. static int
  448. enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
  449. {
  450. ENetProtocolHeader * header;
  451. ENetProtocol * command;
  452. ENetPeer * peer;
  453. enet_uint8 * currentData;
  454. size_t commandCount;
  455. if (host -> receivedDataLength < sizeof (ENetProtocolHeader))
  456. return 0;
  457. header = (ENetProtocolHeader *) host -> receivedData;
  458. header -> peerID = ENET_NET_TO_HOST_16 (header -> peerID);
  459. header -> sentTime = ENET_NET_TO_HOST_32 (header -> sentTime);
  460. if (header -> peerID == 0xFFFF)
  461. peer = NULL;
  462. else
  463. if (header -> peerID >= host -> peerCount)
  464. return 0;
  465. else
  466. {
  467. peer = & host -> peers [header -> peerID];
  468. if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
  469. peer -> state == ENET_PEER_STATE_ZOMBIE ||
  470. host -> receivedAddress.host != peer -> address.host ||
  471. header -> challenge != peer -> challenge)
  472. return 0;
  473. else
  474. peer -> address.port = host -> receivedAddress.port;
  475. }
  476. if (peer != NULL)
  477. peer -> incomingDataTotal += host -> receivedDataLength;
  478. commandCount = header -> commandCount;
  479. currentData = host -> receivedData + sizeof (ENetProtocolHeader);
  480. while (commandCount > 0 &&
  481. currentData < & host -> receivedData [host -> receivedDataLength])
  482. {
  483. command = (ENetProtocol *) currentData;
  484. if (currentData + sizeof (ENetProtocolCommandHeader) > & host -> receivedData [host -> receivedDataLength])
  485. return 0;
  486. command -> header.commandLength = ENET_NET_TO_HOST_32 (command -> header.commandLength);
  487. if (currentData + command -> header.commandLength > & host -> receivedData [host -> receivedDataLength])
  488. return 0;
  489. -- commandCount;
  490. currentData += command -> header.commandLength;
  491. if (peer == NULL)
  492. {
  493. if (command -> header.command != ENET_PROTOCOL_COMMAND_CONNECT)
  494. return 0;
  495. }
  496. command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> header.reliableSequenceNumber);
  497. switch (command -> header.command)
  498. {
  499. case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
  500. enet_protocol_handle_acknowledge (host, event, peer, command);
  501. break;
  502. case ENET_PROTOCOL_COMMAND_CONNECT:
  503. peer = enet_protocol_handle_connect (host, header, command);
  504. break;
  505. case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
  506. enet_protocol_handle_verify_connect (host, event, peer, command);
  507. break;
  508. case ENET_PROTOCOL_COMMAND_DISCONNECT:
  509. enet_protocol_handle_disconnect (host, peer, command);
  510. break;
  511. case ENET_PROTOCOL_COMMAND_PING:
  512. enet_protocol_handle_ping (host, peer, command);
  513. break;
  514. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  515. enet_protocol_handle_send_reliable (host, peer, command);
  516. break;
  517. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  518. enet_protocol_handle_send_unreliable (host, peer, command);
  519. break;
  520. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  521. enet_protocol_handle_send_fragment (host, peer, command);
  522. break;
  523. case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
  524. enet_protocol_handle_bandwidth_limit (host, peer, command);
  525. break;
  526. case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
  527. enet_protocol_handle_throttle_configure (host, peer, command);
  528. break;
  529. default:
  530. break;
  531. }
  532. if (peer != NULL &&
  533. (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE) != 0)
  534. {
  535. switch (peer -> state)
  536. {
  537. case ENET_PEER_STATE_DISCONNECTING:
  538. break;
  539. case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
  540. if (command -> header.command != ENET_PROTOCOL_COMMAND_DISCONNECT)
  541. break;
  542. default:
  543. enet_peer_queue_acknowledgement (peer, command, header -> sentTime);
  544. break;
  545. }
  546. }
  547. }
  548. if (event -> type != ENET_EVENT_TYPE_NONE)
  549. return 1;
  550. return 0;
  551. }
  552. static int
  553. enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
  554. {
  555. for (;;)
  556. {
  557. int receivedLength;
  558. ENetBuffer buffer;
  559. buffer.data = host -> receivedData;
  560. buffer.dataLength = sizeof (host -> receivedData);
  561. receivedLength = enet_socket_receive (host -> socket,
  562. & host -> receivedAddress,
  563. & buffer,
  564. 1);
  565. if (receivedLength < 0)
  566. return -1;
  567. if (receivedLength == 0)
  568. return 0;
  569. host -> receivedDataLength = receivedLength;
  570. switch (enet_protocol_handle_incoming_commands (host, event))
  571. {
  572. case 1:
  573. return 1;
  574. case -1:
  575. return -1;
  576. default:
  577. break;
  578. }
  579. }
  580. return -1;
  581. }
  582. static void
  583. enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
  584. {
  585. ENetProtocol * command = & host -> commands [host -> commandCount];
  586. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  587. ENetAcknowledgement * acknowledgement;
  588. ENetListIterator currentAcknowledgement;
  589. currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
  590. while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
  591. {
  592. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  593. buffer >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  594. peer -> mtu - host -> packetSize < sizeof (ENetProtocolAcknowledge))
  595. break;
  596. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  597. if (peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT &&
  598. acknowledgement -> command.header.command != ENET_PROTOCOL_COMMAND_DISCONNECT)
  599. continue;
  600. currentAcknowledgement = enet_list_next (currentAcknowledgement);
  601. buffer -> data = command;
  602. buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
  603. host -> packetSize += buffer -> dataLength;
  604. command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  605. command -> header.channelID = acknowledgement -> command.header.channelID;
  606. command -> header.flags = 0;
  607. command -> header.commandLength = ENET_HOST_TO_NET_32 (sizeof (ENetProtocolAcknowledge));
  608. command -> acknowledge.receivedReliableSequenceNumber = ENET_HOST_TO_NET_32 (acknowledgement -> command.header.reliableSequenceNumber);
  609. command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_32 (acknowledgement -> sentTime);
  610. if (acknowledgement -> command.header.command == ENET_PROTOCOL_COMMAND_DISCONNECT)
  611. peer -> state = ENET_PEER_STATE_ZOMBIE;
  612. enet_list_remove (& acknowledgement -> acknowledgementList);
  613. enet_free (acknowledgement);
  614. ++ command;
  615. ++ buffer;
  616. }
  617. host -> commandCount = command - host -> commands;
  618. host -> bufferCount = buffer - host -> buffers;
  619. }
  620. static void
  621. enet_protocol_send_unreliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  622. {
  623. ENetProtocol * command = & host -> commands [host -> commandCount];
  624. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  625. ENetOutgoingCommand * outgoingCommand;
  626. ENetListIterator currentCommand;
  627. currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
  628. while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
  629. {
  630. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  631. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  632. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  633. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength ||
  634. (outgoingCommand -> packet != NULL &&
  635. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength +
  636. outgoingCommand -> packet -> dataLength))
  637. break;
  638. currentCommand = enet_list_next (currentCommand);
  639. if (outgoingCommand -> packet != NULL)
  640. {
  641. peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  642. peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  643. if (peer -> packetThrottleCounter > peer -> packetThrottle)
  644. {
  645. -- outgoingCommand -> packet -> referenceCount;
  646. if (outgoingCommand -> packet -> referenceCount == 0)
  647. enet_packet_destroy (outgoingCommand -> packet);
  648. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  649. enet_free (outgoingCommand);
  650. continue;
  651. }
  652. }
  653. buffer -> data = command;
  654. buffer -> dataLength = outgoingCommand -> command.header.commandLength;
  655. host -> packetSize += buffer -> dataLength;
  656. * command = outgoingCommand -> command;
  657. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  658. if (outgoingCommand -> packet != NULL)
  659. {
  660. ++ buffer;
  661. buffer -> data = outgoingCommand -> packet -> data;
  662. buffer -> dataLength = outgoingCommand -> packet -> dataLength;
  663. command -> header.commandLength += buffer -> dataLength;
  664. host -> packetSize += buffer -> dataLength;
  665. enet_list_insert (enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
  666. }
  667. else
  668. enet_free (outgoingCommand);
  669. command -> header.commandLength = ENET_HOST_TO_NET_32 (command -> header.commandLength);
  670. ++ command;
  671. ++ buffer;
  672. }
  673. host -> commandCount = command - host -> commands;
  674. host -> bufferCount = buffer - host -> buffers;
  675. }
  676. static int
  677. enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  678. {
  679. ENetOutgoingCommand * outgoingCommand;
  680. ENetListIterator currentCommand;
  681. currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  682. while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
  683. {
  684. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  685. currentCommand = enet_list_next (currentCommand);
  686. if (ENET_TIME_DIFFERENCE (timeCurrent, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
  687. continue;
  688. if (outgoingCommand -> roundTripTimeout >= outgoingCommand -> roundTripTimeoutLimit)
  689. {
  690. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  691. event -> peer = peer;
  692. enet_peer_reset (peer);
  693. return 1;
  694. }
  695. if (outgoingCommand -> packet != NULL)
  696. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  697. ++ peer -> packetsLost;
  698. outgoingCommand -> roundTripTimeout *= 2;
  699. enet_list_insert (enet_list_begin (& peer -> outgoingReliableCommands),
  700. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  701. if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
  702. enet_list_empty (& peer -> sentReliableCommands) == 0)
  703. {
  704. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  705. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  706. }
  707. }
  708. return 0;
  709. }
  710. static void
  711. enet_protocol_send_reliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  712. {
  713. ENetProtocol * command = & host -> commands [host -> commandCount];
  714. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  715. ENetOutgoingCommand * outgoingCommand;
  716. ENetListIterator currentCommand;
  717. currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  718. while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
  719. {
  720. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  721. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  722. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  723. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength)
  724. break;
  725. currentCommand = enet_list_next (currentCommand);
  726. if (outgoingCommand -> packet != NULL)
  727. {
  728. if ((enet_uint16) (peer -> mtu - host -> packetSize) <
  729. (enet_uint16) (outgoingCommand -> command.header.commandLength +
  730. outgoingCommand -> fragmentLength) ||
  731. peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > peer -> windowSize)
  732. break;
  733. }
  734. if (outgoingCommand -> roundTripTimeout == 0)
  735. {
  736. outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
  737. outgoingCommand -> roundTripTimeoutLimit = ENET_PEER_TIMEOUT_LIMIT * outgoingCommand -> roundTripTimeout;
  738. }
  739. if (enet_list_empty (& peer -> sentReliableCommands))
  740. peer -> nextTimeout = timeCurrent + outgoingCommand -> roundTripTimeout;
  741. enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
  742. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  743. outgoingCommand -> sentTime = timeCurrent;
  744. buffer -> data = command;
  745. buffer -> dataLength = outgoingCommand -> command.header.commandLength;
  746. host -> packetSize += buffer -> dataLength;
  747. * command = outgoingCommand -> command;
  748. if (outgoingCommand -> packet != NULL)
  749. {
  750. ++ buffer;
  751. buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
  752. buffer -> dataLength = outgoingCommand -> fragmentLength;
  753. command -> header.commandLength += outgoingCommand -> fragmentLength;
  754. host -> packetSize += outgoingCommand -> fragmentLength;
  755. peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
  756. }
  757. command -> header.commandLength = ENET_HOST_TO_NET_32 (command -> header.commandLength);
  758. ++ peer -> packetsSent;
  759. ++ command;
  760. ++ buffer;
  761. }
  762. host -> commandCount = command - host -> commands;
  763. host -> bufferCount = buffer - host -> buffers;
  764. }
  765. static int
  766. enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
  767. {
  768. size_t packetsSent = 1;
  769. ENetProtocolHeader header;
  770. ENetPeer * currentPeer;
  771. int sentLength;
  772. while (packetsSent > 0)
  773. for (currentPeer = host -> peers,
  774. packetsSent = 0;
  775. currentPeer < & host -> peers [host -> peerCount];
  776. ++ currentPeer)
  777. {
  778. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
  779. currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
  780. continue;
  781. host -> commandCount = 0;
  782. host -> bufferCount = 1;
  783. host -> packetSize = sizeof (ENetProtocolHeader);
  784. if (enet_list_empty (& currentPeer -> acknowledgements) == 0)
  785. enet_protocol_send_acknowledgements (host, currentPeer);
  786. if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol))
  787. {
  788. if (checkForTimeouts != 0 &&
  789. enet_list_empty (& currentPeer -> sentReliableCommands) == 0 &&
  790. ENET_TIME_GREATER_EQUAL (timeCurrent, currentPeer -> nextTimeout) &&
  791. enet_protocol_check_timeouts (host, currentPeer, event) == 1)
  792. return 1;
  793. }
  794. if (enet_list_empty (& currentPeer -> outgoingReliableCommands) == 0)
  795. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  796. else
  797. if (enet_list_empty (& currentPeer -> sentReliableCommands) &&
  798. ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL &&
  799. currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
  800. {
  801. enet_peer_ping (currentPeer);
  802. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  803. }
  804. if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol) &&
  805. enet_list_empty (& currentPeer -> outgoingUnreliableCommands) == 0)
  806. enet_protocol_send_unreliable_outgoing_commands (host, currentPeer);
  807. if (host -> commandCount == 0)
  808. continue;
  809. if (currentPeer -> packetLossEpoch == 0)
  810. currentPeer -> packetLossEpoch = timeCurrent;
  811. else
  812. if (ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
  813. currentPeer -> packetsSent > 0)
  814. {
  815. enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
  816. #ifdef ENET_DEBUG
  817. #ifdef WIN32
  818. printf ("peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands));
  819. #else
  820. fprintf (stderr, "peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands));
  821. #endif
  822. #endif
  823. currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;
  824. if (packetLoss >= currentPeer -> packetLoss)
  825. {
  826. currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8;
  827. currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4;
  828. }
  829. else
  830. {
  831. currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8;
  832. currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4;
  833. }
  834. currentPeer -> packetLossEpoch = timeCurrent;
  835. currentPeer -> packetsSent = 0;
  836. currentPeer -> packetsLost = 0;
  837. }
  838. header.peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID);
  839. header.flags = 0;
  840. header.commandCount = host -> commandCount;
  841. header.sentTime = ENET_HOST_TO_NET_32 (timeCurrent);
  842. header.challenge = currentPeer -> challenge;
  843. host -> buffers -> data = & header;
  844. host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
  845. currentPeer -> lastSendTime = timeCurrent;
  846. ++ packetsSent;
  847. sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
  848. enet_protocol_remove_sent_unreliable_commands (currentPeer);
  849. if (sentLength < 0)
  850. return -1;
  851. }
  852. return 0;
  853. }
  854. /** Sends any queued packets on the host specified to its designated peers.
  855. @param host host to flush
  856. @remarks this function need only be used in circumstances where one wishes to send queued packets earlier than in a call to enet_host_service().
  857. @ingroup host
  858. */
  859. void
  860. enet_host_flush (ENetHost * host)
  861. {
  862. timeCurrent = enet_time_get ();
  863. enet_protocol_send_outgoing_commands (host, NULL, 0);
  864. }
  865. /** Waits for events on the host specified and shuttles packets between
  866. the host and its peers.
  867. @param host host to service
  868. @param event an event structure where event details will be placed if one occurs
  869. @param timeout number of milliseconds that ENet should wait for events
  870. @retval > 1 if an event occurred within the specified time limit
  871. @retval 0 if no event occurred
  872. @retval < 1 on failure
  873. @remarks enet_host_service should be called fairly regularly for adequate performance
  874. @ingroup host
  875. */
  876. int
  877. enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
  878. {
  879. enet_uint32 waitCondition;
  880. event -> type = ENET_EVENT_TYPE_NONE;
  881. event -> peer = NULL;
  882. event -> packet = NULL;
  883. switch (enet_protocol_dispatch_incoming_commands (host, event))
  884. {
  885. case 1:
  886. return 1;
  887. case -1:
  888. perror ("Error dispatching incoming packets");
  889. return -1;
  890. default:
  891. break;
  892. }
  893. timeCurrent = enet_time_get ();
  894. timeout += timeCurrent;
  895. do
  896. {
  897. if (ENET_TIME_DIFFERENCE (timeCurrent, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  898. enet_host_bandwidth_throttle (host);
  899. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  900. {
  901. case 1:
  902. return 1;
  903. case -1:
  904. perror ("Error sending outgoing packets");
  905. return -1;
  906. default:
  907. break;
  908. }
  909. switch (enet_protocol_receive_incoming_commands (host, event))
  910. {
  911. case 1:
  912. return 1;
  913. case -1:
  914. perror ("Error receiving incoming packets");
  915. return -1;
  916. default:
  917. break;
  918. }
  919. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  920. {
  921. case 1:
  922. return 1;
  923. case -1:
  924. perror ("Error sending outgoing packets");
  925. return -1;
  926. default:
  927. break;
  928. }
  929. switch (enet_protocol_dispatch_incoming_commands (host, event))
  930. {
  931. case 1:
  932. return 1;
  933. case -1:
  934. perror ("Error dispatching incoming packets");
  935. return -1;
  936. default:
  937. break;
  938. }
  939. timeCurrent = enet_time_get ();
  940. if (ENET_TIME_GREATER_EQUAL (timeCurrent, timeout))
  941. return 0;
  942. waitCondition = ENET_SOCKET_WAIT_RECEIVE;
  943. if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, timeCurrent)) != 0)
  944. return -1;
  945. timeCurrent = enet_time_get ();
  946. } while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);
  947. return 0;
  948. }