protocol.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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 enet_uint32 timeCurrent;
  12. static int
  13. enet_protocol_dispatch_incoming_commands (ENetHost * host, ENetEvent * event)
  14. {
  15. ENetPeer * currentPeer = host -> lastServicedPeer;
  16. ENetChannel * channel;
  17. do
  18. {
  19. ++ currentPeer;
  20. if (currentPeer >= & host -> peers [host -> peerCount])
  21. currentPeer = host -> peers;
  22. if (currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
  23. {
  24. host -> recalculateBandwidthLimits = 1;
  25. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  26. event -> peer = currentPeer;
  27. enet_peer_reset (currentPeer);
  28. host -> lastServicedPeer = currentPeer;
  29. return 1;
  30. }
  31. if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
  32. continue;
  33. for (channel = currentPeer -> channels;
  34. channel < & currentPeer -> channels [currentPeer -> channelCount];
  35. ++ channel)
  36. {
  37. if (enet_list_empty (& channel -> incomingReliableCommands) &&
  38. enet_list_empty (& channel -> incomingUnreliableCommands))
  39. continue;
  40. event -> packet = enet_peer_receive (currentPeer, channel - currentPeer -> channels);
  41. if (event -> packet == NULL)
  42. continue;
  43. event -> type = ENET_EVENT_TYPE_RECEIVE;
  44. event -> peer = currentPeer;
  45. event -> channelID = (enet_uint8) (channel - currentPeer -> channels);
  46. host -> lastServicedPeer = currentPeer;
  47. return 1;
  48. }
  49. } while (currentPeer != host -> lastServicedPeer);
  50. return 0;
  51. }
  52. static void
  53. enet_protocol_remove_sent_unreliable_commands (ENetPeer * peer)
  54. {
  55. ENetOutgoingCommand * outgoingCommand;
  56. while (enet_list_empty (& peer -> sentUnreliableCommands) == 0)
  57. {
  58. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentUnreliableCommands);
  59. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  60. if (outgoingCommand -> packet != NULL)
  61. {
  62. -- outgoingCommand -> packet -> referenceCount;
  63. if (outgoingCommand -> packet -> referenceCount == 0)
  64. enet_packet_destroy (outgoingCommand -> packet);
  65. }
  66. enet_free (outgoingCommand);
  67. }
  68. }
  69. static ENetProtocolCommand
  70. enet_protocol_remove_sent_reliable_command (ENetPeer * peer, enet_uint32 reliableSequenceNumber, enet_uint8 channelID)
  71. {
  72. ENetOutgoingCommand * outgoingCommand;
  73. ENetListIterator currentCommand;
  74. ENetProtocolCommand commandNumber;
  75. for (currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  76. currentCommand != enet_list_end (& peer -> sentReliableCommands);
  77. currentCommand = enet_list_next (currentCommand))
  78. {
  79. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  80. if (outgoingCommand -> reliableSequenceNumber == reliableSequenceNumber &&
  81. outgoingCommand -> command.header.channelID == channelID)
  82. break;
  83. }
  84. if (currentCommand == enet_list_end (& peer -> sentReliableCommands))
  85. return ENET_PROTOCOL_COMMAND_NONE;
  86. commandNumber = outgoingCommand -> command.header.command;
  87. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  88. if (outgoingCommand -> packet != NULL)
  89. {
  90. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  91. -- outgoingCommand -> packet -> referenceCount;
  92. if (outgoingCommand -> packet -> referenceCount == 0)
  93. enet_packet_destroy (outgoingCommand -> packet);
  94. }
  95. enet_free (outgoingCommand);
  96. if (enet_list_empty (& peer -> sentReliableCommands))
  97. return commandNumber;
  98. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentReliableCommands);
  99. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  100. return commandNumber;
  101. }
  102. static ENetPeer *
  103. enet_protocol_handle_connect (ENetHost * host, const ENetProtocolHeader * header, const ENetProtocol * command)
  104. {
  105. enet_uint16 mtu;
  106. enet_uint32 windowSize;
  107. ENetChannel * channel;
  108. size_t channelCount;
  109. ENetPeer * currentPeer;
  110. ENetProtocol verifyCommand;
  111. if (command -> header.commandLength < sizeof (ENetProtocolConnect))
  112. return NULL;
  113. channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount);
  114. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT ||
  115. channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  116. return NULL;
  117. for (currentPeer = host -> peers;
  118. currentPeer < & host -> peers [host -> peerCount];
  119. ++ currentPeer)
  120. {
  121. if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
  122. currentPeer -> address.host == host -> receivedAddress.host &&
  123. currentPeer -> address.port == host -> receivedAddress.port &&
  124. currentPeer -> challenge == header -> challenge)
  125. return NULL;
  126. }
  127. for (currentPeer = host -> peers;
  128. currentPeer < & host -> peers [host -> peerCount];
  129. ++ currentPeer)
  130. {
  131. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
  132. break;
  133. }
  134. if (currentPeer >= & host -> peers [host -> peerCount])
  135. return NULL;
  136. currentPeer -> state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
  137. currentPeer -> challenge = header -> challenge;
  138. currentPeer -> address = host -> receivedAddress;
  139. currentPeer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
  140. currentPeer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.incomingBandwidth);
  141. currentPeer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.outgoingBandwidth);
  142. currentPeer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleInterval);
  143. currentPeer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleAcceleration);
  144. currentPeer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleDeceleration);
  145. currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
  146. currentPeer -> channelCount = channelCount;
  147. for (channel = currentPeer -> channels;
  148. channel < & currentPeer -> channels [channelCount];
  149. ++ channel)
  150. {
  151. channel -> outgoingReliableSequenceNumber = 0;
  152. channel -> outgoingUnreliableSequenceNumber = 0;
  153. channel -> incomingReliableSequenceNumber = 0;
  154. channel -> incomingUnreliableSequenceNumber = 0;
  155. enet_list_clear (& channel -> incomingReliableCommands);
  156. enet_list_clear (& channel -> incomingUnreliableCommands);
  157. }
  158. mtu = ENET_NET_TO_HOST_16 (command -> connect.mtu);
  159. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  160. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  161. else
  162. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  163. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  164. currentPeer -> mtu = mtu;
  165. if (host -> outgoingBandwidth == 0 &&
  166. currentPeer -> incomingBandwidth == 0)
  167. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  168. else
  169. currentPeer -> windowSize = (ENET_MIN (host -> outgoingBandwidth, currentPeer -> incomingBandwidth) /
  170. ENET_PEER_WINDOW_SIZE_SCALE) *
  171. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  172. if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  173. currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  174. else
  175. if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  176. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  177. if (host -> incomingBandwidth == 0)
  178. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  179. else
  180. windowSize = (host -> incomingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE) *
  181. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  182. if (windowSize > ENET_NET_TO_HOST_32 (command -> connect.windowSize))
  183. windowSize = ENET_NET_TO_HOST_32 (command -> connect.windowSize);
  184. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  185. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  186. else
  187. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  188. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  189. verifyCommand.header.command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT;
  190. verifyCommand.header.channelID = 0xFF;
  191. verifyCommand.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE;
  192. verifyCommand.header.commandLength = sizeof (ENetProtocolVerifyConnect);
  193. verifyCommand.verifyConnect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
  194. verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_16 (currentPeer -> mtu);
  195. verifyCommand.verifyConnect.windowSize = ENET_HOST_TO_NET_32 (windowSize);
  196. verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
  197. verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
  198. verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  199. verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
  200. verifyCommand.verifyConnect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
  201. verifyCommand.verifyConnect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
  202. enet_peer_queue_outgoing_command (currentPeer, & verifyCommand, NULL, 0, 0);
  203. return currentPeer;
  204. }
  205. static void
  206. enet_protocol_handle_send_reliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  207. {
  208. ENetPacket * packet;
  209. if (command -> header.commandLength <= sizeof (ENetProtocolSendReliable) ||
  210. command -> header.channelID >= peer -> channelCount ||
  211. peer -> state != ENET_PEER_STATE_CONNECTED)
  212. return;
  213. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendReliable),
  214. command -> header.commandLength - sizeof (ENetProtocolSendReliable),
  215. ENET_PACKET_FLAG_RELIABLE);
  216. enet_peer_queue_incoming_command (peer, command, packet, 0);
  217. }
  218. static void
  219. enet_protocol_handle_send_unsequenced (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  220. {
  221. ENetPacket * packet;
  222. enet_uint32 unsequencedGroup, index;
  223. if (command -> header.commandLength <= sizeof (ENetProtocolSendUnsequenced) ||
  224. command -> header.channelID >= peer -> channelCount ||
  225. peer -> state != ENET_PEER_STATE_CONNECTED)
  226. return;
  227. unsequencedGroup = ENET_NET_TO_HOST_32 (command -> sendUnsequenced.unsequencedGroup);
  228. index = unsequencedGroup % ENET_PEER_UNSEQUENCED_WINDOW_SIZE;
  229. if (unsequencedGroup >= peer -> incomingUnsequencedGroup + ENET_PEER_UNSEQUENCED_WINDOW_SIZE)
  230. {
  231. peer -> incomingUnsequencedGroup = unsequencedGroup - index;
  232. memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
  233. }
  234. else
  235. if (unsequencedGroup < peer -> incomingUnsequencedGroup ||
  236. peer -> unsequencedWindow [index / 32] & (1 << (index % 32)))
  237. return;
  238. peer -> unsequencedWindow [index / 32] |= 1 << (index % 32);
  239. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnsequenced),
  240. command -> header.commandLength - sizeof (ENetProtocolSendUnsequenced),
  241. ENET_PACKET_FLAG_UNSEQUENCED);
  242. enet_peer_queue_incoming_command (peer, command, packet, 0);
  243. }
  244. static void
  245. enet_protocol_handle_send_unreliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  246. {
  247. ENetPacket * packet;
  248. if (command -> header.commandLength <= sizeof (ENetProtocolSendUnreliable) ||
  249. command -> header.channelID >= peer -> channelCount ||
  250. peer -> state != ENET_PEER_STATE_CONNECTED)
  251. return;
  252. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable),
  253. command -> header.commandLength - sizeof (ENetProtocolSendUnreliable),
  254. 0);
  255. enet_peer_queue_incoming_command (peer, command, packet, 0);
  256. }
  257. static void
  258. enet_protocol_handle_send_fragment (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  259. {
  260. enet_uint32 fragmentNumber,
  261. fragmentCount,
  262. fragmentOffset,
  263. fragmentLength,
  264. startSequenceNumber,
  265. totalLength;
  266. ENetChannel * channel;
  267. ENetListIterator currentCommand;
  268. ENetIncomingCommand * startCommand;
  269. if (command -> header.commandLength <= sizeof (ENetProtocolSendFragment) ||
  270. command -> header.channelID >= peer -> channelCount ||
  271. peer -> state != ENET_PEER_STATE_CONNECTED)
  272. return;
  273. startSequenceNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.startSequenceNumber);
  274. fragmentNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentNumber);
  275. fragmentCount = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentCount);
  276. fragmentOffset = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentOffset);
  277. totalLength = ENET_NET_TO_HOST_32 (command -> sendFragment.totalLength);
  278. fragmentLength = command -> header.commandLength - sizeof (ENetProtocolSendFragment);
  279. if (fragmentOffset >= totalLength ||
  280. fragmentOffset + fragmentLength > totalLength ||
  281. fragmentNumber >= fragmentCount)
  282. return;
  283. channel = & peer -> channels [command -> header.channelID];
  284. if (startSequenceNumber <= channel -> incomingReliableSequenceNumber)
  285. return;
  286. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  287. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  288. currentCommand = enet_list_previous (currentCommand))
  289. {
  290. startCommand = (ENetIncomingCommand *) currentCommand;
  291. if (startCommand -> command.header.command == ENET_PROTOCOL_COMMAND_SEND_FRAGMENT &&
  292. startCommand -> command.sendFragment.startSequenceNumber == startSequenceNumber)
  293. break;
  294. }
  295. if (currentCommand == enet_list_end (& channel -> incomingReliableCommands))
  296. {
  297. ENetProtocol hostCommand = * command;
  298. hostCommand.header.reliableSequenceNumber = startSequenceNumber;
  299. hostCommand.sendFragment.startSequenceNumber = startSequenceNumber;
  300. hostCommand.sendFragment.fragmentNumber = fragmentNumber;
  301. hostCommand.sendFragment.fragmentCount = fragmentCount;
  302. hostCommand.sendFragment.fragmentOffset = fragmentOffset;
  303. hostCommand.sendFragment.totalLength = totalLength;
  304. startCommand = enet_peer_queue_incoming_command (peer,
  305. & hostCommand,
  306. enet_packet_create (NULL, totalLength, ENET_PACKET_FLAG_RELIABLE),
  307. fragmentCount);
  308. }
  309. else
  310. if (totalLength != startCommand -> packet -> dataLength ||
  311. fragmentCount != startCommand -> fragmentCount)
  312. return;
  313. if ((startCommand -> fragments [fragmentNumber / 32] & (1 << (fragmentNumber & 32))) == 0)
  314. {
  315. -- startCommand -> fragmentsRemaining;
  316. startCommand -> fragments [fragmentNumber / 32] |= (1 << (fragmentNumber & 32));
  317. if (fragmentOffset + fragmentLength > startCommand -> packet -> dataLength)
  318. fragmentLength = startCommand -> packet -> dataLength - fragmentOffset;
  319. memcpy (startCommand -> packet -> data + fragmentOffset,
  320. (enet_uint8 *) command + sizeof (ENetProtocolSendFragment),
  321. fragmentLength);
  322. }
  323. }
  324. static void
  325. enet_protocol_handle_ping (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  326. {
  327. if (command -> header.commandLength < sizeof (ENetProtocolPing))
  328. return;
  329. }
  330. static void
  331. enet_protocol_handle_bandwidth_limit (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  332. {
  333. if (command -> header.commandLength < sizeof (ENetProtocolBandwidthLimit))
  334. return;
  335. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
  336. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
  337. if (peer -> incomingBandwidth == 0 &&
  338. host -> outgoingBandwidth == 0)
  339. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  340. else
  341. peer -> windowSize = (ENET_MIN (peer -> incomingBandwidth, host -> outgoingBandwidth) /
  342. ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  343. if (peer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  344. peer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  345. else
  346. if (peer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  347. peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  348. }
  349. static void
  350. enet_protocol_handle_throttle_configure (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  351. {
  352. if (command -> header.commandLength < sizeof (ENetProtocolThrottleConfigure))
  353. return;
  354. peer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleInterval);
  355. peer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleAcceleration);
  356. peer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> throttleConfigure.packetThrottleDeceleration);
  357. }
  358. static void
  359. enet_protocol_handle_disconnect (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  360. {
  361. if (command -> header.commandLength < sizeof (ENetProtocolDisconnect))
  362. return;
  363. enet_peer_reset_queues (peer);
  364. if (peer -> state != ENET_PEER_STATE_CONNECTED)
  365. enet_peer_reset (peer);
  366. else
  367. if (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE)
  368. peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT;
  369. else
  370. peer -> state = ENET_PEER_STATE_ZOMBIE;
  371. }
  372. static int
  373. enet_protocol_handle_acknowledge (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  374. {
  375. enet_uint32 roundTripTime,
  376. receivedSentTime,
  377. receivedReliableSequenceNumber;
  378. ENetProtocolCommand commandNumber;
  379. if (command -> header.commandLength < sizeof (ENetProtocolAcknowledge))
  380. return 0;
  381. receivedSentTime = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedSentTime);
  382. if (ENET_TIME_LESS (timeCurrent, receivedSentTime))
  383. return 0;
  384. peer -> lastReceiveTime = timeCurrent;
  385. roundTripTime = ENET_TIME_DIFFERENCE (timeCurrent, receivedSentTime);
  386. enet_peer_throttle (peer, roundTripTime);
  387. peer -> roundTripTimeVariance -= peer -> roundTripTimeVariance / 4;
  388. if (roundTripTime >= peer -> roundTripTime)
  389. {
  390. peer -> roundTripTime += (roundTripTime - peer -> roundTripTime) / 8;
  391. peer -> roundTripTimeVariance += (roundTripTime - peer -> roundTripTime) / 4;
  392. }
  393. else
  394. {
  395. peer -> roundTripTime -= (peer -> roundTripTime - roundTripTime) / 8;
  396. peer -> roundTripTimeVariance += (peer -> roundTripTime - roundTripTime) / 4;
  397. }
  398. if (peer -> roundTripTime < peer -> lowestRoundTripTime)
  399. peer -> lowestRoundTripTime = peer -> roundTripTime;
  400. if (peer -> roundTripTimeVariance > peer -> highestRoundTripTimeVariance)
  401. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  402. if (peer -> packetThrottleEpoch == 0 ||
  403. ENET_TIME_DIFFERENCE(timeCurrent, peer -> packetThrottleEpoch) >= peer -> packetThrottleInterval)
  404. {
  405. peer -> lastRoundTripTime = peer -> lowestRoundTripTime;
  406. peer -> lastRoundTripTimeVariance = peer -> highestRoundTripTimeVariance;
  407. peer -> lowestRoundTripTime = peer -> roundTripTime;
  408. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  409. peer -> packetThrottleEpoch = timeCurrent;
  410. }
  411. receivedReliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> acknowledge.receivedReliableSequenceNumber);
  412. commandNumber = enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command -> header.channelID);
  413. switch (peer -> state)
  414. {
  415. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  416. if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT)
  417. return 0;
  418. host -> recalculateBandwidthLimits = 1;
  419. peer -> state = ENET_PEER_STATE_CONNECTED;
  420. event -> type = ENET_EVENT_TYPE_CONNECT;
  421. event -> peer = peer;
  422. return 1;
  423. case ENET_PEER_STATE_DISCONNECTING:
  424. if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
  425. return 0;
  426. host -> recalculateBandwidthLimits = 1;
  427. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  428. event -> peer = peer;
  429. enet_peer_reset (peer);
  430. return 1;
  431. default:
  432. break;
  433. }
  434. return 0;
  435. }
  436. static void
  437. enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  438. {
  439. enet_uint16 mtu;
  440. enet_uint32 windowSize;
  441. if (command -> header.commandLength < sizeof (ENetProtocolVerifyConnect) ||
  442. peer -> state != ENET_PEER_STATE_CONNECTING)
  443. return;
  444. if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount ||
  445. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
  446. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
  447. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration)
  448. {
  449. peer -> state = ENET_PEER_STATE_ZOMBIE;
  450. return;
  451. }
  452. peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
  453. mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu);
  454. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  455. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  456. else
  457. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  458. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  459. if (mtu < peer -> mtu)
  460. peer -> mtu = mtu;
  461. windowSize = ENET_NET_TO_HOST_32 (command -> verifyConnect.windowSize);
  462. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  463. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  464. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  465. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  466. if (windowSize < peer -> windowSize)
  467. peer -> windowSize = windowSize;
  468. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.incomingBandwidth);
  469. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.outgoingBandwidth);
  470. host -> recalculateBandwidthLimits = 1;
  471. peer -> state = ENET_PEER_STATE_CONNECTED;
  472. event -> type = ENET_EVENT_TYPE_CONNECT;
  473. event -> peer = peer;
  474. }
  475. static int
  476. enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
  477. {
  478. ENetProtocolHeader * header;
  479. ENetProtocol * command;
  480. ENetPeer * peer;
  481. enet_uint8 * currentData;
  482. size_t commandCount;
  483. if (host -> receivedDataLength < sizeof (ENetProtocolHeader))
  484. return 0;
  485. header = (ENetProtocolHeader *) host -> receivedData;
  486. header -> peerID = ENET_NET_TO_HOST_16 (header -> peerID);
  487. header -> sentTime = ENET_NET_TO_HOST_32 (header -> sentTime);
  488. if (header -> peerID == 0xFFFF)
  489. peer = NULL;
  490. else
  491. if (header -> peerID >= host -> peerCount)
  492. return 0;
  493. else
  494. {
  495. peer = & host -> peers [header -> peerID];
  496. if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
  497. peer -> state == ENET_PEER_STATE_ZOMBIE ||
  498. host -> receivedAddress.host != peer -> address.host ||
  499. header -> challenge != peer -> challenge)
  500. return 0;
  501. else
  502. peer -> address.port = host -> receivedAddress.port;
  503. }
  504. if (peer != NULL)
  505. peer -> incomingDataTotal += host -> receivedDataLength;
  506. commandCount = header -> commandCount;
  507. currentData = host -> receivedData + sizeof (ENetProtocolHeader);
  508. while (commandCount > 0 &&
  509. currentData < & host -> receivedData [host -> receivedDataLength])
  510. {
  511. command = (ENetProtocol *) currentData;
  512. if (currentData + sizeof (ENetProtocolCommandHeader) > & host -> receivedData [host -> receivedDataLength])
  513. return 0;
  514. command -> header.commandLength = ENET_NET_TO_HOST_32 (command -> header.commandLength);
  515. if (currentData + command -> header.commandLength > & host -> receivedData [host -> receivedDataLength])
  516. return 0;
  517. -- commandCount;
  518. currentData += command -> header.commandLength;
  519. if (peer == NULL)
  520. {
  521. if (command -> header.command != ENET_PROTOCOL_COMMAND_CONNECT)
  522. return 0;
  523. }
  524. command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_32 (command -> header.reliableSequenceNumber);
  525. switch (command -> header.command)
  526. {
  527. case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
  528. enet_protocol_handle_acknowledge (host, event, peer, command);
  529. break;
  530. case ENET_PROTOCOL_COMMAND_CONNECT:
  531. peer = enet_protocol_handle_connect (host, header, command);
  532. break;
  533. case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
  534. enet_protocol_handle_verify_connect (host, event, peer, command);
  535. break;
  536. case ENET_PROTOCOL_COMMAND_DISCONNECT:
  537. enet_protocol_handle_disconnect (host, peer, command);
  538. break;
  539. case ENET_PROTOCOL_COMMAND_PING:
  540. enet_protocol_handle_ping (host, peer, command);
  541. break;
  542. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  543. enet_protocol_handle_send_reliable (host, peer, command);
  544. break;
  545. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  546. enet_protocol_handle_send_unreliable (host, peer, command);
  547. break;
  548. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  549. enet_protocol_handle_send_unsequenced (host, peer, command);
  550. break;
  551. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  552. enet_protocol_handle_send_fragment (host, peer, command);
  553. break;
  554. case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
  555. enet_protocol_handle_bandwidth_limit (host, peer, command);
  556. break;
  557. case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
  558. enet_protocol_handle_throttle_configure (host, peer, command);
  559. break;
  560. default:
  561. break;
  562. }
  563. if (peer != NULL &&
  564. (command -> header.flags & ENET_PROTOCOL_FLAG_ACKNOWLEDGE) != 0)
  565. {
  566. switch (peer -> state)
  567. {
  568. case ENET_PEER_STATE_DISCONNECTING:
  569. break;
  570. case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
  571. if (command -> header.command != ENET_PROTOCOL_COMMAND_DISCONNECT)
  572. break;
  573. default:
  574. enet_peer_queue_acknowledgement (peer, command, header -> sentTime);
  575. break;
  576. }
  577. }
  578. }
  579. if (event -> type != ENET_EVENT_TYPE_NONE)
  580. return 1;
  581. return 0;
  582. }
  583. static int
  584. enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
  585. {
  586. for (;;)
  587. {
  588. int receivedLength;
  589. ENetBuffer buffer;
  590. buffer.data = host -> receivedData;
  591. buffer.dataLength = sizeof (host -> receivedData);
  592. receivedLength = enet_socket_receive (host -> socket,
  593. & host -> receivedAddress,
  594. & buffer,
  595. 1);
  596. if (receivedLength < 0)
  597. return -1;
  598. if (receivedLength == 0)
  599. return 0;
  600. host -> receivedDataLength = receivedLength;
  601. switch (enet_protocol_handle_incoming_commands (host, event))
  602. {
  603. case 1:
  604. return 1;
  605. case -1:
  606. return -1;
  607. default:
  608. break;
  609. }
  610. }
  611. return -1;
  612. }
  613. static void
  614. enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
  615. {
  616. ENetProtocol * command = & host -> commands [host -> commandCount];
  617. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  618. ENetAcknowledgement * acknowledgement;
  619. ENetListIterator currentAcknowledgement;
  620. currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
  621. while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
  622. {
  623. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  624. buffer >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  625. peer -> mtu - host -> packetSize < sizeof (ENetProtocolAcknowledge))
  626. break;
  627. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  628. currentAcknowledgement = enet_list_next (currentAcknowledgement);
  629. buffer -> data = command;
  630. buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
  631. host -> packetSize += buffer -> dataLength;
  632. command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  633. command -> header.channelID = acknowledgement -> command.header.channelID;
  634. command -> header.flags = 0;
  635. command -> header.commandLength = ENET_HOST_TO_NET_32 (sizeof (ENetProtocolAcknowledge));
  636. command -> acknowledge.receivedReliableSequenceNumber = ENET_HOST_TO_NET_32 (acknowledgement -> command.header.reliableSequenceNumber);
  637. command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_32 (acknowledgement -> sentTime);
  638. if (acknowledgement -> command.header.command == ENET_PROTOCOL_COMMAND_DISCONNECT)
  639. peer -> state = ENET_PEER_STATE_ZOMBIE;
  640. enet_list_remove (& acknowledgement -> acknowledgementList);
  641. enet_free (acknowledgement);
  642. ++ command;
  643. ++ buffer;
  644. }
  645. host -> commandCount = command - host -> commands;
  646. host -> bufferCount = buffer - host -> buffers;
  647. }
  648. static void
  649. enet_protocol_send_unreliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  650. {
  651. ENetProtocol * command = & host -> commands [host -> commandCount];
  652. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  653. ENetOutgoingCommand * outgoingCommand;
  654. ENetListIterator currentCommand;
  655. currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
  656. while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
  657. {
  658. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  659. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  660. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  661. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength ||
  662. (outgoingCommand -> packet != NULL &&
  663. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength +
  664. outgoingCommand -> packet -> dataLength))
  665. break;
  666. currentCommand = enet_list_next (currentCommand);
  667. if (outgoingCommand -> packet != NULL)
  668. {
  669. peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  670. peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  671. if (peer -> packetThrottleCounter > peer -> packetThrottle)
  672. {
  673. -- outgoingCommand -> packet -> referenceCount;
  674. if (outgoingCommand -> packet -> referenceCount == 0)
  675. enet_packet_destroy (outgoingCommand -> packet);
  676. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  677. enet_free (outgoingCommand);
  678. continue;
  679. }
  680. }
  681. buffer -> data = command;
  682. buffer -> dataLength = outgoingCommand -> command.header.commandLength;
  683. host -> packetSize += buffer -> dataLength;
  684. * command = outgoingCommand -> command;
  685. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  686. if (outgoingCommand -> packet != NULL)
  687. {
  688. ++ buffer;
  689. buffer -> data = outgoingCommand -> packet -> data;
  690. buffer -> dataLength = outgoingCommand -> packet -> dataLength;
  691. command -> header.commandLength += buffer -> dataLength;
  692. host -> packetSize += buffer -> dataLength;
  693. enet_list_insert (enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
  694. }
  695. else
  696. enet_free (outgoingCommand);
  697. command -> header.commandLength = ENET_HOST_TO_NET_32 (command -> header.commandLength);
  698. ++ command;
  699. ++ buffer;
  700. }
  701. host -> commandCount = command - host -> commands;
  702. host -> bufferCount = buffer - host -> buffers;
  703. }
  704. static int
  705. enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  706. {
  707. ENetOutgoingCommand * outgoingCommand;
  708. ENetListIterator currentCommand;
  709. currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  710. while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
  711. {
  712. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  713. currentCommand = enet_list_next (currentCommand);
  714. if (ENET_TIME_DIFFERENCE (timeCurrent, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
  715. continue;
  716. if (outgoingCommand -> roundTripTimeout >= outgoingCommand -> roundTripTimeoutLimit)
  717. {
  718. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  719. event -> peer = peer;
  720. enet_peer_reset (peer);
  721. return 1;
  722. }
  723. if (outgoingCommand -> packet != NULL)
  724. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  725. ++ peer -> packetsLost;
  726. outgoingCommand -> roundTripTimeout *= 2;
  727. enet_list_insert (enet_list_begin (& peer -> outgoingReliableCommands),
  728. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  729. if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
  730. enet_list_empty (& peer -> sentReliableCommands) == 0)
  731. {
  732. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  733. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  734. }
  735. }
  736. return 0;
  737. }
  738. static void
  739. enet_protocol_send_reliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  740. {
  741. ENetProtocol * command = & host -> commands [host -> commandCount];
  742. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  743. ENetOutgoingCommand * outgoingCommand;
  744. ENetListIterator currentCommand;
  745. currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  746. while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
  747. {
  748. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  749. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  750. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  751. peer -> mtu - host -> packetSize < outgoingCommand -> command.header.commandLength)
  752. break;
  753. currentCommand = enet_list_next (currentCommand);
  754. if (outgoingCommand -> packet != NULL)
  755. {
  756. if ((enet_uint16) (peer -> mtu - host -> packetSize) <
  757. (enet_uint16) (outgoingCommand -> command.header.commandLength +
  758. outgoingCommand -> fragmentLength) ||
  759. peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > peer -> windowSize)
  760. break;
  761. }
  762. if (outgoingCommand -> roundTripTimeout == 0)
  763. {
  764. outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
  765. outgoingCommand -> roundTripTimeoutLimit = ENET_PEER_TIMEOUT_LIMIT * outgoingCommand -> roundTripTimeout;
  766. }
  767. if (enet_list_empty (& peer -> sentReliableCommands))
  768. peer -> nextTimeout = timeCurrent + outgoingCommand -> roundTripTimeout;
  769. enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
  770. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  771. outgoingCommand -> sentTime = timeCurrent;
  772. buffer -> data = command;
  773. buffer -> dataLength = outgoingCommand -> command.header.commandLength;
  774. host -> packetSize += buffer -> dataLength;
  775. * command = outgoingCommand -> command;
  776. if (outgoingCommand -> packet != NULL)
  777. {
  778. ++ buffer;
  779. buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
  780. buffer -> dataLength = outgoingCommand -> fragmentLength;
  781. command -> header.commandLength += outgoingCommand -> fragmentLength;
  782. host -> packetSize += outgoingCommand -> fragmentLength;
  783. peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
  784. }
  785. command -> header.commandLength = ENET_HOST_TO_NET_32 (command -> header.commandLength);
  786. ++ peer -> packetsSent;
  787. ++ command;
  788. ++ buffer;
  789. }
  790. host -> commandCount = command - host -> commands;
  791. host -> bufferCount = buffer - host -> buffers;
  792. }
  793. static int
  794. enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
  795. {
  796. size_t packetsSent = 1;
  797. ENetProtocolHeader header;
  798. ENetPeer * currentPeer;
  799. int sentLength;
  800. while (packetsSent > 0)
  801. for (currentPeer = host -> peers,
  802. packetsSent = 0;
  803. currentPeer < & host -> peers [host -> peerCount];
  804. ++ currentPeer)
  805. {
  806. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
  807. currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
  808. continue;
  809. host -> commandCount = 0;
  810. host -> bufferCount = 1;
  811. host -> packetSize = sizeof (ENetProtocolHeader);
  812. if (enet_list_empty (& currentPeer -> acknowledgements) == 0)
  813. enet_protocol_send_acknowledgements (host, currentPeer);
  814. if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol))
  815. {
  816. if (checkForTimeouts != 0 &&
  817. enet_list_empty (& currentPeer -> sentReliableCommands) == 0 &&
  818. ENET_TIME_GREATER_EQUAL (timeCurrent, currentPeer -> nextTimeout) &&
  819. enet_protocol_check_timeouts (host, currentPeer, event) == 1)
  820. return 1;
  821. }
  822. if (enet_list_empty (& currentPeer -> outgoingReliableCommands) == 0)
  823. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  824. else
  825. if (enet_list_empty (& currentPeer -> sentReliableCommands) &&
  826. ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL &&
  827. currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
  828. {
  829. enet_peer_ping (currentPeer);
  830. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  831. }
  832. if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol) &&
  833. enet_list_empty (& currentPeer -> outgoingUnreliableCommands) == 0)
  834. enet_protocol_send_unreliable_outgoing_commands (host, currentPeer);
  835. if (host -> commandCount == 0)
  836. continue;
  837. if (currentPeer -> packetLossEpoch == 0)
  838. currentPeer -> packetLossEpoch = timeCurrent;
  839. else
  840. if (ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
  841. currentPeer -> packetsSent > 0)
  842. {
  843. enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
  844. #ifdef ENET_DEBUG
  845. #ifdef WIN32
  846. printf ("peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands));
  847. #else
  848. fprintf (stderr, "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, enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands));
  849. #endif
  850. #endif
  851. currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;
  852. if (packetLoss >= currentPeer -> packetLoss)
  853. {
  854. currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8;
  855. currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4;
  856. }
  857. else
  858. {
  859. currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8;
  860. currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4;
  861. }
  862. currentPeer -> packetLossEpoch = timeCurrent;
  863. currentPeer -> packetsSent = 0;
  864. currentPeer -> packetsLost = 0;
  865. }
  866. header.peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID);
  867. header.flags = 0;
  868. header.commandCount = host -> commandCount;
  869. header.sentTime = ENET_HOST_TO_NET_32 (timeCurrent);
  870. header.challenge = currentPeer -> challenge;
  871. host -> buffers -> data = & header;
  872. host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
  873. currentPeer -> lastSendTime = timeCurrent;
  874. ++ packetsSent;
  875. sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
  876. enet_protocol_remove_sent_unreliable_commands (currentPeer);
  877. if (sentLength < 0)
  878. return -1;
  879. }
  880. return 0;
  881. }
  882. /** Sends any queued packets on the host specified to its designated peers.
  883. @param host host to flush
  884. @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().
  885. @ingroup host
  886. */
  887. void
  888. enet_host_flush (ENetHost * host)
  889. {
  890. timeCurrent = enet_time_get ();
  891. enet_protocol_send_outgoing_commands (host, NULL, 0);
  892. }
  893. /** Waits for events on the host specified and shuttles packets between
  894. the host and its peers.
  895. @param host host to service
  896. @param event an event structure where event details will be placed if one occurs
  897. @param timeout number of milliseconds that ENet should wait for events
  898. @retval > 0 if an event occurred within the specified time limit
  899. @retval 0 if no event occurred
  900. @retval < 0 on failure
  901. @remarks enet_host_service should be called fairly regularly for adequate performance
  902. @ingroup host
  903. */
  904. int
  905. enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
  906. {
  907. enet_uint32 waitCondition;
  908. event -> type = ENET_EVENT_TYPE_NONE;
  909. event -> peer = NULL;
  910. event -> packet = NULL;
  911. switch (enet_protocol_dispatch_incoming_commands (host, event))
  912. {
  913. case 1:
  914. return 1;
  915. case -1:
  916. perror ("Error dispatching incoming packets");
  917. return -1;
  918. default:
  919. break;
  920. }
  921. timeCurrent = enet_time_get ();
  922. timeout += timeCurrent;
  923. do
  924. {
  925. if (ENET_TIME_DIFFERENCE (timeCurrent, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  926. enet_host_bandwidth_throttle (host);
  927. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  928. {
  929. case 1:
  930. return 1;
  931. case -1:
  932. perror ("Error sending outgoing packets");
  933. return -1;
  934. default:
  935. break;
  936. }
  937. switch (enet_protocol_receive_incoming_commands (host, event))
  938. {
  939. case 1:
  940. return 1;
  941. case -1:
  942. perror ("Error receiving incoming packets");
  943. return -1;
  944. default:
  945. break;
  946. }
  947. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  948. {
  949. case 1:
  950. return 1;
  951. case -1:
  952. perror ("Error sending outgoing packets");
  953. return -1;
  954. default:
  955. break;
  956. }
  957. switch (enet_protocol_dispatch_incoming_commands (host, event))
  958. {
  959. case 1:
  960. return 1;
  961. case -1:
  962. perror ("Error dispatching incoming packets");
  963. return -1;
  964. default:
  965. break;
  966. }
  967. timeCurrent = enet_time_get ();
  968. if (ENET_TIME_GREATER_EQUAL (timeCurrent, timeout))
  969. return 0;
  970. waitCondition = ENET_SOCKET_WAIT_RECEIVE;
  971. if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, timeCurrent)) != 0)
  972. return -1;
  973. timeCurrent = enet_time_get ();
  974. } while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);
  975. return 0;
  976. }