protocol.c 70 KB

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