protocol.c 61 KB

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