protocol.c 55 KB

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