protocol.c 67 KB

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