protocol.c 56 KB

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