protocol.c 55 KB

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