protocol.c 68 KB

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