protocol.c 61 KB

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