protocol.c 69 KB

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