protocol.c 45 KB

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