protocol.c 55 KB

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