protocol.c 70 KB

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