protocol.c 54 KB

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