protocol.c 67 KB

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