protocol.c 53 KB

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