protocol.c 69 KB

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