protocol.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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 % 32))) == 0)
  314. {
  315. -- startCommand -> fragmentsRemaining;
  316. startCommand -> fragments [fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
  317. if (fragmentOffset + fragmentLength > startCommand -> packet -> dataLength)
  318. fragmentLength = startCommand -> packet -> dataLength - fragmentOffset;
  319. memcpy (startCommand -> packet -> data + fragmentOffset,
  320. (enet_uint8 *) command + sizeof (ENetProtocolSendFragment),
  321. fragmentLength);
  322. }
  323. }
  324. static void
  325. enet_protocol_handle_ping (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  326. {
  327. if (command -> header.commandLength < sizeof (ENetProtocolPing))
  328. return;
  329. }
  330. static void
  331. enet_protocol_handle_bandwidth_limit (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  332. {
  333. if (command -> header.commandLength < sizeof (ENetProtocolBandwidthLimit))
  334. return;
  335. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
  336. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
  337. if (peer -> incomingBandwidth == 0 &&
  338. host -> outgoingBandwidth == 0)
  339. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  340. else
  341. peer -> windowSize = (ENET_MIN (peer -> incomingBandwidth, host -> outgoingBandwidth) /
  342. ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  343. if (peer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  344. peer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  345. else
  346. if (peer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  347. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  348. }
  349. static void
  350. enet_protocol_handle_throttle_configure (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  351. {
  352. if (command -> header.commandLength < sizeof (ENetProtocolThrottleConfigure))
  353. return;
  354. peer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleInterval);
  355. peer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleAcceleration);
  356. peer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleDeceleration);
  357. }
  358. static void
  359. enet_protocol_handle_disconnect (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  360. {
  361. if (command -> header.commandLength < sizeof (ENetProtocolDisconnect))
  362. return;
  363. enet_peer_reset_queues (peer);
  364. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  365. enet_peer_reset (peer);
  366. else
  367. if (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE)
  368. peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT;
  369. else
  370. peer -> state = ENET_PEER_STATE_ZOMBIE;
  371. }
  372. static int
  373. enet_protocol_handle_acknowledge (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  374. {
  375. enet_uint32 roundTripTime,
  376. receivedSentTime,
  377. receivedReliableSequenceNumber;
  378. ENetProtocolCommand commandNumber;
  379. if (command -> header.commandLength < sizeof (ENetProtocolAcknowledge))
  380. return 0;
  381. receivedSentTime = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedSentTime);
  382. if (ENET_TIME_LESS (timeCurrent, receivedSentTime))
  383. return 0;
  384. peer -> lastReceiveTime = timeCurrent;
  385. peer -> earliestTimeout = 0;
  386. roundTripTime = ENET_TIME_DIFFERENCE (timeCurrent, receivedSentTime);
  387. enet_peer_throttle (peer, roundTripTime);
  388. peer -> roundTripTimeVariance -= peer -> roundTripTimeVariance / 4;
  389. if (roundTripTime >= peer -> roundTripTime)
  390. {
  391. peer -> roundTripTime += (roundTripTime - peer -> roundTripTime) / 8;
  392. peer -> roundTripTimeVariance += (roundTripTime - peer -> roundTripTime) / 4;
  393. }
  394. else
  395. {
  396. peer -> roundTripTime -= (peer -> roundTripTime - roundTripTime) / 8;
  397. peer -> roundTripTimeVariance += (peer -> roundTripTime - roundTripTime) / 4;
  398. }
  399. if (peer -> roundTripTime < peer -> lowestRoundTripTime)
  400. peer -> lowestRoundTripTime = peer -> roundTripTime;
  401. if (peer -> roundTripTimeVariance > peer -> highestRoundTripTimeVariance)
  402. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  403. if (peer -> packetThrottleEpoch == 0 ||
  404. ENET_TIME_DIFFERENCE(timeCurrent, peer -> packetThrottleEpoch) >= peer -> packetThrottleInterval)
  405. {
  406. peer -> lastRoundTripTime = peer -> lowestRoundTripTime;
  407. peer -> lastRoundTripTimeVariance = peer -> highestRoundTripTimeVariance;
  408. peer -> lowestRoundTripTime = peer -> roundTripTime;
  409. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  410. peer -> packetThrottleEpoch = timeCurrent;
  411. }
  412. receivedReliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedReliableSequenceNumber);
  413. commandNumber = enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command -> header.channelID);
  414. switch (peer -> state)
  415. {
  416. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  417. if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT)
  418. return 0;
  419. host -> recalculateBandwidthLimits = 1;
  420. peer -> state = ENET_PEER_STATE_CONNECTED;
  421. event -> type = ENET_EVENT_TYPE_CONNECT;
  422. event -> peer = peer;
  423. return 1;
  424. case ENET_PEER_STATE_DISCONNECTING:
  425. if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
  426. return 0;
  427. host -> recalculateBandwidthLimits = 1;
  428. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  429. event -> peer = peer;
  430. enet_peer_reset (peer);
  431. return 1;
  432. default:
  433. break;
  434. }
  435. return 0;
  436. }
  437. static void
  438. enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  439. {
  440. enet_uint16 mtu;
  441. enet_uint32 windowSize;
  442. if (command -> header.commandLength < sizeof (ENetProtocolVerifyConnect) ||
  443. peer -> state != ENET_PEER_STATE_CONNECTING)
  444. return;
  445. if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount ||
  446. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
  447. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
  448. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration)
  449. {
  450. peer -> state = ENET_PEER_STATE_ZOMBIE;
  451. return;
  452. }
  453. peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
  454. mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu);
  455. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  456. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  457. else
  458. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  459. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  460. if (mtu < peer -> mtu)
  461. peer -> mtu = mtu;
  462. windowSize = ENET_NET_TO_HOST_32 (command -> verifyConnect.windowSize);
  463. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  464. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  465. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  466. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  467. if (windowSize < peer -> windowSize)
  468. peer -> windowSize = windowSize;
  469. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.incomingBandwidth);
  470. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.outgoingBandwidth);
  471. host -> recalculateBandwidthLimits = 1;
  472. peer -> state = ENET_PEER_STATE_CONNECTED;
  473. event -> type = ENET_EVENT_TYPE_CONNECT;
  474. event -> peer = peer;
  475. }
  476. static int
  477. enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
  478. {
  479. ENetProtocolHeader * header;
  480. ENetProtocol * command;
  481. ENetPeer * peer;
  482. enet_uint8 * currentData;
  483. size_t commandCount;
  484. if (host -> receivedDataLength < sizeof (ENetProtocolHeader))
  485. return 0;
  486. header = (ENetProtocolHeader *) host -> receivedData;
  487. header -> peerID = ENET_NET_TO_HOST_16 (header -> peerID);
  488. header -> sentTime = ENET_NET_TO_HOST_32 (header -> sentTime);
  489. if (header -> peerID == 0xFFFF)
  490. peer = NULL;
  491. else
  492. if (header -> peerID >= host -> peerCount)
  493. return 0;
  494. else
  495. {
  496. peer = & host -> peers [header -> peerID];
  497. if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
  498. peer -> state == ENET_PEER_STATE_ZOMBIE ||
  499. (host -> receivedAddress.host != peer -> address.host &&
  500. peer -> address.host != ENET_HOST_BROADCAST) ||
  501. header -> challenge != peer -> challenge)
  502. return 0;
  503. else
  504. {
  505. peer -> address.host = host -> receivedAddress.host;
  506. peer -> address.port = host -> receivedAddress.port;
  507. }
  508. }
  509. if (peer != NULL)
  510. peer -> incomingDataTotal += host -> receivedDataLength;
  511. commandCount = header -> commandCount;
  512. currentData = host -> receivedData + sizeof (ENetProtocolHeader);
  513. while (commandCount > 0 &&
  514. currentData < & host -> receivedData [host -> receivedDataLength])
  515. {
  516. command = (ENetProtocol *) currentData;
  517. if (currentData + sizeof (ENetProtocolCommandHeader) > & host -> receivedData [host -> receivedDataLength])
  518. return 0;
  519. command -> header.commandLength = ENET_NET_TO_HOST_32 (command -> header.commandLength);
  520. if (currentData + command -> header.commandLength > & host -> receivedData [host -> receivedDataLength])
  521. return 0;
  522. -- commandCount;
  523. currentData += command -> header.commandLength;
  524. if (peer == NULL)
  525. {
  526. if (command -> header.command != ENET_PROTOCOL_COMMAND_CONNECT)
  527. return 0;
  528. }
  529. command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> header.reliableSequenceNumber);
  530. switch (command -> header.command)
  531. {
  532. case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
  533. enet_protocol_handle_acknowledge (host, event, peer, command);
  534. break;
  535. case ENET_PROTOCOL_COMMAND_CONNECT:
  536. peer = enet_protocol_handle_connect (host, header, command);
  537. break;
  538. case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
  539. enet_protocol_handle_verify_connect (host, event, peer, command);
  540. break;
  541. case ENET_PROTOCOL_COMMAND_DISCONNECT:
  542. enet_protocol_handle_disconnect (host, peer, command);
  543. break;
  544. case ENET_PROTOCOL_COMMAND_PING:
  545. enet_protocol_handle_ping (host, peer, command);
  546. break;
  547. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  548. enet_protocol_handle_send_reliable (host, peer, command);
  549. break;
  550. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  551. enet_protocol_handle_send_unreliable (host, peer, command);
  552. break;
  553. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  554. enet_protocol_handle_send_unsequenced (host, peer, command);
  555. break;
  556. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  557. enet_protocol_handle_send_fragment (host, peer, command);
  558. break;
  559. case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
  560. enet_protocol_handle_bandwidth_limit (host, peer, command);
  561. break;
  562. case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
  563. enet_protocol_handle_throttle_configure (host, peer, command);
  564. break;
  565. default:
  566. break;
  567. }
  568. if (peer != NULL &&
  569. (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE) != 0)
  570. {
  571. switch (peer -> state)
  572. {
  573. case ENET_PEER_STATE_DISCONNECTING:
  574. break;
  575. case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
  576. if (command -> header.command != ENET_PROTOCOL_COMMAND_DISCONNECT)
  577. break;
  578. default:
  579. enet_peer_queue_acknowledgement (peer, command, header -> sentTime);
  580. break;
  581. }
  582. }
  583. }
  584. if (event -> type != ENET_EVENT_TYPE_NONE)
  585. return 1;
  586. return 0;
  587. }
  588. static int
  589. enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
  590. {
  591. for (;;)
  592. {
  593. int receivedLength;
  594. ENetBuffer buffer;
  595. buffer.data = host -> receivedData;
  596. buffer.dataLength = sizeof (host -> receivedData);
  597. receivedLength = enet_socket_receive (host -> socket,
  598. & host -> receivedAddress,
  599. & buffer,
  600. 1);
  601. if (receivedLength < 0)
  602. return -1;
  603. if (receivedLength == 0)
  604. return 0;
  605. host -> receivedDataLength = receivedLength;
  606. switch (enet_protocol_handle_incoming_commands (host, event))
  607. {
  608. case 1:
  609. return 1;
  610. case -1:
  611. return -1;
  612. default:
  613. break;
  614. }
  615. }
  616. return -1;
  617. }
  618. static void
  619. enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
  620. {
  621. ENetProtocol * command = & host -> commands [host -> commandCount];
  622. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  623. ENetAcknowledgement * acknowledgement;
  624. ENetListIterator currentAcknowledgement;
  625. currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
  626. while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
  627. {
  628. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  629. buffer >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  630. peer -> mtu - host -> packetSize < sizeof (ENetProtocolAcknowledge))
  631. break;
  632. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  633. currentAcknowledgement = enet_list_next (currentAcknowledgement);
  634. buffer -> data = command;
  635. buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
  636. host -> packetSize += buffer -> dataLength;
  637. command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  638. command -> header.channelID = acknowledgement -> command.header.channelID;
  639. command -> header.flags = 0;
  640. command -> header.commandLength = ENET_HOST_TO_NET_32 (sizeof (ENetProtocolAcknowledge));
  641. command -> acknowledge.receivedReliableSequenceNumber = ENET_HOST_TO_NET_32 (acknowledgement -> command.header.reliableSequenceNumber);
  642. command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_32 (acknowledgement -> sentTime);
  643. if (acknowledgement -> command.header.command == ENET_PROTOCOL_COMMAND_DISCONNECT)
  644. peer -> state = ENET_PEER_STATE_ZOMBIE;
  645. enet_list_remove (& acknowledgement -> acknowledgementList);
  646. enet_free (acknowledgement);
  647. ++ command;
  648. ++ buffer;
  649. }
  650. host -> commandCount = command - host -> commands;
  651. host -> bufferCount = buffer - host -> buffers;
  652. }
  653. static void
  654. enet_protocol_send_unreliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  655. {
  656. ENetProtocol * command = & host -> commands [host -> commandCount];
  657. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  658. ENetOutgoingCommand * outgoingCommand;
  659. ENetListIterator currentCommand;
  660. currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
  661. while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
  662. {
  663. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  664. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  665. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  666. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength ||
  667. (outgoingCommand -> packet != NULL &&
  668. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength +
  669. outgoingCommand -> packet -> dataLength))
  670. break;
  671. currentCommand = enet_list_next (currentCommand);
  672. if (outgoingCommand -> packet != NULL)
  673. {
  674. peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  675. peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  676. if (peer -> packetThrottleCounter > peer -> packetThrottle)
  677. {
  678. -- outgoingCommand -> packet -> referenceCount;
  679. if (outgoingCommand -> packet -> referenceCount == 0)
  680. enet_packet_destroy (outgoingCommand -> packet);
  681. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  682. enet_free (outgoingCommand);
  683. continue;
  684. }
  685. }
  686. buffer -> data = command;
  687. buffer -> dataLength = outgoingCommand -> command.header.commandLength;
  688. host -> packetSize += buffer -> dataLength;
  689. * command = outgoingCommand -> command;
  690. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  691. if (outgoingCommand -> packet != NULL)
  692. {
  693. ++ buffer;
  694. buffer -> data = outgoingCommand -> packet -> data;
  695. buffer -> dataLength = outgoingCommand -> packet -> dataLength;
  696. command -> header.commandLength += buffer -> dataLength;
  697. host -> packetSize += buffer -> dataLength;
  698. enet_list_insert (enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
  699. }
  700. else
  701. enet_free (outgoingCommand);
  702. command -> header.commandLength = ENET_HOST_TO_NET_32 (command -> header.commandLength);
  703. ++ command;
  704. ++ buffer;
  705. }
  706. host -> commandCount = command - host -> commands;
  707. host -> bufferCount = buffer - host -> buffers;
  708. }
  709. static int
  710. enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  711. {
  712. ENetOutgoingCommand * outgoingCommand;
  713. ENetListIterator currentCommand;
  714. currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  715. while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
  716. {
  717. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  718. currentCommand = enet_list_next (currentCommand);
  719. if (ENET_TIME_DIFFERENCE (timeCurrent, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
  720. continue;
  721. if(peer -> earliestTimeout == 0 ||
  722. ENET_TIME_LESS(outgoingCommand -> sentTime, peer -> earliestTimeout))
  723. peer -> earliestTimeout = outgoingCommand -> sentTime;
  724. if (peer -> earliestTimeout != 0 &&
  725. (ENET_TIME_DIFFERENCE(timeCurrent, peer -> earliestTimeout) >= ENET_PEER_TIMEOUT_MAXIMUM ||
  726. (outgoingCommand -> roundTripTimeout >= outgoingCommand -> roundTripTimeoutLimit &&
  727. ENET_TIME_DIFFERENCE(timeCurrent, peer -> earliestTimeout) >= ENET_PEER_TIMEOUT_MINIMUM)))
  728. {
  729. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  730. event -> peer = peer;
  731. enet_peer_reset (peer);
  732. return 1;
  733. }
  734. if (outgoingCommand -> packet != NULL)
  735. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  736. ++ peer -> packetsLost;
  737. outgoingCommand -> roundTripTimeout *= 2;
  738. enet_list_insert (enet_list_begin (& peer -> outgoingReliableCommands),
  739. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  740. if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
  741. enet_list_empty (& peer -> sentReliableCommands) == 0)
  742. {
  743. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  744. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  745. }
  746. }
  747. return 0;
  748. }
  749. static void
  750. enet_protocol_send_reliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  751. {
  752. ENetProtocol * command = & host -> commands [host -> commandCount];
  753. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  754. ENetOutgoingCommand * outgoingCommand;
  755. ENetListIterator currentCommand;
  756. currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  757. while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
  758. {
  759. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  760. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  761. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  762. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength)
  763. break;
  764. currentCommand = enet_list_next (currentCommand);
  765. if (outgoingCommand -> packet != NULL)
  766. {
  767. if ((enet_uint16) (peer -> mtu - host -> packetSize) <
  768. (enet_uint16) (outgoingCommand -> command.header.commandLength +
  769. outgoingCommand -> fragmentLength) ||
  770. peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > peer -> windowSize)
  771. break;
  772. }
  773. if (outgoingCommand -> roundTripTimeout == 0)
  774. {
  775. outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
  776. outgoingCommand -> roundTripTimeoutLimit = ENET_PEER_TIMEOUT_LIMIT * outgoingCommand -> roundTripTimeout;
  777. }
  778. if (enet_list_empty (& peer -> sentReliableCommands))
  779. peer -> nextTimeout = timeCurrent + outgoingCommand -> roundTripTimeout;
  780. enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
  781. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  782. outgoingCommand -> sentTime = timeCurrent;
  783. buffer -> data = command;
  784. buffer -> dataLength = outgoingCommand -> command.header.commandLength;
  785. host -> packetSize += buffer -> dataLength;
  786. * command = outgoingCommand -> command;
  787. if (outgoingCommand -> packet != NULL)
  788. {
  789. ++ buffer;
  790. buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
  791. buffer -> dataLength = outgoingCommand -> fragmentLength;
  792. command -> header.commandLength += outgoingCommand -> fragmentLength;
  793. host -> packetSize += outgoingCommand -> fragmentLength;
  794. peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
  795. }
  796. command -> header.commandLength = ENET_HOST_TO_NET_32 (command -> header.commandLength);
  797. ++ peer -> packetsSent;
  798. ++ command;
  799. ++ buffer;
  800. }
  801. host -> commandCount = command - host -> commands;
  802. host -> bufferCount = buffer - host -> buffers;
  803. }
  804. static int
  805. enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
  806. {
  807. size_t packetsSent = 1;
  808. ENetProtocolHeader header;
  809. ENetPeer * currentPeer;
  810. int sentLength;
  811. while (packetsSent > 0)
  812. for (currentPeer = host -> peers,
  813. packetsSent = 0;
  814. currentPeer < & host -> peers [host -> peerCount];
  815. ++ currentPeer)
  816. {
  817. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
  818. currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
  819. continue;
  820. host -> commandCount = 0;
  821. host -> bufferCount = 1;
  822. host -> packetSize = sizeof (ENetProtocolHeader);
  823. if (enet_list_empty (& currentPeer -> acknowledgements) == 0)
  824. enet_protocol_send_acknowledgements (host, currentPeer);
  825. if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol))
  826. {
  827. if (checkForTimeouts != 0 &&
  828. enet_list_empty (& currentPeer -> sentReliableCommands) == 0 &&
  829. ENET_TIME_GREATER_EQUAL (timeCurrent, currentPeer -> nextTimeout) &&
  830. enet_protocol_check_timeouts (host, currentPeer, event) == 1)
  831. return 1;
  832. }
  833. if (enet_list_empty (& currentPeer -> outgoingReliableCommands) == 0)
  834. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  835. else
  836. if (enet_list_empty (& currentPeer -> sentReliableCommands) &&
  837. ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL &&
  838. currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
  839. {
  840. enet_peer_ping (currentPeer);
  841. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  842. }
  843. if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol) &&
  844. enet_list_empty (& currentPeer -> outgoingUnreliableCommands) == 0)
  845. enet_protocol_send_unreliable_outgoing_commands (host, currentPeer);
  846. if (host -> commandCount == 0)
  847. continue;
  848. if (currentPeer -> packetLossEpoch == 0)
  849. currentPeer -> packetLossEpoch = timeCurrent;
  850. else
  851. if (ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
  852. currentPeer -> packetsSent > 0)
  853. {
  854. enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
  855. #ifdef ENET_DEBUG
  856. #ifdef WIN32
  857. printf (
  858. #else
  859. fprintf (stderr,
  860. #endif
  861. "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));
  862. #endif
  863. currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;
  864. if (packetLoss >= currentPeer -> packetLoss)
  865. {
  866. currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8;
  867. currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4;
  868. }
  869. else
  870. {
  871. currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8;
  872. currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4;
  873. }
  874. currentPeer -> packetLossEpoch = timeCurrent;
  875. currentPeer -> packetsSent = 0;
  876. currentPeer -> packetsLost = 0;
  877. }
  878. header.peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID);
  879. header.flags = 0;
  880. header.commandCount = host -> commandCount;
  881. header.sentTime = ENET_HOST_TO_NET_32 (timeCurrent);
  882. header.challenge = currentPeer -> challenge;
  883. host -> buffers -> data = & header;
  884. host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
  885. currentPeer -> lastSendTime = timeCurrent;
  886. ++ packetsSent;
  887. sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
  888. enet_protocol_remove_sent_unreliable_commands (currentPeer);
  889. if (sentLength < 0)
  890. return -1;
  891. }
  892. return 0;
  893. }
  894. /** Sends any queued packets on the host specified to its designated peers.
  895. @param host host to flush
  896. @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().
  897. @ingroup host
  898. */
  899. void
  900. enet_host_flush (ENetHost * host)
  901. {
  902. timeCurrent = enet_time_get ();
  903. enet_protocol_send_outgoing_commands (host, NULL, 0);
  904. }
  905. /** Waits for events on the host specified and shuttles packets between
  906. the host and its peers.
  907. @param host host to service
  908. @param event an event structure where event details will be placed if one occurs
  909. @param timeout number of milliseconds that ENet should wait for events
  910. @retval > 0 if an event occurred within the specified time limit
  911. @retval 0 if no event occurred
  912. @retval < 0 on failure
  913. @remarks enet_host_service should be called fairly regularly for adequate performance
  914. @ingroup host
  915. */
  916. int
  917. enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
  918. {
  919. enet_uint32 waitCondition;
  920. event -> type = ENET_EVENT_TYPE_NONE;
  921. event -> peer = NULL;
  922. event -> packet = NULL;
  923. switch (enet_protocol_dispatch_incoming_commands (host, event))
  924. {
  925. case 1:
  926. return 1;
  927. case -1:
  928. perror ("Error dispatching incoming packets");
  929. return -1;
  930. default:
  931. break;
  932. }
  933. timeCurrent = enet_time_get ();
  934. timeout += timeCurrent;
  935. do
  936. {
  937. if (ENET_TIME_DIFFERENCE (timeCurrent, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  938. enet_host_bandwidth_throttle (host);
  939. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  940. {
  941. case 1:
  942. return 1;
  943. case -1:
  944. perror ("Error sending outgoing packets");
  945. return -1;
  946. default:
  947. break;
  948. }
  949. switch (enet_protocol_receive_incoming_commands (host, event))
  950. {
  951. case 1:
  952. return 1;
  953. case -1:
  954. perror ("Error receiving incoming packets");
  955. return -1;
  956. default:
  957. break;
  958. }
  959. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  960. {
  961. case 1:
  962. return 1;
  963. case -1:
  964. perror ("Error sending outgoing packets");
  965. return -1;
  966. default:
  967. break;
  968. }
  969. switch (enet_protocol_dispatch_incoming_commands (host, event))
  970. {
  971. case 1:
  972. return 1;
  973. case -1:
  974. perror ("Error dispatching incoming packets");
  975. return -1;
  976. default:
  977. break;
  978. }
  979. timeCurrent = enet_time_get ();
  980. if (ENET_TIME_GREATER_EQUAL (timeCurrent, timeout))
  981. return 0;
  982. waitCondition = ENET_SOCKET_WAIT_RECEIVE;
  983. if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, timeCurrent)) != 0)
  984. return -1;
  985. timeCurrent = enet_time_get ();
  986. } while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);
  987. return 0;
  988. }