protocol.c 69 KB

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