protocol.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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 = 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. enet_peer_reset_queues (peer);
  504. if (peer -> state == ENET_PEER_STATE_CONNECTION_SUCCEEDED)
  505. peer -> state = ENET_PEER_STATE_ZOMBIE;
  506. else
  507. if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
  508. {
  509. if (peer -> state == ENET_PEER_STATE_CONNECTION_PENDING) host -> recalculateBandwidthLimits = 1;
  510. enet_peer_reset (peer);
  511. }
  512. else
  513. if (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)
  514. peer -> state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT;
  515. else
  516. peer -> state = ENET_PEER_STATE_ZOMBIE;
  517. peer -> disconnectData = ENET_NET_TO_HOST_32 (command -> disconnect.data);
  518. return 0;
  519. }
  520. static int
  521. enet_protocol_handle_acknowledge (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  522. {
  523. enet_uint32 roundTripTime,
  524. receivedSentTime,
  525. receivedReliableSequenceNumber;
  526. ENetProtocolCommand commandNumber;
  527. receivedSentTime = ENET_NET_TO_HOST_16 (command -> acknowledge.receivedSentTime);
  528. receivedSentTime |= host -> serviceTime & 0xFFFF0000;
  529. if ((receivedSentTime & 0x8000) > (host -> serviceTime & 0x8000))
  530. receivedSentTime -= 0x10000;
  531. if (ENET_TIME_LESS (host -> serviceTime, receivedSentTime))
  532. return 0;
  533. peer -> lastReceiveTime = host -> serviceTime;
  534. peer -> earliestTimeout = 0;
  535. roundTripTime = ENET_TIME_DIFFERENCE (host -> serviceTime, receivedSentTime);
  536. enet_peer_throttle (peer, roundTripTime);
  537. peer -> roundTripTimeVariance -= peer -> roundTripTimeVariance / 4;
  538. if (roundTripTime >= peer -> roundTripTime)
  539. {
  540. peer -> roundTripTime += (roundTripTime - peer -> roundTripTime) / 8;
  541. peer -> roundTripTimeVariance += (roundTripTime - peer -> roundTripTime) / 4;
  542. }
  543. else
  544. {
  545. peer -> roundTripTime -= (peer -> roundTripTime - roundTripTime) / 8;
  546. peer -> roundTripTimeVariance += (peer -> roundTripTime - roundTripTime) / 4;
  547. }
  548. if (peer -> roundTripTime < peer -> lowestRoundTripTime)
  549. peer -> lowestRoundTripTime = peer -> roundTripTime;
  550. if (peer -> roundTripTimeVariance > peer -> highestRoundTripTimeVariance)
  551. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  552. if (peer -> packetThrottleEpoch == 0 ||
  553. ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> packetThrottleEpoch) >= peer -> packetThrottleInterval)
  554. {
  555. peer -> lastRoundTripTime = peer -> lowestRoundTripTime;
  556. peer -> lastRoundTripTimeVariance = peer -> highestRoundTripTimeVariance;
  557. peer -> lowestRoundTripTime = peer -> roundTripTime;
  558. peer -> highestRoundTripTimeVariance = peer -> roundTripTimeVariance;
  559. peer -> packetThrottleEpoch = host -> serviceTime;
  560. }
  561. receivedReliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> acknowledge.receivedReliableSequenceNumber);
  562. commandNumber = enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command -> header.channelID);
  563. switch (peer -> state)
  564. {
  565. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  566. if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT)
  567. return -1;
  568. enet_protocol_notify_connect (host, peer, event);
  569. break;
  570. case ENET_PEER_STATE_DISCONNECTING:
  571. if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT)
  572. return -1;
  573. enet_protocol_notify_disconnect (host, peer, event);
  574. break;
  575. case ENET_PEER_STATE_DISCONNECT_LATER:
  576. if (enet_list_empty (& peer -> outgoingReliableCommands) &&
  577. enet_list_empty (& peer -> outgoingUnreliableCommands) &&
  578. enet_list_empty (& peer -> sentReliableCommands))
  579. enet_peer_disconnect (peer, peer -> disconnectData);
  580. break;
  581. }
  582. return 0;
  583. }
  584. static int
  585. enet_protocol_handle_verify_connect (ENetHost * host, ENetEvent * event, ENetPeer * peer, const ENetProtocol * command)
  586. {
  587. enet_uint16 mtu;
  588. enet_uint32 windowSize;
  589. if (peer -> state != ENET_PEER_STATE_CONNECTING)
  590. return -1;
  591. if (ENET_NET_TO_HOST_32 (command -> verifyConnect.channelCount) != peer -> channelCount ||
  592. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleInterval) != peer -> packetThrottleInterval ||
  593. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleAcceleration) != peer -> packetThrottleAcceleration ||
  594. ENET_NET_TO_HOST_32 (command -> verifyConnect.packetThrottleDeceleration) != peer -> packetThrottleDeceleration)
  595. {
  596. peer -> state = ENET_PEER_STATE_ZOMBIE;
  597. return -1;
  598. }
  599. enet_protocol_remove_sent_reliable_command (peer, 1, 0xFF);
  600. peer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> verifyConnect.outgoingPeerID);
  601. mtu = ENET_NET_TO_HOST_16 (command -> verifyConnect.mtu);
  602. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  603. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  604. else
  605. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  606. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  607. if (mtu < peer -> mtu)
  608. peer -> mtu = mtu;
  609. windowSize = ENET_NET_TO_HOST_32 (command -> verifyConnect.windowSize);
  610. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  611. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  612. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  613. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  614. if (windowSize < peer -> windowSize)
  615. peer -> windowSize = windowSize;
  616. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.incomingBandwidth);
  617. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> verifyConnect.outgoingBandwidth);
  618. enet_protocol_notify_connect (host, peer, event);
  619. return 0;
  620. }
  621. static int
  622. enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
  623. {
  624. ENetProtocolHeader * header;
  625. ENetProtocol * command;
  626. ENetPeer * peer;
  627. enet_uint8 * currentData;
  628. size_t headerSize;
  629. enet_uint16 peerID, flags;
  630. if (host -> receivedDataLength < sizeof (ENetProtocolHeader))
  631. return 0;
  632. header = (ENetProtocolHeader *) host -> receivedData;
  633. peerID = ENET_NET_TO_HOST_16 (header -> peerID);
  634. flags = peerID & ENET_PROTOCOL_HEADER_FLAG_MASK;
  635. peerID &= ~ ENET_PROTOCOL_HEADER_FLAG_MASK;
  636. if (peerID == ENET_PROTOCOL_MAXIMUM_PEER_ID)
  637. peer = NULL;
  638. else
  639. if (peerID >= host -> peerCount)
  640. return 0;
  641. else
  642. {
  643. peer = & host -> peers [peerID];
  644. if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
  645. peer -> state == ENET_PEER_STATE_ZOMBIE ||
  646. (host -> receivedAddress.host != peer -> address.host &&
  647. peer -> address.host != ENET_HOST_BROADCAST))
  648. return 0;
  649. #ifdef USE_CRC32
  650. {
  651. enet_uint32 crc = header -> checksum;
  652. ENetBuffer buffer;
  653. header -> checksum = peer -> sessionID;
  654. buffer.data = host -> receivedData;
  655. buffer.dataLength = host -> receivedDataLength;
  656. if (enet_crc32 (& buffer, 1) != crc)
  657. return 0;
  658. }
  659. #else
  660. if (header -> checksum != peer -> sessionID)
  661. return 0;
  662. #endif
  663. peer -> address.host = host -> receivedAddress.host;
  664. peer -> address.port = host -> receivedAddress.port;
  665. peer -> incomingDataTotal += host -> receivedDataLength;
  666. }
  667. headerSize = (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ? sizeof (ENetProtocolHeader) : (size_t) & ((ENetProtocolHeader *) 0) -> sentTime);
  668. currentData = host -> receivedData + headerSize;
  669. while (currentData < & host -> receivedData [host -> receivedDataLength])
  670. {
  671. enet_uint8 commandNumber;
  672. size_t commandSize;
  673. command = (ENetProtocol *) currentData;
  674. if (currentData + sizeof (ENetProtocolCommandHeader) > & host -> receivedData [host -> receivedDataLength])
  675. break;
  676. commandNumber = command -> header.command & ENET_PROTOCOL_COMMAND_MASK;
  677. if (commandNumber >= ENET_PROTOCOL_COMMAND_COUNT)
  678. break;
  679. commandSize = commandSizes [commandNumber];
  680. if (commandSize == 0 || currentData + commandSize > & host -> receivedData [host -> receivedDataLength])
  681. break;
  682. currentData += commandSize;
  683. if (peer == NULL && commandNumber != ENET_PROTOCOL_COMMAND_CONNECT)
  684. break;
  685. command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);
  686. switch (command -> header.command & ENET_PROTOCOL_COMMAND_MASK)
  687. {
  688. case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
  689. if (enet_protocol_handle_acknowledge (host, event, peer, command))
  690. goto commandError;
  691. break;
  692. case ENET_PROTOCOL_COMMAND_CONNECT:
  693. peer = enet_protocol_handle_connect (host, header, command);
  694. if (peer == NULL)
  695. goto commandError;
  696. break;
  697. case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
  698. if (enet_protocol_handle_verify_connect (host, event, peer, command))
  699. goto commandError;
  700. break;
  701. case ENET_PROTOCOL_COMMAND_DISCONNECT:
  702. if (enet_protocol_handle_disconnect (host, peer, command))
  703. goto commandError;
  704. break;
  705. case ENET_PROTOCOL_COMMAND_PING:
  706. if (enet_protocol_handle_ping (host, peer, command))
  707. goto commandError;
  708. break;
  709. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  710. if (enet_protocol_handle_send_reliable (host, peer, command, & currentData))
  711. goto commandError;
  712. break;
  713. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  714. if (enet_protocol_handle_send_unreliable (host, peer, command, & currentData))
  715. goto commandError;
  716. break;
  717. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  718. if (enet_protocol_handle_send_unsequenced (host, peer, command, & currentData))
  719. goto commandError;
  720. break;
  721. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  722. if (enet_protocol_handle_send_fragment (host, peer, command, & currentData))
  723. goto commandError;
  724. break;
  725. case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
  726. if (enet_protocol_handle_bandwidth_limit (host, peer, command))
  727. goto commandError;
  728. break;
  729. case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
  730. if (enet_protocol_handle_throttle_configure (host, peer, command))
  731. goto commandError;
  732. break;
  733. default:
  734. goto commandError;
  735. }
  736. if (peer != NULL &&
  737. (command -> header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0)
  738. {
  739. enet_uint16 sentTime;
  740. if (! (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME))
  741. break;
  742. sentTime = ENET_NET_TO_HOST_16 (header -> sentTime);
  743. switch (peer -> state)
  744. {
  745. case ENET_PEER_STATE_DISCONNECTING:
  746. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  747. break;
  748. case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
  749. if ((command -> header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
  750. enet_peer_queue_acknowledgement (peer, command, sentTime);
  751. break;
  752. default:
  753. enet_peer_queue_acknowledgement (peer, command, sentTime);
  754. break;
  755. }
  756. }
  757. }
  758. commandError:
  759. if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
  760. return 1;
  761. return 0;
  762. }
  763. static int
  764. enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
  765. {
  766. for (;;)
  767. {
  768. int receivedLength;
  769. ENetBuffer buffer;
  770. buffer.data = host -> receivedData;
  771. buffer.dataLength = sizeof (host -> receivedData);
  772. receivedLength = enet_socket_receive (host -> socket,
  773. & host -> receivedAddress,
  774. & buffer,
  775. 1);
  776. if (receivedLength < 0)
  777. return -1;
  778. if (receivedLength == 0)
  779. return 0;
  780. host -> receivedDataLength = receivedLength;
  781. switch (enet_protocol_handle_incoming_commands (host, event))
  782. {
  783. case 1:
  784. return 1;
  785. case -1:
  786. return -1;
  787. default:
  788. break;
  789. }
  790. }
  791. return -1;
  792. }
  793. static void
  794. enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
  795. {
  796. ENetProtocol * command = & host -> commands [host -> commandCount];
  797. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  798. ENetAcknowledgement * acknowledgement;
  799. ENetListIterator currentAcknowledgement;
  800. currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
  801. while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
  802. {
  803. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  804. buffer >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  805. peer -> mtu - host -> packetSize < sizeof (ENetProtocolAcknowledge))
  806. {
  807. host -> continueSending = 1;
  808. break;
  809. }
  810. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  811. currentAcknowledgement = enet_list_next (currentAcknowledgement);
  812. buffer -> data = command;
  813. buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
  814. host -> packetSize += buffer -> dataLength;
  815. command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  816. command -> header.channelID = acknowledgement -> command.header.channelID;
  817. command -> acknowledge.receivedReliableSequenceNumber = ENET_HOST_TO_NET_16 (acknowledgement -> command.header.reliableSequenceNumber);
  818. command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_16 (acknowledgement -> sentTime);
  819. if ((acknowledgement -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
  820. peer -> state = ENET_PEER_STATE_ZOMBIE;
  821. enet_list_remove (& acknowledgement -> acknowledgementList);
  822. enet_free (acknowledgement);
  823. ++ command;
  824. ++ buffer;
  825. }
  826. host -> commandCount = command - host -> commands;
  827. host -> bufferCount = buffer - host -> buffers;
  828. }
  829. static void
  830. enet_protocol_send_unreliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  831. {
  832. ENetProtocol * command = & host -> commands [host -> commandCount];
  833. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  834. ENetOutgoingCommand * outgoingCommand;
  835. ENetListIterator currentCommand;
  836. currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
  837. while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
  838. {
  839. size_t commandSize;
  840. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  841. commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
  842. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  843. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  844. peer -> mtu - host -> packetSize < commandSize ||
  845. (outgoingCommand -> packet != NULL &&
  846. peer -> mtu - host -> packetSize < commandSize + outgoingCommand -> packet -> dataLength))
  847. {
  848. host -> continueSending = 1;
  849. break;
  850. }
  851. currentCommand = enet_list_next (currentCommand);
  852. if (outgoingCommand -> packet != NULL)
  853. {
  854. peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  855. peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  856. if (peer -> packetThrottleCounter > peer -> packetThrottle)
  857. {
  858. -- outgoingCommand -> packet -> referenceCount;
  859. if (outgoingCommand -> packet -> referenceCount == 0)
  860. enet_packet_destroy (outgoingCommand -> packet);
  861. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  862. enet_free (outgoingCommand);
  863. continue;
  864. }
  865. }
  866. buffer -> data = command;
  867. buffer -> dataLength = commandSize;
  868. host -> packetSize += buffer -> dataLength;
  869. * command = outgoingCommand -> command;
  870. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  871. if (outgoingCommand -> packet != NULL)
  872. {
  873. ++ buffer;
  874. buffer -> data = outgoingCommand -> packet -> data;
  875. buffer -> dataLength = outgoingCommand -> packet -> dataLength;
  876. host -> packetSize += buffer -> dataLength;
  877. enet_list_insert (enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
  878. }
  879. else
  880. enet_free (outgoingCommand);
  881. ++ command;
  882. ++ buffer;
  883. }
  884. host -> commandCount = command - host -> commands;
  885. host -> bufferCount = buffer - host -> buffers;
  886. if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER &&
  887. enet_list_empty (& peer -> outgoingReliableCommands) &&
  888. enet_list_empty (& peer -> outgoingUnreliableCommands) &&
  889. enet_list_empty (& peer -> sentReliableCommands))
  890. enet_peer_disconnect (peer, peer -> disconnectData);
  891. }
  892. static int
  893. enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  894. {
  895. ENetOutgoingCommand * outgoingCommand;
  896. ENetListIterator currentCommand, insertPosition;
  897. currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  898. insertPosition = enet_list_begin (& peer -> outgoingReliableCommands);
  899. while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
  900. {
  901. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  902. currentCommand = enet_list_next (currentCommand);
  903. if (ENET_TIME_DIFFERENCE (host -> serviceTime, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
  904. continue;
  905. if (peer -> earliestTimeout == 0 ||
  906. ENET_TIME_LESS (outgoingCommand -> sentTime, peer -> earliestTimeout))
  907. peer -> earliestTimeout = outgoingCommand -> sentTime;
  908. if (peer -> earliestTimeout != 0 &&
  909. (ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= ENET_PEER_TIMEOUT_MAXIMUM ||
  910. (outgoingCommand -> roundTripTimeout >= outgoingCommand -> roundTripTimeoutLimit &&
  911. ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= ENET_PEER_TIMEOUT_MINIMUM)))
  912. {
  913. enet_protocol_notify_disconnect (host, peer, event);
  914. return 1;
  915. }
  916. if (outgoingCommand -> packet != NULL)
  917. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  918. ++ peer -> packetsLost;
  919. outgoingCommand -> roundTripTimeout *= 2;
  920. enet_list_insert (insertPosition, enet_list_remove (& outgoingCommand -> outgoingCommandList));
  921. if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
  922. ! enet_list_empty (& peer -> sentReliableCommands))
  923. {
  924. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  925. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  926. }
  927. }
  928. return 0;
  929. }
  930. static void
  931. enet_protocol_send_reliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  932. {
  933. ENetProtocol * command = & host -> commands [host -> commandCount];
  934. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  935. ENetOutgoingCommand * outgoingCommand;
  936. ENetListIterator currentCommand;
  937. ENetChannel *channel;
  938. enet_uint16 reliableWindow;
  939. size_t commandSize;
  940. currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  941. while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
  942. {
  943. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  944. channel = outgoingCommand -> command.header.channelID < peer -> channelCount ? & peer -> channels [outgoingCommand -> command.header.channelID] : NULL;
  945. reliableWindow = outgoingCommand -> reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  946. if (channel != NULL &&
  947. outgoingCommand -> sendAttempts < 1 &&
  948. ! (outgoingCommand -> reliableSequenceNumber % ENET_PEER_RELIABLE_WINDOW_SIZE) &&
  949. channel -> usedReliableWindows & ((((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1) << reliableWindow) |
  950. (((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1) >> (ENET_PEER_RELIABLE_WINDOW_SIZE - reliableWindow))))
  951. break;
  952. commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
  953. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  954. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  955. peer -> mtu - host -> packetSize < commandSize)
  956. {
  957. host -> continueSending = 1;
  958. break;
  959. }
  960. if (outgoingCommand -> packet != NULL)
  961. {
  962. if (peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > peer -> windowSize)
  963. break;
  964. if ((enet_uint16) (peer -> mtu - host -> packetSize) < (enet_uint16) (commandSize + outgoingCommand -> fragmentLength))
  965. {
  966. host -> continueSending = 1;
  967. break;
  968. }
  969. }
  970. currentCommand = enet_list_next (currentCommand);
  971. if (channel != NULL && outgoingCommand -> sendAttempts < 1)
  972. {
  973. channel -> usedReliableWindows |= 1 << reliableWindow;
  974. ++ channel -> reliableWindows [reliableWindow];
  975. }
  976. ++ outgoingCommand -> sendAttempts;
  977. if (outgoingCommand -> roundTripTimeout == 0)
  978. {
  979. outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
  980. outgoingCommand -> roundTripTimeoutLimit = ENET_PEER_TIMEOUT_LIMIT * outgoingCommand -> roundTripTimeout;
  981. }
  982. if (enet_list_empty (& peer -> sentReliableCommands))
  983. peer -> nextTimeout = host -> serviceTime + outgoingCommand -> roundTripTimeout;
  984. enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
  985. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  986. outgoingCommand -> sentTime = host -> serviceTime;
  987. buffer -> data = command;
  988. buffer -> dataLength = commandSize;
  989. host -> packetSize += buffer -> dataLength;
  990. host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
  991. * command = outgoingCommand -> command;
  992. if (outgoingCommand -> packet != NULL)
  993. {
  994. ++ buffer;
  995. buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
  996. buffer -> dataLength = outgoingCommand -> fragmentLength;
  997. host -> packetSize += outgoingCommand -> fragmentLength;
  998. peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
  999. }
  1000. ++ peer -> packetsSent;
  1001. ++ command;
  1002. ++ buffer;
  1003. }
  1004. host -> commandCount = command - host -> commands;
  1005. host -> bufferCount = buffer - host -> buffers;
  1006. }
  1007. static int
  1008. enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
  1009. {
  1010. ENetProtocolHeader header;
  1011. ENetPeer * currentPeer;
  1012. int sentLength;
  1013. host -> continueSending = 1;
  1014. while (host -> continueSending)
  1015. for (host -> continueSending = 0,
  1016. currentPeer = host -> peers;
  1017. currentPeer < & host -> peers [host -> peerCount];
  1018. ++ currentPeer)
  1019. {
  1020. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
  1021. currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
  1022. continue;
  1023. host -> headerFlags = 0;
  1024. host -> commandCount = 0;
  1025. host -> bufferCount = 1;
  1026. host -> packetSize = sizeof (ENetProtocolHeader);
  1027. if (! enet_list_empty (& currentPeer -> acknowledgements))
  1028. enet_protocol_send_acknowledgements (host, currentPeer);
  1029. if (checkForTimeouts != 0 &&
  1030. ! enet_list_empty (& currentPeer -> sentReliableCommands) &&
  1031. ENET_TIME_GREATER_EQUAL (host -> serviceTime, currentPeer -> nextTimeout) &&
  1032. enet_protocol_check_timeouts (host, currentPeer, event) == 1)
  1033. return 1;
  1034. if (! enet_list_empty (& currentPeer -> outgoingReliableCommands))
  1035. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  1036. else
  1037. if (enet_list_empty (& currentPeer -> sentReliableCommands) &&
  1038. ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL &&
  1039. currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
  1040. {
  1041. enet_peer_ping (currentPeer);
  1042. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  1043. }
  1044. if (! enet_list_empty (& currentPeer -> outgoingUnreliableCommands))
  1045. enet_protocol_send_unreliable_outgoing_commands (host, currentPeer);
  1046. if (host -> commandCount == 0)
  1047. continue;
  1048. if (currentPeer -> packetLossEpoch == 0)
  1049. currentPeer -> packetLossEpoch = host -> serviceTime;
  1050. else
  1051. if (ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
  1052. currentPeer -> packetsSent > 0)
  1053. {
  1054. enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
  1055. #ifdef ENET_DEBUG
  1056. #ifdef WIN32
  1057. printf (
  1058. #else
  1059. fprintf (stderr,
  1060. #endif
  1061. "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);
  1062. #endif
  1063. currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;
  1064. if (packetLoss >= currentPeer -> packetLoss)
  1065. {
  1066. currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8;
  1067. currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4;
  1068. }
  1069. else
  1070. {
  1071. currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8;
  1072. currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4;
  1073. }
  1074. currentPeer -> packetLossEpoch = host -> serviceTime;
  1075. currentPeer -> packetsSent = 0;
  1076. currentPeer -> packetsLost = 0;
  1077. }
  1078. header.checksum = currentPeer -> sessionID;
  1079. header.peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID | host -> headerFlags);
  1080. host -> buffers -> data = & header;
  1081. if (host -> headerFlags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)
  1082. {
  1083. header.sentTime = ENET_HOST_TO_NET_16 (host -> serviceTime & 0xFFFF);
  1084. host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
  1085. }
  1086. else
  1087. host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;
  1088. #ifdef USE_CRC32
  1089. header.checksum = enet_crc32 (host -> buffers, host -> bufferCount);
  1090. #endif
  1091. currentPeer -> lastSendTime = host -> serviceTime;
  1092. sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
  1093. enet_protocol_remove_sent_unreliable_commands (currentPeer);
  1094. if (sentLength < 0)
  1095. return -1;
  1096. }
  1097. return 0;
  1098. }
  1099. /** Sends any queued packets on the host specified to its designated peers.
  1100. @param host host to flush
  1101. @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().
  1102. @ingroup host
  1103. */
  1104. void
  1105. enet_host_flush (ENetHost * host)
  1106. {
  1107. host -> serviceTime = enet_time_get ();
  1108. enet_protocol_send_outgoing_commands (host, NULL, 0);
  1109. }
  1110. /** Checks for any queued events on the host and dispatches one if available.
  1111. @param host host to check for events
  1112. @param event an event structure where event details will be placed if available
  1113. @retval > 0 if an event was dispatched
  1114. @retval 0 if no events are available
  1115. @retval < 0 on failure
  1116. @ingroup host
  1117. */
  1118. int
  1119. enet_host_check_events (ENetHost * host, ENetEvent * event)
  1120. {
  1121. if (event == NULL) return -1;
  1122. event -> type = ENET_EVENT_TYPE_NONE;
  1123. event -> peer = NULL;
  1124. event -> packet = NULL;
  1125. return enet_protocol_dispatch_incoming_commands (host, event);
  1126. }
  1127. /** Waits for events on the host specified and shuttles packets between
  1128. the host and its peers.
  1129. @param host host to service
  1130. @param event an event structure where event details will be placed if one occurs
  1131. if event == NULL then no events will be delivered
  1132. @param timeout number of milliseconds that ENet should wait for events
  1133. @retval > 0 if an event occurred within the specified time limit
  1134. @retval 0 if no event occurred
  1135. @retval < 0 on failure
  1136. @remarks enet_host_service should be called fairly regularly for adequate performance
  1137. @ingroup host
  1138. */
  1139. int
  1140. enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
  1141. {
  1142. enet_uint32 waitCondition;
  1143. if (event != NULL)
  1144. {
  1145. event -> type = ENET_EVENT_TYPE_NONE;
  1146. event -> peer = NULL;
  1147. event -> packet = NULL;
  1148. switch (enet_protocol_dispatch_incoming_commands (host, event))
  1149. {
  1150. case 1:
  1151. return 1;
  1152. case -1:
  1153. perror ("Error dispatching incoming packets");
  1154. return -1;
  1155. default:
  1156. break;
  1157. }
  1158. }
  1159. host -> serviceTime = enet_time_get ();
  1160. timeout += host -> serviceTime;
  1161. do
  1162. {
  1163. if (ENET_TIME_DIFFERENCE (host -> serviceTime, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  1164. enet_host_bandwidth_throttle (host);
  1165. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  1166. {
  1167. case 1:
  1168. return 1;
  1169. case -1:
  1170. perror ("Error sending outgoing packets");
  1171. return -1;
  1172. default:
  1173. break;
  1174. }
  1175. switch (enet_protocol_receive_incoming_commands (host, event))
  1176. {
  1177. case 1:
  1178. return 1;
  1179. case -1:
  1180. perror ("Error receiving incoming packets");
  1181. return -1;
  1182. default:
  1183. break;
  1184. }
  1185. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  1186. {
  1187. case 1:
  1188. return 1;
  1189. case -1:
  1190. perror ("Error sending outgoing packets");
  1191. return -1;
  1192. default:
  1193. break;
  1194. }
  1195. if (event != NULL)
  1196. {
  1197. switch (enet_protocol_dispatch_incoming_commands (host, event))
  1198. {
  1199. case 1:
  1200. return 1;
  1201. case -1:
  1202. perror ("Error dispatching incoming packets");
  1203. return -1;
  1204. default:
  1205. break;
  1206. }
  1207. }
  1208. host -> serviceTime = enet_time_get ();
  1209. if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
  1210. return 0;
  1211. waitCondition = ENET_SOCKET_WAIT_RECEIVE;
  1212. if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
  1213. return -1;
  1214. host -> serviceTime = enet_time_get ();
  1215. } while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);
  1216. return 0;
  1217. }