protocol.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. /**
  2. @file protocol.c
  3. @brief ENet protocol functions
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #define ENET_BUILDING_LIB 1
  8. #include "enet/utility.h"
  9. #include "enet/time.h"
  10. #include "enet/enet.h"
  11. static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] =
  12. {
  13. 0,
  14. sizeof (ENetProtocolAcknowledge),
  15. sizeof (ENetProtocolConnect),
  16. sizeof (ENetProtocolVerifyConnect),
  17. sizeof (ENetProtocolDisconnect),
  18. sizeof (ENetProtocolPing),
  19. sizeof (ENetProtocolSendReliable),
  20. sizeof (ENetProtocolSendUnreliable),
  21. sizeof (ENetProtocolSendFragment),
  22. sizeof (ENetProtocolSendUnsequenced),
  23. sizeof (ENetProtocolBandwidthLimit),
  24. sizeof (ENetProtocolThrottleConfigure),
  25. sizeof (ENetProtocolSendFragment)
  26. };
  27. size_t
  28. enet_protocol_command_size (enet_uint8 commandNumber)
  29. {
  30. return commandSizes [commandNumber & ENET_PROTOCOL_COMMAND_MASK];
  31. }
  32. static int
  33. enet_protocol_dispatch_incoming_commands (ENetHost * host, ENetEvent * event)
  34. {
  35. while (! enet_list_empty (& host -> dispatchQueue))
  36. {
  37. ENetPeer * peer = (ENetPeer *) enet_list_remove (enet_list_begin (& host -> dispatchQueue));
  38. peer -> needsDispatch = 0;
  39. switch (peer -> state)
  40. {
  41. case ENET_PEER_STATE_CONNECTION_PENDING:
  42. case ENET_PEER_STATE_CONNECTION_SUCCEEDED:
  43. peer -> state = ENET_PEER_STATE_CONNECTED;
  44. event -> type = ENET_EVENT_TYPE_CONNECT;
  45. event -> peer = peer;
  46. event -> data = peer -> eventData;
  47. return 1;
  48. case ENET_PEER_STATE_ZOMBIE:
  49. host -> recalculateBandwidthLimits = 1;
  50. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  51. event -> peer = peer;
  52. event -> data = peer -> eventData;
  53. enet_peer_reset (peer);
  54. return 1;
  55. case ENET_PEER_STATE_CONNECTED:
  56. if (enet_list_empty (& peer -> dispatchedCommands))
  57. continue;
  58. event -> packet = enet_peer_receive (peer, & event -> channelID);
  59. if (event -> packet == NULL)
  60. continue;
  61. event -> type = ENET_EVENT_TYPE_RECEIVE;
  62. event -> peer = peer;
  63. if (! enet_list_empty (& peer -> dispatchedCommands))
  64. {
  65. peer -> needsDispatch = 1;
  66. enet_list_insert (enet_list_end (& host -> dispatchQueue), & peer -> dispatchList);
  67. }
  68. return 1;
  69. default:
  70. break;
  71. }
  72. }
  73. return 0;
  74. }
  75. static void
  76. enet_protocol_dispatch_state (ENetHost * host, ENetPeer * peer, ENetPeerState state)
  77. {
  78. peer -> state = state;
  79. if (! peer -> needsDispatch)
  80. {
  81. enet_list_insert (enet_list_end (& host -> dispatchQueue), & peer -> dispatchList);
  82. peer -> needsDispatch = 1;
  83. }
  84. }
  85. static void
  86. enet_protocol_notify_connect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  87. {
  88. host -> recalculateBandwidthLimits = 1;
  89. if (event != NULL)
  90. {
  91. peer -> state = ENET_PEER_STATE_CONNECTED;
  92. event -> type = ENET_EVENT_TYPE_CONNECT;
  93. event -> peer = peer;
  94. event -> data = peer -> eventData;
  95. }
  96. else
  97. enet_protocol_dispatch_state (host, peer, peer -> state == ENET_PEER_STATE_CONNECTING ? ENET_PEER_STATE_CONNECTION_SUCCEEDED : ENET_PEER_STATE_CONNECTION_PENDING);
  98. }
  99. static void
  100. enet_protocol_notify_disconnect (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  101. {
  102. if (peer -> state >= ENET_PEER_STATE_CONNECTION_PENDING)
  103. host -> recalculateBandwidthLimits = 1;
  104. if (peer -> state != ENET_PEER_STATE_CONNECTING && peer -> state < ENET_PEER_STATE_CONNECTION_SUCCEEDED)
  105. enet_peer_reset (peer);
  106. else
  107. if (event != NULL)
  108. {
  109. event -> type = ENET_EVENT_TYPE_DISCONNECT;
  110. event -> peer = peer;
  111. event -> data = 0;
  112. enet_peer_reset (peer);
  113. }
  114. else
  115. {
  116. peer -> eventData = 0;
  117. enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
  118. }
  119. }
  120. static void
  121. enet_protocol_remove_sent_unreliable_commands (ENetPeer * peer)
  122. {
  123. ENetOutgoingCommand * outgoingCommand;
  124. while (! enet_list_empty (& peer -> sentUnreliableCommands))
  125. {
  126. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentUnreliableCommands);
  127. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  128. if (outgoingCommand -> packet != NULL)
  129. {
  130. -- outgoingCommand -> packet -> referenceCount;
  131. if (outgoingCommand -> packet -> referenceCount == 0)
  132. enet_packet_destroy (outgoingCommand -> packet);
  133. }
  134. enet_free (outgoingCommand);
  135. }
  136. }
  137. static ENetProtocolCommand
  138. enet_protocol_remove_sent_reliable_command (ENetPeer * peer, enet_uint16 reliableSequenceNumber, enet_uint8 channelID)
  139. {
  140. ENetOutgoingCommand * outgoingCommand = NULL;
  141. ENetListIterator currentCommand;
  142. ENetProtocolCommand commandNumber;
  143. int wasSent = 1;
  144. for (currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  145. currentCommand != enet_list_end (& peer -> sentReliableCommands);
  146. currentCommand = enet_list_next (currentCommand))
  147. {
  148. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  149. if (outgoingCommand -> reliableSequenceNumber == reliableSequenceNumber &&
  150. outgoingCommand -> command.header.channelID == channelID)
  151. break;
  152. }
  153. if (currentCommand == enet_list_end (& peer -> sentReliableCommands))
  154. {
  155. for (currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  156. currentCommand != enet_list_end (& peer -> outgoingReliableCommands);
  157. currentCommand = enet_list_next (currentCommand))
  158. {
  159. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  160. if (outgoingCommand -> sendAttempts < 1) return ENET_PROTOCOL_COMMAND_NONE;
  161. if (outgoingCommand -> reliableSequenceNumber == reliableSequenceNumber &&
  162. outgoingCommand -> command.header.channelID == channelID)
  163. break;
  164. }
  165. if (currentCommand == enet_list_end (& peer -> outgoingReliableCommands))
  166. return ENET_PROTOCOL_COMMAND_NONE;
  167. wasSent = 0;
  168. }
  169. if (outgoingCommand == NULL)
  170. return ENET_PROTOCOL_COMMAND_NONE;
  171. if (channelID < peer -> channelCount)
  172. {
  173. ENetChannel * channel = & peer -> channels [channelID];
  174. enet_uint16 reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  175. if (channel -> reliableWindows [reliableWindow] > 0)
  176. {
  177. -- channel -> reliableWindows [reliableWindow];
  178. if (! channel -> reliableWindows [reliableWindow])
  179. channel -> usedReliableWindows &= ~ (1 << reliableWindow);
  180. }
  181. }
  182. commandNumber = (ENetProtocolCommand) (outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK);
  183. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  184. if (outgoingCommand -> packet != NULL)
  185. {
  186. if (wasSent)
  187. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  188. -- outgoingCommand -> packet -> referenceCount;
  189. if (outgoingCommand -> packet -> referenceCount == 0)
  190. enet_packet_destroy (outgoingCommand -> packet);
  191. }
  192. enet_free (outgoingCommand);
  193. if (enet_list_empty (& peer -> sentReliableCommands))
  194. return commandNumber;
  195. outgoingCommand = (ENetOutgoingCommand *) enet_list_front (& peer -> sentReliableCommands);
  196. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  197. return commandNumber;
  198. }
  199. static ENetPeer *
  200. enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENetProtocol * command)
  201. {
  202. enet_uint8 incomingSessionID, outgoingSessionID;
  203. enet_uint32 mtu, windowSize;
  204. ENetChannel * channel;
  205. size_t channelCount;
  206. ENetPeer * currentPeer;
  207. ENetProtocol verifyCommand;
  208. channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount);
  209. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT ||
  210. channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  211. return NULL;
  212. for (currentPeer = host -> peers;
  213. currentPeer < & host -> peers [host -> peerCount];
  214. ++ currentPeer)
  215. {
  216. if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
  217. currentPeer -> address.host == host -> receivedAddress.host &&
  218. currentPeer -> address.port == host -> receivedAddress.port &&
  219. currentPeer -> connectID == command -> connect.connectID)
  220. return NULL;
  221. }
  222. for (currentPeer = host -> peers;
  223. currentPeer < & host -> peers [host -> peerCount];
  224. ++ currentPeer)
  225. {
  226. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED)
  227. break;
  228. }
  229. if (currentPeer >= & host -> peers [host -> peerCount])
  230. return NULL;
  231. if (channelCount > host -> channelLimit)
  232. channelCount = host -> channelLimit;
  233. currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel));
  234. if (currentPeer -> channels == NULL)
  235. return NULL;
  236. currentPeer -> channelCount = channelCount;
  237. currentPeer -> state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
  238. currentPeer -> connectID = command -> connect.connectID;
  239. currentPeer -> address = host -> receivedAddress;
  240. currentPeer -> outgoingPeerID = ENET_NET_TO_HOST_16 (command -> connect.outgoingPeerID);
  241. currentPeer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.incomingBandwidth);
  242. currentPeer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> connect.outgoingBandwidth);
  243. currentPeer -> packetThrottleInterval = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleInterval);
  244. currentPeer -> packetThrottleAcceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleAcceleration);
  245. currentPeer -> packetThrottleDeceleration = ENET_NET_TO_HOST_32 (command -> connect.packetThrottleDeceleration);
  246. currentPeer -> eventData = ENET_NET_TO_HOST_32 (command -> connect.data);
  247. incomingSessionID = command -> connect.incomingSessionID == 0xFF ? currentPeer -> outgoingSessionID : command -> connect.incomingSessionID;
  248. incomingSessionID = (incomingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  249. if (incomingSessionID == currentPeer -> outgoingSessionID)
  250. incomingSessionID = (incomingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  251. currentPeer -> outgoingSessionID = incomingSessionID;
  252. outgoingSessionID = command -> connect.outgoingSessionID == 0xFF ? currentPeer -> incomingSessionID : command -> connect.outgoingSessionID;
  253. outgoingSessionID = (outgoingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  254. if (outgoingSessionID == currentPeer -> incomingSessionID)
  255. outgoingSessionID = (outgoingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  256. currentPeer -> incomingSessionID = outgoingSessionID;
  257. for (channel = currentPeer -> channels;
  258. channel < & currentPeer -> channels [channelCount];
  259. ++ channel)
  260. {
  261. channel -> outgoingReliableSequenceNumber = 0;
  262. channel -> outgoingUnreliableSequenceNumber = 0;
  263. channel -> incomingReliableSequenceNumber = 0;
  264. channel -> incomingUnreliableSequenceNumber = 0;
  265. enet_list_clear (& channel -> incomingReliableCommands);
  266. enet_list_clear (& channel -> incomingUnreliableCommands);
  267. channel -> usedReliableWindows = 0;
  268. memset (channel -> reliableWindows, 0, sizeof (channel -> reliableWindows));
  269. }
  270. mtu = ENET_NET_TO_HOST_32 (command -> connect.mtu);
  271. if (mtu < ENET_PROTOCOL_MINIMUM_MTU)
  272. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  273. else
  274. if (mtu > ENET_PROTOCOL_MAXIMUM_MTU)
  275. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  276. currentPeer -> mtu = mtu;
  277. if (host -> outgoingBandwidth == 0 &&
  278. currentPeer -> incomingBandwidth == 0)
  279. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  280. else
  281. if (host -> outgoingBandwidth == 0 ||
  282. currentPeer -> incomingBandwidth == 0)
  283. currentPeer -> windowSize = (ENET_MAX (host -> outgoingBandwidth, currentPeer -> incomingBandwidth) /
  284. ENET_PEER_WINDOW_SIZE_SCALE) *
  285. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  286. else
  287. currentPeer -> windowSize = (ENET_MIN (host -> outgoingBandwidth, currentPeer -> incomingBandwidth) /
  288. ENET_PEER_WINDOW_SIZE_SCALE) *
  289. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  290. if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  291. currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  292. else
  293. if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  294. currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  295. if (host -> incomingBandwidth == 0)
  296. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  297. else
  298. windowSize = (host -> incomingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE) *
  299. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  300. if (windowSize > ENET_NET_TO_HOST_32 (command -> connect.windowSize))
  301. windowSize = ENET_NET_TO_HOST_32 (command -> connect.windowSize);
  302. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
  303. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  304. else
  305. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
  306. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  307. verifyCommand.header.command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  308. verifyCommand.header.channelID = 0xFF;
  309. verifyCommand.verifyConnect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID);
  310. verifyCommand.verifyConnect.incomingSessionID = incomingSessionID;
  311. verifyCommand.verifyConnect.outgoingSessionID = outgoingSessionID;
  312. verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_32 (currentPeer -> mtu);
  313. verifyCommand.verifyConnect.windowSize = ENET_HOST_TO_NET_32 (windowSize);
  314. verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32 (channelCount);
  315. verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth);
  316. verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth);
  317. verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval);
  318. verifyCommand.verifyConnect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration);
  319. verifyCommand.verifyConnect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration);
  320. verifyCommand.verifyConnect.connectID = currentPeer -> connectID;
  321. enet_peer_queue_outgoing_command (currentPeer, & verifyCommand, NULL, 0, 0);
  322. return currentPeer;
  323. }
  324. static int
  325. enet_protocol_handle_send_reliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
  326. {
  327. ENetPacket * packet;
  328. size_t dataLength;
  329. if (command -> header.channelID >= peer -> channelCount ||
  330. (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
  331. return -1;
  332. dataLength = ENET_NET_TO_HOST_16 (command -> sendReliable.dataLength);
  333. * currentData += dataLength;
  334. if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE ||
  335. * currentData < host -> receivedData ||
  336. * currentData > & host -> receivedData [host -> receivedDataLength])
  337. return -1;
  338. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendReliable),
  339. dataLength,
  340. ENET_PACKET_FLAG_RELIABLE);
  341. if (packet == NULL ||
  342. enet_peer_queue_incoming_command (peer, command, packet, 0) == NULL)
  343. return -1;
  344. return 0;
  345. }
  346. static int
  347. enet_protocol_handle_send_unsequenced (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
  348. {
  349. ENetPacket * packet;
  350. enet_uint32 unsequencedGroup, index;
  351. size_t dataLength;
  352. if (command -> header.channelID >= peer -> channelCount ||
  353. (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
  354. return -1;
  355. dataLength = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.dataLength);
  356. * currentData += dataLength;
  357. if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE ||
  358. * currentData < host -> receivedData ||
  359. * currentData > & host -> receivedData [host -> receivedDataLength])
  360. return -1;
  361. unsequencedGroup = ENET_NET_TO_HOST_16 (command -> sendUnsequenced.unsequencedGroup);
  362. index = unsequencedGroup % ENET_PEER_UNSEQUENCED_WINDOW_SIZE;
  363. if (unsequencedGroup < peer -> incomingUnsequencedGroup)
  364. unsequencedGroup += 0x10000;
  365. if (unsequencedGroup >= (enet_uint32) peer -> incomingUnsequencedGroup + ENET_PEER_FREE_UNSEQUENCED_WINDOWS * ENET_PEER_UNSEQUENCED_WINDOW_SIZE)
  366. return 0;
  367. unsequencedGroup &= 0xFFFF;
  368. if (unsequencedGroup - index != peer -> incomingUnsequencedGroup)
  369. {
  370. peer -> incomingUnsequencedGroup = unsequencedGroup - index;
  371. memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow));
  372. }
  373. else
  374. if (peer -> unsequencedWindow [index / 32] & (1 << (index % 32)))
  375. return 0;
  376. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnsequenced),
  377. dataLength,
  378. ENET_PACKET_FLAG_UNSEQUENCED);
  379. if (packet == NULL ||
  380. enet_peer_queue_incoming_command (peer, command, packet, 0) == NULL)
  381. return -1;
  382. peer -> unsequencedWindow [index / 32] |= 1 << (index % 32);
  383. return 0;
  384. }
  385. static int
  386. enet_protocol_handle_send_unreliable (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
  387. {
  388. ENetPacket * packet;
  389. size_t dataLength;
  390. if (command -> header.channelID >= peer -> channelCount ||
  391. (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
  392. return -1;
  393. dataLength = ENET_NET_TO_HOST_16 (command -> sendUnreliable.dataLength);
  394. * currentData += dataLength;
  395. if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE ||
  396. * currentData < host -> receivedData ||
  397. * currentData > & host -> receivedData [host -> receivedDataLength])
  398. return -1;
  399. packet = enet_packet_create ((const enet_uint8 *) command + sizeof (ENetProtocolSendUnreliable),
  400. dataLength,
  401. 0);
  402. if (packet == NULL ||
  403. enet_peer_queue_incoming_command (peer, command, packet, 0) == NULL)
  404. return -1;
  405. return 0;
  406. }
  407. static int
  408. enet_protocol_handle_send_fragment (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
  409. {
  410. enet_uint32 fragmentNumber,
  411. fragmentCount,
  412. fragmentOffset,
  413. fragmentLength,
  414. startSequenceNumber,
  415. totalLength;
  416. ENetChannel * channel;
  417. enet_uint16 startWindow, currentWindow;
  418. ENetListIterator currentCommand;
  419. ENetIncomingCommand * startCommand = NULL;
  420. if (command -> header.channelID >= peer -> channelCount ||
  421. (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
  422. return -1;
  423. fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
  424. * currentData += fragmentLength;
  425. if (fragmentLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE ||
  426. * currentData < host -> receivedData ||
  427. * currentData > & host -> receivedData [host -> receivedDataLength])
  428. return -1;
  429. channel = & peer -> channels [command -> header.channelID];
  430. startSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendFragment.startSequenceNumber);
  431. startWindow = startSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  432. currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  433. if (startSequenceNumber < channel -> incomingReliableSequenceNumber)
  434. startWindow += ENET_PEER_RELIABLE_WINDOWS;
  435. if (startWindow < currentWindow || startWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
  436. return 0;
  437. fragmentNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentNumber);
  438. fragmentCount = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentCount);
  439. fragmentOffset = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentOffset);
  440. totalLength = ENET_NET_TO_HOST_32 (command -> sendFragment.totalLength);
  441. if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT ||
  442. fragmentNumber >= fragmentCount ||
  443. totalLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE ||
  444. fragmentOffset >= totalLength ||
  445. fragmentLength > totalLength - fragmentOffset)
  446. return -1;
  447. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingReliableCommands));
  448. currentCommand != enet_list_end (& channel -> incomingReliableCommands);
  449. currentCommand = enet_list_previous (currentCommand))
  450. {
  451. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  452. if (startSequenceNumber >= channel -> incomingReliableSequenceNumber)
  453. {
  454. if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
  455. continue;
  456. }
  457. else
  458. if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
  459. break;
  460. if (incomingCommand -> reliableSequenceNumber <= startSequenceNumber)
  461. {
  462. if (incomingCommand -> reliableSequenceNumber < startSequenceNumber)
  463. break;
  464. if ((incomingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_FRAGMENT ||
  465. totalLength != incomingCommand -> packet -> dataLength ||
  466. fragmentCount != incomingCommand -> fragmentCount)
  467. return -1;
  468. startCommand = incomingCommand;
  469. break;
  470. }
  471. }
  472. if (startCommand == NULL)
  473. {
  474. ENetProtocol hostCommand = * command;
  475. ENetPacket * packet = enet_packet_create (NULL, totalLength, ENET_PACKET_FLAG_RELIABLE);
  476. if (packet == NULL)
  477. return -1;
  478. hostCommand.header.reliableSequenceNumber = startSequenceNumber;
  479. startCommand = enet_peer_queue_incoming_command (peer, & hostCommand, packet, fragmentCount);
  480. if (startCommand == NULL)
  481. return -1;
  482. }
  483. if ((startCommand -> fragments [fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0)
  484. {
  485. -- startCommand -> fragmentsRemaining;
  486. startCommand -> fragments [fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
  487. if (fragmentOffset + fragmentLength > startCommand -> packet -> dataLength)
  488. fragmentLength = startCommand -> packet -> dataLength - fragmentOffset;
  489. memcpy (startCommand -> packet -> data + fragmentOffset,
  490. (enet_uint8 *) command + sizeof (ENetProtocolSendFragment),
  491. fragmentLength);
  492. if (startCommand -> fragmentsRemaining <= 0)
  493. enet_peer_dispatch_incoming_reliable_commands (peer, channel);
  494. }
  495. return 0;
  496. }
  497. static int
  498. enet_protocol_handle_send_unreliable_fragment (ENetHost * host, ENetPeer * peer, const ENetProtocol * command, enet_uint8 ** currentData)
  499. {
  500. enet_uint32 fragmentNumber,
  501. fragmentCount,
  502. fragmentOffset,
  503. fragmentLength,
  504. reliableSequenceNumber,
  505. startSequenceNumber,
  506. totalLength;
  507. enet_uint16 reliableWindow, currentWindow;
  508. ENetChannel * channel;
  509. ENetListIterator currentCommand;
  510. ENetIncomingCommand * startCommand = NULL;
  511. if (command -> header.channelID >= peer -> channelCount ||
  512. (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER))
  513. return -1;
  514. fragmentLength = ENET_NET_TO_HOST_16 (command -> sendFragment.dataLength);
  515. * currentData += fragmentLength;
  516. if (fragmentLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE ||
  517. * currentData < host -> receivedData ||
  518. * currentData > & host -> receivedData [host -> receivedDataLength])
  519. return -1;
  520. channel = & peer -> channels [command -> header.channelID];
  521. reliableSequenceNumber = command -> header.reliableSequenceNumber;
  522. startSequenceNumber = ENET_NET_TO_HOST_16 (command -> sendFragment.startSequenceNumber);
  523. reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  524. currentWindow = channel -> incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  525. if (reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
  526. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  527. if (reliableWindow < currentWindow || reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)
  528. return 0;
  529. if (reliableSequenceNumber == channel -> incomingReliableSequenceNumber &&
  530. startSequenceNumber <= channel -> incomingUnreliableSequenceNumber)
  531. return 0;
  532. fragmentNumber = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentNumber);
  533. fragmentCount = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentCount);
  534. fragmentOffset = ENET_NET_TO_HOST_32 (command -> sendFragment.fragmentOffset);
  535. totalLength = ENET_NET_TO_HOST_32 (command -> sendFragment.totalLength);
  536. if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT ||
  537. fragmentNumber >= fragmentCount ||
  538. totalLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE ||
  539. fragmentOffset >= totalLength ||
  540. fragmentLength > totalLength - fragmentOffset)
  541. return -1;
  542. for (currentCommand = enet_list_previous (enet_list_end (& channel -> incomingUnreliableCommands));
  543. currentCommand != enet_list_end (& channel -> incomingUnreliableCommands);
  544. currentCommand = enet_list_previous (currentCommand))
  545. {
  546. ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;
  547. if (reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
  548. {
  549. if (incomingCommand -> reliableSequenceNumber < channel -> incomingReliableSequenceNumber)
  550. continue;
  551. }
  552. else
  553. if (incomingCommand -> reliableSequenceNumber >= channel -> incomingReliableSequenceNumber)
  554. break;
  555. if (incomingCommand -> reliableSequenceNumber < reliableSequenceNumber)
  556. break;
  557. if (incomingCommand -> reliableSequenceNumber > reliableSequenceNumber)
  558. continue;
  559. if (incomingCommand -> unreliableSequenceNumber <= startSequenceNumber)
  560. {
  561. if (incomingCommand -> unreliableSequenceNumber < startSequenceNumber)
  562. break;
  563. if ((incomingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT ||
  564. totalLength != incomingCommand -> packet -> dataLength ||
  565. fragmentCount != incomingCommand -> fragmentCount)
  566. return -1;
  567. startCommand = incomingCommand;
  568. break;
  569. }
  570. }
  571. if (startCommand == NULL)
  572. {
  573. ENetPacket * packet = enet_packet_create (NULL, totalLength, ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT);
  574. if (packet == NULL)
  575. return -1;
  576. startCommand = enet_peer_queue_incoming_command (peer, command, packet, fragmentCount);
  577. if (startCommand == NULL)
  578. return -1;
  579. }
  580. if ((startCommand -> fragments [fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0)
  581. {
  582. -- startCommand -> fragmentsRemaining;
  583. startCommand -> fragments [fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
  584. if (fragmentOffset + fragmentLength > startCommand -> packet -> dataLength)
  585. fragmentLength = startCommand -> packet -> dataLength - fragmentOffset;
  586. memcpy (startCommand -> packet -> data + fragmentOffset,
  587. (enet_uint8 *) command + sizeof (ENetProtocolSendFragment),
  588. fragmentLength);
  589. if (startCommand -> fragmentsRemaining <= 0)
  590. enet_peer_dispatch_incoming_unreliable_commands (peer, channel);
  591. }
  592. return 0;
  593. }
  594. static int
  595. enet_protocol_handle_ping (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  596. {
  597. if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
  598. return -1;
  599. return 0;
  600. }
  601. static int
  602. enet_protocol_handle_bandwidth_limit (ENetHost * host, ENetPeer * peer, const ENetProtocol * command)
  603. {
  604. if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER)
  605. return -1;
  606. peer -> incomingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.incomingBandwidth);
  607. peer -> outgoingBandwidth = ENET_NET_TO_HOST_32 (command -> bandwidthLimit.outgoingBandwidth);
  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)
  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. peer -> state = 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. for (;;)
  942. {
  943. int receivedLength;
  944. ENetBuffer buffer;
  945. buffer.data = host -> packetData [0];
  946. buffer.dataLength = sizeof (host -> packetData [0]);
  947. receivedLength = enet_socket_receive (host -> socket,
  948. & host -> receivedAddress,
  949. & buffer,
  950. 1);
  951. if (receivedLength < 0)
  952. return -1;
  953. if (receivedLength == 0)
  954. return 0;
  955. host -> receivedData = host -> packetData [0];
  956. host -> receivedDataLength = receivedLength;
  957. host -> totalReceivedData += receivedLength;
  958. host -> totalReceivedPackets ++;
  959. if (host -> intercept != NULL)
  960. {
  961. switch (host -> intercept (host, event))
  962. {
  963. case 1:
  964. if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
  965. return 1;
  966. continue;
  967. case -1:
  968. return -1;
  969. default:
  970. break;
  971. }
  972. }
  973. switch (enet_protocol_handle_incoming_commands (host, event))
  974. {
  975. case 1:
  976. return 1;
  977. case -1:
  978. return -1;
  979. default:
  980. break;
  981. }
  982. }
  983. return -1;
  984. }
  985. static void
  986. enet_protocol_send_acknowledgements (ENetHost * host, ENetPeer * peer)
  987. {
  988. ENetProtocol * command = & host -> commands [host -> commandCount];
  989. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  990. ENetAcknowledgement * acknowledgement;
  991. ENetListIterator currentAcknowledgement;
  992. currentAcknowledgement = enet_list_begin (& peer -> acknowledgements);
  993. while (currentAcknowledgement != enet_list_end (& peer -> acknowledgements))
  994. {
  995. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  996. buffer >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  997. peer -> mtu - host -> packetSize < sizeof (ENetProtocolAcknowledge))
  998. {
  999. host -> continueSending = 1;
  1000. break;
  1001. }
  1002. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  1003. currentAcknowledgement = enet_list_next (currentAcknowledgement);
  1004. buffer -> data = command;
  1005. buffer -> dataLength = sizeof (ENetProtocolAcknowledge);
  1006. host -> packetSize += buffer -> dataLength;
  1007. command -> header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  1008. command -> header.channelID = acknowledgement -> command.header.channelID;
  1009. command -> acknowledge.receivedReliableSequenceNumber = ENET_HOST_TO_NET_16 (acknowledgement -> command.header.reliableSequenceNumber);
  1010. command -> acknowledge.receivedSentTime = ENET_HOST_TO_NET_16 (acknowledgement -> sentTime);
  1011. if ((acknowledgement -> command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT)
  1012. enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
  1013. enet_list_remove (& acknowledgement -> acknowledgementList);
  1014. enet_free (acknowledgement);
  1015. ++ command;
  1016. ++ buffer;
  1017. }
  1018. host -> commandCount = command - host -> commands;
  1019. host -> bufferCount = buffer - host -> buffers;
  1020. }
  1021. static void
  1022. enet_protocol_send_unreliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  1023. {
  1024. ENetProtocol * command = & host -> commands [host -> commandCount];
  1025. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  1026. ENetOutgoingCommand * outgoingCommand;
  1027. ENetListIterator currentCommand;
  1028. currentCommand = enet_list_begin (& peer -> outgoingUnreliableCommands);
  1029. while (currentCommand != enet_list_end (& peer -> outgoingUnreliableCommands))
  1030. {
  1031. size_t commandSize;
  1032. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1033. commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
  1034. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  1035. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  1036. peer -> mtu - host -> packetSize < commandSize ||
  1037. (outgoingCommand -> packet != NULL &&
  1038. peer -> mtu - host -> packetSize < commandSize + outgoingCommand -> fragmentLength))
  1039. {
  1040. host -> continueSending = 1;
  1041. break;
  1042. }
  1043. currentCommand = enet_list_next (currentCommand);
  1044. if (outgoingCommand -> packet != NULL && outgoingCommand -> fragmentOffset == 0)
  1045. {
  1046. peer -> packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  1047. peer -> packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  1048. if (peer -> packetThrottleCounter > peer -> packetThrottle)
  1049. {
  1050. enet_uint16 reliableSequenceNumber = outgoingCommand -> reliableSequenceNumber,
  1051. unreliableSequenceNumber = outgoingCommand -> unreliableSequenceNumber;
  1052. for (;;)
  1053. {
  1054. -- outgoingCommand -> packet -> referenceCount;
  1055. if (outgoingCommand -> packet -> referenceCount == 0)
  1056. enet_packet_destroy (outgoingCommand -> packet);
  1057. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  1058. enet_free (outgoingCommand);
  1059. if (currentCommand == enet_list_end (& peer -> outgoingUnreliableCommands))
  1060. break;
  1061. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1062. if (outgoingCommand -> reliableSequenceNumber != reliableSequenceNumber ||
  1063. outgoingCommand -> unreliableSequenceNumber != unreliableSequenceNumber)
  1064. break;
  1065. currentCommand = enet_list_next (currentCommand);
  1066. }
  1067. continue;
  1068. }
  1069. }
  1070. buffer -> data = command;
  1071. buffer -> dataLength = commandSize;
  1072. host -> packetSize += buffer -> dataLength;
  1073. * command = outgoingCommand -> command;
  1074. enet_list_remove (& outgoingCommand -> outgoingCommandList);
  1075. if (outgoingCommand -> packet != NULL)
  1076. {
  1077. ++ buffer;
  1078. buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
  1079. buffer -> dataLength = outgoingCommand -> fragmentLength;
  1080. host -> packetSize += buffer -> dataLength;
  1081. enet_list_insert (enet_list_end (& peer -> sentUnreliableCommands), outgoingCommand);
  1082. }
  1083. else
  1084. enet_free (outgoingCommand);
  1085. ++ command;
  1086. ++ buffer;
  1087. }
  1088. host -> commandCount = command - host -> commands;
  1089. host -> bufferCount = buffer - host -> buffers;
  1090. if (peer -> state == ENET_PEER_STATE_DISCONNECT_LATER &&
  1091. enet_list_empty (& peer -> outgoingReliableCommands) &&
  1092. enet_list_empty (& peer -> outgoingUnreliableCommands) &&
  1093. enet_list_empty (& peer -> sentReliableCommands))
  1094. enet_peer_disconnect (peer, peer -> eventData);
  1095. }
  1096. static int
  1097. enet_protocol_check_timeouts (ENetHost * host, ENetPeer * peer, ENetEvent * event)
  1098. {
  1099. ENetOutgoingCommand * outgoingCommand;
  1100. ENetListIterator currentCommand, insertPosition;
  1101. currentCommand = enet_list_begin (& peer -> sentReliableCommands);
  1102. insertPosition = enet_list_begin (& peer -> outgoingReliableCommands);
  1103. while (currentCommand != enet_list_end (& peer -> sentReliableCommands))
  1104. {
  1105. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1106. currentCommand = enet_list_next (currentCommand);
  1107. if (ENET_TIME_DIFFERENCE (host -> serviceTime, outgoingCommand -> sentTime) < outgoingCommand -> roundTripTimeout)
  1108. continue;
  1109. if (peer -> earliestTimeout == 0 ||
  1110. ENET_TIME_LESS (outgoingCommand -> sentTime, peer -> earliestTimeout))
  1111. peer -> earliestTimeout = outgoingCommand -> sentTime;
  1112. if (peer -> earliestTimeout != 0 &&
  1113. (ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= peer -> timeoutMaximum ||
  1114. (outgoingCommand -> roundTripTimeout >= outgoingCommand -> roundTripTimeoutLimit &&
  1115. ENET_TIME_DIFFERENCE (host -> serviceTime, peer -> earliestTimeout) >= peer -> timeoutMinimum)))
  1116. {
  1117. enet_protocol_notify_disconnect (host, peer, event);
  1118. return 1;
  1119. }
  1120. if (outgoingCommand -> packet != NULL)
  1121. peer -> reliableDataInTransit -= outgoingCommand -> fragmentLength;
  1122. ++ peer -> packetsLost;
  1123. outgoingCommand -> roundTripTimeout *= 2;
  1124. enet_list_insert (insertPosition, enet_list_remove (& outgoingCommand -> outgoingCommandList));
  1125. if (currentCommand == enet_list_begin (& peer -> sentReliableCommands) &&
  1126. ! enet_list_empty (& peer -> sentReliableCommands))
  1127. {
  1128. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1129. peer -> nextTimeout = outgoingCommand -> sentTime + outgoingCommand -> roundTripTimeout;
  1130. }
  1131. }
  1132. return 0;
  1133. }
  1134. static int
  1135. enet_protocol_send_reliable_outgoing_commands (ENetHost * host, ENetPeer * peer)
  1136. {
  1137. ENetProtocol * command = & host -> commands [host -> commandCount];
  1138. ENetBuffer * buffer = & host -> buffers [host -> bufferCount];
  1139. ENetOutgoingCommand * outgoingCommand;
  1140. ENetListIterator currentCommand;
  1141. ENetChannel *channel;
  1142. enet_uint16 reliableWindow;
  1143. size_t commandSize;
  1144. int windowExceeded = 0, windowWrap = 0, canPing = 1;
  1145. currentCommand = enet_list_begin (& peer -> outgoingReliableCommands);
  1146. while (currentCommand != enet_list_end (& peer -> outgoingReliableCommands))
  1147. {
  1148. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1149. channel = outgoingCommand -> command.header.channelID < peer -> channelCount ? & peer -> channels [outgoingCommand -> command.header.channelID] : NULL;
  1150. reliableWindow = outgoingCommand -> reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  1151. if (channel != NULL)
  1152. {
  1153. if (! windowWrap &&
  1154. outgoingCommand -> sendAttempts < 1 &&
  1155. ! (outgoingCommand -> reliableSequenceNumber % ENET_PEER_RELIABLE_WINDOW_SIZE) &&
  1156. (channel -> reliableWindows [(reliableWindow + ENET_PEER_RELIABLE_WINDOWS - 1) % ENET_PEER_RELIABLE_WINDOWS] >= ENET_PEER_RELIABLE_WINDOW_SIZE ||
  1157. channel -> usedReliableWindows & ((((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1) << reliableWindow) |
  1158. (((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1) >> (ENET_PEER_RELIABLE_WINDOW_SIZE - reliableWindow)))))
  1159. windowWrap = 1;
  1160. if (windowWrap)
  1161. {
  1162. currentCommand = enet_list_next (currentCommand);
  1163. continue;
  1164. }
  1165. }
  1166. if (outgoingCommand -> packet != NULL)
  1167. {
  1168. if (! windowExceeded)
  1169. {
  1170. enet_uint32 windowSize = (peer -> packetThrottle * peer -> windowSize) / ENET_PEER_PACKET_THROTTLE_SCALE;
  1171. if (peer -> reliableDataInTransit + outgoingCommand -> fragmentLength > ENET_MAX (windowSize, peer -> mtu))
  1172. windowExceeded = 1;
  1173. }
  1174. if (windowExceeded)
  1175. {
  1176. currentCommand = enet_list_next (currentCommand);
  1177. continue;
  1178. }
  1179. }
  1180. canPing = 0;
  1181. commandSize = commandSizes [outgoingCommand -> command.header.command & ENET_PROTOCOL_COMMAND_MASK];
  1182. if (command >= & host -> commands [sizeof (host -> commands) / sizeof (ENetProtocol)] ||
  1183. buffer + 1 >= & host -> buffers [sizeof (host -> buffers) / sizeof (ENetBuffer)] ||
  1184. peer -> mtu - host -> packetSize < commandSize ||
  1185. (outgoingCommand -> packet != NULL &&
  1186. (enet_uint16) (peer -> mtu - host -> packetSize) < (enet_uint16) (commandSize + outgoingCommand -> fragmentLength)))
  1187. {
  1188. host -> continueSending = 1;
  1189. break;
  1190. }
  1191. currentCommand = enet_list_next (currentCommand);
  1192. if (channel != NULL && outgoingCommand -> sendAttempts < 1)
  1193. {
  1194. channel -> usedReliableWindows |= 1 << reliableWindow;
  1195. ++ channel -> reliableWindows [reliableWindow];
  1196. }
  1197. ++ outgoingCommand -> sendAttempts;
  1198. if (outgoingCommand -> roundTripTimeout == 0)
  1199. {
  1200. outgoingCommand -> roundTripTimeout = peer -> roundTripTime + 4 * peer -> roundTripTimeVariance;
  1201. outgoingCommand -> roundTripTimeoutLimit = peer -> timeoutLimit * outgoingCommand -> roundTripTimeout;
  1202. }
  1203. if (enet_list_empty (& peer -> sentReliableCommands))
  1204. peer -> nextTimeout = host -> serviceTime + outgoingCommand -> roundTripTimeout;
  1205. enet_list_insert (enet_list_end (& peer -> sentReliableCommands),
  1206. enet_list_remove (& outgoingCommand -> outgoingCommandList));
  1207. outgoingCommand -> sentTime = host -> serviceTime;
  1208. buffer -> data = command;
  1209. buffer -> dataLength = commandSize;
  1210. host -> packetSize += buffer -> dataLength;
  1211. host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
  1212. * command = outgoingCommand -> command;
  1213. if (outgoingCommand -> packet != NULL)
  1214. {
  1215. ++ buffer;
  1216. buffer -> data = outgoingCommand -> packet -> data + outgoingCommand -> fragmentOffset;
  1217. buffer -> dataLength = outgoingCommand -> fragmentLength;
  1218. host -> packetSize += outgoingCommand -> fragmentLength;
  1219. peer -> reliableDataInTransit += outgoingCommand -> fragmentLength;
  1220. }
  1221. ++ peer -> packetsSent;
  1222. ++ command;
  1223. ++ buffer;
  1224. }
  1225. host -> commandCount = command - host -> commands;
  1226. host -> bufferCount = buffer - host -> buffers;
  1227. return canPing;
  1228. }
  1229. static int
  1230. enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts)
  1231. {
  1232. enet_uint8 headerData [sizeof (ENetProtocolHeader) + sizeof (enet_uint32)];
  1233. ENetProtocolHeader * header = (ENetProtocolHeader *) headerData;
  1234. ENetPeer * currentPeer;
  1235. int sentLength;
  1236. size_t shouldCompress = 0;
  1237. host -> continueSending = 1;
  1238. while (host -> continueSending)
  1239. for (host -> continueSending = 0,
  1240. currentPeer = host -> peers;
  1241. currentPeer < & host -> peers [host -> peerCount];
  1242. ++ currentPeer)
  1243. {
  1244. if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
  1245. currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
  1246. continue;
  1247. host -> headerFlags = 0;
  1248. host -> commandCount = 0;
  1249. host -> bufferCount = 1;
  1250. host -> packetSize = sizeof (ENetProtocolHeader);
  1251. if (! enet_list_empty (& currentPeer -> acknowledgements))
  1252. enet_protocol_send_acknowledgements (host, currentPeer);
  1253. if (checkForTimeouts != 0 &&
  1254. ! enet_list_empty (& currentPeer -> sentReliableCommands) &&
  1255. ENET_TIME_GREATER_EQUAL (host -> serviceTime, currentPeer -> nextTimeout) &&
  1256. enet_protocol_check_timeouts (host, currentPeer, event) == 1)
  1257. {
  1258. if (event != NULL && event -> type != ENET_EVENT_TYPE_NONE)
  1259. return 1;
  1260. else
  1261. continue;
  1262. }
  1263. if ((enet_list_empty (& currentPeer -> outgoingReliableCommands) ||
  1264. enet_protocol_send_reliable_outgoing_commands (host, currentPeer)) &&
  1265. enet_list_empty (& currentPeer -> sentReliableCommands) &&
  1266. ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> lastReceiveTime) >= currentPeer -> pingInterval &&
  1267. currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing))
  1268. {
  1269. enet_peer_ping (currentPeer);
  1270. enet_protocol_send_reliable_outgoing_commands (host, currentPeer);
  1271. }
  1272. if (! enet_list_empty (& currentPeer -> outgoingUnreliableCommands))
  1273. enet_protocol_send_unreliable_outgoing_commands (host, currentPeer);
  1274. if (host -> commandCount == 0)
  1275. continue;
  1276. if (currentPeer -> packetLossEpoch == 0)
  1277. currentPeer -> packetLossEpoch = host -> serviceTime;
  1278. else
  1279. if (ENET_TIME_DIFFERENCE (host -> serviceTime, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL &&
  1280. currentPeer -> packetsSent > 0)
  1281. {
  1282. enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent;
  1283. #ifdef ENET_DEBUG
  1284. #ifdef WIN32
  1285. printf (
  1286. #else
  1287. fprintf (stderr,
  1288. #endif
  1289. "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);
  1290. #endif
  1291. currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4;
  1292. if (packetLoss >= currentPeer -> packetLoss)
  1293. {
  1294. currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8;
  1295. currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4;
  1296. }
  1297. else
  1298. {
  1299. currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8;
  1300. currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4;
  1301. }
  1302. currentPeer -> packetLossEpoch = host -> serviceTime;
  1303. currentPeer -> packetsSent = 0;
  1304. currentPeer -> packetsLost = 0;
  1305. }
  1306. host -> buffers -> data = headerData;
  1307. if (host -> headerFlags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)
  1308. {
  1309. header -> sentTime = ENET_HOST_TO_NET_16 (host -> serviceTime & 0xFFFF);
  1310. host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
  1311. }
  1312. else
  1313. host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;
  1314. shouldCompress = 0;
  1315. if (host -> compressor.context != NULL && host -> compressor.compress != NULL)
  1316. {
  1317. size_t originalSize = host -> packetSize - sizeof(ENetProtocolHeader),
  1318. compressedSize = host -> compressor.compress (host -> compressor.context,
  1319. & host -> buffers [1], host -> bufferCount - 1,
  1320. originalSize,
  1321. host -> packetData [1],
  1322. originalSize);
  1323. if (compressedSize > 0 && compressedSize < originalSize)
  1324. {
  1325. host -> headerFlags |= ENET_PROTOCOL_HEADER_FLAG_COMPRESSED;
  1326. shouldCompress = compressedSize;
  1327. #ifdef ENET_DEBUG_COMPRESS
  1328. #ifdef WIN32
  1329. printf (
  1330. #else
  1331. fprintf (stderr,
  1332. #endif
  1333. "peer %u: compressed %u -> %u (%u%%)\n", currentPeer -> incomingPeerID, originalSize, compressedSize, (compressedSize * 100) / originalSize);
  1334. #endif
  1335. }
  1336. }
  1337. if (currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID)
  1338. host -> headerFlags |= currentPeer -> outgoingSessionID << ENET_PROTOCOL_HEADER_SESSION_SHIFT;
  1339. header -> peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID | host -> headerFlags);
  1340. if (host -> checksum != NULL)
  1341. {
  1342. enet_uint32 * checksum = (enet_uint32 *) & headerData [host -> buffers -> dataLength];
  1343. * checksum = currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID ? currentPeer -> connectID : 0;
  1344. host -> buffers -> dataLength += sizeof (enet_uint32);
  1345. * checksum = host -> checksum (host -> buffers, host -> bufferCount);
  1346. }
  1347. if (shouldCompress > 0)
  1348. {
  1349. host -> buffers [1].data = host -> packetData [1];
  1350. host -> buffers [1].dataLength = shouldCompress;
  1351. host -> bufferCount = 2;
  1352. }
  1353. currentPeer -> lastSendTime = host -> serviceTime;
  1354. sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
  1355. enet_protocol_remove_sent_unreliable_commands (currentPeer);
  1356. if (sentLength < 0)
  1357. return -1;
  1358. host -> totalSentData += sentLength;
  1359. host -> totalSentPackets ++;
  1360. }
  1361. return 0;
  1362. }
  1363. /** Sends any queued packets on the host specified to its designated peers.
  1364. @param host host to flush
  1365. @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().
  1366. @ingroup host
  1367. */
  1368. void
  1369. enet_host_flush (ENetHost * host)
  1370. {
  1371. host -> serviceTime = enet_time_get ();
  1372. enet_protocol_send_outgoing_commands (host, NULL, 0);
  1373. }
  1374. /** Checks for any queued events on the host and dispatches one if available.
  1375. @param host host to check for events
  1376. @param event an event structure where event details will be placed if available
  1377. @retval > 0 if an event was dispatched
  1378. @retval 0 if no events are available
  1379. @retval < 0 on failure
  1380. @ingroup host
  1381. */
  1382. int
  1383. enet_host_check_events (ENetHost * host, ENetEvent * event)
  1384. {
  1385. if (event == NULL) return -1;
  1386. event -> type = ENET_EVENT_TYPE_NONE;
  1387. event -> peer = NULL;
  1388. event -> packet = NULL;
  1389. return enet_protocol_dispatch_incoming_commands (host, event);
  1390. }
  1391. /** Waits for events on the host specified and shuttles packets between
  1392. the host and its peers.
  1393. @param host host to service
  1394. @param event an event structure where event details will be placed if one occurs
  1395. if event == NULL then no events will be delivered
  1396. @param timeout number of milliseconds that ENet should wait for events
  1397. @retval > 0 if an event occurred within the specified time limit
  1398. @retval 0 if no event occurred
  1399. @retval < 0 on failure
  1400. @remarks enet_host_service should be called fairly regularly for adequate performance
  1401. @ingroup host
  1402. */
  1403. int
  1404. enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
  1405. {
  1406. enet_uint32 waitCondition;
  1407. if (event != NULL)
  1408. {
  1409. event -> type = ENET_EVENT_TYPE_NONE;
  1410. event -> peer = NULL;
  1411. event -> packet = NULL;
  1412. switch (enet_protocol_dispatch_incoming_commands (host, event))
  1413. {
  1414. case 1:
  1415. return 1;
  1416. case -1:
  1417. perror ("Error dispatching incoming packets");
  1418. return -1;
  1419. default:
  1420. break;
  1421. }
  1422. }
  1423. host -> serviceTime = enet_time_get ();
  1424. timeout += host -> serviceTime;
  1425. do
  1426. {
  1427. if (ENET_TIME_DIFFERENCE (host -> serviceTime, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
  1428. enet_host_bandwidth_throttle (host);
  1429. switch (enet_protocol_send_outgoing_commands (host, event, 1))
  1430. {
  1431. case 1:
  1432. return 1;
  1433. case -1:
  1434. perror ("Error sending outgoing packets");
  1435. return -1;
  1436. default:
  1437. break;
  1438. }
  1439. switch (enet_protocol_receive_incoming_commands (host, event))
  1440. {
  1441. case 1:
  1442. return 1;
  1443. case -1:
  1444. perror ("Error receiving incoming packets");
  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. perror ("Error sending outgoing packets");
  1455. return -1;
  1456. default:
  1457. break;
  1458. }
  1459. if (event != NULL)
  1460. {
  1461. switch (enet_protocol_dispatch_incoming_commands (host, event))
  1462. {
  1463. case 1:
  1464. return 1;
  1465. case -1:
  1466. perror ("Error dispatching incoming packets");
  1467. return -1;
  1468. default:
  1469. break;
  1470. }
  1471. }
  1472. host -> serviceTime = enet_time_get ();
  1473. if (ENET_TIME_GREATER_EQUAL (host -> serviceTime, timeout))
  1474. return 0;
  1475. waitCondition = ENET_SOCKET_WAIT_RECEIVE;
  1476. if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
  1477. return -1;
  1478. host -> serviceTime = enet_time_get ();
  1479. } while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);
  1480. return 0;
  1481. }