protocol.c 69 KB

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