protocol.c 69 KB

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