2
0

protocol.c 59 KB

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