protocol.c 67 KB

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