protocol.c 60 KB

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