protocol.c 67 KB

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