enet.h 245 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093
  1. /**
  2. * include/enet.h - a Single-Header auto-generated variant of enet.h library.
  3. *
  4. * Usage:
  5. * #define ENET_IMPLEMENTATION exactly in ONE source file right BEFORE including the library, like:
  6. *
  7. * #define ENET_IMPLEMENTATION
  8. * #include <enet.h>
  9. *
  10. * License:
  11. * The MIT License (MIT)
  12. *
  13. * Copyright (c) 2002-2016 Lee Salzman
  14. * Copyright (c) 2017-2021 Vladyslav Hrytsenko, Dominik Madarász
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the "Software"), to deal
  18. * in the Software without restriction, including without limitation the rights
  19. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. * copies of the Software, and to permit persons to whom the Software is
  21. * furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in all
  24. * copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. */
  35. #ifndef ENET_INCLUDE_H
  36. #define ENET_INCLUDE_H
  37. #include <stdlib.h>
  38. #include <stdbool.h>
  39. #include <stdint.h>
  40. #include <time.h>
  41. #define ENET_VERSION_MAJOR 2
  42. #define ENET_VERSION_MINOR 3
  43. #define ENET_VERSION_PATCH 0
  44. #define ENET_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch))
  45. #define ENET_VERSION_GET_MAJOR(version) (((version)>>16)&0xFF)
  46. #define ENET_VERSION_GET_MINOR(version) (((version)>>8)&0xFF)
  47. #define ENET_VERSION_GET_PATCH(version) ((version)&0xFF)
  48. #define ENET_VERSION ENET_VERSION_CREATE(ENET_VERSION_MAJOR, ENET_VERSION_MINOR, ENET_VERSION_PATCH)
  49. #define ENET_TIME_OVERFLOW 86400000
  50. #define ENET_TIME_LESS(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW)
  51. #define ENET_TIME_GREATER(a, b) ((b) - (a) >= ENET_TIME_OVERFLOW)
  52. #define ENET_TIME_LESS_EQUAL(a, b) (! ENET_TIME_GREATER (a, b))
  53. #define ENET_TIME_GREATER_EQUAL(a, b) (! ENET_TIME_LESS (a, b))
  54. #define ENET_TIME_DIFFERENCE(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW ? (b) - (a) : (a) - (b))
  55. // =======================================================================//
  56. // !
  57. // ! System differences
  58. // !
  59. // =======================================================================//
  60. #if defined(_WIN32)
  61. #if _WIN32_WINNT < 0x0600
  62. #undef _WIN32_WINNT
  63. #define _WIN32_WINNT 0x0600
  64. #endif
  65. #if defined(_MSC_VER) && defined(ENET_IMPLEMENTATION)
  66. #pragma warning (disable: 4267) // size_t to int conversion
  67. #pragma warning (disable: 4244) // 64bit to 32bit int
  68. #pragma warning (disable: 4018) // signed/unsigned mismatch
  69. #pragma warning (disable: 4146) // unary minus operator applied to unsigned type
  70. #endif
  71. #if !defined(ENET_NO_PRAGMA_LINK) && !defined(__MINGW32__)
  72. #pragma comment(lib, "ws2_32.lib")
  73. #pragma comment(lib, "winmm.lib")
  74. #endif
  75. #if _MSC_VER >= 1910
  76. /* It looks like there were changes as of Visual Studio 2017 and there are no 32/64 bit
  77. versions of _InterlockedExchange[operation], only InterlockedExchange[operation]
  78. (without leading underscore), so we have to distinguish between compiler versions */
  79. #define NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  80. #endif
  81. #ifdef __GNUC__
  82. #if (_WIN32_WINNT < 0x0501)
  83. #undef _WIN32_WINNT
  84. #define _WIN32_WINNT 0x0501
  85. #endif
  86. #endif
  87. #include <winsock2.h>
  88. #include <ws2tcpip.h>
  89. #include <mmsystem.h>
  90. #include <intrin.h>
  91. #if defined(_WIN32) && defined(_MSC_VER)
  92. #if _MSC_VER < 1900
  93. typedef struct timespec {
  94. long tv_sec;
  95. long tv_nsec;
  96. };
  97. #endif
  98. #define CLOCK_MONOTONIC 0
  99. #endif
  100. typedef SOCKET ENetSocket;
  101. #define ENET_SOCKET_NULL INVALID_SOCKET
  102. #define ENET_HOST_TO_NET_16(value) (htons(value))
  103. #define ENET_HOST_TO_NET_32(value) (htonl(value))
  104. #define ENET_NET_TO_HOST_16(value) (ntohs(value))
  105. #define ENET_NET_TO_HOST_32(value) (ntohl(value))
  106. typedef struct {
  107. size_t dataLength;
  108. void * data;
  109. } ENetBuffer;
  110. #define ENET_CALLBACK __cdecl
  111. #ifdef ENET_DLL
  112. #ifdef ENET_IMPLEMENTATION
  113. #define ENET_API __declspec( dllexport )
  114. #else
  115. #define ENET_API __declspec( dllimport )
  116. #endif // ENET_IMPLEMENTATION
  117. #else
  118. #define ENET_API extern
  119. #endif // ENET_DLL
  120. typedef fd_set ENetSocketSet;
  121. #define ENET_SOCKETSET_EMPTY(sockset) FD_ZERO(&(sockset))
  122. #define ENET_SOCKETSET_ADD(sockset, socket) FD_SET(socket, &(sockset))
  123. #define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLR(socket, &(sockset))
  124. #define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET(socket, &(sockset))
  125. #else
  126. #include <sys/types.h>
  127. #include <sys/ioctl.h>
  128. #include <sys/time.h>
  129. #include <sys/socket.h>
  130. #include <poll.h>
  131. #include <arpa/inet.h>
  132. #include <netinet/in.h>
  133. #include <netinet/tcp.h>
  134. #include <netdb.h>
  135. #include <unistd.h>
  136. #include <string.h>
  137. #include <errno.h>
  138. #include <fcntl.h>
  139. #ifdef __APPLE__
  140. #include <mach/clock.h>
  141. #include <mach/mach.h>
  142. #include <Availability.h>
  143. #endif
  144. #ifndef MSG_NOSIGNAL
  145. #define MSG_NOSIGNAL 0
  146. #endif
  147. #ifdef MSG_MAXIOVLEN
  148. #define ENET_BUFFER_MAXIMUM MSG_MAXIOVLEN
  149. #endif
  150. typedef int ENetSocket;
  151. #define ENET_SOCKET_NULL -1
  152. #define ENET_HOST_TO_NET_16(value) (htons(value)) /**< macro that converts host to net byte-order of a 16-bit value */
  153. #define ENET_HOST_TO_NET_32(value) (htonl(value)) /**< macro that converts host to net byte-order of a 32-bit value */
  154. #define ENET_NET_TO_HOST_16(value) (ntohs(value)) /**< macro that converts net to host byte-order of a 16-bit value */
  155. #define ENET_NET_TO_HOST_32(value) (ntohl(value)) /**< macro that converts net to host byte-order of a 32-bit value */
  156. typedef struct {
  157. void * data;
  158. size_t dataLength;
  159. } ENetBuffer;
  160. #define ENET_CALLBACK
  161. #define ENET_API extern
  162. typedef fd_set ENetSocketSet;
  163. #define ENET_SOCKETSET_EMPTY(sockset) FD_ZERO(&(sockset))
  164. #define ENET_SOCKETSET_ADD(sockset, socket) FD_SET(socket, &(sockset))
  165. #define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLR(socket, &(sockset))
  166. #define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET(socket, &(sockset))
  167. #endif
  168. #ifdef __GNUC__
  169. #define ENET_DEPRECATED(func) func __attribute__ ((deprecated))
  170. #elif defined(_MSC_VER)
  171. #define ENET_DEPRECATED(func) __declspec(deprecated) func
  172. #else
  173. #pragma message("WARNING: Please ENET_DEPRECATED for this compiler")
  174. #define ENET_DEPRECATED(func) func
  175. #endif
  176. #ifndef ENET_BUFFER_MAXIMUM
  177. #define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS)
  178. #endif
  179. #define ENET_UNUSED(x) (void)x;
  180. #define ENET_MAX(x, y) ((x) > (y) ? (x) : (y))
  181. #define ENET_MIN(x, y) ((x) < (y) ? (x) : (y))
  182. #define ENET_IPV6 1
  183. static const struct in6_addr enet_v4_anyaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}};
  184. static const struct in6_addr enet_v4_noaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}};
  185. static const struct in6_addr enet_v4_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }}};
  186. static const struct in6_addr enet_v6_anyaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}};
  187. static const struct in6_addr enet_v6_noaddr = {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}};
  188. static const struct in6_addr enet_v6_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}};
  189. #define ENET_HOST_ANY in6addr_any
  190. #define ENET_HOST_BROADCAST 0xFFFFFFFFU
  191. #define ENET_PORT_ANY 0
  192. #ifdef __cplusplus
  193. extern "C" {
  194. #endif
  195. // =======================================================================//
  196. // !
  197. // ! Basic stuff
  198. // !
  199. // =======================================================================//
  200. typedef uint8_t enet_uint8; /**< unsigned 8-bit type */
  201. typedef uint16_t enet_uint16; /**< unsigned 16-bit type */
  202. typedef uint32_t enet_uint32; /**< unsigned 32-bit type */
  203. typedef uint64_t enet_uint64; /**< unsigned 64-bit type */
  204. typedef enet_uint32 ENetVersion;
  205. typedef struct _ENetPacket ENetPacket;
  206. typedef struct _ENetCallbacks {
  207. void *(ENET_CALLBACK *malloc) (size_t size);
  208. void (ENET_CALLBACK *free) (void *memory);
  209. void (ENET_CALLBACK *no_memory) (void);
  210. ENetPacket *(ENET_CALLBACK *packet_create) (const void *data, size_t dataLength, enet_uint32 flags);
  211. void (ENET_CALLBACK *packet_destroy) (ENetPacket *packet);
  212. } ENetCallbacks;
  213. extern void *enet_malloc(size_t);
  214. extern void enet_free(void *);
  215. extern ENetPacket* enet_packet_create(const void*,size_t,enet_uint32);
  216. extern ENetPacket* enet_packet_copy(ENetPacket*);
  217. extern void enet_packet_destroy(ENetPacket*);
  218. // =======================================================================//
  219. // !
  220. // ! List
  221. // !
  222. // =======================================================================//
  223. typedef struct _ENetListNode {
  224. struct _ENetListNode *next;
  225. struct _ENetListNode *previous;
  226. } ENetListNode;
  227. typedef ENetListNode *ENetListIterator;
  228. typedef struct _ENetList {
  229. ENetListNode sentinel;
  230. } ENetList;
  231. extern ENetListIterator enet_list_insert(ENetListIterator, void *);
  232. extern ENetListIterator enet_list_move(ENetListIterator, void *, void *);
  233. extern void *enet_list_remove(ENetListIterator);
  234. extern void enet_list_clear(ENetList *);
  235. extern size_t enet_list_size(ENetList *);
  236. #define enet_list_begin(list) ((list)->sentinel.next)
  237. #define enet_list_end(list) (&(list)->sentinel)
  238. #define enet_list_empty(list) (enet_list_begin(list) == enet_list_end(list))
  239. #define enet_list_next(iterator) ((iterator)->next)
  240. #define enet_list_previous(iterator) ((iterator)->previous)
  241. #define enet_list_front(list) ((void *)(list)->sentinel.next)
  242. #define enet_list_back(list) ((void *)(list)->sentinel.previous)
  243. // =======================================================================//
  244. // !
  245. // ! Protocol
  246. // !
  247. // =======================================================================//
  248. enum {
  249. ENET_PROTOCOL_MINIMUM_MTU = 576,
  250. ENET_PROTOCOL_MAXIMUM_MTU = 4096,
  251. ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS = 32,
  252. ENET_PROTOCOL_MINIMUM_WINDOW_SIZE = 4096,
  253. ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE = 65536,
  254. ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 1,
  255. ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 255,
  256. ENET_PROTOCOL_MAXIMUM_PEER_ID = 0xFFF,
  257. ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT = 1024 * 1024
  258. };
  259. typedef enum _ENetProtocolCommand {
  260. ENET_PROTOCOL_COMMAND_NONE = 0,
  261. ENET_PROTOCOL_COMMAND_ACKNOWLEDGE = 1,
  262. ENET_PROTOCOL_COMMAND_CONNECT = 2,
  263. ENET_PROTOCOL_COMMAND_VERIFY_CONNECT = 3,
  264. ENET_PROTOCOL_COMMAND_DISCONNECT = 4,
  265. ENET_PROTOCOL_COMMAND_PING = 5,
  266. ENET_PROTOCOL_COMMAND_SEND_RELIABLE = 6,
  267. ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE = 7,
  268. ENET_PROTOCOL_COMMAND_SEND_FRAGMENT = 8,
  269. ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED = 9,
  270. ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT = 10,
  271. ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE = 11,
  272. ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT = 12,
  273. ENET_PROTOCOL_COMMAND_COUNT = 13,
  274. ENET_PROTOCOL_COMMAND_MASK = 0x0F
  275. } ENetProtocolCommand;
  276. typedef enum _ENetProtocolFlag {
  277. ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE = (1 << 7),
  278. ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED = (1 << 6),
  279. ENET_PROTOCOL_HEADER_FLAG_COMPRESSED = (1 << 14),
  280. ENET_PROTOCOL_HEADER_FLAG_SENT_TIME = (1 << 15),
  281. ENET_PROTOCOL_HEADER_FLAG_MASK = ENET_PROTOCOL_HEADER_FLAG_COMPRESSED | ENET_PROTOCOL_HEADER_FLAG_SENT_TIME,
  282. ENET_PROTOCOL_HEADER_SESSION_MASK = (3 << 12),
  283. ENET_PROTOCOL_HEADER_SESSION_SHIFT = 12
  284. } ENetProtocolFlag;
  285. #ifdef _MSC_VER
  286. #pragma pack(push, 1)
  287. #define ENET_PACKED
  288. #elif defined(__GNUC__) || defined(__clang__)
  289. #define ENET_PACKED __attribute__ ((packed))
  290. #else
  291. #define ENET_PACKED
  292. #endif
  293. typedef struct _ENetProtocolHeader {
  294. enet_uint16 peerID;
  295. enet_uint16 sentTime;
  296. } ENET_PACKED ENetProtocolHeader;
  297. typedef struct _ENetProtocolCommandHeader {
  298. enet_uint8 command;
  299. enet_uint8 channelID;
  300. enet_uint16 reliableSequenceNumber;
  301. } ENET_PACKED ENetProtocolCommandHeader;
  302. typedef struct _ENetProtocolAcknowledge {
  303. ENetProtocolCommandHeader header;
  304. enet_uint16 receivedReliableSequenceNumber;
  305. enet_uint16 receivedSentTime;
  306. } ENET_PACKED ENetProtocolAcknowledge;
  307. typedef struct _ENetProtocolConnect {
  308. ENetProtocolCommandHeader header;
  309. enet_uint16 outgoingPeerID;
  310. enet_uint8 incomingSessionID;
  311. enet_uint8 outgoingSessionID;
  312. enet_uint32 mtu;
  313. enet_uint32 windowSize;
  314. enet_uint32 channelCount;
  315. enet_uint32 incomingBandwidth;
  316. enet_uint32 outgoingBandwidth;
  317. enet_uint32 packetThrottleInterval;
  318. enet_uint32 packetThrottleAcceleration;
  319. enet_uint32 packetThrottleDeceleration;
  320. enet_uint32 connectID;
  321. enet_uint32 data;
  322. } ENET_PACKED ENetProtocolConnect;
  323. typedef struct _ENetProtocolVerifyConnect {
  324. ENetProtocolCommandHeader header;
  325. enet_uint16 outgoingPeerID;
  326. enet_uint8 incomingSessionID;
  327. enet_uint8 outgoingSessionID;
  328. enet_uint32 mtu;
  329. enet_uint32 windowSize;
  330. enet_uint32 channelCount;
  331. enet_uint32 incomingBandwidth;
  332. enet_uint32 outgoingBandwidth;
  333. enet_uint32 packetThrottleInterval;
  334. enet_uint32 packetThrottleAcceleration;
  335. enet_uint32 packetThrottleDeceleration;
  336. enet_uint32 connectID;
  337. } ENET_PACKED ENetProtocolVerifyConnect;
  338. typedef struct _ENetProtocolBandwidthLimit {
  339. ENetProtocolCommandHeader header;
  340. enet_uint32 incomingBandwidth;
  341. enet_uint32 outgoingBandwidth;
  342. } ENET_PACKED ENetProtocolBandwidthLimit;
  343. typedef struct _ENetProtocolThrottleConfigure {
  344. ENetProtocolCommandHeader header;
  345. enet_uint32 packetThrottleInterval;
  346. enet_uint32 packetThrottleAcceleration;
  347. enet_uint32 packetThrottleDeceleration;
  348. } ENET_PACKED ENetProtocolThrottleConfigure;
  349. typedef struct _ENetProtocolDisconnect {
  350. ENetProtocolCommandHeader header;
  351. enet_uint32 data;
  352. } ENET_PACKED ENetProtocolDisconnect;
  353. typedef struct _ENetProtocolPing {
  354. ENetProtocolCommandHeader header;
  355. } ENET_PACKED ENetProtocolPing;
  356. typedef struct _ENetProtocolSendReliable {
  357. ENetProtocolCommandHeader header;
  358. enet_uint16 dataLength;
  359. } ENET_PACKED ENetProtocolSendReliable;
  360. typedef struct _ENetProtocolSendUnreliable {
  361. ENetProtocolCommandHeader header;
  362. enet_uint16 unreliableSequenceNumber;
  363. enet_uint16 dataLength;
  364. } ENET_PACKED ENetProtocolSendUnreliable;
  365. typedef struct _ENetProtocolSendUnsequenced {
  366. ENetProtocolCommandHeader header;
  367. enet_uint16 unsequencedGroup;
  368. enet_uint16 dataLength;
  369. } ENET_PACKED ENetProtocolSendUnsequenced;
  370. typedef struct _ENetProtocolSendFragment {
  371. ENetProtocolCommandHeader header;
  372. enet_uint16 startSequenceNumber;
  373. enet_uint16 dataLength;
  374. enet_uint32 fragmentCount;
  375. enet_uint32 fragmentNumber;
  376. enet_uint32 totalLength;
  377. enet_uint32 fragmentOffset;
  378. } ENET_PACKED ENetProtocolSendFragment;
  379. typedef union _ENetProtocol {
  380. ENetProtocolCommandHeader header;
  381. ENetProtocolAcknowledge acknowledge;
  382. ENetProtocolConnect connect;
  383. ENetProtocolVerifyConnect verifyConnect;
  384. ENetProtocolDisconnect disconnect;
  385. ENetProtocolPing ping;
  386. ENetProtocolSendReliable sendReliable;
  387. ENetProtocolSendUnreliable sendUnreliable;
  388. ENetProtocolSendUnsequenced sendUnsequenced;
  389. ENetProtocolSendFragment sendFragment;
  390. ENetProtocolBandwidthLimit bandwidthLimit;
  391. ENetProtocolThrottleConfigure throttleConfigure;
  392. } ENET_PACKED ENetProtocol;
  393. #ifdef _MSC_VER
  394. #pragma pack(pop)
  395. #endif
  396. // =======================================================================//
  397. // !
  398. // ! General ENet structs/enums
  399. // !
  400. // =======================================================================//
  401. typedef enum _ENetSocketType {
  402. ENET_SOCKET_TYPE_STREAM = 1,
  403. ENET_SOCKET_TYPE_DATAGRAM = 2
  404. } ENetSocketType;
  405. typedef enum _ENetSocketWait {
  406. ENET_SOCKET_WAIT_NONE = 0,
  407. ENET_SOCKET_WAIT_SEND = (1 << 0),
  408. ENET_SOCKET_WAIT_RECEIVE = (1 << 1),
  409. ENET_SOCKET_WAIT_INTERRUPT = (1 << 2)
  410. } ENetSocketWait;
  411. typedef enum _ENetSocketOption {
  412. ENET_SOCKOPT_NONBLOCK = 1,
  413. ENET_SOCKOPT_BROADCAST = 2,
  414. ENET_SOCKOPT_RCVBUF = 3,
  415. ENET_SOCKOPT_SNDBUF = 4,
  416. ENET_SOCKOPT_REUSEADDR = 5,
  417. ENET_SOCKOPT_RCVTIMEO = 6,
  418. ENET_SOCKOPT_SNDTIMEO = 7,
  419. ENET_SOCKOPT_ERROR = 8,
  420. ENET_SOCKOPT_NODELAY = 9,
  421. ENET_SOCKOPT_IPV6_V6ONLY = 10,
  422. } ENetSocketOption;
  423. typedef enum _ENetSocketShutdown {
  424. ENET_SOCKET_SHUTDOWN_READ = 0,
  425. ENET_SOCKET_SHUTDOWN_WRITE = 1,
  426. ENET_SOCKET_SHUTDOWN_READ_WRITE = 2
  427. } ENetSocketShutdown;
  428. /**
  429. * Portable internet address structure.
  430. *
  431. * The host must be specified in network byte-order, and the port must be in host
  432. * byte-order. The constant ENET_HOST_ANY may be used to specify the default
  433. * server host. The constant ENET_HOST_BROADCAST may be used to specify the
  434. * broadcast address (255.255.255.255). This makes sense for enet_host_connect,
  435. * but not for enet_host_create. Once a server responds to a broadcast, the
  436. * address is updated from ENET_HOST_BROADCAST to the server's actual IP address.
  437. */
  438. typedef struct _ENetAddress {
  439. struct in6_addr host;
  440. enet_uint16 port;
  441. enet_uint16 sin6_scope_id;
  442. } ENetAddress;
  443. #define in6_equal(in6_addr_a, in6_addr_b) (memcmp(&in6_addr_a, &in6_addr_b, sizeof(struct in6_addr)) == 0)
  444. /**
  445. * Packet flag bit constants.
  446. *
  447. * The host must be specified in network byte-order, and the port must be in
  448. * host byte-order. The constant ENET_HOST_ANY may be used to specify the
  449. * default server host.
  450. *
  451. * @sa ENetPacket
  452. */
  453. typedef enum _ENetPacketFlag {
  454. ENET_PACKET_FLAG_RELIABLE = (1 << 0), /** packet must be received by the target peer and resend attempts should be made until the packet is delivered */
  455. ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1), /** packet will not be sequenced with other packets not supported for reliable packets */
  456. ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2), /** packet will not allocate data, and user must supply it instead */
  457. ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT = (1 << 3), /** packet will be fragmented using unreliable (instead of reliable) sends if it exceeds the MTU */
  458. ENET_PACKET_FLAG_SENT = (1 << 8), /** whether the packet has been sent from all queues it has been entered into */
  459. } ENetPacketFlag;
  460. typedef void (ENET_CALLBACK *ENetPacketFreeCallback)(void *);
  461. /**
  462. * ENet packet structure.
  463. *
  464. * An ENet data packet that may be sent to or received from a peer. The shown
  465. * fields should only be read and never modified. The data field contains the
  466. * allocated data for the packet. The dataLength fields specifies the length
  467. * of the allocated data. The flags field is either 0 (specifying no flags),
  468. * or a bitwise-or of any combination of the following flags:
  469. *
  470. * ENET_PACKET_FLAG_RELIABLE - packet must be received by the target peer and resend attempts should be made until the packet is delivered
  471. * ENET_PACKET_FLAG_UNSEQUENCED - packet will not be sequenced with other packets (not supported for reliable packets)
  472. * ENET_PACKET_FLAG_NO_ALLOCATE - packet will not allocate data, and user must supply it instead
  473. * ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT - packet will be fragmented using unreliable (instead of reliable) sends if it exceeds the MTU
  474. * ENET_PACKET_FLAG_SENT - whether the packet has been sent from all queues it has been entered into
  475. * @sa ENetPacketFlag
  476. */
  477. typedef struct _ENetPacket {
  478. size_t referenceCount; /**< internal use only */
  479. enet_uint32 flags; /**< bitwise-or of ENetPacketFlag constants */
  480. enet_uint8 * data; /**< allocated data for packet */
  481. size_t dataLength; /**< length of data */
  482. ENetPacketFreeCallback freeCallback; /**< function to be called when the packet is no longer in use */
  483. void * userData; /**< application private data, may be freely modified */
  484. } ENetPacket;
  485. typedef struct _ENetAcknowledgement {
  486. ENetListNode acknowledgementList;
  487. enet_uint32 sentTime;
  488. ENetProtocol command;
  489. } ENetAcknowledgement;
  490. typedef struct _ENetOutgoingCommand {
  491. ENetListNode outgoingCommandList;
  492. enet_uint16 reliableSequenceNumber;
  493. enet_uint16 unreliableSequenceNumber;
  494. enet_uint32 sentTime;
  495. enet_uint32 roundTripTimeout;
  496. enet_uint32 roundTripTimeoutLimit;
  497. enet_uint32 fragmentOffset;
  498. enet_uint16 fragmentLength;
  499. enet_uint16 sendAttempts;
  500. ENetProtocol command;
  501. ENetPacket * packet;
  502. } ENetOutgoingCommand;
  503. typedef struct _ENetIncomingCommand {
  504. ENetListNode incomingCommandList;
  505. enet_uint16 reliableSequenceNumber;
  506. enet_uint16 unreliableSequenceNumber;
  507. ENetProtocol command;
  508. enet_uint32 fragmentCount;
  509. enet_uint32 fragmentsRemaining;
  510. enet_uint32 *fragments;
  511. ENetPacket * packet;
  512. } ENetIncomingCommand;
  513. typedef enum _ENetPeerState {
  514. ENET_PEER_STATE_DISCONNECTED = 0,
  515. ENET_PEER_STATE_CONNECTING = 1,
  516. ENET_PEER_STATE_ACKNOWLEDGING_CONNECT = 2,
  517. ENET_PEER_STATE_CONNECTION_PENDING = 3,
  518. ENET_PEER_STATE_CONNECTION_SUCCEEDED = 4,
  519. ENET_PEER_STATE_CONNECTED = 5,
  520. ENET_PEER_STATE_DISCONNECT_LATER = 6,
  521. ENET_PEER_STATE_DISCONNECTING = 7,
  522. ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT = 8,
  523. ENET_PEER_STATE_ZOMBIE = 9
  524. } ENetPeerState;
  525. enum {
  526. ENET_HOST_RECEIVE_BUFFER_SIZE = 256 * 1024,
  527. ENET_HOST_SEND_BUFFER_SIZE = 256 * 1024,
  528. ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000,
  529. ENET_HOST_DEFAULT_MTU = 1400,
  530. ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE = 32 * 1024 * 1024,
  531. ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA = 32 * 1024 * 1024,
  532. ENET_PEER_DEFAULT_ROUND_TRIP_TIME = 500,
  533. ENET_PEER_DEFAULT_PACKET_THROTTLE = 32,
  534. ENET_PEER_PACKET_THROTTLE_SCALE = 32,
  535. ENET_PEER_PACKET_THROTTLE_COUNTER = 7,
  536. ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2,
  537. ENET_PEER_PACKET_THROTTLE_DECELERATION = 2,
  538. ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000,
  539. ENET_PEER_PACKET_LOSS_SCALE = (1 << 16),
  540. ENET_PEER_PACKET_LOSS_INTERVAL = 10000,
  541. ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024,
  542. ENET_PEER_TIMEOUT_LIMIT = 32,
  543. ENET_PEER_TIMEOUT_MINIMUM = 5000,
  544. ENET_PEER_TIMEOUT_MAXIMUM = 30000,
  545. ENET_PEER_PING_INTERVAL = 500,
  546. ENET_PEER_UNSEQUENCED_WINDOWS = 64,
  547. ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024,
  548. ENET_PEER_FREE_UNSEQUENCED_WINDOWS = 32,
  549. ENET_PEER_RELIABLE_WINDOWS = 16,
  550. ENET_PEER_RELIABLE_WINDOW_SIZE = 0x1000,
  551. ENET_PEER_FREE_RELIABLE_WINDOWS = 8
  552. };
  553. typedef struct _ENetChannel {
  554. enet_uint16 outgoingReliableSequenceNumber;
  555. enet_uint16 outgoingUnreliableSequenceNumber;
  556. enet_uint16 usedReliableWindows;
  557. enet_uint16 reliableWindows[ENET_PEER_RELIABLE_WINDOWS];
  558. enet_uint16 incomingReliableSequenceNumber;
  559. enet_uint16 incomingUnreliableSequenceNumber;
  560. ENetList incomingReliableCommands;
  561. ENetList incomingUnreliableCommands;
  562. } ENetChannel;
  563. /**
  564. * An ENet peer which data packets may be sent or received from.
  565. *
  566. * No fields should be modified unless otherwise specified.
  567. */
  568. typedef struct _ENetPeer {
  569. ENetListNode dispatchList;
  570. struct _ENetHost *host;
  571. enet_uint16 outgoingPeerID;
  572. enet_uint16 incomingPeerID;
  573. enet_uint32 connectID;
  574. enet_uint8 outgoingSessionID;
  575. enet_uint8 incomingSessionID;
  576. ENetAddress address; /**< Internet address of the peer */
  577. void * data; /**< Application private data, may be freely modified */
  578. ENetPeerState state;
  579. ENetChannel * channels;
  580. size_t channelCount; /**< Number of channels allocated for communication with peer */
  581. enet_uint32 incomingBandwidth; /**< Downstream bandwidth of the client in bytes/second */
  582. enet_uint32 outgoingBandwidth; /**< Upstream bandwidth of the client in bytes/second */
  583. enet_uint32 incomingBandwidthThrottleEpoch;
  584. enet_uint32 outgoingBandwidthThrottleEpoch;
  585. enet_uint32 incomingDataTotal;
  586. enet_uint64 totalDataReceived;
  587. enet_uint32 outgoingDataTotal;
  588. enet_uint64 totalDataSent;
  589. enet_uint32 lastSendTime;
  590. enet_uint32 lastReceiveTime;
  591. enet_uint32 nextTimeout;
  592. enet_uint32 earliestTimeout;
  593. enet_uint32 packetLossEpoch;
  594. enet_uint32 packetsSent;
  595. enet_uint64 totalPacketsSent; /**< total number of packets sent during a session */
  596. enet_uint32 packetsLost;
  597. enet_uint32 totalPacketsLost; /**< total number of packets lost during a session */
  598. enet_uint32 packetLoss; /**< mean packet loss of reliable packets as a ratio with respect to the constant ENET_PEER_PACKET_LOSS_SCALE */
  599. enet_uint32 packetLossVariance;
  600. enet_uint32 packetThrottle;
  601. enet_uint32 packetThrottleLimit;
  602. enet_uint32 packetThrottleCounter;
  603. enet_uint32 packetThrottleEpoch;
  604. enet_uint32 packetThrottleAcceleration;
  605. enet_uint32 packetThrottleDeceleration;
  606. enet_uint32 packetThrottleInterval;
  607. enet_uint32 pingInterval;
  608. enet_uint32 timeoutLimit;
  609. enet_uint32 timeoutMinimum;
  610. enet_uint32 timeoutMaximum;
  611. enet_uint32 lastRoundTripTime;
  612. enet_uint32 lowestRoundTripTime;
  613. enet_uint32 lastRoundTripTimeVariance;
  614. enet_uint32 highestRoundTripTimeVariance;
  615. enet_uint32 roundTripTime; /**< mean round trip time (RTT), in milliseconds, between sending a reliable packet and receiving its acknowledgement */
  616. enet_uint32 roundTripTimeVariance;
  617. enet_uint32 mtu;
  618. enet_uint32 windowSize;
  619. enet_uint32 reliableDataInTransit;
  620. enet_uint16 outgoingReliableSequenceNumber;
  621. ENetList acknowledgements;
  622. ENetList sentReliableCommands;
  623. ENetList sentUnreliableCommands;
  624. ENetList outgoingReliableCommands;
  625. ENetList outgoingUnreliableCommands;
  626. ENetList dispatchedCommands;
  627. int needsDispatch;
  628. enet_uint16 incomingUnsequencedGroup;
  629. enet_uint16 outgoingUnsequencedGroup;
  630. enet_uint32 unsequencedWindow[ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32];
  631. enet_uint32 eventData;
  632. size_t totalWaitingData;
  633. } ENetPeer;
  634. /** An ENet packet compressor for compressing UDP packets before socket sends or receives. */
  635. typedef struct _ENetCompressor {
  636. /** Context data for the compressor. Must be non-NULL. */
  637. void *context;
  638. /** Compresses from inBuffers[0:inBufferCount-1], containing inLimit bytes, to outData, outputting at most outLimit bytes. Should return 0 on failure. */
  639. size_t(ENET_CALLBACK * compress) (void *context, const ENetBuffer * inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 * outData, size_t outLimit);
  640. /** Decompresses from inData, containing inLimit bytes, to outData, outputting at most outLimit bytes. Should return 0 on failure. */
  641. size_t(ENET_CALLBACK * decompress) (void *context, const enet_uint8 * inData, size_t inLimit, enet_uint8 * outData, size_t outLimit);
  642. /** Destroys the context when compression is disabled or the host is destroyed. May be NULL. */
  643. void (ENET_CALLBACK * destroy)(void *context);
  644. } ENetCompressor;
  645. /** Callback that computes the checksum of the data held in buffers[0:bufferCount-1] */
  646. typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback)(const ENetBuffer *buffers, size_t bufferCount);
  647. /** Callback for intercepting received raw UDP packets. Should return 1 to intercept, 0 to ignore, or -1 to propagate an error. */
  648. typedef int (ENET_CALLBACK * ENetInterceptCallback)(struct _ENetHost *host, void *event);
  649. /** An ENet host for communicating with peers.
  650. *
  651. * No fields should be modified unless otherwise stated.
  652. *
  653. * @sa enet_host_create()
  654. * @sa enet_host_destroy()
  655. * @sa enet_host_connect()
  656. * @sa enet_host_service()
  657. * @sa enet_host_flush()
  658. * @sa enet_host_broadcast()
  659. * @sa enet_host_compress()
  660. * @sa enet_host_channel_limit()
  661. * @sa enet_host_bandwidth_limit()
  662. * @sa enet_host_bandwidth_throttle()
  663. */
  664. typedef struct _ENetHost {
  665. ENetSocket socket;
  666. ENetAddress address; /**< Internet address of the host */
  667. enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */
  668. enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */
  669. enet_uint32 bandwidthThrottleEpoch;
  670. enet_uint32 mtu;
  671. enet_uint32 randomSeed;
  672. int recalculateBandwidthLimits;
  673. ENetPeer * peers; /**< array of peers allocated for this host */
  674. size_t peerCount; /**< number of peers allocated for this host */
  675. size_t channelLimit; /**< maximum number of channels allowed for connected peers */
  676. enet_uint32 serviceTime;
  677. ENetList dispatchQueue;
  678. int continueSending;
  679. size_t packetSize;
  680. enet_uint16 headerFlags;
  681. ENetProtocol commands[ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
  682. size_t commandCount;
  683. ENetBuffer buffers[ENET_BUFFER_MAXIMUM];
  684. size_t bufferCount;
  685. ENetChecksumCallback checksum; /**< callback the user can set to enable packet checksums for this host */
  686. ENetCompressor compressor;
  687. enet_uint8 packetData[2][ENET_PROTOCOL_MAXIMUM_MTU];
  688. ENetAddress receivedAddress;
  689. enet_uint8 * receivedData;
  690. size_t receivedDataLength;
  691. enet_uint32 totalSentData; /**< total data sent, user should reset to 0 as needed to prevent overflow */
  692. enet_uint32 totalSentPackets; /**< total UDP packets sent, user should reset to 0 as needed to prevent overflow */
  693. enet_uint32 totalReceivedData; /**< total data received, user should reset to 0 as needed to prevent overflow */
  694. enet_uint32 totalReceivedPackets; /**< total UDP packets received, user should reset to 0 as needed to prevent overflow */
  695. ENetInterceptCallback intercept; /**< callback the user can set to intercept received raw UDP packets */
  696. size_t connectedPeers;
  697. size_t bandwidthLimitedPeers;
  698. size_t duplicatePeers; /**< optional number of allowed peers from duplicate IPs, defaults to ENET_PROTOCOL_MAXIMUM_PEER_ID */
  699. size_t maximumPacketSize; /**< the maximum allowable packet size that may be sent or received on a peer */
  700. size_t maximumWaitingData; /**< the maximum aggregate amount of buffer space a peer may use waiting for packets to be delivered */
  701. } ENetHost;
  702. /**
  703. * An ENet event type, as specified in @ref ENetEvent.
  704. */
  705. typedef enum _ENetEventType {
  706. /** no event occurred within the specified time limit */
  707. ENET_EVENT_TYPE_NONE = 0,
  708. /** a connection request initiated by enet_host_connect has completed.
  709. * The peer field contains the peer which successfully connected.
  710. */
  711. ENET_EVENT_TYPE_CONNECT = 1,
  712. /** a peer has disconnected. This event is generated on a successful
  713. * completion of a disconnect initiated by enet_peer_disconnect, if
  714. * a peer has timed out. The peer field contains the peer
  715. * which disconnected. The data field contains user supplied data
  716. * describing the disconnection, or 0, if none is available.
  717. */
  718. ENET_EVENT_TYPE_DISCONNECT = 2,
  719. /** a packet has been received from a peer. The peer field specifies the
  720. * peer which sent the packet. The channelID field specifies the channel
  721. * number upon which the packet was received. The packet field contains
  722. * the packet that was received; this packet must be destroyed with
  723. * enet_packet_destroy after use.
  724. */
  725. ENET_EVENT_TYPE_RECEIVE = 3,
  726. /** a peer is disconnected because the host didn't receive the acknowledgment
  727. * packet within certain maximum time out. The reason could be because of bad
  728. * network connection or host crashed.
  729. */
  730. ENET_EVENT_TYPE_DISCONNECT_TIMEOUT = 4,
  731. } ENetEventType;
  732. /**
  733. * An ENet event as returned by enet_host_service().
  734. *
  735. * @sa enet_host_service
  736. */
  737. typedef struct _ENetEvent {
  738. ENetEventType type; /**< type of the event */
  739. ENetPeer * peer; /**< peer that generated a connect, disconnect or receive event */
  740. enet_uint8 channelID; /**< channel on the peer that generated the event, if appropriate */
  741. enet_uint32 data; /**< data associated with the event, if appropriate */
  742. ENetPacket * packet; /**< packet associated with the event, if appropriate */
  743. } ENetEvent;
  744. // =======================================================================//
  745. // !
  746. // ! Public API
  747. // !
  748. // =======================================================================//
  749. /**
  750. * Initializes ENet globally. Must be called prior to using any functions in ENet.
  751. * @returns 0 on success, < 0 on failure
  752. */
  753. ENET_API int enet_initialize(void);
  754. /**
  755. * Initializes ENet globally and supplies user-overridden callbacks. Must be called prior to using any functions in ENet. Do not use enet_initialize() if you use this variant. Make sure the ENetCallbacks structure is zeroed out so that any additional callbacks added in future versions will be properly ignored.
  756. *
  757. * @param version the constant ENET_VERSION should be supplied so ENet knows which version of ENetCallbacks struct to use
  758. * @param inits user-overridden callbacks where any NULL callbacks will use ENet's defaults
  759. * @returns 0 on success, < 0 on failure
  760. */
  761. ENET_API int enet_initialize_with_callbacks(ENetVersion version, const ENetCallbacks * inits);
  762. /**
  763. * Shuts down ENet globally. Should be called when a program that has initialized ENet exits.
  764. */
  765. ENET_API void enet_deinitialize(void);
  766. /**
  767. * Gives the linked version of the ENet library.
  768. * @returns the version number
  769. */
  770. ENET_API ENetVersion enet_linked_version(void);
  771. /** Returns the monotonic time in milliseconds. Its initial value is unspecified unless otherwise set. */
  772. ENET_API enet_uint32 enet_time_get(void);
  773. /** ENet socket functions */
  774. ENET_API ENetSocket enet_socket_create(ENetSocketType);
  775. ENET_API int enet_socket_bind(ENetSocket, const ENetAddress *);
  776. ENET_API int enet_socket_get_address(ENetSocket, ENetAddress *);
  777. ENET_API int enet_socket_listen(ENetSocket, int);
  778. ENET_API ENetSocket enet_socket_accept(ENetSocket, ENetAddress *);
  779. ENET_API int enet_socket_connect(ENetSocket, const ENetAddress *);
  780. ENET_API int enet_socket_send(ENetSocket, const ENetAddress *, const ENetBuffer *, size_t);
  781. ENET_API int enet_socket_receive(ENetSocket, ENetAddress *, ENetBuffer *, size_t);
  782. ENET_API int enet_socket_wait(ENetSocket, enet_uint32 *, enet_uint64);
  783. ENET_API int enet_socket_set_option(ENetSocket, ENetSocketOption, int);
  784. ENET_API int enet_socket_get_option(ENetSocket, ENetSocketOption, int *);
  785. ENET_API int enet_socket_shutdown(ENetSocket, ENetSocketShutdown);
  786. ENET_API void enet_socket_destroy(ENetSocket);
  787. ENET_API int enet_socketset_select(ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32);
  788. /** Attempts to parse the printable form of the IP address in the parameter hostName
  789. and sets the host field in the address parameter if successful.
  790. @param address destination to store the parsed IP address
  791. @param hostName IP address to parse
  792. @retval 0 on success
  793. @retval < 0 on failure
  794. @returns the address of the given hostName in address on success
  795. */
  796. ENET_API int enet_address_set_host_ip_old(ENetAddress * address, const char * hostName);
  797. /** Attempts to resolve the host named by the parameter hostName and sets
  798. the host field in the address parameter if successful.
  799. @param address destination to store resolved address
  800. @param hostName host name to lookup
  801. @retval 0 on success
  802. @retval < 0 on failure
  803. @returns the address of the given hostName in address on success
  804. */
  805. ENET_API int enet_address_set_host_old(ENetAddress * address, const char * hostName);
  806. /** Gives the printable form of the IP address specified in the address parameter.
  807. @param address address printed
  808. @param hostName destination for name, must not be NULL
  809. @param nameLength maximum length of hostName.
  810. @returns the null-terminated name of the host in hostName on success
  811. @retval 0 on success
  812. @retval < 0 on failure
  813. */
  814. ENET_API int enet_address_get_host_ip_old(const ENetAddress * address, char * hostName, size_t nameLength);
  815. /** Attempts to do a reverse lookup of the host field in the address parameter.
  816. @param address address used for reverse lookup
  817. @param hostName destination for name, must not be NULL
  818. @param nameLength maximum length of hostName.
  819. @returns the null-terminated name of the host in hostName on success
  820. @retval 0 on success
  821. @retval < 0 on failure
  822. */
  823. ENET_API int enet_address_get_host_old(const ENetAddress * address, char * hostName, size_t nameLength);
  824. ENET_API int enet_address_set_host_ip_new(ENetAddress * address, const char * hostName);
  825. ENET_API int enet_address_set_host_new(ENetAddress * address, const char * hostName);
  826. ENET_API int enet_address_get_host_ip_new(const ENetAddress * address, char * hostName, size_t nameLength);
  827. ENET_API int enet_address_get_host_new(const ENetAddress * address, char * hostName, size_t nameLength);
  828. #ifdef ENET_FEATURE_ADDRESS_MAPPING
  829. #define enet_address_set_host_ip enet_address_set_host_ip_new
  830. #define enet_address_set_host enet_address_set_host_new
  831. #define enet_address_get_host_ip enet_address_get_host_ip_new
  832. #define enet_address_get_host enet_address_get_host_new
  833. #else
  834. #define enet_address_set_host_ip enet_address_set_host_ip_old
  835. #define enet_address_set_host enet_address_set_host_old
  836. #define enet_address_get_host_ip enet_address_get_host_ip_old
  837. #define enet_address_get_host enet_address_get_host_old
  838. #endif
  839. ENET_API enet_uint32 enet_host_get_peers_count(ENetHost *);
  840. ENET_API enet_uint32 enet_host_get_packets_sent(ENetHost *);
  841. ENET_API enet_uint32 enet_host_get_packets_received(ENetHost *);
  842. ENET_API enet_uint32 enet_host_get_bytes_sent(ENetHost *);
  843. ENET_API enet_uint32 enet_host_get_bytes_received(ENetHost *);
  844. ENET_API enet_uint32 enet_host_get_received_data(ENetHost *, enet_uint8** data);
  845. ENET_API enet_uint32 enet_host_get_mtu(ENetHost *);
  846. ENET_API enet_uint32 enet_peer_get_id(ENetPeer *);
  847. ENET_API enet_uint32 enet_peer_get_ip(ENetPeer *, char * ip, size_t ipLength);
  848. ENET_API enet_uint16 enet_peer_get_port(ENetPeer *);
  849. ENET_API enet_uint32 enet_peer_get_rtt(ENetPeer *);
  850. ENET_API enet_uint64 enet_peer_get_packets_sent(ENetPeer *);
  851. ENET_API enet_uint32 enet_peer_get_packets_lost(ENetPeer *);
  852. ENET_API enet_uint64 enet_peer_get_bytes_sent(ENetPeer *);
  853. ENET_API enet_uint64 enet_peer_get_bytes_received(ENetPeer *);
  854. ENET_API ENetPeerState enet_peer_get_state(ENetPeer *);
  855. ENET_API void * enet_peer_get_data(ENetPeer *);
  856. ENET_API void enet_peer_set_data(ENetPeer *, const void *);
  857. ENET_API void * enet_packet_get_data(ENetPacket *);
  858. ENET_API enet_uint32 enet_packet_get_length(ENetPacket *);
  859. ENET_API void enet_packet_set_free_callback(ENetPacket *, void *);
  860. ENET_API ENetPacket * enet_packet_create_offset(const void *, size_t, size_t, enet_uint32);
  861. ENET_API enet_uint32 enet_crc32(const ENetBuffer *, size_t);
  862. ENET_API ENetHost * enet_host_create(const ENetAddress *, size_t, size_t, enet_uint32, enet_uint32);
  863. ENET_API void enet_host_destroy(ENetHost *);
  864. ENET_API ENetPeer * enet_host_connect(ENetHost *, const ENetAddress *, size_t, enet_uint32);
  865. ENET_API int enet_host_check_events(ENetHost *, ENetEvent *);
  866. ENET_API int enet_host_service(ENetHost *, ENetEvent *, enet_uint32);
  867. ENET_API int enet_host_send_raw(ENetHost *, const ENetAddress *, enet_uint8 *, size_t);
  868. ENET_API int enet_host_send_raw_ex(ENetHost *host, const ENetAddress* address, enet_uint8* data, size_t skipBytes, size_t bytesToSend);
  869. ENET_API void enet_host_set_intercept(ENetHost *, const ENetInterceptCallback);
  870. ENET_API void enet_host_flush(ENetHost *);
  871. ENET_API void enet_host_broadcast(ENetHost *, enet_uint8, ENetPacket *);
  872. ENET_API void enet_host_compress(ENetHost *, const ENetCompressor *);
  873. ENET_API void enet_host_channel_limit(ENetHost *, size_t);
  874. ENET_API void enet_host_bandwidth_limit(ENetHost *, enet_uint32, enet_uint32);
  875. extern void enet_host_bandwidth_throttle(ENetHost *);
  876. extern enet_uint64 enet_host_random_seed(void);
  877. ENET_API int enet_peer_send(ENetPeer *, enet_uint8, ENetPacket *);
  878. ENET_API ENetPacket * enet_peer_receive(ENetPeer *, enet_uint8 * channelID);
  879. ENET_API void enet_peer_ping(ENetPeer *);
  880. ENET_API void enet_peer_ping_interval(ENetPeer *, enet_uint32);
  881. ENET_API void enet_peer_timeout(ENetPeer *, enet_uint32, enet_uint32, enet_uint32);
  882. ENET_API void enet_peer_reset(ENetPeer *);
  883. ENET_API void enet_peer_disconnect(ENetPeer *, enet_uint32);
  884. ENET_API void enet_peer_disconnect_now(ENetPeer *, enet_uint32);
  885. ENET_API void enet_peer_disconnect_later(ENetPeer *, enet_uint32);
  886. ENET_API void enet_peer_throttle_configure(ENetPeer *, enet_uint32, enet_uint32, enet_uint32);
  887. extern int enet_peer_throttle(ENetPeer *, enet_uint32);
  888. extern void enet_peer_reset_queues(ENetPeer *);
  889. extern void enet_peer_setup_outgoing_command(ENetPeer *, ENetOutgoingCommand *);
  890. extern ENetOutgoingCommand * enet_peer_queue_outgoing_command(ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32, enet_uint16);
  891. extern ENetIncomingCommand * enet_peer_queue_incoming_command(ENetPeer *, const ENetProtocol *, const void *, size_t, enet_uint32, enet_uint32);
  892. extern ENetAcknowledgement * enet_peer_queue_acknowledgement(ENetPeer *, const ENetProtocol *, enet_uint16);
  893. extern void enet_peer_dispatch_incoming_unreliable_commands(ENetPeer *, ENetChannel *);
  894. extern void enet_peer_dispatch_incoming_reliable_commands(ENetPeer *, ENetChannel *);
  895. extern void enet_peer_on_connect(ENetPeer *);
  896. extern void enet_peer_on_disconnect(ENetPeer *);
  897. extern size_t enet_protocol_command_size (enet_uint8);
  898. #ifdef __cplusplus
  899. }
  900. #endif
  901. #if defined(ENET_IMPLEMENTATION) && !defined(ENET_IMPLEMENTATION_DONE)
  902. #define ENET_IMPLEMENTATION_DONE 1
  903. #ifdef __cplusplus
  904. extern "C" {
  905. #endif
  906. // =======================================================================//
  907. // !
  908. // ! Atomics
  909. // !
  910. // =======================================================================//
  911. #if defined(_MSC_VER)
  912. #define ENET_AT_CASSERT_PRED(predicate) sizeof(char[2 * !!(predicate)-1])
  913. #define ENET_IS_SUPPORTED_ATOMIC(size) ENET_AT_CASSERT_PRED(size == 1 || size == 2 || size == 4 || size == 8)
  914. #define ENET_ATOMIC_SIZEOF(variable) (ENET_IS_SUPPORTED_ATOMIC(sizeof(*(variable))), sizeof(*(variable)))
  915. __inline int64_t enet_at_atomic_read(char *ptr, size_t size)
  916. {
  917. switch (size) {
  918. case 1:
  919. return _InterlockedExchangeAdd8((volatile char *)ptr, 0);
  920. case 2:
  921. return _InterlockedExchangeAdd16((volatile SHORT *)ptr, 0);
  922. case 4:
  923. #ifdef NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  924. return InterlockedExchangeAdd((volatile LONG *)ptr, 0);
  925. #else
  926. return _InterlockedExchangeAdd((volatile LONG *)ptr, 0);
  927. #endif
  928. case 8:
  929. #ifdef NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  930. return InterlockedExchangeAdd64((volatile LONGLONG *)ptr, 0);
  931. #else
  932. return _InterlockedExchangeAdd64((volatile LONGLONG *)ptr, 0);
  933. #endif
  934. default:
  935. return 0xbad13bad; /* never reached */
  936. }
  937. }
  938. __inline int64_t enet_at_atomic_write(char *ptr, int64_t value, size_t size)
  939. {
  940. switch (size) {
  941. case 1:
  942. return _InterlockedExchange8((volatile char *)ptr, (char)value);
  943. case 2:
  944. return _InterlockedExchange16((volatile SHORT *)ptr, (SHORT)value);
  945. case 4:
  946. #ifdef NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  947. return InterlockedExchange((volatile LONG *)ptr, (LONG)value);
  948. #else
  949. return _InterlockedExchange((volatile LONG *)ptr, (LONG)value);
  950. #endif
  951. case 8:
  952. #ifdef NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  953. return InterlockedExchange64((volatile LONGLONG *)ptr, (LONGLONG)value);
  954. #else
  955. return _InterlockedExchange64((volatile LONGLONG *)ptr, (LONGLONG)value);
  956. #endif
  957. default:
  958. return 0xbad13bad; /* never reached */
  959. }
  960. }
  961. __inline int64_t enet_at_atomic_cas(char *ptr, int64_t new_val, int64_t old_val, size_t size)
  962. {
  963. switch (size) {
  964. case 1:
  965. return _InterlockedCompareExchange8((volatile char *)ptr, (char)new_val, (char)old_val);
  966. case 2:
  967. return _InterlockedCompareExchange16((volatile SHORT *)ptr, (SHORT)new_val,
  968. (SHORT)old_val);
  969. case 4:
  970. #ifdef NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  971. return InterlockedCompareExchange((volatile LONG *)ptr, (LONG)new_val, (LONG)old_val);
  972. #else
  973. return _InterlockedCompareExchange((volatile LONG *)ptr, (LONG)new_val, (LONG)old_val);
  974. #endif
  975. case 8:
  976. #ifdef NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  977. return InterlockedCompareExchange64((volatile LONGLONG *)ptr, (LONGLONG)new_val,
  978. (LONGLONG)old_val);
  979. #else
  980. return _InterlockedCompareExchange64((volatile LONGLONG *)ptr, (LONGLONG)new_val,
  981. (LONGLONG)old_val);
  982. #endif
  983. default:
  984. return 0xbad13bad; /* never reached */
  985. }
  986. }
  987. __inline int64_t enet_at_atomic_inc(char *ptr, int64_t delta, size_t data_size)
  988. {
  989. switch (data_size) {
  990. case 1:
  991. return _InterlockedExchangeAdd8((volatile char *)ptr, (char)delta);
  992. case 2:
  993. return _InterlockedExchangeAdd16((volatile SHORT *)ptr, (SHORT)delta);
  994. case 4:
  995. #ifdef NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  996. return InterlockedExchangeAdd((volatile LONG *)ptr, (LONG)delta);
  997. #else
  998. return _InterlockedExchangeAdd((volatile LONG *)ptr, (LONG)delta);
  999. #endif
  1000. case 8:
  1001. #ifdef NOT_UNDERSCORED_INTERLOCKED_EXCHANGE
  1002. return InterlockedExchangeAdd64((volatile LONGLONG *)ptr, (LONGLONG)delta);
  1003. #else
  1004. return _InterlockedExchangeAdd64((volatile LONGLONG *)ptr, (LONGLONG)delta);
  1005. #endif
  1006. default:
  1007. return 0xbad13bad; /* never reached */
  1008. }
  1009. }
  1010. #define ENET_ATOMIC_READ(variable) enet_at_atomic_read((char *)(variable), ENET_ATOMIC_SIZEOF(variable))
  1011. #define ENET_ATOMIC_WRITE(variable, new_val) \
  1012. enet_at_atomic_write((char *)(variable), (int64_t)(new_val), ENET_ATOMIC_SIZEOF(variable))
  1013. #define ENET_ATOMIC_CAS(variable, old_value, new_val, assign) \
  1014. enet_at_atomic_cas((char *)(variable), (int64_t)(new_val), (int64_t)(old_value), \
  1015. ENET_ATOMIC_SIZEOF(variable))
  1016. #define ENET_ATOMIC_INC(variable) enet_at_atomic_inc((char *)(variable), 1, ENET_ATOMIC_SIZEOF(variable))
  1017. #define ENET_ATOMIC_DEC(variable) enet_at_atomic_inc((char *)(variable), -1, ENET_ATOMIC_SIZEOF(variable))
  1018. #define ENET_ATOMIC_INC_BY(variable, delta) \
  1019. enet_at_atomic_inc((char *)(variable), (delta), ENET_ATOMIC_SIZEOF(variable))
  1020. #define ENET_ATOMIC_DEC_BY(variable, delta) \
  1021. enet_at_atomic_inc((char *)(variable), -(delta), ENET_ATOMIC_SIZEOF(variable))
  1022. #elif defined(__GNUC__) || defined(__clang__)
  1023. #if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
  1024. #define AT_HAVE_ATOMICS
  1025. #endif
  1026. /* We want to use __atomic built-ins if possible because the __sync primitives are
  1027. deprecated, because the __atomic build-ins allow us to use ENET_ATOMIC_WRITE on
  1028. uninitialized memory without running into undefined behavior, and because the
  1029. __atomic versions generate more efficient code since we don't need to rely on
  1030. CAS when we don't actually want it.
  1031. Note that we use acquire-release memory order (like mutexes do). We could use
  1032. sequentially consistent memory order but that has lower performance and is
  1033. almost always unneeded. */
  1034. #ifdef AT_HAVE_ATOMICS
  1035. #define ENET_ATOMIC_READ(ptr) __atomic_load_n((ptr), __ATOMIC_ACQUIRE)
  1036. #define ENET_ATOMIC_WRITE(ptr, value) __atomic_store_n((ptr), (value), __ATOMIC_RELEASE)
  1037. #ifndef typeof
  1038. #define typeof __typeof__
  1039. #endif
  1040. /* clang_analyzer doesn't know that CAS writes to memory so it complains about
  1041. potentially lost data. Replace the code with the equivalent non-sync code. */
  1042. #ifdef __clang_analyzer__
  1043. #define ENET_ATOMIC_CAS(ptr, old_value, new_value, assign) \
  1044. ({ \
  1045. typeof(*(ptr)) ENET_ATOMIC_CAS_old_actual_ = (*(ptr)); \
  1046. if (ATOMIC_CAS_old_actual_ == (old_value)) { \
  1047. *(ptr) = new_value; \
  1048. } \
  1049. ENET_ATOMIC_CAS_old_actual_; \
  1050. })
  1051. #else
  1052. /* Could use __auto_type instead of typeof but that shouldn't work in C++.
  1053. The ({ }) syntax is a GCC extension called statement expression. It lets
  1054. us return a value out of the macro.
  1055. TODO We should return bool here instead of the old value to avoid the ABA
  1056. problem. */
  1057. #define ENET_ATOMIC_CAS(ptr, old_value, new_value, assign) \
  1058. 0; \
  1059. typeof(*(ptr)) ENET_ATOMIC_CAS_expected_ = (old_value); \
  1060. __atomic_compare_exchange_n((ptr), &ENET_ATOMIC_CAS_expected_, (new_value), false, \
  1061. __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE); \
  1062. assign = ENET_ATOMIC_CAS_expected_;
  1063. #endif /* __clang_analyzer__ */
  1064. #define ENET_ATOMIC_INC(ptr) __atomic_fetch_add((ptr), 1, __ATOMIC_ACQ_REL)
  1065. #define ENET_ATOMIC_DEC(ptr) __atomic_fetch_sub((ptr), 1, __ATOMIC_ACQ_REL)
  1066. #define ENET_ATOMIC_INC_BY(ptr, delta) __atomic_fetch_add((ptr), (delta), __ATOMIC_ACQ_REL)
  1067. #define ENET_ATOMIC_DEC_BY(ptr, delta) __atomic_fetch_sub((ptr), (delta), __ATOMIC_ACQ_REL)
  1068. #else
  1069. #define ENET_ATOMIC_READ(variable) __sync_fetch_and_add(variable, 0)
  1070. #define ENET_ATOMIC_WRITE(variable, new_val) \
  1071. (void) __sync_val_compare_and_swap((variable), *(variable), (new_val))
  1072. #define ENET_ATOMIC_CAS(variable, old_value, new_val, assign) \
  1073. __sync_val_compare_and_swap((variable), (old_value), (new_val))
  1074. #define ENET_ATOMIC_INC(variable) __sync_fetch_and_add((variable), 1)
  1075. #define ENET_ATOMIC_DEC(variable) __sync_fetch_and_sub((variable), 1)
  1076. #define ENET_ATOMIC_INC_BY(variable, delta) __sync_fetch_and_add((variable), (delta), 1)
  1077. #define ENET_ATOMIC_DEC_BY(variable, delta) __sync_fetch_and_sub((variable), (delta), 1)
  1078. #endif /* AT_HAVE_ATOMICS */
  1079. #undef AT_HAVE_ATOMICS
  1080. #endif /* defined(_MSC_VER) */
  1081. // =======================================================================//
  1082. // !
  1083. // ! Callbacks
  1084. // !
  1085. // =======================================================================//
  1086. ENetCallbacks callbacks = { malloc, free, abort, enet_packet_create, enet_packet_destroy };
  1087. int enet_initialize_with_callbacks(ENetVersion version, const ENetCallbacks *inits) {
  1088. if (version < ENET_VERSION_CREATE(1, 3, 0)) {
  1089. return -1;
  1090. }
  1091. if (inits->malloc != NULL || inits->free != NULL) {
  1092. if (inits->malloc == NULL || inits->free == NULL) {
  1093. return -1;
  1094. }
  1095. callbacks.malloc = inits->malloc;
  1096. callbacks.free = inits->free;
  1097. }
  1098. if (inits->no_memory != NULL) {
  1099. callbacks.no_memory = inits->no_memory;
  1100. }
  1101. if (inits->packet_create != NULL || inits->packet_destroy != NULL) {
  1102. if (inits->packet_create == NULL || inits->packet_destroy == NULL) {
  1103. return -1;
  1104. }
  1105. callbacks.packet_create = inits->packet_create;
  1106. callbacks.packet_destroy = inits->packet_destroy;
  1107. }
  1108. return enet_initialize();
  1109. }
  1110. ENetVersion enet_linked_version(void) {
  1111. return ENET_VERSION;
  1112. }
  1113. void * enet_malloc(size_t size) {
  1114. void *memory = callbacks.malloc(size);
  1115. if (memory == NULL) {
  1116. callbacks.no_memory();
  1117. }
  1118. return memory;
  1119. }
  1120. void enet_free(void *memory) {
  1121. callbacks.free(memory);
  1122. }
  1123. // =======================================================================//
  1124. // !
  1125. // ! List
  1126. // !
  1127. // =======================================================================//
  1128. void enet_list_clear(ENetList *list) {
  1129. list->sentinel.next = &list->sentinel;
  1130. list->sentinel.previous = &list->sentinel;
  1131. }
  1132. ENetListIterator enet_list_insert(ENetListIterator position, void *data) {
  1133. ENetListIterator result = (ENetListIterator)data;
  1134. result->previous = position->previous;
  1135. result->next = position;
  1136. result->previous->next = result;
  1137. position->previous = result;
  1138. return result;
  1139. }
  1140. void *enet_list_remove(ENetListIterator position) {
  1141. position->previous->next = position->next;
  1142. position->next->previous = position->previous;
  1143. return position;
  1144. }
  1145. ENetListIterator enet_list_move(ENetListIterator position, void *dataFirst, void *dataLast) {
  1146. ENetListIterator first = (ENetListIterator)dataFirst;
  1147. ENetListIterator last = (ENetListIterator)dataLast;
  1148. first->previous->next = last->next;
  1149. last->next->previous = first->previous;
  1150. first->previous = position->previous;
  1151. last->next = position;
  1152. first->previous->next = first;
  1153. position->previous = last;
  1154. return first;
  1155. }
  1156. size_t enet_list_size(ENetList *list) {
  1157. size_t size = 0;
  1158. ENetListIterator position;
  1159. for (position = enet_list_begin(list); position != enet_list_end(list); position = enet_list_next(position)) {
  1160. ++size;
  1161. }
  1162. return size;
  1163. }
  1164. // =======================================================================//
  1165. // !
  1166. // ! Packet
  1167. // !
  1168. // =======================================================================//
  1169. /**
  1170. * Creates a packet that may be sent to a peer.
  1171. * @param data initial contents of the packet's data; the packet's data will remain uninitialized if data is NULL.
  1172. * @param dataLength size of the data allocated for this packet
  1173. * @param flags flags for this packet as described for the ENetPacket structure.
  1174. * @returns the packet on success, NULL on failure
  1175. */
  1176. ENetPacket *enet_packet_create(const void *data, size_t dataLength, enet_uint32 flags) {
  1177. ENetPacket *packet;
  1178. if (flags & ENET_PACKET_FLAG_NO_ALLOCATE) {
  1179. packet = (ENetPacket *)enet_malloc(sizeof (ENetPacket));
  1180. if (packet == NULL) {
  1181. return NULL;
  1182. }
  1183. packet->data = (enet_uint8 *)data;
  1184. }
  1185. else {
  1186. packet = (ENetPacket *)enet_malloc(sizeof (ENetPacket) + dataLength);
  1187. if (packet == NULL) {
  1188. return NULL;
  1189. }
  1190. packet->data = (enet_uint8 *)packet + sizeof(ENetPacket);
  1191. if (data != NULL) {
  1192. memcpy(packet->data, data, dataLength);
  1193. }
  1194. }
  1195. packet->referenceCount = 0;
  1196. packet->flags = flags;
  1197. packet->dataLength = dataLength;
  1198. packet->freeCallback = NULL;
  1199. packet->userData = NULL;
  1200. return packet;
  1201. }
  1202. ENetPacket *enet_packet_create_offset(const void *data, size_t dataLength, size_t dataOffset, enet_uint32 flags) {
  1203. ENetPacket *packet;
  1204. if (flags & ENET_PACKET_FLAG_NO_ALLOCATE) {
  1205. packet = (ENetPacket *)enet_malloc(sizeof (ENetPacket));
  1206. if (packet == NULL) {
  1207. return NULL;
  1208. }
  1209. packet->data = (enet_uint8 *)data;
  1210. }
  1211. else {
  1212. packet = (ENetPacket *)enet_malloc(sizeof (ENetPacket) + dataLength + dataOffset);
  1213. if (packet == NULL) {
  1214. return NULL;
  1215. }
  1216. packet->data = (enet_uint8 *)packet + sizeof(ENetPacket);
  1217. if (data != NULL) {
  1218. memcpy(packet->data + dataOffset, data, dataLength);
  1219. }
  1220. }
  1221. packet->referenceCount = 0;
  1222. packet->flags = flags;
  1223. packet->dataLength = dataLength + dataOffset;
  1224. packet->freeCallback = NULL;
  1225. packet->userData = NULL;
  1226. return packet;
  1227. }
  1228. ENetPacket *enet_packet_copy(ENetPacket *packet) {
  1229. return enet_packet_create(packet->data, packet->dataLength, packet->flags);
  1230. }
  1231. /**
  1232. * Destroys the packet and deallocates its data.
  1233. * @param packet packet to be destroyed
  1234. */
  1235. void enet_packet_destroy(ENetPacket *packet) {
  1236. if (packet == NULL) {
  1237. return;
  1238. }
  1239. if (packet->freeCallback != NULL) {
  1240. (*packet->freeCallback)((void *)packet);
  1241. }
  1242. enet_free(packet);
  1243. }
  1244. static int initializedCRC32 = 0;
  1245. static enet_uint32 crcTable[256];
  1246. static enet_uint32 reflect_crc(int val, int bits) {
  1247. int result = 0, bit;
  1248. for (bit = 0; bit < bits; bit++) {
  1249. if (val & 1) { result |= 1 << (bits - 1 - bit); }
  1250. val >>= 1;
  1251. }
  1252. return result;
  1253. }
  1254. static void initialize_crc32(void) {
  1255. int byte;
  1256. for (byte = 0; byte < 256; ++byte) {
  1257. enet_uint32 crc = reflect_crc(byte, 8) << 24;
  1258. int offset;
  1259. for (offset = 0; offset < 8; ++offset) {
  1260. if (crc & 0x80000000) {
  1261. crc = (crc << 1) ^ 0x04c11db7;
  1262. } else {
  1263. crc <<= 1;
  1264. }
  1265. }
  1266. crcTable[byte] = reflect_crc(crc, 32);
  1267. }
  1268. initializedCRC32 = 1;
  1269. }
  1270. enet_uint32 enet_crc32(const ENetBuffer *buffers, size_t bufferCount) {
  1271. enet_uint32 crc = 0xFFFFFFFF;
  1272. if (!initializedCRC32) { initialize_crc32(); }
  1273. while (bufferCount-- > 0) {
  1274. const enet_uint8 *data = (const enet_uint8 *)buffers->data;
  1275. const enet_uint8 *dataEnd = &data[buffers->dataLength];
  1276. while (data < dataEnd) {
  1277. crc = (crc >> 8) ^ crcTable[(crc & 0xFF) ^ *data++];
  1278. }
  1279. ++buffers;
  1280. }
  1281. return ENET_HOST_TO_NET_32(~crc);
  1282. }
  1283. // =======================================================================//
  1284. // !
  1285. // ! Protocol
  1286. // !
  1287. // =======================================================================//
  1288. static size_t commandSizes[ENET_PROTOCOL_COMMAND_COUNT] = {
  1289. 0,
  1290. sizeof(ENetProtocolAcknowledge),
  1291. sizeof(ENetProtocolConnect),
  1292. sizeof(ENetProtocolVerifyConnect),
  1293. sizeof(ENetProtocolDisconnect),
  1294. sizeof(ENetProtocolPing),
  1295. sizeof(ENetProtocolSendReliable),
  1296. sizeof(ENetProtocolSendUnreliable),
  1297. sizeof(ENetProtocolSendFragment),
  1298. sizeof(ENetProtocolSendUnsequenced),
  1299. sizeof(ENetProtocolBandwidthLimit),
  1300. sizeof(ENetProtocolThrottleConfigure),
  1301. sizeof(ENetProtocolSendFragment)
  1302. };
  1303. size_t enet_protocol_command_size(enet_uint8 commandNumber) {
  1304. return commandSizes[commandNumber & ENET_PROTOCOL_COMMAND_MASK];
  1305. }
  1306. static void enet_protocol_change_state(ENetHost *host, ENetPeer *peer, ENetPeerState state) {
  1307. ENET_UNUSED(host)
  1308. if (state == ENET_PEER_STATE_CONNECTED || state == ENET_PEER_STATE_DISCONNECT_LATER) {
  1309. enet_peer_on_connect(peer);
  1310. } else {
  1311. enet_peer_on_disconnect(peer);
  1312. }
  1313. peer->state = state;
  1314. }
  1315. static void enet_protocol_dispatch_state(ENetHost *host, ENetPeer *peer, ENetPeerState state) {
  1316. enet_protocol_change_state(host, peer, state);
  1317. if (!peer->needsDispatch) {
  1318. enet_list_insert(enet_list_end(&host->dispatchQueue), &peer->dispatchList);
  1319. peer->needsDispatch = 1;
  1320. }
  1321. }
  1322. static int enet_protocol_dispatch_incoming_commands(ENetHost *host, ENetEvent *event) {
  1323. while (!enet_list_empty(&host->dispatchQueue)) {
  1324. ENetPeer *peer = (ENetPeer *) enet_list_remove(enet_list_begin(&host->dispatchQueue));
  1325. peer->needsDispatch = 0;
  1326. switch (peer->state) {
  1327. case ENET_PEER_STATE_CONNECTION_PENDING:
  1328. case ENET_PEER_STATE_CONNECTION_SUCCEEDED:
  1329. enet_protocol_change_state(host, peer, ENET_PEER_STATE_CONNECTED);
  1330. event->type = ENET_EVENT_TYPE_CONNECT;
  1331. event->peer = peer;
  1332. event->data = peer->eventData;
  1333. return 1;
  1334. case ENET_PEER_STATE_ZOMBIE:
  1335. host->recalculateBandwidthLimits = 1;
  1336. event->type = ENET_EVENT_TYPE_DISCONNECT;
  1337. event->peer = peer;
  1338. event->data = peer->eventData;
  1339. enet_peer_reset(peer);
  1340. return 1;
  1341. case ENET_PEER_STATE_CONNECTED:
  1342. if (enet_list_empty(&peer->dispatchedCommands)) {
  1343. continue;
  1344. }
  1345. event->packet = enet_peer_receive(peer, &event->channelID);
  1346. if (event->packet == NULL) {
  1347. continue;
  1348. }
  1349. event->type = ENET_EVENT_TYPE_RECEIVE;
  1350. event->peer = peer;
  1351. if (!enet_list_empty(&peer->dispatchedCommands)) {
  1352. peer->needsDispatch = 1;
  1353. enet_list_insert(enet_list_end(&host->dispatchQueue), &peer->dispatchList);
  1354. }
  1355. return 1;
  1356. default:
  1357. break;
  1358. }
  1359. }
  1360. return 0;
  1361. } /* enet_protocol_dispatch_incoming_commands */
  1362. static void enet_protocol_notify_connect(ENetHost *host, ENetPeer *peer, ENetEvent *event) {
  1363. host->recalculateBandwidthLimits = 1;
  1364. if (event != NULL) {
  1365. enet_protocol_change_state(host, peer, ENET_PEER_STATE_CONNECTED);
  1366. peer->totalDataSent = 0;
  1367. peer->totalDataReceived = 0;
  1368. peer->totalPacketsSent = 0;
  1369. peer->totalPacketsLost = 0;
  1370. event->type = ENET_EVENT_TYPE_CONNECT;
  1371. event->peer = peer;
  1372. event->data = peer->eventData;
  1373. } else {
  1374. enet_protocol_dispatch_state(host, peer, peer->state == ENET_PEER_STATE_CONNECTING ? ENET_PEER_STATE_CONNECTION_SUCCEEDED : ENET_PEER_STATE_CONNECTION_PENDING);
  1375. }
  1376. }
  1377. static void enet_protocol_notify_disconnect(ENetHost *host, ENetPeer *peer, ENetEvent *event) {
  1378. if (peer->state >= ENET_PEER_STATE_CONNECTION_PENDING) {
  1379. host->recalculateBandwidthLimits = 1;
  1380. }
  1381. if (peer->state != ENET_PEER_STATE_CONNECTING && peer->state < ENET_PEER_STATE_CONNECTION_SUCCEEDED) {
  1382. enet_peer_reset(peer);
  1383. } else if (event != NULL) {
  1384. event->type = ENET_EVENT_TYPE_DISCONNECT;
  1385. event->peer = peer;
  1386. event->data = 0;
  1387. enet_peer_reset(peer);
  1388. } else {
  1389. peer->eventData = 0;
  1390. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  1391. }
  1392. }
  1393. static void enet_protocol_notify_disconnect_timeout (ENetHost * host, ENetPeer * peer, ENetEvent * event) {
  1394. if (peer->state >= ENET_PEER_STATE_CONNECTION_PENDING) {
  1395. host->recalculateBandwidthLimits = 1;
  1396. }
  1397. if (peer->state != ENET_PEER_STATE_CONNECTING && peer->state < ENET_PEER_STATE_CONNECTION_SUCCEEDED) {
  1398. enet_peer_reset (peer);
  1399. }
  1400. else if (event != NULL) {
  1401. event->type = ENET_EVENT_TYPE_DISCONNECT_TIMEOUT;
  1402. event->peer = peer;
  1403. event->data = 0;
  1404. enet_peer_reset(peer);
  1405. }
  1406. else {
  1407. peer->eventData = 0;
  1408. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  1409. }
  1410. }
  1411. static void enet_protocol_remove_sent_unreliable_commands(ENetPeer *peer) {
  1412. ENetOutgoingCommand *outgoingCommand;
  1413. while (!enet_list_empty(&peer->sentUnreliableCommands)) {
  1414. outgoingCommand = (ENetOutgoingCommand *) enet_list_front(&peer->sentUnreliableCommands);
  1415. enet_list_remove(&outgoingCommand->outgoingCommandList);
  1416. if (outgoingCommand->packet != NULL) {
  1417. --outgoingCommand->packet->referenceCount;
  1418. if (outgoingCommand->packet->referenceCount == 0) {
  1419. outgoingCommand->packet->flags |= ENET_PACKET_FLAG_SENT;
  1420. callbacks.packet_destroy(outgoingCommand->packet);
  1421. }
  1422. }
  1423. enet_free(outgoingCommand);
  1424. }
  1425. }
  1426. static ENetProtocolCommand enet_protocol_remove_sent_reliable_command(ENetPeer *peer, enet_uint16 reliableSequenceNumber, enet_uint8 channelID) {
  1427. ENetOutgoingCommand *outgoingCommand = NULL;
  1428. ENetListIterator currentCommand;
  1429. ENetProtocolCommand commandNumber;
  1430. int wasSent = 1;
  1431. for (currentCommand = enet_list_begin(&peer->sentReliableCommands);
  1432. currentCommand != enet_list_end(&peer->sentReliableCommands);
  1433. currentCommand = enet_list_next(currentCommand)
  1434. ) {
  1435. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1436. if (outgoingCommand->reliableSequenceNumber == reliableSequenceNumber && outgoingCommand->command.header.channelID == channelID) {
  1437. break;
  1438. }
  1439. }
  1440. if (currentCommand == enet_list_end(&peer->sentReliableCommands)) {
  1441. for (currentCommand = enet_list_begin(&peer->outgoingReliableCommands);
  1442. currentCommand != enet_list_end(&peer->outgoingReliableCommands);
  1443. currentCommand = enet_list_next(currentCommand)
  1444. ) {
  1445. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  1446. if (outgoingCommand->sendAttempts < 1) { return ENET_PROTOCOL_COMMAND_NONE; }
  1447. if (outgoingCommand->reliableSequenceNumber == reliableSequenceNumber && outgoingCommand->command.header.channelID == channelID) {
  1448. break;
  1449. }
  1450. }
  1451. if (currentCommand == enet_list_end(&peer->outgoingReliableCommands)) {
  1452. return ENET_PROTOCOL_COMMAND_NONE;
  1453. }
  1454. wasSent = 0;
  1455. }
  1456. if (outgoingCommand == NULL) {
  1457. return ENET_PROTOCOL_COMMAND_NONE;
  1458. }
  1459. if (channelID < peer->channelCount) {
  1460. ENetChannel *channel = &peer->channels[channelID];
  1461. enet_uint16 reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  1462. if (channel->reliableWindows[reliableWindow] > 0) {
  1463. --channel->reliableWindows[reliableWindow];
  1464. if (!channel->reliableWindows[reliableWindow]) {
  1465. channel->usedReliableWindows &= ~(1 << reliableWindow);
  1466. }
  1467. }
  1468. }
  1469. commandNumber = (ENetProtocolCommand) (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK);
  1470. enet_list_remove(&outgoingCommand->outgoingCommandList);
  1471. if (outgoingCommand->packet != NULL) {
  1472. if (wasSent) {
  1473. peer->reliableDataInTransit -= outgoingCommand->fragmentLength;
  1474. }
  1475. --outgoingCommand->packet->referenceCount;
  1476. if (outgoingCommand->packet->referenceCount == 0) {
  1477. outgoingCommand->packet->flags |= ENET_PACKET_FLAG_SENT;
  1478. callbacks.packet_destroy(outgoingCommand->packet);
  1479. }
  1480. }
  1481. enet_free(outgoingCommand);
  1482. if (enet_list_empty(&peer->sentReliableCommands)) {
  1483. return commandNumber;
  1484. }
  1485. outgoingCommand = (ENetOutgoingCommand *) enet_list_front(&peer->sentReliableCommands);
  1486. peer->nextTimeout = outgoingCommand->sentTime + outgoingCommand->roundTripTimeout;
  1487. return commandNumber;
  1488. } /* enet_protocol_remove_sent_reliable_command */
  1489. static ENetPeer * enet_protocol_handle_connect(ENetHost *host, ENetProtocolHeader *header, ENetProtocol *command) {
  1490. ENET_UNUSED(header)
  1491. enet_uint8 incomingSessionID, outgoingSessionID;
  1492. enet_uint32 mtu, windowSize;
  1493. ENetChannel *channel;
  1494. size_t channelCount, duplicatePeers = 0;
  1495. ENetPeer *currentPeer, *peer = NULL;
  1496. ENetProtocol verifyCommand;
  1497. channelCount = ENET_NET_TO_HOST_32(command->connect.channelCount);
  1498. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT || channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) {
  1499. return NULL;
  1500. }
  1501. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer) {
  1502. if (currentPeer->state == ENET_PEER_STATE_DISCONNECTED) {
  1503. if (peer == NULL) {
  1504. peer = currentPeer;
  1505. }
  1506. } else if (currentPeer->state != ENET_PEER_STATE_CONNECTING && in6_equal(currentPeer->address.host, host->receivedAddress.host)) {
  1507. if (currentPeer->address.port == host->receivedAddress.port && currentPeer->connectID == command->connect.connectID) {
  1508. return NULL;
  1509. }
  1510. ++duplicatePeers;
  1511. }
  1512. }
  1513. if (peer == NULL || duplicatePeers >= host->duplicatePeers) {
  1514. return NULL;
  1515. }
  1516. if (channelCount > host->channelLimit) {
  1517. channelCount = host->channelLimit;
  1518. }
  1519. peer->channels = (ENetChannel *) enet_malloc(channelCount * sizeof(ENetChannel));
  1520. if (peer->channels == NULL) {
  1521. return NULL;
  1522. }
  1523. peer->channelCount = channelCount;
  1524. peer->state = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
  1525. peer->connectID = command->connect.connectID;
  1526. peer->address = host->receivedAddress;
  1527. peer->outgoingPeerID = ENET_NET_TO_HOST_16(command->connect.outgoingPeerID);
  1528. peer->incomingBandwidth = ENET_NET_TO_HOST_32(command->connect.incomingBandwidth);
  1529. peer->outgoingBandwidth = ENET_NET_TO_HOST_32(command->connect.outgoingBandwidth);
  1530. peer->packetThrottleInterval = ENET_NET_TO_HOST_32(command->connect.packetThrottleInterval);
  1531. peer->packetThrottleAcceleration = ENET_NET_TO_HOST_32(command->connect.packetThrottleAcceleration);
  1532. peer->packetThrottleDeceleration = ENET_NET_TO_HOST_32(command->connect.packetThrottleDeceleration);
  1533. peer->eventData = ENET_NET_TO_HOST_32(command->connect.data);
  1534. incomingSessionID = command->connect.incomingSessionID == 0xFF ? peer->outgoingSessionID : command->connect.incomingSessionID;
  1535. incomingSessionID = (incomingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  1536. if (incomingSessionID == peer->outgoingSessionID) {
  1537. incomingSessionID = (incomingSessionID + 1)
  1538. & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  1539. }
  1540. peer->outgoingSessionID = incomingSessionID;
  1541. outgoingSessionID = command->connect.outgoingSessionID == 0xFF ? peer->incomingSessionID : command->connect.outgoingSessionID;
  1542. outgoingSessionID = (outgoingSessionID + 1) & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  1543. if (outgoingSessionID == peer->incomingSessionID) {
  1544. outgoingSessionID = (outgoingSessionID + 1)
  1545. & (ENET_PROTOCOL_HEADER_SESSION_MASK >> ENET_PROTOCOL_HEADER_SESSION_SHIFT);
  1546. }
  1547. peer->incomingSessionID = outgoingSessionID;
  1548. for (channel = peer->channels; channel < &peer->channels[channelCount]; ++channel) {
  1549. channel->outgoingReliableSequenceNumber = 0;
  1550. channel->outgoingUnreliableSequenceNumber = 0;
  1551. channel->incomingReliableSequenceNumber = 0;
  1552. channel->incomingUnreliableSequenceNumber = 0;
  1553. enet_list_clear(&channel->incomingReliableCommands);
  1554. enet_list_clear(&channel->incomingUnreliableCommands);
  1555. channel->usedReliableWindows = 0;
  1556. memset(channel->reliableWindows, 0, sizeof(channel->reliableWindows));
  1557. }
  1558. mtu = ENET_NET_TO_HOST_32(command->connect.mtu);
  1559. if (mtu < ENET_PROTOCOL_MINIMUM_MTU) {
  1560. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  1561. } else if (mtu > ENET_PROTOCOL_MAXIMUM_MTU) {
  1562. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  1563. }
  1564. peer->mtu = mtu;
  1565. if (host->outgoingBandwidth == 0 && peer->incomingBandwidth == 0) {
  1566. peer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  1567. } else if (host->outgoingBandwidth == 0 || peer->incomingBandwidth == 0) {
  1568. peer->windowSize = (ENET_MAX(host->outgoingBandwidth, peer->incomingBandwidth) / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  1569. } else {
  1570. peer->windowSize = (ENET_MIN(host->outgoingBandwidth, peer->incomingBandwidth) / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  1571. }
  1572. if (peer->windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) {
  1573. peer->windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  1574. } else if (peer->windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) {
  1575. peer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  1576. }
  1577. if (host->incomingBandwidth == 0) {
  1578. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  1579. } else {
  1580. windowSize = (host->incomingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  1581. }
  1582. if (windowSize > ENET_NET_TO_HOST_32(command->connect.windowSize)) {
  1583. windowSize = ENET_NET_TO_HOST_32(command->connect.windowSize);
  1584. }
  1585. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) {
  1586. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  1587. } else if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) {
  1588. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  1589. }
  1590. verifyCommand.header.command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  1591. verifyCommand.header.channelID = 0xFF;
  1592. verifyCommand.verifyConnect.outgoingPeerID = ENET_HOST_TO_NET_16(peer->incomingPeerID);
  1593. verifyCommand.verifyConnect.incomingSessionID = incomingSessionID;
  1594. verifyCommand.verifyConnect.outgoingSessionID = outgoingSessionID;
  1595. verifyCommand.verifyConnect.mtu = ENET_HOST_TO_NET_32(peer->mtu);
  1596. verifyCommand.verifyConnect.windowSize = ENET_HOST_TO_NET_32(windowSize);
  1597. verifyCommand.verifyConnect.channelCount = ENET_HOST_TO_NET_32(channelCount);
  1598. verifyCommand.verifyConnect.incomingBandwidth = ENET_HOST_TO_NET_32(host->incomingBandwidth);
  1599. verifyCommand.verifyConnect.outgoingBandwidth = ENET_HOST_TO_NET_32(host->outgoingBandwidth);
  1600. verifyCommand.verifyConnect.packetThrottleInterval = ENET_HOST_TO_NET_32(peer->packetThrottleInterval);
  1601. verifyCommand.verifyConnect.packetThrottleAcceleration = ENET_HOST_TO_NET_32(peer->packetThrottleAcceleration);
  1602. verifyCommand.verifyConnect.packetThrottleDeceleration = ENET_HOST_TO_NET_32(peer->packetThrottleDeceleration);
  1603. verifyCommand.verifyConnect.connectID = peer->connectID;
  1604. enet_peer_queue_outgoing_command(peer, &verifyCommand, NULL, 0, 0);
  1605. return peer;
  1606. } /* enet_protocol_handle_connect */
  1607. static int enet_protocol_handle_send_reliable(ENetHost *host, ENetPeer *peer, const ENetProtocol *command, enet_uint8 **currentData) {
  1608. size_t dataLength;
  1609. if (command->header.channelID >= peer->channelCount || (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)) {
  1610. return -1;
  1611. }
  1612. dataLength = ENET_NET_TO_HOST_16(command->sendReliable.dataLength);
  1613. *currentData += dataLength;
  1614. if (dataLength > host->maximumPacketSize || *currentData < host->receivedData || *currentData > &host->receivedData[host->receivedDataLength]) {
  1615. return -1;
  1616. }
  1617. if (enet_peer_queue_incoming_command(peer, command, (const enet_uint8 *) command + sizeof(ENetProtocolSendReliable), dataLength, ENET_PACKET_FLAG_RELIABLE, 0) == NULL) {
  1618. return -1;
  1619. }
  1620. return 0;
  1621. }
  1622. static int enet_protocol_handle_send_unsequenced(ENetHost *host, ENetPeer *peer, const ENetProtocol *command, enet_uint8 **currentData) {
  1623. enet_uint32 unsequencedGroup, index;
  1624. size_t dataLength;
  1625. if (command->header.channelID >= peer->channelCount || (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)) {
  1626. return -1;
  1627. }
  1628. dataLength = ENET_NET_TO_HOST_16(command->sendUnsequenced.dataLength);
  1629. *currentData += dataLength;
  1630. if (dataLength > host->maximumPacketSize || *currentData < host->receivedData || *currentData > &host->receivedData[host->receivedDataLength]) {
  1631. return -1;
  1632. }
  1633. unsequencedGroup = ENET_NET_TO_HOST_16(command->sendUnsequenced.unsequencedGroup);
  1634. index = unsequencedGroup % ENET_PEER_UNSEQUENCED_WINDOW_SIZE;
  1635. if (unsequencedGroup < peer->incomingUnsequencedGroup) {
  1636. unsequencedGroup += 0x10000;
  1637. }
  1638. if (unsequencedGroup >= (enet_uint32) peer->incomingUnsequencedGroup + ENET_PEER_FREE_UNSEQUENCED_WINDOWS * ENET_PEER_UNSEQUENCED_WINDOW_SIZE) {
  1639. return 0;
  1640. }
  1641. unsequencedGroup &= 0xFFFF;
  1642. if (unsequencedGroup - index != peer->incomingUnsequencedGroup) {
  1643. peer->incomingUnsequencedGroup = unsequencedGroup - index;
  1644. memset(peer->unsequencedWindow, 0, sizeof(peer->unsequencedWindow));
  1645. } else if (peer->unsequencedWindow[index / 32] & (1 << (index % 32))) {
  1646. return 0;
  1647. }
  1648. if (enet_peer_queue_incoming_command(peer, command, (const enet_uint8 *) command + sizeof(ENetProtocolSendUnsequenced), dataLength, ENET_PACKET_FLAG_UNSEQUENCED,0) == NULL) {
  1649. return -1;
  1650. }
  1651. peer->unsequencedWindow[index / 32] |= 1 << (index % 32);
  1652. return 0;
  1653. } /* enet_protocol_handle_send_unsequenced */
  1654. static int enet_protocol_handle_send_unreliable(ENetHost *host, ENetPeer *peer, const ENetProtocol *command,
  1655. enet_uint8 **currentData) {
  1656. size_t dataLength;
  1657. if (command->header.channelID >= peer->channelCount ||
  1658. (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER))
  1659. {
  1660. return -1;
  1661. }
  1662. dataLength = ENET_NET_TO_HOST_16(command->sendUnreliable.dataLength);
  1663. *currentData += dataLength;
  1664. if (dataLength > host->maximumPacketSize || *currentData < host->receivedData || *currentData > &host->receivedData[host->receivedDataLength]) {
  1665. return -1;
  1666. }
  1667. if (enet_peer_queue_incoming_command(peer, command, (const enet_uint8 *) command + sizeof(ENetProtocolSendUnreliable), dataLength, 0, 0) == NULL) {
  1668. return -1;
  1669. }
  1670. return 0;
  1671. }
  1672. static int enet_protocol_handle_send_fragment(ENetHost *host, ENetPeer *peer, const ENetProtocol *command, enet_uint8 **currentData) {
  1673. enet_uint32 fragmentNumber, fragmentCount, fragmentOffset, fragmentLength, startSequenceNumber, totalLength;
  1674. ENetChannel *channel;
  1675. enet_uint16 startWindow, currentWindow;
  1676. ENetListIterator currentCommand;
  1677. ENetIncomingCommand *startCommand = NULL;
  1678. if (command->header.channelID >= peer->channelCount || (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)) {
  1679. return -1;
  1680. }
  1681. fragmentLength = ENET_NET_TO_HOST_16(command->sendFragment.dataLength);
  1682. *currentData += fragmentLength;
  1683. if (fragmentLength > host->maximumPacketSize || *currentData < host->receivedData || *currentData > &host->receivedData[host->receivedDataLength]) {
  1684. return -1;
  1685. }
  1686. channel = &peer->channels[command->header.channelID];
  1687. startSequenceNumber = ENET_NET_TO_HOST_16(command->sendFragment.startSequenceNumber);
  1688. startWindow = startSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  1689. currentWindow = channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  1690. if (startSequenceNumber < channel->incomingReliableSequenceNumber) {
  1691. startWindow += ENET_PEER_RELIABLE_WINDOWS;
  1692. }
  1693. if (startWindow < currentWindow || startWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1) {
  1694. return 0;
  1695. }
  1696. fragmentNumber = ENET_NET_TO_HOST_32(command->sendFragment.fragmentNumber);
  1697. fragmentCount = ENET_NET_TO_HOST_32(command->sendFragment.fragmentCount);
  1698. fragmentOffset = ENET_NET_TO_HOST_32(command->sendFragment.fragmentOffset);
  1699. totalLength = ENET_NET_TO_HOST_32(command->sendFragment.totalLength);
  1700. if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT ||
  1701. fragmentNumber >= fragmentCount ||
  1702. totalLength > host->maximumPacketSize ||
  1703. fragmentOffset >= totalLength ||
  1704. fragmentLength > totalLength - fragmentOffset
  1705. ) {
  1706. return -1;
  1707. }
  1708. for (currentCommand = enet_list_previous(enet_list_end(&channel->incomingReliableCommands));
  1709. currentCommand != enet_list_end(&channel->incomingReliableCommands);
  1710. currentCommand = enet_list_previous(currentCommand)
  1711. ) {
  1712. ENetIncomingCommand *incomingCommand = (ENetIncomingCommand *) currentCommand;
  1713. if (startSequenceNumber >= channel->incomingReliableSequenceNumber) {
  1714. if (incomingCommand->reliableSequenceNumber < channel->incomingReliableSequenceNumber) {
  1715. continue;
  1716. }
  1717. } else if (incomingCommand->reliableSequenceNumber >= channel->incomingReliableSequenceNumber) {
  1718. break;
  1719. }
  1720. if (incomingCommand->reliableSequenceNumber <= startSequenceNumber) {
  1721. if (incomingCommand->reliableSequenceNumber < startSequenceNumber) {
  1722. break;
  1723. }
  1724. if ((incomingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK) !=
  1725. ENET_PROTOCOL_COMMAND_SEND_FRAGMENT ||
  1726. totalLength != incomingCommand->packet->dataLength ||
  1727. fragmentCount != incomingCommand->fragmentCount
  1728. ) {
  1729. return -1;
  1730. }
  1731. startCommand = incomingCommand;
  1732. break;
  1733. }
  1734. }
  1735. if (startCommand == NULL) {
  1736. ENetProtocol hostCommand = *command;
  1737. hostCommand.header.reliableSequenceNumber = startSequenceNumber;
  1738. startCommand = enet_peer_queue_incoming_command(peer, &hostCommand, NULL, totalLength, ENET_PACKET_FLAG_RELIABLE, fragmentCount);
  1739. if (startCommand == NULL) {
  1740. return -1;
  1741. }
  1742. }
  1743. if ((startCommand->fragments[fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0) {
  1744. --startCommand->fragmentsRemaining;
  1745. startCommand->fragments[fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
  1746. if (fragmentOffset + fragmentLength > startCommand->packet->dataLength) {
  1747. fragmentLength = startCommand->packet->dataLength - fragmentOffset;
  1748. }
  1749. memcpy(startCommand->packet->data + fragmentOffset, (enet_uint8 *) command + sizeof(ENetProtocolSendFragment), fragmentLength);
  1750. if (startCommand->fragmentsRemaining <= 0) {
  1751. enet_peer_dispatch_incoming_reliable_commands(peer, channel);
  1752. }
  1753. }
  1754. return 0;
  1755. } /* enet_protocol_handle_send_fragment */
  1756. static int enet_protocol_handle_send_unreliable_fragment(ENetHost *host, ENetPeer *peer, const ENetProtocol *command, enet_uint8 **currentData) {
  1757. enet_uint32 fragmentNumber, fragmentCount, fragmentOffset, fragmentLength, reliableSequenceNumber, startSequenceNumber, totalLength;
  1758. enet_uint16 reliableWindow, currentWindow;
  1759. ENetChannel *channel;
  1760. ENetListIterator currentCommand;
  1761. ENetIncomingCommand *startCommand = NULL;
  1762. if (command->header.channelID >= peer->channelCount || (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER)) {
  1763. return -1;
  1764. }
  1765. fragmentLength = ENET_NET_TO_HOST_16(command->sendFragment.dataLength);
  1766. *currentData += fragmentLength;
  1767. if (fragmentLength > host->maximumPacketSize || *currentData < host->receivedData || *currentData > &host->receivedData[host->receivedDataLength]) {
  1768. return -1;
  1769. }
  1770. channel = &peer->channels[command->header.channelID];
  1771. reliableSequenceNumber = command->header.reliableSequenceNumber;
  1772. startSequenceNumber = ENET_NET_TO_HOST_16(command->sendFragment.startSequenceNumber);
  1773. reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  1774. currentWindow = channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  1775. if (reliableSequenceNumber < channel->incomingReliableSequenceNumber) {
  1776. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  1777. }
  1778. if (reliableWindow < currentWindow || reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1) {
  1779. return 0;
  1780. }
  1781. if (reliableSequenceNumber == channel->incomingReliableSequenceNumber && startSequenceNumber <= channel->incomingUnreliableSequenceNumber) {
  1782. return 0;
  1783. }
  1784. fragmentNumber = ENET_NET_TO_HOST_32(command->sendFragment.fragmentNumber);
  1785. fragmentCount = ENET_NET_TO_HOST_32(command->sendFragment.fragmentCount);
  1786. fragmentOffset = ENET_NET_TO_HOST_32(command->sendFragment.fragmentOffset);
  1787. totalLength = ENET_NET_TO_HOST_32(command->sendFragment.totalLength);
  1788. if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT ||
  1789. fragmentNumber >= fragmentCount ||
  1790. totalLength > host->maximumPacketSize ||
  1791. fragmentOffset >= totalLength ||
  1792. fragmentLength > totalLength - fragmentOffset
  1793. ) {
  1794. return -1;
  1795. }
  1796. for (currentCommand = enet_list_previous(enet_list_end(&channel->incomingUnreliableCommands));
  1797. currentCommand != enet_list_end(&channel->incomingUnreliableCommands);
  1798. currentCommand = enet_list_previous(currentCommand)
  1799. ) {
  1800. ENetIncomingCommand *incomingCommand = (ENetIncomingCommand *) currentCommand;
  1801. if (reliableSequenceNumber >= channel->incomingReliableSequenceNumber) {
  1802. if (incomingCommand->reliableSequenceNumber < channel->incomingReliableSequenceNumber) {
  1803. continue;
  1804. }
  1805. } else if (incomingCommand->reliableSequenceNumber >= channel->incomingReliableSequenceNumber) {
  1806. break;
  1807. }
  1808. if (incomingCommand->reliableSequenceNumber < reliableSequenceNumber) {
  1809. break;
  1810. }
  1811. if (incomingCommand->reliableSequenceNumber > reliableSequenceNumber) {
  1812. continue;
  1813. }
  1814. if (incomingCommand->unreliableSequenceNumber <= startSequenceNumber) {
  1815. if (incomingCommand->unreliableSequenceNumber < startSequenceNumber) {
  1816. break;
  1817. }
  1818. if ((incomingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK) !=
  1819. ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT ||
  1820. totalLength != incomingCommand->packet->dataLength ||
  1821. fragmentCount != incomingCommand->fragmentCount
  1822. ) {
  1823. return -1;
  1824. }
  1825. startCommand = incomingCommand;
  1826. break;
  1827. }
  1828. }
  1829. if (startCommand == NULL) {
  1830. startCommand = enet_peer_queue_incoming_command(peer, command, NULL, totalLength,
  1831. ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT, fragmentCount);
  1832. if (startCommand == NULL) {
  1833. return -1;
  1834. }
  1835. }
  1836. if ((startCommand->fragments[fragmentNumber / 32] & (1 << (fragmentNumber % 32))) == 0) {
  1837. --startCommand->fragmentsRemaining;
  1838. startCommand->fragments[fragmentNumber / 32] |= (1 << (fragmentNumber % 32));
  1839. if (fragmentOffset + fragmentLength > startCommand->packet->dataLength) {
  1840. fragmentLength = startCommand->packet->dataLength - fragmentOffset;
  1841. }
  1842. memcpy(startCommand->packet->data + fragmentOffset, (enet_uint8 *) command + sizeof(ENetProtocolSendFragment), fragmentLength);
  1843. if (startCommand->fragmentsRemaining <= 0) {
  1844. enet_peer_dispatch_incoming_unreliable_commands(peer, channel);
  1845. }
  1846. }
  1847. return 0;
  1848. } /* enet_protocol_handle_send_unreliable_fragment */
  1849. static int enet_protocol_handle_ping(ENetHost *host, ENetPeer *peer, const ENetProtocol *command) {
  1850. ENET_UNUSED(host)
  1851. ENET_UNUSED(command)
  1852. if (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) {
  1853. return -1;
  1854. }
  1855. return 0;
  1856. }
  1857. static int enet_protocol_handle_bandwidth_limit(ENetHost *host, ENetPeer *peer, const ENetProtocol *command) {
  1858. if (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) {
  1859. return -1;
  1860. }
  1861. if (peer->incomingBandwidth != 0) {
  1862. --host->bandwidthLimitedPeers;
  1863. }
  1864. peer->incomingBandwidth = ENET_NET_TO_HOST_32(command->bandwidthLimit.incomingBandwidth);
  1865. peer->outgoingBandwidth = ENET_NET_TO_HOST_32(command->bandwidthLimit.outgoingBandwidth);
  1866. if (peer->incomingBandwidth != 0) {
  1867. ++host->bandwidthLimitedPeers;
  1868. }
  1869. if (peer->incomingBandwidth == 0 && host->outgoingBandwidth == 0) {
  1870. peer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  1871. } else if (peer->incomingBandwidth == 0 || host->outgoingBandwidth == 0) {
  1872. peer->windowSize = (ENET_MAX(peer->incomingBandwidth, host->outgoingBandwidth)
  1873. / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  1874. } else {
  1875. peer->windowSize = (ENET_MIN(peer->incomingBandwidth, host->outgoingBandwidth)
  1876. / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  1877. }
  1878. if (peer->windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) {
  1879. peer->windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  1880. } else if (peer->windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) {
  1881. peer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  1882. }
  1883. return 0;
  1884. } /* enet_protocol_handle_bandwidth_limit */
  1885. static int enet_protocol_handle_throttle_configure(ENetHost *host, ENetPeer *peer, const ENetProtocol *command) {
  1886. ENET_UNUSED(host)
  1887. if (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) {
  1888. return -1;
  1889. }
  1890. peer->packetThrottleInterval = ENET_NET_TO_HOST_32(command->throttleConfigure.packetThrottleInterval);
  1891. peer->packetThrottleAcceleration = ENET_NET_TO_HOST_32(command->throttleConfigure.packetThrottleAcceleration);
  1892. peer->packetThrottleDeceleration = ENET_NET_TO_HOST_32(command->throttleConfigure.packetThrottleDeceleration);
  1893. return 0;
  1894. }
  1895. static int enet_protocol_handle_disconnect(ENetHost *host, ENetPeer *peer, const ENetProtocol *command) {
  1896. if (peer->state == ENET_PEER_STATE_DISCONNECTED || peer->state == ENET_PEER_STATE_ZOMBIE ||
  1897. peer->state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT
  1898. ) {
  1899. return 0;
  1900. }
  1901. enet_peer_reset_queues(peer);
  1902. if (peer->state == ENET_PEER_STATE_CONNECTION_SUCCEEDED || peer->state == ENET_PEER_STATE_DISCONNECTING || peer->state == ENET_PEER_STATE_CONNECTING) {
  1903. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  1904. }
  1905. else if (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) {
  1906. if (peer->state == ENET_PEER_STATE_CONNECTION_PENDING) { host->recalculateBandwidthLimits = 1; }
  1907. enet_peer_reset(peer);
  1908. }
  1909. else if (command->header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) {
  1910. enet_protocol_change_state(host, peer, ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT);
  1911. }
  1912. else {
  1913. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  1914. }
  1915. if (peer->state != ENET_PEER_STATE_DISCONNECTED) {
  1916. peer->eventData = ENET_NET_TO_HOST_32(command->disconnect.data);
  1917. }
  1918. return 0;
  1919. }
  1920. static int enet_protocol_handle_acknowledge(ENetHost *host, ENetEvent *event, ENetPeer *peer, const ENetProtocol *command) {
  1921. enet_uint32 roundTripTime, receivedSentTime, receivedReliableSequenceNumber;
  1922. ENetProtocolCommand commandNumber;
  1923. if (peer->state == ENET_PEER_STATE_DISCONNECTED || peer->state == ENET_PEER_STATE_ZOMBIE) {
  1924. return 0;
  1925. }
  1926. receivedSentTime = ENET_NET_TO_HOST_16(command->acknowledge.receivedSentTime);
  1927. receivedSentTime |= host->serviceTime & 0xFFFF0000;
  1928. if ((receivedSentTime & 0x8000) > (host->serviceTime & 0x8000)) {
  1929. receivedSentTime -= 0x10000;
  1930. }
  1931. if (ENET_TIME_LESS(host->serviceTime, receivedSentTime)) {
  1932. return 0;
  1933. }
  1934. peer->lastReceiveTime = host->serviceTime;
  1935. peer->earliestTimeout = 0;
  1936. roundTripTime = ENET_TIME_DIFFERENCE(host->serviceTime, receivedSentTime);
  1937. enet_peer_throttle(peer, roundTripTime);
  1938. peer->roundTripTimeVariance -= peer->roundTripTimeVariance / 4;
  1939. if (roundTripTime >= peer->roundTripTime) {
  1940. peer->roundTripTime += (roundTripTime - peer->roundTripTime) / 8;
  1941. peer->roundTripTimeVariance += (roundTripTime - peer->roundTripTime) / 4;
  1942. } else {
  1943. peer->roundTripTime -= (peer->roundTripTime - roundTripTime) / 8;
  1944. peer->roundTripTimeVariance += (peer->roundTripTime - roundTripTime) / 4;
  1945. }
  1946. if (peer->roundTripTime < peer->lowestRoundTripTime) {
  1947. peer->lowestRoundTripTime = peer->roundTripTime;
  1948. }
  1949. if (peer->roundTripTimeVariance > peer->highestRoundTripTimeVariance) {
  1950. peer->highestRoundTripTimeVariance = peer->roundTripTimeVariance;
  1951. }
  1952. if (peer->packetThrottleEpoch == 0 ||
  1953. ENET_TIME_DIFFERENCE(host->serviceTime, peer->packetThrottleEpoch) >= peer->packetThrottleInterval
  1954. ) {
  1955. peer->lastRoundTripTime = peer->lowestRoundTripTime;
  1956. peer->lastRoundTripTimeVariance = peer->highestRoundTripTimeVariance;
  1957. peer->lowestRoundTripTime = peer->roundTripTime;
  1958. peer->highestRoundTripTimeVariance = peer->roundTripTimeVariance;
  1959. peer->packetThrottleEpoch = host->serviceTime;
  1960. }
  1961. receivedReliableSequenceNumber = ENET_NET_TO_HOST_16(command->acknowledge.receivedReliableSequenceNumber);
  1962. commandNumber = enet_protocol_remove_sent_reliable_command(peer, receivedReliableSequenceNumber, command->header.channelID);
  1963. switch (peer->state) {
  1964. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  1965. if (commandNumber != ENET_PROTOCOL_COMMAND_VERIFY_CONNECT) {
  1966. return -1;
  1967. }
  1968. enet_protocol_notify_connect(host, peer, event);
  1969. break;
  1970. case ENET_PEER_STATE_DISCONNECTING:
  1971. if (commandNumber != ENET_PROTOCOL_COMMAND_DISCONNECT) {
  1972. return -1;
  1973. }
  1974. enet_protocol_notify_disconnect(host, peer, event);
  1975. break;
  1976. case ENET_PEER_STATE_DISCONNECT_LATER:
  1977. if (enet_list_empty(&peer->outgoingReliableCommands) &&
  1978. enet_list_empty(&peer->outgoingUnreliableCommands) &&
  1979. enet_list_empty(&peer->sentReliableCommands))
  1980. {
  1981. enet_peer_disconnect(peer, peer->eventData);
  1982. }
  1983. break;
  1984. default:
  1985. break;
  1986. }
  1987. return 0;
  1988. } /* enet_protocol_handle_acknowledge */
  1989. static int enet_protocol_handle_verify_connect(ENetHost *host, ENetEvent *event, ENetPeer *peer, const ENetProtocol *command) {
  1990. enet_uint32 mtu, windowSize;
  1991. size_t channelCount;
  1992. if (peer->state != ENET_PEER_STATE_CONNECTING) {
  1993. return 0;
  1994. }
  1995. channelCount = ENET_NET_TO_HOST_32(command->verifyConnect.channelCount);
  1996. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT || channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT ||
  1997. ENET_NET_TO_HOST_32(command->verifyConnect.packetThrottleInterval) != peer->packetThrottleInterval ||
  1998. ENET_NET_TO_HOST_32(command->verifyConnect.packetThrottleAcceleration) != peer->packetThrottleAcceleration ||
  1999. ENET_NET_TO_HOST_32(command->verifyConnect.packetThrottleDeceleration) != peer->packetThrottleDeceleration ||
  2000. command->verifyConnect.connectID != peer->connectID
  2001. ) {
  2002. peer->eventData = 0;
  2003. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  2004. return -1;
  2005. }
  2006. enet_protocol_remove_sent_reliable_command(peer, 1, 0xFF);
  2007. if (channelCount < peer->channelCount) {
  2008. peer->channelCount = channelCount;
  2009. }
  2010. peer->outgoingPeerID = ENET_NET_TO_HOST_16(command->verifyConnect.outgoingPeerID);
  2011. peer->incomingSessionID = command->verifyConnect.incomingSessionID;
  2012. peer->outgoingSessionID = command->verifyConnect.outgoingSessionID;
  2013. mtu = ENET_NET_TO_HOST_32(command->verifyConnect.mtu);
  2014. if (mtu < ENET_PROTOCOL_MINIMUM_MTU) {
  2015. mtu = ENET_PROTOCOL_MINIMUM_MTU;
  2016. } else if (mtu > ENET_PROTOCOL_MAXIMUM_MTU) {
  2017. mtu = ENET_PROTOCOL_MAXIMUM_MTU;
  2018. }
  2019. if (mtu < peer->mtu) {
  2020. peer->mtu = mtu;
  2021. }
  2022. windowSize = ENET_NET_TO_HOST_32(command->verifyConnect.windowSize);
  2023. if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) {
  2024. windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  2025. }
  2026. if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) {
  2027. windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  2028. }
  2029. if (windowSize < peer->windowSize) {
  2030. peer->windowSize = windowSize;
  2031. }
  2032. peer->incomingBandwidth = ENET_NET_TO_HOST_32(command->verifyConnect.incomingBandwidth);
  2033. peer->outgoingBandwidth = ENET_NET_TO_HOST_32(command->verifyConnect.outgoingBandwidth);
  2034. enet_protocol_notify_connect(host, peer, event);
  2035. return 0;
  2036. } /* enet_protocol_handle_verify_connect */
  2037. static int enet_protocol_handle_incoming_commands(ENetHost *host, ENetEvent *event) {
  2038. ENetProtocolHeader *header;
  2039. ENetProtocol *command;
  2040. ENetPeer *peer;
  2041. enet_uint8 *currentData;
  2042. size_t headerSize;
  2043. enet_uint16 peerID, flags;
  2044. enet_uint8 sessionID;
  2045. if (host->receivedDataLength < (size_t) &((ENetProtocolHeader *) 0)->sentTime) {
  2046. return 0;
  2047. }
  2048. header = (ENetProtocolHeader *) host->receivedData;
  2049. peerID = ENET_NET_TO_HOST_16(header->peerID);
  2050. sessionID = (peerID & ENET_PROTOCOL_HEADER_SESSION_MASK) >> ENET_PROTOCOL_HEADER_SESSION_SHIFT;
  2051. flags = peerID & ENET_PROTOCOL_HEADER_FLAG_MASK;
  2052. peerID &= ~(ENET_PROTOCOL_HEADER_FLAG_MASK | ENET_PROTOCOL_HEADER_SESSION_MASK);
  2053. headerSize = (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ? sizeof(ENetProtocolHeader) : (size_t) &((ENetProtocolHeader *) 0)->sentTime);
  2054. if (host->checksum != NULL) {
  2055. headerSize += sizeof(enet_uint32);
  2056. }
  2057. if (peerID == ENET_PROTOCOL_MAXIMUM_PEER_ID) {
  2058. peer = NULL;
  2059. } else if (peerID >= host->peerCount) {
  2060. return 0;
  2061. } else {
  2062. peer = &host->peers[peerID];
  2063. if (peer->state == ENET_PEER_STATE_DISCONNECTED ||
  2064. peer->state == ENET_PEER_STATE_ZOMBIE ||
  2065. ((!in6_equal(host->receivedAddress.host , peer->address.host) ||
  2066. host->receivedAddress.port != peer->address.port) &&
  2067. 1 /* no broadcast in ipv6 !in6_equal(peer->address.host , ENET_HOST_BROADCAST)*/) ||
  2068. (peer->outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID &&
  2069. sessionID != peer->incomingSessionID)
  2070. ) {
  2071. return 0;
  2072. }
  2073. }
  2074. if (flags & ENET_PROTOCOL_HEADER_FLAG_COMPRESSED) {
  2075. size_t originalSize;
  2076. if (host->compressor.context == NULL || host->compressor.decompress == NULL) {
  2077. return 0;
  2078. }
  2079. originalSize = host->compressor.decompress(host->compressor.context,
  2080. host->receivedData + headerSize,
  2081. host->receivedDataLength - headerSize,
  2082. host->packetData[1] + headerSize,
  2083. sizeof(host->packetData[1]) - headerSize
  2084. );
  2085. if (originalSize <= 0 || originalSize > sizeof(host->packetData[1]) - headerSize) {
  2086. return 0;
  2087. }
  2088. memcpy(host->packetData[1], header, headerSize);
  2089. host->receivedData = host->packetData[1];
  2090. host->receivedDataLength = headerSize + originalSize;
  2091. }
  2092. if (host->checksum != NULL) {
  2093. enet_uint32 *checksum = (enet_uint32 *) &host->receivedData[headerSize - sizeof(enet_uint32)];
  2094. enet_uint32 desiredChecksum = *checksum;
  2095. ENetBuffer buffer;
  2096. *checksum = peer != NULL ? peer->connectID : 0;
  2097. buffer.data = host->receivedData;
  2098. buffer.dataLength = host->receivedDataLength;
  2099. if (host->checksum(&buffer, 1) != desiredChecksum) {
  2100. return 0;
  2101. }
  2102. }
  2103. if (peer != NULL) {
  2104. peer->address.host = host->receivedAddress.host;
  2105. peer->address.port = host->receivedAddress.port;
  2106. peer->incomingDataTotal += host->receivedDataLength;
  2107. peer->totalDataReceived += host->receivedDataLength;
  2108. }
  2109. currentData = host->receivedData + headerSize;
  2110. while (currentData < &host->receivedData[host->receivedDataLength]) {
  2111. enet_uint8 commandNumber;
  2112. size_t commandSize;
  2113. command = (ENetProtocol *) currentData;
  2114. if (currentData + sizeof(ENetProtocolCommandHeader) > &host->receivedData[host->receivedDataLength]) {
  2115. break;
  2116. }
  2117. commandNumber = command->header.command & ENET_PROTOCOL_COMMAND_MASK;
  2118. if (commandNumber >= ENET_PROTOCOL_COMMAND_COUNT) {
  2119. break;
  2120. }
  2121. commandSize = commandSizes[commandNumber];
  2122. if (commandSize == 0 || currentData + commandSize > &host->receivedData[host->receivedDataLength]) {
  2123. break;
  2124. }
  2125. currentData += commandSize;
  2126. if (peer == NULL && (commandNumber != ENET_PROTOCOL_COMMAND_CONNECT || currentData < &host->receivedData[host->receivedDataLength])) {
  2127. break;
  2128. }
  2129. command->header.reliableSequenceNumber = ENET_NET_TO_HOST_16(command->header.reliableSequenceNumber);
  2130. switch (commandNumber) {
  2131. case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
  2132. if (enet_protocol_handle_acknowledge(host, event, peer, command)) {
  2133. goto commandError;
  2134. }
  2135. break;
  2136. case ENET_PROTOCOL_COMMAND_CONNECT:
  2137. if (peer != NULL) {
  2138. goto commandError;
  2139. }
  2140. peer = enet_protocol_handle_connect(host, header, command);
  2141. if (peer == NULL) {
  2142. goto commandError;
  2143. }
  2144. break;
  2145. case ENET_PROTOCOL_COMMAND_VERIFY_CONNECT:
  2146. if (enet_protocol_handle_verify_connect(host, event, peer, command)) {
  2147. goto commandError;
  2148. }
  2149. break;
  2150. case ENET_PROTOCOL_COMMAND_DISCONNECT:
  2151. if (enet_protocol_handle_disconnect(host, peer, command)) {
  2152. goto commandError;
  2153. }
  2154. break;
  2155. case ENET_PROTOCOL_COMMAND_PING:
  2156. if (enet_protocol_handle_ping(host, peer, command)) {
  2157. goto commandError;
  2158. }
  2159. break;
  2160. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  2161. if (enet_protocol_handle_send_reliable(host, peer, command, &currentData)) {
  2162. goto commandError;
  2163. }
  2164. break;
  2165. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  2166. if (enet_protocol_handle_send_unreliable(host, peer, command, &currentData)) {
  2167. goto commandError;
  2168. }
  2169. break;
  2170. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  2171. if (enet_protocol_handle_send_unsequenced(host, peer, command, &currentData)) {
  2172. goto commandError;
  2173. }
  2174. break;
  2175. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  2176. if (enet_protocol_handle_send_fragment(host, peer, command, &currentData)) {
  2177. goto commandError;
  2178. }
  2179. break;
  2180. case ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT:
  2181. if (enet_protocol_handle_bandwidth_limit(host, peer, command)) {
  2182. goto commandError;
  2183. }
  2184. break;
  2185. case ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE:
  2186. if (enet_protocol_handle_throttle_configure(host, peer, command)) {
  2187. goto commandError;
  2188. }
  2189. break;
  2190. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
  2191. if (enet_protocol_handle_send_unreliable_fragment(host, peer, command, &currentData)) {
  2192. goto commandError;
  2193. }
  2194. break;
  2195. default:
  2196. goto commandError;
  2197. }
  2198. if (peer != NULL && (command->header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0) {
  2199. enet_uint16 sentTime;
  2200. if (!(flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)) {
  2201. break;
  2202. }
  2203. sentTime = ENET_NET_TO_HOST_16(header->sentTime);
  2204. switch (peer->state) {
  2205. case ENET_PEER_STATE_DISCONNECTING:
  2206. case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
  2207. case ENET_PEER_STATE_DISCONNECTED:
  2208. case ENET_PEER_STATE_ZOMBIE:
  2209. break;
  2210. case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
  2211. if ((command->header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT) {
  2212. enet_peer_queue_acknowledgement(peer, command, sentTime);
  2213. }
  2214. break;
  2215. default:
  2216. enet_peer_queue_acknowledgement(peer, command, sentTime);
  2217. break;
  2218. }
  2219. }
  2220. }
  2221. commandError:
  2222. if (event != NULL && event->type != ENET_EVENT_TYPE_NONE) {
  2223. return 1;
  2224. }
  2225. return 0;
  2226. } /* enet_protocol_handle_incoming_commands */
  2227. static int enet_protocol_receive_incoming_commands(ENetHost *host, ENetEvent *event) {
  2228. int packets;
  2229. for (packets = 0; packets < 256; ++packets) {
  2230. int receivedLength;
  2231. ENetBuffer buffer;
  2232. buffer.data = host->packetData[0];
  2233. // buffer.dataLength = sizeof (host->packetData[0]);
  2234. buffer.dataLength = host->mtu;
  2235. receivedLength = enet_socket_receive(host->socket, &host->receivedAddress, &buffer, 1);
  2236. if (receivedLength == -2)
  2237. continue;
  2238. if (receivedLength < 0) {
  2239. return -1;
  2240. }
  2241. if (receivedLength == 0) {
  2242. return 0;
  2243. }
  2244. host->receivedData = host->packetData[0];
  2245. host->receivedDataLength = receivedLength;
  2246. host->totalReceivedData += receivedLength;
  2247. host->totalReceivedPackets++;
  2248. if (host->intercept != NULL) {
  2249. switch (host->intercept(host, (void *)event)) {
  2250. case 1:
  2251. if (event != NULL && event->type != ENET_EVENT_TYPE_NONE) {
  2252. return 1;
  2253. }
  2254. continue;
  2255. case -1:
  2256. return -1;
  2257. default:
  2258. break;
  2259. }
  2260. }
  2261. switch (enet_protocol_handle_incoming_commands(host, event)) {
  2262. case 1:
  2263. return 1;
  2264. case -1:
  2265. return -1;
  2266. default:
  2267. break;
  2268. }
  2269. }
  2270. return -1;
  2271. } /* enet_protocol_receive_incoming_commands */
  2272. static void enet_protocol_send_acknowledgements(ENetHost *host, ENetPeer *peer) {
  2273. ENetProtocol *command = &host->commands[host->commandCount];
  2274. ENetBuffer *buffer = &host->buffers[host->bufferCount];
  2275. ENetAcknowledgement *acknowledgement;
  2276. ENetListIterator currentAcknowledgement;
  2277. enet_uint16 reliableSequenceNumber;
  2278. currentAcknowledgement = enet_list_begin(&peer->acknowledgements);
  2279. while (currentAcknowledgement != enet_list_end(&peer->acknowledgements)) {
  2280. if (command >= &host->commands[sizeof(host->commands) / sizeof(ENetProtocol)] ||
  2281. buffer >= &host->buffers[sizeof(host->buffers) / sizeof(ENetBuffer)] ||
  2282. peer->mtu - host->packetSize < sizeof(ENetProtocolAcknowledge)
  2283. ) {
  2284. host->continueSending = 1;
  2285. break;
  2286. }
  2287. acknowledgement = (ENetAcknowledgement *) currentAcknowledgement;
  2288. currentAcknowledgement = enet_list_next(currentAcknowledgement);
  2289. buffer->data = command;
  2290. buffer->dataLength = sizeof(ENetProtocolAcknowledge);
  2291. host->packetSize += buffer->dataLength;
  2292. reliableSequenceNumber = ENET_HOST_TO_NET_16(acknowledgement->command.header.reliableSequenceNumber);
  2293. command->header.command = ENET_PROTOCOL_COMMAND_ACKNOWLEDGE;
  2294. command->header.channelID = acknowledgement->command.header.channelID;
  2295. command->header.reliableSequenceNumber = reliableSequenceNumber;
  2296. command->acknowledge.receivedReliableSequenceNumber = reliableSequenceNumber;
  2297. command->acknowledge.receivedSentTime = ENET_HOST_TO_NET_16(acknowledgement->sentTime);
  2298. if ((acknowledgement->command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_DISCONNECT) {
  2299. enet_protocol_dispatch_state(host, peer, ENET_PEER_STATE_ZOMBIE);
  2300. }
  2301. enet_list_remove(&acknowledgement->acknowledgementList);
  2302. enet_free(acknowledgement);
  2303. ++command;
  2304. ++buffer;
  2305. }
  2306. host->commandCount = command - host->commands;
  2307. host->bufferCount = buffer - host->buffers;
  2308. } /* enet_protocol_send_acknowledgements */
  2309. static void enet_protocol_send_unreliable_outgoing_commands(ENetHost *host, ENetPeer *peer) {
  2310. ENetProtocol *command = &host->commands[host->commandCount];
  2311. ENetBuffer *buffer = &host->buffers[host->bufferCount];
  2312. ENetOutgoingCommand *outgoingCommand;
  2313. ENetListIterator currentCommand;
  2314. currentCommand = enet_list_begin(&peer->outgoingUnreliableCommands);
  2315. while (currentCommand != enet_list_end(&peer->outgoingUnreliableCommands)) {
  2316. size_t commandSize;
  2317. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  2318. commandSize = commandSizes[outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK];
  2319. if (command >= &host->commands[sizeof(host->commands) / sizeof(ENetProtocol)] ||
  2320. buffer + 1 >= &host->buffers[sizeof(host->buffers) / sizeof(ENetBuffer)] ||
  2321. peer->mtu - host->packetSize < commandSize ||
  2322. (outgoingCommand->packet != NULL &&
  2323. peer->mtu - host->packetSize < commandSize + outgoingCommand->fragmentLength)
  2324. ) {
  2325. host->continueSending = 1;
  2326. break;
  2327. }
  2328. currentCommand = enet_list_next(currentCommand);
  2329. if (outgoingCommand->packet != NULL && outgoingCommand->fragmentOffset == 0) {
  2330. peer->packetThrottleCounter += ENET_PEER_PACKET_THROTTLE_COUNTER;
  2331. peer->packetThrottleCounter %= ENET_PEER_PACKET_THROTTLE_SCALE;
  2332. if (peer->packetThrottleCounter > peer->packetThrottle) {
  2333. enet_uint16 reliableSequenceNumber = outgoingCommand->reliableSequenceNumber;
  2334. enet_uint16 unreliableSequenceNumber = outgoingCommand->unreliableSequenceNumber;
  2335. for (;;) {
  2336. --outgoingCommand->packet->referenceCount;
  2337. if (outgoingCommand->packet->referenceCount == 0) {
  2338. callbacks.packet_destroy(outgoingCommand->packet);
  2339. }
  2340. enet_list_remove(&outgoingCommand->outgoingCommandList);
  2341. enet_free(outgoingCommand);
  2342. if (currentCommand == enet_list_end(&peer->outgoingUnreliableCommands)) {
  2343. break;
  2344. }
  2345. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  2346. if (outgoingCommand->reliableSequenceNumber != reliableSequenceNumber || outgoingCommand->unreliableSequenceNumber != unreliableSequenceNumber) {
  2347. break;
  2348. }
  2349. currentCommand = enet_list_next(currentCommand);
  2350. }
  2351. continue;
  2352. }
  2353. }
  2354. buffer->data = command;
  2355. buffer->dataLength = commandSize;
  2356. host->packetSize += buffer->dataLength;
  2357. *command = outgoingCommand->command;
  2358. enet_list_remove(&outgoingCommand->outgoingCommandList);
  2359. if (outgoingCommand->packet != NULL) {
  2360. ++buffer;
  2361. buffer->data = outgoingCommand->packet->data + outgoingCommand->fragmentOffset;
  2362. buffer->dataLength = outgoingCommand->fragmentLength;
  2363. host->packetSize += buffer->dataLength;
  2364. enet_list_insert(enet_list_end(&peer->sentUnreliableCommands), outgoingCommand);
  2365. } else {
  2366. enet_free(outgoingCommand);
  2367. }
  2368. ++command;
  2369. ++buffer;
  2370. }
  2371. host->commandCount = command - host->commands;
  2372. host->bufferCount = buffer - host->buffers;
  2373. if (peer->state == ENET_PEER_STATE_DISCONNECT_LATER &&
  2374. enet_list_empty(&peer->outgoingReliableCommands) &&
  2375. enet_list_empty(&peer->outgoingUnreliableCommands) &&
  2376. enet_list_empty(&peer->sentReliableCommands))
  2377. {
  2378. enet_peer_disconnect(peer, peer->eventData);
  2379. }
  2380. } /* enet_protocol_send_unreliable_outgoing_commands */
  2381. static int enet_protocol_check_timeouts(ENetHost *host, ENetPeer *peer, ENetEvent *event) {
  2382. ENetOutgoingCommand *outgoingCommand;
  2383. ENetListIterator currentCommand, insertPosition;
  2384. currentCommand = enet_list_begin(&peer->sentReliableCommands);
  2385. insertPosition = enet_list_begin(&peer->outgoingReliableCommands);
  2386. while (currentCommand != enet_list_end(&peer->sentReliableCommands)) {
  2387. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  2388. currentCommand = enet_list_next(currentCommand);
  2389. if (ENET_TIME_DIFFERENCE(host->serviceTime, outgoingCommand->sentTime) < outgoingCommand->roundTripTimeout) {
  2390. continue;
  2391. }
  2392. if (peer->earliestTimeout == 0 || ENET_TIME_LESS(outgoingCommand->sentTime, peer->earliestTimeout)) {
  2393. peer->earliestTimeout = outgoingCommand->sentTime;
  2394. }
  2395. if (peer->earliestTimeout != 0 &&
  2396. (ENET_TIME_DIFFERENCE(host->serviceTime, peer->earliestTimeout) >= peer->timeoutMaximum ||
  2397. (outgoingCommand->roundTripTimeout >= outgoingCommand->roundTripTimeoutLimit &&
  2398. ENET_TIME_DIFFERENCE(host->serviceTime, peer->earliestTimeout) >= peer->timeoutMinimum))
  2399. ) {
  2400. enet_protocol_notify_disconnect_timeout(host, peer, event);
  2401. return 1;
  2402. }
  2403. if (outgoingCommand->packet != NULL) {
  2404. peer->reliableDataInTransit -= outgoingCommand->fragmentLength;
  2405. }
  2406. ++peer->packetsLost;
  2407. ++peer->totalPacketsLost;
  2408. /* Replaced exponential backoff time with something more linear */
  2409. /* Source: http://lists.cubik.org/pipermail/enet-discuss/2014-May/002308.html */
  2410. outgoingCommand->roundTripTimeout = peer->roundTripTime + 4 * peer->roundTripTimeVariance;
  2411. outgoingCommand->roundTripTimeoutLimit = peer->timeoutLimit * outgoingCommand->roundTripTimeout;
  2412. enet_list_insert(insertPosition, enet_list_remove(&outgoingCommand->outgoingCommandList));
  2413. if (currentCommand == enet_list_begin(&peer->sentReliableCommands) && !enet_list_empty(&peer->sentReliableCommands)) {
  2414. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  2415. peer->nextTimeout = outgoingCommand->sentTime + outgoingCommand->roundTripTimeout;
  2416. }
  2417. }
  2418. return 0;
  2419. } /* enet_protocol_check_timeouts */
  2420. static int enet_protocol_send_reliable_outgoing_commands(ENetHost *host, ENetPeer *peer) {
  2421. ENetProtocol *command = &host->commands[host->commandCount];
  2422. ENetBuffer *buffer = &host->buffers[host->bufferCount];
  2423. ENetOutgoingCommand *outgoingCommand;
  2424. ENetListIterator currentCommand;
  2425. ENetChannel *channel;
  2426. enet_uint16 reliableWindow;
  2427. size_t commandSize;
  2428. int windowExceeded = 0, windowWrap = 0, canPing = 1;
  2429. currentCommand = enet_list_begin(&peer->outgoingReliableCommands);
  2430. while (currentCommand != enet_list_end(&peer->outgoingReliableCommands)) {
  2431. outgoingCommand = (ENetOutgoingCommand *) currentCommand;
  2432. channel = outgoingCommand->command.header.channelID < peer->channelCount ? &peer->channels[outgoingCommand->command.header.channelID] : NULL;
  2433. reliableWindow = outgoingCommand->reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  2434. if (channel != NULL) {
  2435. if (!windowWrap &&
  2436. outgoingCommand->sendAttempts < 1 &&
  2437. !(outgoingCommand->reliableSequenceNumber % ENET_PEER_RELIABLE_WINDOW_SIZE) &&
  2438. (channel->reliableWindows[(reliableWindow + ENET_PEER_RELIABLE_WINDOWS - 1)
  2439. % ENET_PEER_RELIABLE_WINDOWS] >= ENET_PEER_RELIABLE_WINDOW_SIZE ||
  2440. channel->usedReliableWindows & ((((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1) << reliableWindow)
  2441. | (((1 << ENET_PEER_FREE_RELIABLE_WINDOWS) - 1) >> (ENET_PEER_RELIABLE_WINDOWS - reliableWindow))))
  2442. ) {
  2443. windowWrap = 1;
  2444. }
  2445. if (windowWrap) {
  2446. currentCommand = enet_list_next(currentCommand);
  2447. continue;
  2448. }
  2449. }
  2450. if (outgoingCommand->packet != NULL) {
  2451. if (!windowExceeded) {
  2452. enet_uint32 windowSize = (peer->packetThrottle * peer->windowSize) / ENET_PEER_PACKET_THROTTLE_SCALE;
  2453. if (peer->reliableDataInTransit + outgoingCommand->fragmentLength > ENET_MAX(windowSize, peer->mtu)) {
  2454. windowExceeded = 1;
  2455. }
  2456. }
  2457. if (windowExceeded) {
  2458. currentCommand = enet_list_next(currentCommand);
  2459. continue;
  2460. }
  2461. }
  2462. canPing = 0;
  2463. commandSize = commandSizes[outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK];
  2464. if (command >= &host->commands[sizeof(host->commands) / sizeof(ENetProtocol)] ||
  2465. buffer + 1 >= &host->buffers[sizeof(host->buffers) / sizeof(ENetBuffer)] ||
  2466. peer->mtu - host->packetSize < commandSize ||
  2467. (outgoingCommand->packet != NULL &&
  2468. (enet_uint16) (peer->mtu - host->packetSize) < (enet_uint16) (commandSize + outgoingCommand->fragmentLength))
  2469. ) {
  2470. host->continueSending = 1;
  2471. break;
  2472. }
  2473. currentCommand = enet_list_next(currentCommand);
  2474. if (channel != NULL && outgoingCommand->sendAttempts < 1) {
  2475. channel->usedReliableWindows |= 1 << reliableWindow;
  2476. ++channel->reliableWindows[reliableWindow];
  2477. }
  2478. ++outgoingCommand->sendAttempts;
  2479. if (outgoingCommand->roundTripTimeout == 0) {
  2480. outgoingCommand->roundTripTimeout = peer->roundTripTime + 4 * peer->roundTripTimeVariance;
  2481. outgoingCommand->roundTripTimeoutLimit = peer->timeoutLimit * outgoingCommand->roundTripTimeout;
  2482. }
  2483. if (enet_list_empty(&peer->sentReliableCommands)) {
  2484. peer->nextTimeout = host->serviceTime + outgoingCommand->roundTripTimeout;
  2485. }
  2486. enet_list_insert(enet_list_end(&peer->sentReliableCommands), enet_list_remove(&outgoingCommand->outgoingCommandList));
  2487. outgoingCommand->sentTime = host->serviceTime;
  2488. buffer->data = command;
  2489. buffer->dataLength = commandSize;
  2490. host->packetSize += buffer->dataLength;
  2491. host->headerFlags |= ENET_PROTOCOL_HEADER_FLAG_SENT_TIME;
  2492. *command = outgoingCommand->command;
  2493. if (outgoingCommand->packet != NULL) {
  2494. ++buffer;
  2495. buffer->data = outgoingCommand->packet->data + outgoingCommand->fragmentOffset;
  2496. buffer->dataLength = outgoingCommand->fragmentLength;
  2497. host->packetSize += outgoingCommand->fragmentLength;
  2498. peer->reliableDataInTransit += outgoingCommand->fragmentLength;
  2499. }
  2500. ++peer->packetsSent;
  2501. ++peer->totalPacketsSent;
  2502. ++command;
  2503. ++buffer;
  2504. }
  2505. host->commandCount = command - host->commands;
  2506. host->bufferCount = buffer - host->buffers;
  2507. return canPing;
  2508. } /* enet_protocol_send_reliable_outgoing_commands */
  2509. static int enet_protocol_send_outgoing_commands(ENetHost *host, ENetEvent *event, int checkForTimeouts) {
  2510. enet_uint8 headerData[sizeof(ENetProtocolHeader) + sizeof(enet_uint32)];
  2511. ENetProtocolHeader *header = (ENetProtocolHeader *) headerData;
  2512. ENetPeer *currentPeer;
  2513. int sentLength;
  2514. size_t shouldCompress = 0;
  2515. host->continueSending = 1;
  2516. while (host->continueSending)
  2517. for (host->continueSending = 0, currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer) {
  2518. if (currentPeer->state == ENET_PEER_STATE_DISCONNECTED || currentPeer->state == ENET_PEER_STATE_ZOMBIE) {
  2519. continue;
  2520. }
  2521. host->headerFlags = 0;
  2522. host->commandCount = 0;
  2523. host->bufferCount = 1;
  2524. host->packetSize = sizeof(ENetProtocolHeader);
  2525. if (!enet_list_empty(&currentPeer->acknowledgements)) {
  2526. enet_protocol_send_acknowledgements(host, currentPeer);
  2527. }
  2528. if (checkForTimeouts != 0 &&
  2529. !enet_list_empty(&currentPeer->sentReliableCommands) &&
  2530. ENET_TIME_GREATER_EQUAL(host->serviceTime, currentPeer->nextTimeout) &&
  2531. enet_protocol_check_timeouts(host, currentPeer, event) == 1
  2532. ) {
  2533. if (event != NULL && event->type != ENET_EVENT_TYPE_NONE) {
  2534. return 1;
  2535. } else {
  2536. continue;
  2537. }
  2538. }
  2539. if ((enet_list_empty(&currentPeer->outgoingReliableCommands) ||
  2540. enet_protocol_send_reliable_outgoing_commands(host, currentPeer)) &&
  2541. enet_list_empty(&currentPeer->sentReliableCommands) &&
  2542. ENET_TIME_DIFFERENCE(host->serviceTime, currentPeer->lastReceiveTime) >= currentPeer->pingInterval &&
  2543. currentPeer->mtu - host->packetSize >= sizeof(ENetProtocolPing)
  2544. ) {
  2545. enet_peer_ping(currentPeer);
  2546. enet_protocol_send_reliable_outgoing_commands(host, currentPeer);
  2547. }
  2548. if (!enet_list_empty(&currentPeer->outgoingUnreliableCommands)) {
  2549. enet_protocol_send_unreliable_outgoing_commands(host, currentPeer);
  2550. }
  2551. if (host->commandCount == 0) {
  2552. continue;
  2553. }
  2554. if (currentPeer->packetLossEpoch == 0) {
  2555. currentPeer->packetLossEpoch = host->serviceTime;
  2556. } else if (ENET_TIME_DIFFERENCE(host->serviceTime, currentPeer->packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL && currentPeer->packetsSent > 0) {
  2557. enet_uint32 packetLoss = currentPeer->packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer->packetsSent;
  2558. #ifdef ENET_DEBUG
  2559. printf(
  2560. "peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer->incomingPeerID,
  2561. currentPeer->packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE,
  2562. currentPeer->packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer->roundTripTime, currentPeer->roundTripTimeVariance,
  2563. currentPeer->packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE,
  2564. enet_list_size(&currentPeer->outgoingReliableCommands),
  2565. enet_list_size(&currentPeer->outgoingUnreliableCommands),
  2566. currentPeer->channels != NULL ? enet_list_size( &currentPeer->channels->incomingReliableCommands) : 0,
  2567. currentPeer->channels != NULL ? enet_list_size(&currentPeer->channels->incomingUnreliableCommands) : 0
  2568. );
  2569. #endif
  2570. currentPeer->packetLossVariance -= currentPeer->packetLossVariance / 4;
  2571. if (packetLoss >= currentPeer->packetLoss) {
  2572. currentPeer->packetLoss += (packetLoss - currentPeer->packetLoss) / 8;
  2573. currentPeer->packetLossVariance += (packetLoss - currentPeer->packetLoss) / 4;
  2574. } else {
  2575. currentPeer->packetLoss -= (currentPeer->packetLoss - packetLoss) / 8;
  2576. currentPeer->packetLossVariance += (currentPeer->packetLoss - packetLoss) / 4;
  2577. }
  2578. currentPeer->packetLossEpoch = host->serviceTime;
  2579. currentPeer->packetsSent = 0;
  2580. currentPeer->packetsLost = 0;
  2581. }
  2582. host->buffers->data = headerData;
  2583. if (host->headerFlags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME) {
  2584. header->sentTime = ENET_HOST_TO_NET_16(host->serviceTime & 0xFFFF);
  2585. host->buffers->dataLength = sizeof(ENetProtocolHeader);
  2586. } else {
  2587. host->buffers->dataLength = (size_t) &((ENetProtocolHeader *) 0)->sentTime;
  2588. }
  2589. shouldCompress = 0;
  2590. if (host->compressor.context != NULL && host->compressor.compress != NULL) {
  2591. size_t originalSize = host->packetSize - sizeof(ENetProtocolHeader),
  2592. compressedSize = host->compressor.compress(host->compressor.context, &host->buffers[1], host->bufferCount - 1, originalSize, host->packetData[1], originalSize);
  2593. if (compressedSize > 0 && compressedSize < originalSize) {
  2594. host->headerFlags |= ENET_PROTOCOL_HEADER_FLAG_COMPRESSED;
  2595. shouldCompress = compressedSize;
  2596. #ifdef ENET_DEBUG_COMPRESS
  2597. printf("peer %u: compressed %u->%u (%u%%)\n", currentPeer->incomingPeerID, originalSize, compressedSize, (compressedSize * 100) / originalSize);
  2598. #endif
  2599. }
  2600. }
  2601. if (currentPeer->outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID) {
  2602. host->headerFlags |= currentPeer->outgoingSessionID << ENET_PROTOCOL_HEADER_SESSION_SHIFT;
  2603. }
  2604. header->peerID = ENET_HOST_TO_NET_16(currentPeer->outgoingPeerID | host->headerFlags);
  2605. if (host->checksum != NULL) {
  2606. enet_uint32 *checksum = (enet_uint32 *) &headerData[host->buffers->dataLength];
  2607. *checksum = currentPeer->outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID ? currentPeer->connectID : 0;
  2608. host->buffers->dataLength += sizeof(enet_uint32);
  2609. *checksum = host->checksum(host->buffers, host->bufferCount);
  2610. }
  2611. if (shouldCompress > 0) {
  2612. host->buffers[1].data = host->packetData[1];
  2613. host->buffers[1].dataLength = shouldCompress;
  2614. host->bufferCount = 2;
  2615. }
  2616. currentPeer->lastSendTime = host->serviceTime;
  2617. sentLength = enet_socket_send(host->socket, &currentPeer->address, host->buffers, host->bufferCount);
  2618. enet_protocol_remove_sent_unreliable_commands(currentPeer);
  2619. if (sentLength < 0) {
  2620. return -1;
  2621. }
  2622. host->totalSentData += sentLength;
  2623. currentPeer->totalDataSent += sentLength;
  2624. host->totalSentPackets++;
  2625. }
  2626. return 0;
  2627. } /* enet_protocol_send_outgoing_commands */
  2628. /** Sends any queued packets on the host specified to its designated peers.
  2629. *
  2630. * @param host host to flush
  2631. * @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().
  2632. * @ingroup host
  2633. */
  2634. void enet_host_flush(ENetHost *host) {
  2635. host->serviceTime = enet_time_get();
  2636. enet_protocol_send_outgoing_commands(host, NULL, 0);
  2637. }
  2638. /** Checks for any queued events on the host and dispatches one if available.
  2639. *
  2640. * @param host host to check for events
  2641. * @param event an event structure where event details will be placed if available
  2642. * @retval > 0 if an event was dispatched
  2643. * @retval 0 if no events are available
  2644. * @retval < 0 on failure
  2645. * @ingroup host
  2646. */
  2647. int enet_host_check_events(ENetHost *host, ENetEvent *event) {
  2648. if (event == NULL) { return -1; }
  2649. event->type = ENET_EVENT_TYPE_NONE;
  2650. event->peer = NULL;
  2651. event->packet = NULL;
  2652. return enet_protocol_dispatch_incoming_commands(host, event);
  2653. }
  2654. /** Waits for events on the host specified and shuttles packets between
  2655. * the host and its peers.
  2656. *
  2657. * @param host host to service
  2658. * @param event an event structure where event details will be placed if one occurs
  2659. * if event == NULL then no events will be delivered
  2660. * @param timeout number of milliseconds that ENet should wait for events
  2661. * @retval > 0 if an event occurred within the specified time limit
  2662. * @retval 0 if no event occurred
  2663. * @retval < 0 on failure
  2664. * @remarks enet_host_service should be called fairly regularly for adequate performance
  2665. * @ingroup host
  2666. */
  2667. int enet_host_service(ENetHost *host, ENetEvent *event, enet_uint32 timeout) {
  2668. enet_uint32 waitCondition;
  2669. if (event != NULL) {
  2670. event->type = ENET_EVENT_TYPE_NONE;
  2671. event->peer = NULL;
  2672. event->packet = NULL;
  2673. switch (enet_protocol_dispatch_incoming_commands(host, event)) {
  2674. case 1:
  2675. return 1;
  2676. case -1:
  2677. #ifdef ENET_DEBUG
  2678. perror("Error dispatching incoming packets");
  2679. #endif
  2680. return -1;
  2681. default:
  2682. break;
  2683. }
  2684. }
  2685. host->serviceTime = enet_time_get();
  2686. timeout += host->serviceTime;
  2687. do {
  2688. if (ENET_TIME_DIFFERENCE(host->serviceTime, host->bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL) {
  2689. enet_host_bandwidth_throttle(host);
  2690. }
  2691. switch (enet_protocol_send_outgoing_commands(host, event, 1)) {
  2692. case 1:
  2693. return 1;
  2694. case -1:
  2695. #ifdef ENET_DEBUG
  2696. perror("Error sending outgoing packets");
  2697. #endif
  2698. return -1;
  2699. default:
  2700. break;
  2701. }
  2702. switch (enet_protocol_receive_incoming_commands(host, event)) {
  2703. case 1:
  2704. return 1;
  2705. case -1:
  2706. #ifdef ENET_DEBUG
  2707. perror("Error receiving incoming packets");
  2708. #endif
  2709. return -1;
  2710. default:
  2711. break;
  2712. }
  2713. switch (enet_protocol_send_outgoing_commands(host, event, 1)) {
  2714. case 1:
  2715. return 1;
  2716. case -1:
  2717. #ifdef ENET_DEBUG
  2718. perror("Error sending outgoing packets");
  2719. #endif
  2720. return -1;
  2721. default:
  2722. break;
  2723. }
  2724. if (event != NULL) {
  2725. switch (enet_protocol_dispatch_incoming_commands(host, event)) {
  2726. case 1:
  2727. return 1;
  2728. case -1:
  2729. #ifdef ENET_DEBUG
  2730. perror("Error dispatching incoming packets");
  2731. #endif
  2732. return -1;
  2733. default:
  2734. break;
  2735. }
  2736. }
  2737. if (ENET_TIME_GREATER_EQUAL(host->serviceTime, timeout)) {
  2738. return 0;
  2739. }
  2740. do {
  2741. host->serviceTime = enet_time_get();
  2742. if (ENET_TIME_GREATER_EQUAL(host->serviceTime, timeout)) {
  2743. return 0;
  2744. }
  2745. waitCondition = ENET_SOCKET_WAIT_RECEIVE | ENET_SOCKET_WAIT_INTERRUPT;
  2746. if (enet_socket_wait(host->socket, &waitCondition, ENET_TIME_DIFFERENCE(timeout, host->serviceTime)) != 0) {
  2747. return -1;
  2748. }
  2749. } while (waitCondition & ENET_SOCKET_WAIT_INTERRUPT);
  2750. host->serviceTime = enet_time_get();
  2751. } while (waitCondition & ENET_SOCKET_WAIT_RECEIVE);
  2752. return 0;
  2753. } /* enet_host_service */
  2754. // =======================================================================//
  2755. // !
  2756. // ! Peer
  2757. // !
  2758. // =======================================================================//
  2759. /** Configures throttle parameter for a peer.
  2760. *
  2761. * Unreliable packets are dropped by ENet in response to the varying conditions
  2762. * of the Internet connection to the peer. The throttle represents a probability
  2763. * that an unreliable packet should not be dropped and thus sent by ENet to the peer.
  2764. * The lowest mean round trip time from the sending of a reliable packet to the
  2765. * receipt of its acknowledgement is measured over an amount of time specified by
  2766. * the interval parameter in milliseconds. If a measured round trip time happens to
  2767. * be significantly less than the mean round trip time measured over the interval,
  2768. * then the throttle probability is increased to allow more traffic by an amount
  2769. * specified in the acceleration parameter, which is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE
  2770. * constant. If a measured round trip time happens to be significantly greater than
  2771. * the mean round trip time measured over the interval, then the throttle probability
  2772. * is decreased to limit traffic by an amount specified in the deceleration parameter, which
  2773. * is a ratio to the ENET_PEER_PACKET_THROTTLE_SCALE constant. When the throttle has
  2774. * a value of ENET_PEER_PACKET_THROTTLE_SCALE, no unreliable packets are dropped by
  2775. * ENet, and so 100% of all unreliable packets will be sent. When the throttle has a
  2776. * value of 0, all unreliable packets are dropped by ENet, and so 0% of all unreliable
  2777. * packets will be sent. Intermediate values for the throttle represent intermediate
  2778. * probabilities between 0% and 100% of unreliable packets being sent. The bandwidth
  2779. * limits of the local and foreign hosts are taken into account to determine a
  2780. * sensible limit for the throttle probability above which it should not raise even in
  2781. * the best of conditions.
  2782. *
  2783. * @param peer peer to configure
  2784. * @param interval interval, in milliseconds, over which to measure lowest mean RTT; the default value is ENET_PEER_PACKET_THROTTLE_INTERVAL.
  2785. * @param acceleration rate at which to increase the throttle probability as mean RTT declines
  2786. * @param deceleration rate at which to decrease the throttle probability as mean RTT increases
  2787. */
  2788. void enet_peer_throttle_configure(ENetPeer *peer, enet_uint32 interval, enet_uint32 acceleration, enet_uint32 deceleration) {
  2789. ENetProtocol command;
  2790. peer->packetThrottleInterval = interval;
  2791. peer->packetThrottleAcceleration = acceleration;
  2792. peer->packetThrottleDeceleration = deceleration;
  2793. command.header.command = ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  2794. command.header.channelID = 0xFF;
  2795. command.throttleConfigure.packetThrottleInterval = ENET_HOST_TO_NET_32(interval);
  2796. command.throttleConfigure.packetThrottleAcceleration = ENET_HOST_TO_NET_32(acceleration);
  2797. command.throttleConfigure.packetThrottleDeceleration = ENET_HOST_TO_NET_32(deceleration);
  2798. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  2799. }
  2800. int enet_peer_throttle(ENetPeer *peer, enet_uint32 rtt) {
  2801. if (peer->lastRoundTripTime <= peer->lastRoundTripTimeVariance) {
  2802. peer->packetThrottle = peer->packetThrottleLimit;
  2803. }
  2804. else if (rtt < peer->lastRoundTripTime) {
  2805. peer->packetThrottle += peer->packetThrottleAcceleration;
  2806. if (peer->packetThrottle > peer->packetThrottleLimit) {
  2807. peer->packetThrottle = peer->packetThrottleLimit;
  2808. }
  2809. return 1;
  2810. }
  2811. else if (rtt > peer->lastRoundTripTime + 2 * peer->lastRoundTripTimeVariance) {
  2812. if (peer->packetThrottle > peer->packetThrottleDeceleration) {
  2813. peer->packetThrottle -= peer->packetThrottleDeceleration;
  2814. } else {
  2815. peer->packetThrottle = 0;
  2816. }
  2817. return -1;
  2818. }
  2819. return 0;
  2820. }
  2821. /* Extended functionality for easier binding in other programming languages */
  2822. enet_uint32 enet_host_get_peers_count(ENetHost *host) {
  2823. return host->connectedPeers;
  2824. }
  2825. enet_uint32 enet_host_get_packets_sent(ENetHost *host) {
  2826. return host->totalSentPackets;
  2827. }
  2828. enet_uint32 enet_host_get_packets_received(ENetHost *host) {
  2829. return host->totalReceivedPackets;
  2830. }
  2831. enet_uint32 enet_host_get_bytes_sent(ENetHost *host) {
  2832. return host->totalSentData;
  2833. }
  2834. enet_uint32 enet_host_get_bytes_received(ENetHost *host) {
  2835. return host->totalReceivedData;
  2836. }
  2837. /** Gets received data buffer. Returns buffer length.
  2838. * @param host host to access recevie buffer
  2839. * @param data ouput parameter for recevied data
  2840. * @retval buffer length
  2841. */
  2842. enet_uint32 enet_host_get_received_data(ENetHost *host, /*out*/ enet_uint8** data) {
  2843. *data = host->receivedData;
  2844. return host->receivedDataLength;
  2845. }
  2846. enet_uint32 enet_host_get_mtu(ENetHost *host) {
  2847. return host->mtu;
  2848. }
  2849. enet_uint32 enet_peer_get_id(ENetPeer *peer) {
  2850. return peer->connectID;
  2851. }
  2852. enet_uint32 enet_peer_get_ip(ENetPeer *peer, char *ip, size_t ipLength) {
  2853. return enet_address_get_host_ip(&peer->address, ip, ipLength);
  2854. }
  2855. enet_uint16 enet_peer_get_port(ENetPeer *peer) {
  2856. return peer->address.port;
  2857. }
  2858. ENetPeerState enet_peer_get_state(ENetPeer *peer) {
  2859. return peer->state;
  2860. }
  2861. enet_uint32 enet_peer_get_rtt(ENetPeer *peer) {
  2862. return peer->roundTripTime;
  2863. }
  2864. enet_uint64 enet_peer_get_packets_sent(ENetPeer *peer) {
  2865. return peer->totalPacketsSent;
  2866. }
  2867. enet_uint32 enet_peer_get_packets_lost(ENetPeer *peer) {
  2868. return peer->totalPacketsLost;
  2869. }
  2870. enet_uint64 enet_peer_get_bytes_sent(ENetPeer *peer) {
  2871. return peer->totalDataSent;
  2872. }
  2873. enet_uint64 enet_peer_get_bytes_received(ENetPeer *peer) {
  2874. return peer->totalDataReceived;
  2875. }
  2876. void * enet_peer_get_data(ENetPeer *peer) {
  2877. return (void *) peer->data;
  2878. }
  2879. void enet_peer_set_data(ENetPeer *peer, const void *data) {
  2880. peer->data = (enet_uint32 *) data;
  2881. }
  2882. void * enet_packet_get_data(ENetPacket *packet) {
  2883. return (void *) packet->data;
  2884. }
  2885. enet_uint32 enet_packet_get_length(ENetPacket *packet) {
  2886. return packet->dataLength;
  2887. }
  2888. void enet_packet_set_free_callback(ENetPacket *packet, void *callback) {
  2889. packet->freeCallback = (ENetPacketFreeCallback)callback;
  2890. }
  2891. /** Queues a packet to be sent.
  2892. * @param peer destination for the packet
  2893. * @param channelID channel on which to send
  2894. * @param packet packet to send
  2895. * @retval 0 on success
  2896. * @retval < 0 on failure
  2897. */
  2898. int enet_peer_send(ENetPeer *peer, enet_uint8 channelID, ENetPacket *packet) {
  2899. ENetChannel *channel = &peer->channels[channelID];
  2900. ENetProtocol command;
  2901. size_t fragmentLength;
  2902. if (peer->state != ENET_PEER_STATE_CONNECTED || channelID >= peer->channelCount || packet->dataLength > peer->host->maximumPacketSize) {
  2903. return -1;
  2904. }
  2905. fragmentLength = peer->mtu - sizeof(ENetProtocolHeader) - sizeof(ENetProtocolSendFragment);
  2906. if (peer->host->checksum != NULL) {
  2907. fragmentLength -= sizeof(enet_uint32);
  2908. }
  2909. if (packet->dataLength > fragmentLength) {
  2910. enet_uint32 fragmentCount = (packet->dataLength + fragmentLength - 1) / fragmentLength, fragmentNumber, fragmentOffset;
  2911. enet_uint8 commandNumber;
  2912. enet_uint16 startSequenceNumber;
  2913. ENetList fragments;
  2914. ENetOutgoingCommand *fragment;
  2915. if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT) {
  2916. return -1;
  2917. }
  2918. if ((packet->flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT)) ==
  2919. ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT &&
  2920. channel->outgoingUnreliableSequenceNumber < 0xFFFF)
  2921. {
  2922. commandNumber = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT;
  2923. startSequenceNumber = ENET_HOST_TO_NET_16(channel->outgoingUnreliableSequenceNumber + 1);
  2924. } else {
  2925. commandNumber = ENET_PROTOCOL_COMMAND_SEND_FRAGMENT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  2926. startSequenceNumber = ENET_HOST_TO_NET_16(channel->outgoingReliableSequenceNumber + 1);
  2927. }
  2928. enet_list_clear(&fragments);
  2929. for (fragmentNumber = 0, fragmentOffset = 0; fragmentOffset < packet->dataLength; ++fragmentNumber, fragmentOffset += fragmentLength) {
  2930. if (packet->dataLength - fragmentOffset < fragmentLength) {
  2931. fragmentLength = packet->dataLength - fragmentOffset;
  2932. }
  2933. fragment = (ENetOutgoingCommand *) enet_malloc(sizeof(ENetOutgoingCommand));
  2934. if (fragment == NULL) {
  2935. while (!enet_list_empty(&fragments)) {
  2936. fragment = (ENetOutgoingCommand *) enet_list_remove(enet_list_begin(&fragments));
  2937. enet_free(fragment);
  2938. }
  2939. return -1;
  2940. }
  2941. fragment->fragmentOffset = fragmentOffset;
  2942. fragment->fragmentLength = fragmentLength;
  2943. fragment->packet = packet;
  2944. fragment->command.header.command = commandNumber;
  2945. fragment->command.header.channelID = channelID;
  2946. fragment->command.sendFragment.startSequenceNumber = startSequenceNumber;
  2947. fragment->command.sendFragment.dataLength = ENET_HOST_TO_NET_16(fragmentLength);
  2948. fragment->command.sendFragment.fragmentCount = ENET_HOST_TO_NET_32(fragmentCount);
  2949. fragment->command.sendFragment.fragmentNumber = ENET_HOST_TO_NET_32(fragmentNumber);
  2950. fragment->command.sendFragment.totalLength = ENET_HOST_TO_NET_32(packet->dataLength);
  2951. fragment->command.sendFragment.fragmentOffset = ENET_NET_TO_HOST_32(fragmentOffset);
  2952. enet_list_insert(enet_list_end(&fragments), fragment);
  2953. }
  2954. packet->referenceCount += fragmentNumber;
  2955. while (!enet_list_empty(&fragments)) {
  2956. fragment = (ENetOutgoingCommand *) enet_list_remove(enet_list_begin(&fragments));
  2957. enet_peer_setup_outgoing_command(peer, fragment);
  2958. }
  2959. return 0;
  2960. }
  2961. command.header.channelID = channelID;
  2962. if ((packet->flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNSEQUENCED)) == ENET_PACKET_FLAG_UNSEQUENCED) {
  2963. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  2964. command.sendUnsequenced.dataLength = ENET_HOST_TO_NET_16(packet->dataLength);
  2965. }
  2966. else if (packet->flags & ENET_PACKET_FLAG_RELIABLE || channel->outgoingUnreliableSequenceNumber >= 0xFFFF) {
  2967. command.header.command = ENET_PROTOCOL_COMMAND_SEND_RELIABLE | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  2968. command.sendReliable.dataLength = ENET_HOST_TO_NET_16(packet->dataLength);
  2969. }
  2970. else {
  2971. command.header.command = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE;
  2972. command.sendUnreliable.dataLength = ENET_HOST_TO_NET_16(packet->dataLength);
  2973. }
  2974. if (enet_peer_queue_outgoing_command(peer, &command, packet, 0, packet->dataLength) == NULL) {
  2975. return -1;
  2976. }
  2977. return 0;
  2978. } // enet_peer_send
  2979. /** Attempts to dequeue any incoming queued packet.
  2980. * @param peer peer to dequeue packets from
  2981. * @param channelID holds the channel ID of the channel the packet was received on success
  2982. * @returns a pointer to the packet, or NULL if there are no available incoming queued packets
  2983. */
  2984. ENetPacket * enet_peer_receive(ENetPeer *peer, enet_uint8 *channelID) {
  2985. ENetIncomingCommand *incomingCommand;
  2986. ENetPacket *packet;
  2987. if (enet_list_empty(&peer->dispatchedCommands)) {
  2988. return NULL;
  2989. }
  2990. incomingCommand = (ENetIncomingCommand *) enet_list_remove(enet_list_begin(&peer->dispatchedCommands));
  2991. if (channelID != NULL) {
  2992. *channelID = incomingCommand->command.header.channelID;
  2993. }
  2994. packet = incomingCommand->packet;
  2995. --packet->referenceCount;
  2996. if (incomingCommand->fragments != NULL) {
  2997. enet_free(incomingCommand->fragments);
  2998. }
  2999. enet_free(incomingCommand);
  3000. peer->totalWaitingData -= packet->dataLength;
  3001. return packet;
  3002. }
  3003. static void enet_peer_reset_outgoing_commands(ENetList *queue) {
  3004. ENetOutgoingCommand *outgoingCommand;
  3005. while (!enet_list_empty(queue)) {
  3006. outgoingCommand = (ENetOutgoingCommand *) enet_list_remove(enet_list_begin(queue));
  3007. if (outgoingCommand->packet != NULL) {
  3008. --outgoingCommand->packet->referenceCount;
  3009. if (outgoingCommand->packet->referenceCount == 0) {
  3010. callbacks.packet_destroy(outgoingCommand->packet);
  3011. }
  3012. }
  3013. enet_free(outgoingCommand);
  3014. }
  3015. }
  3016. static void enet_peer_remove_incoming_commands(ENetList *queue, ENetListIterator startCommand, ENetListIterator endCommand) {
  3017. ENET_UNUSED(queue)
  3018. ENetListIterator currentCommand;
  3019. for (currentCommand = startCommand; currentCommand != endCommand;) {
  3020. ENetIncomingCommand *incomingCommand = (ENetIncomingCommand *) currentCommand;
  3021. currentCommand = enet_list_next(currentCommand);
  3022. enet_list_remove(&incomingCommand->incomingCommandList);
  3023. if (incomingCommand->packet != NULL) {
  3024. --incomingCommand->packet->referenceCount;
  3025. if (incomingCommand->packet->referenceCount == 0) {
  3026. callbacks.packet_destroy(incomingCommand->packet);
  3027. }
  3028. }
  3029. if (incomingCommand->fragments != NULL) {
  3030. enet_free(incomingCommand->fragments);
  3031. }
  3032. enet_free(incomingCommand);
  3033. }
  3034. }
  3035. static void enet_peer_reset_incoming_commands(ENetList *queue) {
  3036. enet_peer_remove_incoming_commands(queue, enet_list_begin(queue), enet_list_end(queue));
  3037. }
  3038. void enet_peer_reset_queues(ENetPeer *peer) {
  3039. ENetChannel *channel;
  3040. if (peer->needsDispatch) {
  3041. enet_list_remove(&peer->dispatchList);
  3042. peer->needsDispatch = 0;
  3043. }
  3044. while (!enet_list_empty(&peer->acknowledgements)) {
  3045. enet_free(enet_list_remove(enet_list_begin(&peer->acknowledgements)));
  3046. }
  3047. enet_peer_reset_outgoing_commands(&peer->sentReliableCommands);
  3048. enet_peer_reset_outgoing_commands(&peer->sentUnreliableCommands);
  3049. enet_peer_reset_outgoing_commands(&peer->outgoingReliableCommands);
  3050. enet_peer_reset_outgoing_commands(&peer->outgoingUnreliableCommands);
  3051. enet_peer_reset_incoming_commands(&peer->dispatchedCommands);
  3052. if (peer->channels != NULL && peer->channelCount > 0) {
  3053. for (channel = peer->channels; channel < &peer->channels[peer->channelCount]; ++channel) {
  3054. enet_peer_reset_incoming_commands(&channel->incomingReliableCommands);
  3055. enet_peer_reset_incoming_commands(&channel->incomingUnreliableCommands);
  3056. }
  3057. enet_free(peer->channels);
  3058. }
  3059. peer->channels = NULL;
  3060. peer->channelCount = 0;
  3061. }
  3062. void enet_peer_on_connect(ENetPeer *peer) {
  3063. if (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) {
  3064. if (peer->incomingBandwidth != 0) {
  3065. ++peer->host->bandwidthLimitedPeers;
  3066. }
  3067. ++peer->host->connectedPeers;
  3068. }
  3069. }
  3070. void enet_peer_on_disconnect(ENetPeer *peer) {
  3071. if (peer->state == ENET_PEER_STATE_CONNECTED || peer->state == ENET_PEER_STATE_DISCONNECT_LATER) {
  3072. if (peer->incomingBandwidth != 0) {
  3073. --peer->host->bandwidthLimitedPeers;
  3074. }
  3075. --peer->host->connectedPeers;
  3076. }
  3077. }
  3078. /** Forcefully disconnects a peer.
  3079. * @param peer peer to forcefully disconnect
  3080. * @remarks The foreign host represented by the peer is not notified of the disconnection and will timeout
  3081. * on its connection to the local host.
  3082. */
  3083. void enet_peer_reset(ENetPeer *peer) {
  3084. enet_peer_on_disconnect(peer);
  3085. // We don't want to reset connectID here, otherwise, we can't get it in the Disconnect event
  3086. // peer->connectID = 0;
  3087. peer->outgoingPeerID = ENET_PROTOCOL_MAXIMUM_PEER_ID;
  3088. peer->state = ENET_PEER_STATE_DISCONNECTED;
  3089. peer->incomingBandwidth = 0;
  3090. peer->outgoingBandwidth = 0;
  3091. peer->incomingBandwidthThrottleEpoch = 0;
  3092. peer->outgoingBandwidthThrottleEpoch = 0;
  3093. peer->incomingDataTotal = 0;
  3094. peer->totalDataReceived = 0;
  3095. peer->outgoingDataTotal = 0;
  3096. peer->totalDataSent = 0;
  3097. peer->lastSendTime = 0;
  3098. peer->lastReceiveTime = 0;
  3099. peer->nextTimeout = 0;
  3100. peer->earliestTimeout = 0;
  3101. peer->packetLossEpoch = 0;
  3102. peer->packetsSent = 0;
  3103. peer->totalPacketsSent = 0;
  3104. peer->packetsLost = 0;
  3105. peer->totalPacketsLost = 0;
  3106. peer->packetLoss = 0;
  3107. peer->packetLossVariance = 0;
  3108. peer->packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE;
  3109. peer->packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE;
  3110. peer->packetThrottleCounter = 0;
  3111. peer->packetThrottleEpoch = 0;
  3112. peer->packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION;
  3113. peer->packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION;
  3114. peer->packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL;
  3115. peer->pingInterval = ENET_PEER_PING_INTERVAL;
  3116. peer->timeoutLimit = ENET_PEER_TIMEOUT_LIMIT;
  3117. peer->timeoutMinimum = ENET_PEER_TIMEOUT_MINIMUM;
  3118. peer->timeoutMaximum = ENET_PEER_TIMEOUT_MAXIMUM;
  3119. peer->lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  3120. peer->lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  3121. peer->lastRoundTripTimeVariance = 0;
  3122. peer->highestRoundTripTimeVariance = 0;
  3123. peer->roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
  3124. peer->roundTripTimeVariance = 0;
  3125. peer->mtu = peer->host->mtu;
  3126. peer->reliableDataInTransit = 0;
  3127. peer->outgoingReliableSequenceNumber = 0;
  3128. peer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  3129. peer->incomingUnsequencedGroup = 0;
  3130. peer->outgoingUnsequencedGroup = 0;
  3131. peer->eventData = 0;
  3132. peer->totalWaitingData = 0;
  3133. memset(peer->unsequencedWindow, 0, sizeof(peer->unsequencedWindow));
  3134. enet_peer_reset_queues(peer);
  3135. }
  3136. /** Sends a ping request to a peer.
  3137. * @param peer destination for the ping request
  3138. * @remarks ping requests factor into the mean round trip time as designated by the
  3139. * roundTripTime field in the ENetPeer structure. ENet automatically pings all connected
  3140. * peers at regular intervals, however, this function may be called to ensure more
  3141. * frequent ping requests.
  3142. */
  3143. void enet_peer_ping(ENetPeer *peer) {
  3144. ENetProtocol command;
  3145. if (peer->state != ENET_PEER_STATE_CONNECTED) {
  3146. return;
  3147. }
  3148. command.header.command = ENET_PROTOCOL_COMMAND_PING | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  3149. command.header.channelID = 0xFF;
  3150. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  3151. }
  3152. /** Sets the interval at which pings will be sent to a peer.
  3153. *
  3154. * Pings are used both to monitor the liveness of the connection and also to dynamically
  3155. * adjust the throttle during periods of low traffic so that the throttle has reasonable
  3156. * responsiveness during traffic spikes.
  3157. *
  3158. * @param peer the peer to adjust
  3159. * @param pingInterval the interval at which to send pings; defaults to ENET_PEER_PING_INTERVAL if 0
  3160. */
  3161. void enet_peer_ping_interval(ENetPeer *peer, enet_uint32 pingInterval) {
  3162. peer->pingInterval = pingInterval ? pingInterval : (enet_uint32)ENET_PEER_PING_INTERVAL;
  3163. }
  3164. /** Sets the timeout parameters for a peer.
  3165. *
  3166. * The timeout parameter control how and when a peer will timeout from a failure to acknowledge
  3167. * reliable traffic. Timeout values use an exponential backoff mechanism, where if a reliable
  3168. * packet is not acknowledge within some multiple of the average RTT plus a variance tolerance,
  3169. * the timeout will be doubled until it reaches a set limit. If the timeout is thus at this
  3170. * limit and reliable packets have been sent but not acknowledged within a certain minimum time
  3171. * period, the peer will be disconnected. Alternatively, if reliable packets have been sent
  3172. * but not acknowledged for a certain maximum time period, the peer will be disconnected regardless
  3173. * of the current timeout limit value.
  3174. *
  3175. * @param peer the peer to adjust
  3176. * @param timeoutLimit the timeout limit; defaults to ENET_PEER_TIMEOUT_LIMIT if 0
  3177. * @param timeoutMinimum the timeout minimum; defaults to ENET_PEER_TIMEOUT_MINIMUM if 0
  3178. * @param timeoutMaximum the timeout maximum; defaults to ENET_PEER_TIMEOUT_MAXIMUM if 0
  3179. */
  3180. void enet_peer_timeout(ENetPeer *peer, enet_uint32 timeoutLimit, enet_uint32 timeoutMinimum, enet_uint32 timeoutMaximum) {
  3181. peer->timeoutLimit = timeoutLimit ? timeoutLimit : (enet_uint32)ENET_PEER_TIMEOUT_LIMIT;
  3182. peer->timeoutMinimum = timeoutMinimum ? timeoutMinimum : (enet_uint32)ENET_PEER_TIMEOUT_MINIMUM;
  3183. peer->timeoutMaximum = timeoutMaximum ? timeoutMaximum : (enet_uint32)ENET_PEER_TIMEOUT_MAXIMUM;
  3184. }
  3185. /** Force an immediate disconnection from a peer.
  3186. * @param peer peer to disconnect
  3187. * @param data data describing the disconnection
  3188. * @remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not
  3189. * guaranteed to receive the disconnect notification, and is reset immediately upon
  3190. * return from this function.
  3191. */
  3192. void enet_peer_disconnect_now(ENetPeer *peer, enet_uint32 data) {
  3193. ENetProtocol command;
  3194. if (peer->state == ENET_PEER_STATE_DISCONNECTED) {
  3195. return;
  3196. }
  3197. if (peer->state != ENET_PEER_STATE_ZOMBIE && peer->state != ENET_PEER_STATE_DISCONNECTING) {
  3198. enet_peer_reset_queues(peer);
  3199. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  3200. command.header.channelID = 0xFF;
  3201. command.disconnect.data = ENET_HOST_TO_NET_32(data);
  3202. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  3203. enet_host_flush(peer->host);
  3204. }
  3205. enet_peer_reset(peer);
  3206. }
  3207. /** Request a disconnection from a peer.
  3208. * @param peer peer to request a disconnection
  3209. * @param data data describing the disconnection
  3210. * @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  3211. * once the disconnection is complete.
  3212. */
  3213. void enet_peer_disconnect(ENetPeer *peer, enet_uint32 data) {
  3214. ENetProtocol command;
  3215. if (peer->state == ENET_PEER_STATE_DISCONNECTING ||
  3216. peer->state == ENET_PEER_STATE_DISCONNECTED ||
  3217. peer->state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT ||
  3218. peer->state == ENET_PEER_STATE_ZOMBIE
  3219. ) {
  3220. return;
  3221. }
  3222. enet_peer_reset_queues(peer);
  3223. command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
  3224. command.header.channelID = 0xFF;
  3225. command.disconnect.data = ENET_HOST_TO_NET_32(data);
  3226. if (peer->state == ENET_PEER_STATE_CONNECTED || peer->state == ENET_PEER_STATE_DISCONNECT_LATER) {
  3227. command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  3228. } else {
  3229. command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
  3230. }
  3231. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  3232. if (peer->state == ENET_PEER_STATE_CONNECTED || peer->state == ENET_PEER_STATE_DISCONNECT_LATER) {
  3233. enet_peer_on_disconnect(peer);
  3234. peer->state = ENET_PEER_STATE_DISCONNECTING;
  3235. } else {
  3236. enet_host_flush(peer->host);
  3237. enet_peer_reset(peer);
  3238. }
  3239. }
  3240. /** Request a disconnection from a peer, but only after all queued outgoing packets are sent.
  3241. * @param peer peer to request a disconnection
  3242. * @param data data describing the disconnection
  3243. * @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
  3244. * once the disconnection is complete.
  3245. */
  3246. void enet_peer_disconnect_later(ENetPeer *peer, enet_uint32 data) {
  3247. if ((peer->state == ENET_PEER_STATE_CONNECTED || peer->state == ENET_PEER_STATE_DISCONNECT_LATER) &&
  3248. !(enet_list_empty(&peer->outgoingReliableCommands) &&
  3249. enet_list_empty(&peer->outgoingUnreliableCommands) &&
  3250. enet_list_empty(&peer->sentReliableCommands))
  3251. ) {
  3252. peer->state = ENET_PEER_STATE_DISCONNECT_LATER;
  3253. peer->eventData = data;
  3254. } else {
  3255. enet_peer_disconnect(peer, data);
  3256. }
  3257. }
  3258. ENetAcknowledgement *enet_peer_queue_acknowledgement(ENetPeer *peer, const ENetProtocol *command, enet_uint16 sentTime) {
  3259. ENetAcknowledgement *acknowledgement;
  3260. if (command->header.channelID < peer->channelCount) {
  3261. ENetChannel *channel = &peer->channels[command->header.channelID];
  3262. enet_uint16 reliableWindow = command->header.reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  3263. enet_uint16 currentWindow = channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  3264. if (command->header.reliableSequenceNumber < channel->incomingReliableSequenceNumber) {
  3265. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  3266. }
  3267. if (reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1 && reliableWindow <= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS) {
  3268. return NULL;
  3269. }
  3270. }
  3271. acknowledgement = (ENetAcknowledgement *) enet_malloc(sizeof(ENetAcknowledgement));
  3272. if (acknowledgement == NULL) {
  3273. return NULL;
  3274. }
  3275. peer->outgoingDataTotal += sizeof(ENetProtocolAcknowledge);
  3276. acknowledgement->sentTime = sentTime;
  3277. acknowledgement->command = *command;
  3278. enet_list_insert(enet_list_end(&peer->acknowledgements), acknowledgement);
  3279. return acknowledgement;
  3280. }
  3281. void enet_peer_setup_outgoing_command(ENetPeer *peer, ENetOutgoingCommand *outgoingCommand) {
  3282. ENetChannel *channel = &peer->channels[outgoingCommand->command.header.channelID];
  3283. peer->outgoingDataTotal += enet_protocol_command_size(outgoingCommand->command.header.command) + outgoingCommand->fragmentLength;
  3284. if (outgoingCommand->command.header.channelID == 0xFF) {
  3285. ++peer->outgoingReliableSequenceNumber;
  3286. outgoingCommand->reliableSequenceNumber = peer->outgoingReliableSequenceNumber;
  3287. outgoingCommand->unreliableSequenceNumber = 0;
  3288. }
  3289. else if (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) {
  3290. ++channel->outgoingReliableSequenceNumber;
  3291. channel->outgoingUnreliableSequenceNumber = 0;
  3292. outgoingCommand->reliableSequenceNumber = channel->outgoingReliableSequenceNumber;
  3293. outgoingCommand->unreliableSequenceNumber = 0;
  3294. }
  3295. else if (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED) {
  3296. ++peer->outgoingUnsequencedGroup;
  3297. outgoingCommand->reliableSequenceNumber = 0;
  3298. outgoingCommand->unreliableSequenceNumber = 0;
  3299. }
  3300. else {
  3301. if (outgoingCommand->fragmentOffset == 0) {
  3302. ++channel->outgoingUnreliableSequenceNumber;
  3303. }
  3304. outgoingCommand->reliableSequenceNumber = channel->outgoingReliableSequenceNumber;
  3305. outgoingCommand->unreliableSequenceNumber = channel->outgoingUnreliableSequenceNumber;
  3306. }
  3307. outgoingCommand->sendAttempts = 0;
  3308. outgoingCommand->sentTime = 0;
  3309. outgoingCommand->roundTripTimeout = 0;
  3310. outgoingCommand->roundTripTimeoutLimit = 0;
  3311. outgoingCommand->command.header.reliableSequenceNumber = ENET_HOST_TO_NET_16(outgoingCommand->reliableSequenceNumber);
  3312. switch (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK) {
  3313. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  3314. outgoingCommand->command.sendUnreliable.unreliableSequenceNumber = ENET_HOST_TO_NET_16(outgoingCommand->unreliableSequenceNumber);
  3315. break;
  3316. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  3317. outgoingCommand->command.sendUnsequenced.unsequencedGroup = ENET_HOST_TO_NET_16(peer->outgoingUnsequencedGroup);
  3318. break;
  3319. default:
  3320. break;
  3321. }
  3322. if (outgoingCommand->command.header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) {
  3323. enet_list_insert(enet_list_end(&peer->outgoingReliableCommands), outgoingCommand);
  3324. } else {
  3325. enet_list_insert(enet_list_end(&peer->outgoingUnreliableCommands), outgoingCommand);
  3326. }
  3327. }
  3328. ENetOutgoingCommand * enet_peer_queue_outgoing_command(ENetPeer *peer, const ENetProtocol *command, ENetPacket *packet, enet_uint32 offset, enet_uint16 length) {
  3329. ENetOutgoingCommand *outgoingCommand = (ENetOutgoingCommand *) enet_malloc(sizeof(ENetOutgoingCommand));
  3330. if (outgoingCommand == NULL) {
  3331. return NULL;
  3332. }
  3333. outgoingCommand->command = *command;
  3334. outgoingCommand->fragmentOffset = offset;
  3335. outgoingCommand->fragmentLength = length;
  3336. outgoingCommand->packet = packet;
  3337. if (packet != NULL) {
  3338. ++packet->referenceCount;
  3339. }
  3340. enet_peer_setup_outgoing_command(peer, outgoingCommand);
  3341. return outgoingCommand;
  3342. }
  3343. void enet_peer_dispatch_incoming_unreliable_commands(ENetPeer *peer, ENetChannel *channel) {
  3344. ENetListIterator droppedCommand, startCommand, currentCommand;
  3345. for (droppedCommand = startCommand = currentCommand = enet_list_begin(&channel->incomingUnreliableCommands);
  3346. currentCommand != enet_list_end(&channel->incomingUnreliableCommands);
  3347. currentCommand = enet_list_next(currentCommand)
  3348. ) {
  3349. ENetIncomingCommand *incomingCommand = (ENetIncomingCommand *) currentCommand;
  3350. if ((incomingCommand->command.header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED) {
  3351. continue;
  3352. }
  3353. if (incomingCommand->reliableSequenceNumber == channel->incomingReliableSequenceNumber) {
  3354. if (incomingCommand->fragmentsRemaining <= 0) {
  3355. channel->incomingUnreliableSequenceNumber = incomingCommand->unreliableSequenceNumber;
  3356. continue;
  3357. }
  3358. if (startCommand != currentCommand) {
  3359. enet_list_move(enet_list_end(&peer->dispatchedCommands), startCommand, enet_list_previous(currentCommand));
  3360. if (!peer->needsDispatch) {
  3361. enet_list_insert(enet_list_end(&peer->host->dispatchQueue), &peer->dispatchList);
  3362. peer->needsDispatch = 1;
  3363. }
  3364. droppedCommand = currentCommand;
  3365. } else if (droppedCommand != currentCommand) {
  3366. droppedCommand = enet_list_previous(currentCommand);
  3367. }
  3368. } else {
  3369. enet_uint16 reliableWindow = incomingCommand->reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  3370. enet_uint16 currentWindow = channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  3371. if (incomingCommand->reliableSequenceNumber < channel->incomingReliableSequenceNumber) {
  3372. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  3373. }
  3374. if (reliableWindow >= currentWindow && reliableWindow < currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1) {
  3375. break;
  3376. }
  3377. droppedCommand = enet_list_next(currentCommand);
  3378. if (startCommand != currentCommand) {
  3379. enet_list_move(enet_list_end(&peer->dispatchedCommands), startCommand, enet_list_previous(currentCommand));
  3380. if (!peer->needsDispatch) {
  3381. enet_list_insert(enet_list_end(&peer->host->dispatchQueue), &peer->dispatchList);
  3382. peer->needsDispatch = 1;
  3383. }
  3384. }
  3385. }
  3386. startCommand = enet_list_next(currentCommand);
  3387. }
  3388. if (startCommand != currentCommand) {
  3389. enet_list_move(enet_list_end(&peer->dispatchedCommands), startCommand, enet_list_previous(currentCommand));
  3390. if (!peer->needsDispatch) {
  3391. enet_list_insert(enet_list_end(&peer->host->dispatchQueue), &peer->dispatchList);
  3392. peer->needsDispatch = 1;
  3393. }
  3394. droppedCommand = currentCommand;
  3395. }
  3396. enet_peer_remove_incoming_commands(&channel->incomingUnreliableCommands,enet_list_begin(&channel->incomingUnreliableCommands), droppedCommand);
  3397. }
  3398. void enet_peer_dispatch_incoming_reliable_commands(ENetPeer *peer, ENetChannel *channel) {
  3399. ENetListIterator currentCommand;
  3400. for (currentCommand = enet_list_begin(&channel->incomingReliableCommands);
  3401. currentCommand != enet_list_end(&channel->incomingReliableCommands);
  3402. currentCommand = enet_list_next(currentCommand)
  3403. ) {
  3404. ENetIncomingCommand *incomingCommand = (ENetIncomingCommand *) currentCommand;
  3405. if (incomingCommand->fragmentsRemaining > 0 || incomingCommand->reliableSequenceNumber != (enet_uint16) (channel->incomingReliableSequenceNumber + 1)) {
  3406. break;
  3407. }
  3408. channel->incomingReliableSequenceNumber = incomingCommand->reliableSequenceNumber;
  3409. if (incomingCommand->fragmentCount > 0) {
  3410. channel->incomingReliableSequenceNumber += incomingCommand->fragmentCount - 1;
  3411. }
  3412. }
  3413. if (currentCommand == enet_list_begin(&channel->incomingReliableCommands)) {
  3414. return;
  3415. }
  3416. channel->incomingUnreliableSequenceNumber = 0;
  3417. enet_list_move(enet_list_end(&peer->dispatchedCommands), enet_list_begin(&channel->incomingReliableCommands), enet_list_previous(currentCommand));
  3418. if (!peer->needsDispatch) {
  3419. enet_list_insert(enet_list_end(&peer->host->dispatchQueue), &peer->dispatchList);
  3420. peer->needsDispatch = 1;
  3421. }
  3422. if (!enet_list_empty(&channel->incomingUnreliableCommands)) {
  3423. enet_peer_dispatch_incoming_unreliable_commands(peer, channel);
  3424. }
  3425. }
  3426. ENetIncomingCommand * enet_peer_queue_incoming_command(ENetPeer *peer, const ENetProtocol *command, const void *data, size_t dataLength, enet_uint32 flags, enet_uint32 fragmentCount) {
  3427. static ENetIncomingCommand dummyCommand;
  3428. ENetChannel *channel = &peer->channels[command->header.channelID];
  3429. enet_uint32 unreliableSequenceNumber = 0, reliableSequenceNumber = 0;
  3430. enet_uint16 reliableWindow, currentWindow;
  3431. ENetIncomingCommand *incomingCommand;
  3432. ENetListIterator currentCommand;
  3433. ENetPacket *packet = NULL;
  3434. if (peer->state == ENET_PEER_STATE_DISCONNECT_LATER) {
  3435. goto discardCommand;
  3436. }
  3437. if ((command->header.command & ENET_PROTOCOL_COMMAND_MASK) != ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED) {
  3438. reliableSequenceNumber = command->header.reliableSequenceNumber;
  3439. reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  3440. currentWindow = channel->incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
  3441. if (reliableSequenceNumber < channel->incomingReliableSequenceNumber) {
  3442. reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
  3443. }
  3444. if (reliableWindow < currentWindow || reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1) {
  3445. goto discardCommand;
  3446. }
  3447. }
  3448. switch (command->header.command & ENET_PROTOCOL_COMMAND_MASK) {
  3449. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  3450. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  3451. if (reliableSequenceNumber == channel->incomingReliableSequenceNumber) {
  3452. goto discardCommand;
  3453. }
  3454. for (currentCommand = enet_list_previous(enet_list_end(&channel->incomingReliableCommands));
  3455. currentCommand != enet_list_end(&channel->incomingReliableCommands);
  3456. currentCommand = enet_list_previous(currentCommand)
  3457. ) {
  3458. incomingCommand = (ENetIncomingCommand *) currentCommand;
  3459. if (reliableSequenceNumber >= channel->incomingReliableSequenceNumber) {
  3460. if (incomingCommand->reliableSequenceNumber < channel->incomingReliableSequenceNumber) {
  3461. continue;
  3462. }
  3463. } else if (incomingCommand->reliableSequenceNumber >= channel->incomingReliableSequenceNumber) {
  3464. break;
  3465. }
  3466. if (incomingCommand->reliableSequenceNumber <= reliableSequenceNumber) {
  3467. if (incomingCommand->reliableSequenceNumber < reliableSequenceNumber) {
  3468. break;
  3469. }
  3470. goto discardCommand;
  3471. }
  3472. }
  3473. break;
  3474. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
  3475. case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT:
  3476. unreliableSequenceNumber = ENET_NET_TO_HOST_16(command->sendUnreliable.unreliableSequenceNumber);
  3477. if (reliableSequenceNumber == channel->incomingReliableSequenceNumber && unreliableSequenceNumber <= channel->incomingUnreliableSequenceNumber) {
  3478. goto discardCommand;
  3479. }
  3480. for (currentCommand = enet_list_previous(enet_list_end(&channel->incomingUnreliableCommands));
  3481. currentCommand != enet_list_end(&channel->incomingUnreliableCommands);
  3482. currentCommand = enet_list_previous(currentCommand)
  3483. ) {
  3484. incomingCommand = (ENetIncomingCommand *) currentCommand;
  3485. if ((command->header.command & ENET_PROTOCOL_COMMAND_MASK) == ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED) {
  3486. continue;
  3487. }
  3488. if (reliableSequenceNumber >= channel->incomingReliableSequenceNumber) {
  3489. if (incomingCommand->reliableSequenceNumber < channel->incomingReliableSequenceNumber) {
  3490. continue;
  3491. }
  3492. } else if (incomingCommand->reliableSequenceNumber >= channel->incomingReliableSequenceNumber) {
  3493. break;
  3494. }
  3495. if (incomingCommand->reliableSequenceNumber < reliableSequenceNumber) {
  3496. break;
  3497. }
  3498. if (incomingCommand->reliableSequenceNumber > reliableSequenceNumber) {
  3499. continue;
  3500. }
  3501. if (incomingCommand->unreliableSequenceNumber <= unreliableSequenceNumber) {
  3502. if (incomingCommand->unreliableSequenceNumber < unreliableSequenceNumber) {
  3503. break;
  3504. }
  3505. goto discardCommand;
  3506. }
  3507. }
  3508. break;
  3509. case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
  3510. currentCommand = enet_list_end(&channel->incomingUnreliableCommands);
  3511. break;
  3512. default:
  3513. goto discardCommand;
  3514. }
  3515. if (peer->totalWaitingData >= peer->host->maximumWaitingData) {
  3516. goto notifyError;
  3517. }
  3518. packet = callbacks.packet_create(data, dataLength, flags);
  3519. if (packet == NULL) {
  3520. goto notifyError;
  3521. }
  3522. incomingCommand = (ENetIncomingCommand *) enet_malloc(sizeof(ENetIncomingCommand));
  3523. if (incomingCommand == NULL) {
  3524. goto notifyError;
  3525. }
  3526. incomingCommand->reliableSequenceNumber = command->header.reliableSequenceNumber;
  3527. incomingCommand->unreliableSequenceNumber = unreliableSequenceNumber & 0xFFFF;
  3528. incomingCommand->command = *command;
  3529. incomingCommand->fragmentCount = fragmentCount;
  3530. incomingCommand->fragmentsRemaining = fragmentCount;
  3531. incomingCommand->packet = packet;
  3532. incomingCommand->fragments = NULL;
  3533. if (fragmentCount > 0) {
  3534. if (fragmentCount <= ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT) {
  3535. incomingCommand->fragments = (enet_uint32 *) enet_malloc((fragmentCount + 31) / 32 * sizeof(enet_uint32));
  3536. }
  3537. if (incomingCommand->fragments == NULL) {
  3538. enet_free(incomingCommand);
  3539. goto notifyError;
  3540. }
  3541. memset(incomingCommand->fragments, 0, (fragmentCount + 31) / 32 * sizeof(enet_uint32));
  3542. }
  3543. if (packet != NULL) {
  3544. ++packet->referenceCount;
  3545. peer->totalWaitingData += packet->dataLength;
  3546. }
  3547. enet_list_insert(enet_list_next(currentCommand), incomingCommand);
  3548. switch (command->header.command & ENET_PROTOCOL_COMMAND_MASK) {
  3549. case ENET_PROTOCOL_COMMAND_SEND_FRAGMENT:
  3550. case ENET_PROTOCOL_COMMAND_SEND_RELIABLE:
  3551. enet_peer_dispatch_incoming_reliable_commands(peer, channel);
  3552. break;
  3553. default:
  3554. enet_peer_dispatch_incoming_unreliable_commands(peer, channel);
  3555. break;
  3556. }
  3557. return incomingCommand;
  3558. discardCommand:
  3559. if (fragmentCount > 0) {
  3560. goto notifyError;
  3561. }
  3562. if (packet != NULL && packet->referenceCount == 0) {
  3563. callbacks.packet_destroy(packet);
  3564. }
  3565. return &dummyCommand;
  3566. notifyError:
  3567. if (packet != NULL && packet->referenceCount == 0) {
  3568. callbacks.packet_destroy(packet);
  3569. }
  3570. return NULL;
  3571. } /* enet_peer_queue_incoming_command */
  3572. // =======================================================================//
  3573. // !
  3574. // ! Host
  3575. // !
  3576. // =======================================================================//
  3577. /** Creates a host for communicating to peers.
  3578. *
  3579. * @param address the address at which other peers may connect to this host. If NULL, then no peers may connect to the host.
  3580. * @param peerCount the maximum number of peers that should be allocated for the host.
  3581. * @param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
  3582. * @param incomingBandwidth downstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth.
  3583. * @param outgoingBandwidth upstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth.
  3584. *
  3585. * @returns the host on success and NULL on failure
  3586. *
  3587. * @remarks ENet will strategically drop packets on specific sides of a connection between hosts
  3588. * to ensure the host's bandwidth is not overwhelmed. The bandwidth parameters also determine
  3589. * the window size of a connection which limits the amount of reliable packets that may be in transit
  3590. * at any given time.
  3591. */
  3592. ENetHost * enet_host_create(const ENetAddress *address, size_t peerCount, size_t channelLimit, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth) {
  3593. ENetHost *host;
  3594. ENetPeer *currentPeer;
  3595. if (peerCount > ENET_PROTOCOL_MAXIMUM_PEER_ID) {
  3596. return NULL;
  3597. }
  3598. host = (ENetHost *) enet_malloc(sizeof(ENetHost));
  3599. if (host == NULL) { return NULL; }
  3600. memset(host, 0, sizeof(ENetHost));
  3601. host->peers = (ENetPeer *) enet_malloc(peerCount * sizeof(ENetPeer));
  3602. if (host->peers == NULL) {
  3603. enet_free(host);
  3604. return NULL;
  3605. }
  3606. memset(host->peers, 0, peerCount * sizeof(ENetPeer));
  3607. host->socket = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
  3608. if (host->socket != ENET_SOCKET_NULL) {
  3609. enet_socket_set_option (host->socket, ENET_SOCKOPT_IPV6_V6ONLY, 0);
  3610. }
  3611. if (host->socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind(host->socket, address) < 0)) {
  3612. if (host->socket != ENET_SOCKET_NULL) {
  3613. enet_socket_destroy(host->socket);
  3614. }
  3615. enet_free(host->peers);
  3616. enet_free(host);
  3617. return NULL;
  3618. }
  3619. enet_socket_set_option(host->socket, ENET_SOCKOPT_NONBLOCK, 1);
  3620. enet_socket_set_option(host->socket, ENET_SOCKOPT_BROADCAST, 1);
  3621. enet_socket_set_option(host->socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
  3622. enet_socket_set_option(host->socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
  3623. enet_socket_set_option(host->socket, ENET_SOCKOPT_IPV6_V6ONLY, 0);
  3624. if (address != NULL && enet_socket_get_address(host->socket, &host->address) < 0) {
  3625. host->address = *address;
  3626. }
  3627. if (!channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) {
  3628. channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  3629. } else if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT) {
  3630. channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
  3631. }
  3632. host->randomSeed = (enet_uint32) (size_t) host;
  3633. host->randomSeed += enet_host_random_seed();
  3634. host->randomSeed = (host->randomSeed << 16) | (host->randomSeed >> 16);
  3635. host->channelLimit = channelLimit;
  3636. host->incomingBandwidth = incomingBandwidth;
  3637. host->outgoingBandwidth = outgoingBandwidth;
  3638. host->bandwidthThrottleEpoch = 0;
  3639. host->recalculateBandwidthLimits = 0;
  3640. host->mtu = ENET_HOST_DEFAULT_MTU;
  3641. host->peerCount = peerCount;
  3642. host->commandCount = 0;
  3643. host->bufferCount = 0;
  3644. host->checksum = NULL;
  3645. host->receivedAddress.host = ENET_HOST_ANY;
  3646. host->receivedAddress.port = 0;
  3647. host->receivedData = NULL;
  3648. host->receivedDataLength = 0;
  3649. host->totalSentData = 0;
  3650. host->totalSentPackets = 0;
  3651. host->totalReceivedData = 0;
  3652. host->totalReceivedPackets = 0;
  3653. host->connectedPeers = 0;
  3654. host->bandwidthLimitedPeers = 0;
  3655. host->duplicatePeers = ENET_PROTOCOL_MAXIMUM_PEER_ID;
  3656. host->maximumPacketSize = ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE;
  3657. host->maximumWaitingData = ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA;
  3658. host->compressor.context = NULL;
  3659. host->compressor.compress = NULL;
  3660. host->compressor.decompress = NULL;
  3661. host->compressor.destroy = NULL;
  3662. host->intercept = NULL;
  3663. enet_list_clear(&host->dispatchQueue);
  3664. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer) {
  3665. currentPeer->host = host;
  3666. currentPeer->incomingPeerID = currentPeer - host->peers;
  3667. currentPeer->outgoingSessionID = currentPeer->incomingSessionID = 0xFF;
  3668. currentPeer->data = NULL;
  3669. enet_list_clear(&currentPeer->acknowledgements);
  3670. enet_list_clear(&currentPeer->sentReliableCommands);
  3671. enet_list_clear(&currentPeer->sentUnreliableCommands);
  3672. enet_list_clear(&currentPeer->outgoingReliableCommands);
  3673. enet_list_clear(&currentPeer->outgoingUnreliableCommands);
  3674. enet_list_clear(&currentPeer->dispatchedCommands);
  3675. enet_peer_reset(currentPeer);
  3676. }
  3677. return host;
  3678. } /* enet_host_create */
  3679. /** Destroys the host and all resources associated with it.
  3680. * @param host pointer to the host to destroy
  3681. */
  3682. void enet_host_destroy(ENetHost *host) {
  3683. ENetPeer *currentPeer;
  3684. if (host == NULL) {
  3685. return;
  3686. }
  3687. enet_socket_destroy(host->socket);
  3688. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer) {
  3689. enet_peer_reset(currentPeer);
  3690. }
  3691. if (host->compressor.context != NULL && host->compressor.destroy) {
  3692. (*host->compressor.destroy)(host->compressor.context);
  3693. }
  3694. enet_free(host->peers);
  3695. enet_free(host);
  3696. }
  3697. /** Initiates a connection to a foreign host.
  3698. * @param host host seeking the connection
  3699. * @param address destination for the connection
  3700. * @param channelCount number of channels to allocate
  3701. * @param data user data supplied to the receiving host
  3702. * @returns a peer representing the foreign host on success, NULL on failure
  3703. * @remarks The peer returned will have not completed the connection until enet_host_service()
  3704. * notifies of an ENET_EVENT_TYPE_CONNECT event for the peer.
  3705. */
  3706. ENetPeer * enet_host_connect(ENetHost *host, const ENetAddress *address, size_t channelCount, enet_uint32 data) {
  3707. ENetPeer *currentPeer;
  3708. ENetChannel *channel;
  3709. ENetProtocol command;
  3710. if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT) {
  3711. channelCount = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
  3712. } else if (channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) {
  3713. channelCount = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  3714. }
  3715. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer) {
  3716. if (currentPeer->state == ENET_PEER_STATE_DISCONNECTED) {
  3717. break;
  3718. }
  3719. }
  3720. if (currentPeer >= &host->peers[host->peerCount]) {
  3721. return NULL;
  3722. }
  3723. currentPeer->channels = (ENetChannel *) enet_malloc(channelCount * sizeof(ENetChannel));
  3724. if (currentPeer->channels == NULL) {
  3725. return NULL;
  3726. }
  3727. currentPeer->channelCount = channelCount;
  3728. currentPeer->state = ENET_PEER_STATE_CONNECTING;
  3729. currentPeer->address = *address;
  3730. currentPeer->connectID = ++host->randomSeed;
  3731. if (host->outgoingBandwidth == 0) {
  3732. currentPeer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  3733. } else {
  3734. currentPeer->windowSize = (host->outgoingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  3735. }
  3736. if (currentPeer->windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) {
  3737. currentPeer->windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
  3738. } else if (currentPeer->windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) {
  3739. currentPeer->windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
  3740. }
  3741. for (channel = currentPeer->channels; channel < &currentPeer->channels[channelCount]; ++channel) {
  3742. channel->outgoingReliableSequenceNumber = 0;
  3743. channel->outgoingUnreliableSequenceNumber = 0;
  3744. channel->incomingReliableSequenceNumber = 0;
  3745. channel->incomingUnreliableSequenceNumber = 0;
  3746. enet_list_clear(&channel->incomingReliableCommands);
  3747. enet_list_clear(&channel->incomingUnreliableCommands);
  3748. channel->usedReliableWindows = 0;
  3749. memset(channel->reliableWindows, 0, sizeof(channel->reliableWindows));
  3750. }
  3751. command.header.command = ENET_PROTOCOL_COMMAND_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  3752. command.header.channelID = 0xFF;
  3753. command.connect.outgoingPeerID = ENET_HOST_TO_NET_16(currentPeer->incomingPeerID);
  3754. command.connect.incomingSessionID = currentPeer->incomingSessionID;
  3755. command.connect.outgoingSessionID = currentPeer->outgoingSessionID;
  3756. command.connect.mtu = ENET_HOST_TO_NET_32(currentPeer->mtu);
  3757. command.connect.windowSize = ENET_HOST_TO_NET_32(currentPeer->windowSize);
  3758. command.connect.channelCount = ENET_HOST_TO_NET_32(channelCount);
  3759. command.connect.incomingBandwidth = ENET_HOST_TO_NET_32(host->incomingBandwidth);
  3760. command.connect.outgoingBandwidth = ENET_HOST_TO_NET_32(host->outgoingBandwidth);
  3761. command.connect.packetThrottleInterval = ENET_HOST_TO_NET_32(currentPeer->packetThrottleInterval);
  3762. command.connect.packetThrottleAcceleration = ENET_HOST_TO_NET_32(currentPeer->packetThrottleAcceleration);
  3763. command.connect.packetThrottleDeceleration = ENET_HOST_TO_NET_32(currentPeer->packetThrottleDeceleration);
  3764. command.connect.connectID = currentPeer->connectID;
  3765. command.connect.data = ENET_HOST_TO_NET_32(data);
  3766. enet_peer_queue_outgoing_command(currentPeer, &command, NULL, 0, 0);
  3767. return currentPeer;
  3768. } /* enet_host_connect */
  3769. /** Queues a packet to be sent to all peers associated with the host.
  3770. * @param host host on which to broadcast the packet
  3771. * @param channelID channel on which to broadcast
  3772. * @param packet packet to broadcast
  3773. */
  3774. void enet_host_broadcast(ENetHost *host, enet_uint8 channelID, ENetPacket *packet) {
  3775. ENetPeer *currentPeer;
  3776. for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer) {
  3777. if (currentPeer->state != ENET_PEER_STATE_CONNECTED) {
  3778. continue;
  3779. }
  3780. enet_peer_send(currentPeer, channelID, packet);
  3781. }
  3782. if (packet->referenceCount == 0) {
  3783. callbacks.packet_destroy(packet);
  3784. }
  3785. }
  3786. /** Sends raw data to specified address. Useful when you want to send unconnected data using host's socket.
  3787. * @param host host sending data
  3788. * @param address destination address
  3789. * @param data data pointer
  3790. * @param dataLength length of data to send
  3791. * @retval >=0 bytes sent
  3792. * @retval <0 error
  3793. * @sa enet_socket_send
  3794. */
  3795. int enet_host_send_raw(ENetHost *host, const ENetAddress* address, enet_uint8* data, size_t dataLength) {
  3796. ENetBuffer buffer;
  3797. buffer.data = data;
  3798. buffer.dataLength = dataLength;
  3799. return enet_socket_send(host->socket, address, &buffer, 1);
  3800. }
  3801. /** Sends raw data to specified address with extended arguments. Allows to send only part of data, handy for other programming languages.
  3802. * I.e. if you have data =- { 0, 1, 2, 3 } and call function as enet_host_send_raw_ex(data, 1, 2) then it will skip 1 byte and send 2 bytes { 1, 2 }.
  3803. * @param host host sending data
  3804. * @param address destination address
  3805. * @param data data pointer
  3806. * @param skipBytes number of bytes to skip from start of data
  3807. * @param bytesToSend number of bytes to send
  3808. * @retval >=0 bytes sent
  3809. * @retval <0 error
  3810. * @sa enet_socket_send
  3811. */
  3812. int enet_host_send_raw_ex(ENetHost *host, const ENetAddress* address, enet_uint8* data, size_t skipBytes, size_t bytesToSend) {
  3813. ENetBuffer buffer;
  3814. buffer.data = data + skipBytes;
  3815. buffer.dataLength = bytesToSend;
  3816. return enet_socket_send(host->socket, address, &buffer, 1);
  3817. }
  3818. /** Sets intercept callback for the host.
  3819. * @param host host to set a callback
  3820. * @param callback intercept callback
  3821. */
  3822. void enet_host_set_intercept(ENetHost *host, const ENetInterceptCallback callback) {
  3823. host->intercept = callback;
  3824. }
  3825. /** Sets the packet compressor the host should use to compress and decompress packets.
  3826. * @param host host to enable or disable compression for
  3827. * @param compressor callbacks for for the packet compressor; if NULL, then compression is disabled
  3828. */
  3829. void enet_host_compress(ENetHost *host, const ENetCompressor *compressor) {
  3830. if (host->compressor.context != NULL && host->compressor.destroy) {
  3831. (*host->compressor.destroy)(host->compressor.context);
  3832. }
  3833. if (compressor) {
  3834. host->compressor = *compressor;
  3835. } else {
  3836. host->compressor.context = NULL;
  3837. }
  3838. }
  3839. /** Limits the maximum allowed channels of future incoming connections.
  3840. * @param host host to limit
  3841. * @param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT
  3842. */
  3843. void enet_host_channel_limit(ENetHost *host, size_t channelLimit) {
  3844. if (!channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) {
  3845. channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
  3846. } else if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT) {
  3847. channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT;
  3848. }
  3849. host->channelLimit = channelLimit;
  3850. }
  3851. /** Adjusts the bandwidth limits of a host.
  3852. * @param host host to adjust
  3853. * @param incomingBandwidth new incoming bandwidth
  3854. * @param outgoingBandwidth new outgoing bandwidth
  3855. * @remarks the incoming and outgoing bandwidth parameters are identical in function to those
  3856. * specified in enet_host_create().
  3857. */
  3858. void enet_host_bandwidth_limit(ENetHost *host, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth) {
  3859. host->incomingBandwidth = incomingBandwidth;
  3860. host->outgoingBandwidth = outgoingBandwidth;
  3861. host->recalculateBandwidthLimits = 1;
  3862. }
  3863. void enet_host_bandwidth_throttle(ENetHost *host) {
  3864. enet_uint32 timeCurrent = enet_time_get();
  3865. enet_uint32 elapsedTime = timeCurrent - host->bandwidthThrottleEpoch;
  3866. enet_uint32 peersRemaining = (enet_uint32) host->connectedPeers;
  3867. enet_uint32 dataTotal = ~0;
  3868. enet_uint32 bandwidth = ~0;
  3869. enet_uint32 throttle = 0;
  3870. enet_uint32 bandwidthLimit = 0;
  3871. int needsAdjustment = host->bandwidthLimitedPeers > 0 ? 1 : 0;
  3872. ENetPeer *peer;
  3873. ENetProtocol command;
  3874. if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL) {
  3875. return;
  3876. }
  3877. if (host->outgoingBandwidth == 0 && host->incomingBandwidth == 0) {
  3878. return;
  3879. }
  3880. host->bandwidthThrottleEpoch = timeCurrent;
  3881. if (peersRemaining == 0) {
  3882. return;
  3883. }
  3884. if (host->outgoingBandwidth != 0) {
  3885. dataTotal = 0;
  3886. bandwidth = (host->outgoingBandwidth * elapsedTime) / 1000;
  3887. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer) {
  3888. if (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) {
  3889. continue;
  3890. }
  3891. dataTotal += peer->outgoingDataTotal;
  3892. }
  3893. }
  3894. while (peersRemaining > 0 && needsAdjustment != 0) {
  3895. needsAdjustment = 0;
  3896. if (dataTotal <= bandwidth) {
  3897. throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
  3898. } else {
  3899. throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
  3900. }
  3901. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer) {
  3902. enet_uint32 peerBandwidth;
  3903. if ((peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) ||
  3904. peer->incomingBandwidth == 0 ||
  3905. peer->outgoingBandwidthThrottleEpoch == timeCurrent
  3906. ) {
  3907. continue;
  3908. }
  3909. peerBandwidth = (peer->incomingBandwidth * elapsedTime) / 1000;
  3910. if ((throttle * peer->outgoingDataTotal) / ENET_PEER_PACKET_THROTTLE_SCALE <= peerBandwidth) {
  3911. continue;
  3912. }
  3913. peer->packetThrottleLimit = (peerBandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / peer->outgoingDataTotal;
  3914. if (peer->packetThrottleLimit == 0) {
  3915. peer->packetThrottleLimit = 1;
  3916. }
  3917. if (peer->packetThrottle > peer->packetThrottleLimit) {
  3918. peer->packetThrottle = peer->packetThrottleLimit;
  3919. }
  3920. peer->outgoingBandwidthThrottleEpoch = timeCurrent;
  3921. peer->incomingDataTotal = 0;
  3922. peer->outgoingDataTotal = 0;
  3923. needsAdjustment = 1;
  3924. --peersRemaining;
  3925. bandwidth -= peerBandwidth;
  3926. dataTotal -= peerBandwidth;
  3927. }
  3928. }
  3929. if (peersRemaining > 0) {
  3930. if (dataTotal <= bandwidth) {
  3931. throttle = ENET_PEER_PACKET_THROTTLE_SCALE;
  3932. } else {
  3933. throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal;
  3934. }
  3935. for (peer = host->peers;
  3936. peer < &host->peers[host->peerCount];
  3937. ++peer)
  3938. {
  3939. if ((peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) || peer->outgoingBandwidthThrottleEpoch == timeCurrent) {
  3940. continue;
  3941. }
  3942. peer->packetThrottleLimit = throttle;
  3943. if (peer->packetThrottle > peer->packetThrottleLimit) {
  3944. peer->packetThrottle = peer->packetThrottleLimit;
  3945. }
  3946. peer->incomingDataTotal = 0;
  3947. peer->outgoingDataTotal = 0;
  3948. }
  3949. }
  3950. if (host->recalculateBandwidthLimits) {
  3951. host->recalculateBandwidthLimits = 0;
  3952. peersRemaining = (enet_uint32) host->connectedPeers;
  3953. bandwidth = host->incomingBandwidth;
  3954. needsAdjustment = 1;
  3955. if (bandwidth == 0) {
  3956. bandwidthLimit = 0;
  3957. } else {
  3958. while (peersRemaining > 0 && needsAdjustment != 0) {
  3959. needsAdjustment = 0;
  3960. bandwidthLimit = bandwidth / peersRemaining;
  3961. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer) {
  3962. if ((peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) ||
  3963. peer->incomingBandwidthThrottleEpoch == timeCurrent
  3964. ) {
  3965. continue;
  3966. }
  3967. if (peer->outgoingBandwidth > 0 && peer->outgoingBandwidth >= bandwidthLimit) {
  3968. continue;
  3969. }
  3970. peer->incomingBandwidthThrottleEpoch = timeCurrent;
  3971. needsAdjustment = 1;
  3972. --peersRemaining;
  3973. bandwidth -= peer->outgoingBandwidth;
  3974. }
  3975. }
  3976. }
  3977. for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer) {
  3978. if (peer->state != ENET_PEER_STATE_CONNECTED && peer->state != ENET_PEER_STATE_DISCONNECT_LATER) {
  3979. continue;
  3980. }
  3981. command.header.command = ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
  3982. command.header.channelID = 0xFF;
  3983. command.bandwidthLimit.outgoingBandwidth = ENET_HOST_TO_NET_32(host->outgoingBandwidth);
  3984. if (peer->incomingBandwidthThrottleEpoch == timeCurrent) {
  3985. command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32(peer->outgoingBandwidth);
  3986. } else {
  3987. command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32(bandwidthLimit);
  3988. }
  3989. enet_peer_queue_outgoing_command(peer, &command, NULL, 0, 0);
  3990. }
  3991. }
  3992. } /* enet_host_bandwidth_throttle */
  3993. // =======================================================================//
  3994. // !
  3995. // ! Time
  3996. // !
  3997. // =======================================================================//
  3998. #ifdef _WIN32
  3999. static LARGE_INTEGER getFILETIMEoffset() {
  4000. SYSTEMTIME s;
  4001. FILETIME f;
  4002. LARGE_INTEGER t;
  4003. s.wYear = 1970;
  4004. s.wMonth = 1;
  4005. s.wDay = 1;
  4006. s.wHour = 0;
  4007. s.wMinute = 0;
  4008. s.wSecond = 0;
  4009. s.wMilliseconds = 0;
  4010. SystemTimeToFileTime(&s, &f);
  4011. t.QuadPart = f.dwHighDateTime;
  4012. t.QuadPart <<= 32;
  4013. t.QuadPart |= f.dwLowDateTime;
  4014. return (t);
  4015. }
  4016. int clock_gettime(int X, struct timespec *tv) {
  4017. (void)X;
  4018. LARGE_INTEGER t;
  4019. FILETIME f;
  4020. double microseconds;
  4021. static LARGE_INTEGER offset;
  4022. static double frequencyToMicroseconds;
  4023. static int initialized = 0;
  4024. static BOOL usePerformanceCounter = 0;
  4025. if (!initialized) {
  4026. LARGE_INTEGER performanceFrequency;
  4027. initialized = 1;
  4028. usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
  4029. if (usePerformanceCounter) {
  4030. QueryPerformanceCounter(&offset);
  4031. frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
  4032. } else {
  4033. offset = getFILETIMEoffset();
  4034. frequencyToMicroseconds = 10.;
  4035. }
  4036. }
  4037. if (usePerformanceCounter) {
  4038. QueryPerformanceCounter(&t);
  4039. } else {
  4040. GetSystemTimeAsFileTime(&f);
  4041. t.QuadPart = f.dwHighDateTime;
  4042. t.QuadPart <<= 32;
  4043. t.QuadPart |= f.dwLowDateTime;
  4044. }
  4045. t.QuadPart -= offset.QuadPart;
  4046. microseconds = (double)t.QuadPart / frequencyToMicroseconds;
  4047. t.QuadPart = (LONGLONG)microseconds;
  4048. tv->tv_sec = (long)(t.QuadPart / 1000000);
  4049. tv->tv_nsec = t.QuadPart % 1000000 * 1000;
  4050. return (0);
  4051. }
  4052. #elif __APPLE__ && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
  4053. #define CLOCK_MONOTONIC 0
  4054. int clock_gettime(int X, struct timespec *ts) {
  4055. clock_serv_t cclock;
  4056. mach_timespec_t mts;
  4057. host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
  4058. clock_get_time(cclock, &mts);
  4059. mach_port_deallocate(mach_task_self(), cclock);
  4060. ts->tv_sec = mts.tv_sec;
  4061. ts->tv_nsec = mts.tv_nsec;
  4062. return 0;
  4063. }
  4064. #endif
  4065. enet_uint32 enet_time_get() {
  4066. // TODO enet uses 32 bit timestamps. We should modify it to use
  4067. // 64 bit timestamps, but this is not trivial since we'd end up
  4068. // changing half the structs in enet. For now, retain 32 bits, but
  4069. // use an offset so we don't run out of bits. Basically, the first
  4070. // call of enet_time_get() will always return 1, and follow-up calls
  4071. // indicate elapsed time since the first call.
  4072. //
  4073. // Note that we don't want to return 0 from the first call, in case
  4074. // some part of enet uses 0 as a special value (meaning time not set
  4075. // for example).
  4076. static uint64_t start_time_ns = 0;
  4077. struct timespec ts;
  4078. #if defined(CLOCK_MONOTONIC_RAW)
  4079. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  4080. #else
  4081. clock_gettime(CLOCK_MONOTONIC, &ts);
  4082. #endif
  4083. static const uint64_t ns_in_s = 1000 * 1000 * 1000;
  4084. static const uint64_t ns_in_ms = 1000 * 1000;
  4085. uint64_t current_time_ns = ts.tv_nsec + (uint64_t)ts.tv_sec * ns_in_s;
  4086. // Most of the time we just want to atomically read the start time. We
  4087. // could just use a single CAS instruction instead of this if, but it
  4088. // would be slower in the average case.
  4089. //
  4090. // Note that statics are auto-initialized to zero, and starting a thread
  4091. // implies a memory barrier. So we know that whatever thread calls this,
  4092. // it correctly sees the start_time_ns as 0 initially.
  4093. uint64_t offset_ns = ENET_ATOMIC_READ(&start_time_ns);
  4094. if (offset_ns == 0) {
  4095. // We still need to CAS, since two different threads can get here
  4096. // at the same time.
  4097. //
  4098. // We assume that current_time_ns is > 1ms.
  4099. //
  4100. // Set the value of the start_time_ns, such that the first timestamp
  4101. // is at 1ms. This ensures 0 remains a special value.
  4102. uint64_t want_value = current_time_ns - 1 * ns_in_ms;
  4103. uint64_t old_value = ENET_ATOMIC_CAS(&start_time_ns, 0, want_value, old_value);
  4104. offset_ns = old_value == 0 ? want_value : old_value;
  4105. }
  4106. uint64_t result_in_ns = current_time_ns - offset_ns;
  4107. return (enet_uint32)(result_in_ns / ns_in_ms);
  4108. }
  4109. void enet_inaddr_map4to6(struct in_addr in, struct in6_addr *out)
  4110. {
  4111. if (in.s_addr == 0x00000000) { /* 0.0.0.0 */
  4112. *out = enet_v6_anyaddr;
  4113. } else if (in.s_addr == 0xFFFFFFFF) { /* 255.255.255.255 */
  4114. *out = enet_v6_noaddr;
  4115. } else {
  4116. *out = enet_v4_anyaddr;
  4117. out->s6_addr[10] = 0xFF;
  4118. out->s6_addr[11] = 0xFF;
  4119. out->s6_addr[12] = ((uint8_t *)&in.s_addr)[0];
  4120. out->s6_addr[13] = ((uint8_t *)&in.s_addr)[1];
  4121. out->s6_addr[14] = ((uint8_t *)&in.s_addr)[2];
  4122. out->s6_addr[15] = ((uint8_t *)&in.s_addr)[3];
  4123. }
  4124. }
  4125. void enet_inaddr_map6to4(const struct in6_addr *in, struct in_addr *out)
  4126. {
  4127. memset(out, 0, sizeof(struct in_addr));
  4128. ((uint8_t *)&out->s_addr)[0] = in->s6_addr[12];
  4129. ((uint8_t *)&out->s_addr)[1] = in->s6_addr[13];
  4130. ((uint8_t *)&out->s_addr)[2] = in->s6_addr[14];
  4131. ((uint8_t *)&out->s_addr)[3] = in->s6_addr[15];
  4132. }
  4133. int enet_in6addr_lookup_host(const char *name, bool nodns, ENetAddress *out) {
  4134. struct addrinfo hints, *resultList = NULL, *result = NULL;
  4135. memset(&hints, 0, sizeof(hints));
  4136. hints.ai_family = AF_UNSPEC;
  4137. if (nodns)
  4138. {
  4139. hints.ai_flags = AI_NUMERICHOST; /* prevent actual DNS lookups! */
  4140. }
  4141. if (getaddrinfo(name, NULL, &hints, &resultList) != 0) {
  4142. if (resultList != NULL) {
  4143. freeaddrinfo(resultList);
  4144. }
  4145. return -1;
  4146. }
  4147. for (result = resultList; result != NULL; result = result->ai_next) {
  4148. if (result->ai_addr != NULL) {
  4149. if (result->ai_family == AF_INET || (result->ai_family == AF_UNSPEC && result->ai_addrlen == sizeof(struct sockaddr_in))) {
  4150. enet_inaddr_map4to6(((struct sockaddr_in*)result->ai_addr)->sin_addr, &out->host);
  4151. out->sin6_scope_id = 0;
  4152. if (resultList != NULL) {
  4153. freeaddrinfo(resultList);
  4154. }
  4155. return 0;
  4156. } else if (result->ai_family == AF_INET6 || (result->ai_family == AF_UNSPEC && result->ai_addrlen == sizeof(struct sockaddr_in6))) {
  4157. memcpy(&out->host, &((struct sockaddr_in6*)result->ai_addr)->sin6_addr, sizeof(struct in6_addr));
  4158. out->sin6_scope_id = (enet_uint16) ((struct sockaddr_in6*)result->ai_addr)->sin6_scope_id;
  4159. if (resultList != NULL) {
  4160. freeaddrinfo(resultList);
  4161. }
  4162. return 0;
  4163. }
  4164. }
  4165. }
  4166. if (resultList != NULL) {
  4167. freeaddrinfo(resultList);
  4168. }
  4169. return -1;
  4170. }
  4171. int enet_address_set_host_ip_new(ENetAddress *address, const char *name) {
  4172. return enet_in6addr_lookup_host(name, true, address);
  4173. }
  4174. int enet_address_set_host_new(ENetAddress *address, const char *name) {
  4175. return enet_in6addr_lookup_host(name, false, address);
  4176. }
  4177. int enet_address_get_host_ip_new(const ENetAddress *address, char *name, size_t nameLength) {
  4178. if (IN6_IS_ADDR_V4MAPPED(&address->host)) {
  4179. struct in_addr buf;
  4180. enet_inaddr_map6to4(&address->host, &buf);
  4181. if (inet_ntop(AF_INET, &buf, name, nameLength) == NULL) {
  4182. return -1;
  4183. }
  4184. }
  4185. else {
  4186. if (inet_ntop(AF_INET6, (void*)&address->host, name, nameLength) == NULL) {
  4187. return -1;
  4188. }
  4189. }
  4190. return 0;
  4191. } /* enet_address_get_host_ip_new */
  4192. int enet_address_get_host_new(const ENetAddress *address, char *name, size_t nameLength) {
  4193. struct sockaddr_in6 sin;
  4194. memset(&sin, 0, sizeof(struct sockaddr_in6));
  4195. int err;
  4196. sin.sin6_family = AF_INET6;
  4197. sin.sin6_port = ENET_HOST_TO_NET_16 (address->port);
  4198. sin.sin6_addr = address->host;
  4199. sin.sin6_scope_id = address->sin6_scope_id;
  4200. err = getnameinfo((struct sockaddr *) &sin, sizeof(sin), name, nameLength, NULL, 0, NI_NAMEREQD);
  4201. if (!err) {
  4202. if (name != NULL && nameLength > 0 && !memchr(name, '\0', nameLength)) {
  4203. return -1;
  4204. }
  4205. return 0;
  4206. }
  4207. if (err != EAI_NONAME) {
  4208. return -1;
  4209. }
  4210. return enet_address_get_host_ip_new(address, name, nameLength);
  4211. } /* enet_address_get_host_new */
  4212. // =======================================================================//
  4213. // !
  4214. // ! Platform Specific (Unix)
  4215. // !
  4216. // =======================================================================//
  4217. #ifndef _WIN32
  4218. #if defined(__MINGW32__) && defined(ENET_MINGW_COMPAT)
  4219. // inet_ntop/inet_pton for MinGW from http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html
  4220. const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) {
  4221. if (af == AF_INET) {
  4222. struct sockaddr_in in;
  4223. memset(&in, 0, sizeof(in));
  4224. in.sin_family = AF_INET;
  4225. memcpy(&in.sin_addr, src, sizeof(struct in_addr));
  4226. getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
  4227. return dst;
  4228. }
  4229. else if (af == AF_INET6) {
  4230. struct sockaddr_in6 in;
  4231. memset(&in, 0, sizeof(in));
  4232. in.sin6_family = AF_INET6;
  4233. memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
  4234. getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
  4235. return dst;
  4236. }
  4237. return NULL;
  4238. }
  4239. #define NS_INADDRSZ 4
  4240. #define NS_IN6ADDRSZ 16
  4241. #define NS_INT16SZ 2
  4242. int inet_pton4(const char *src, char *dst) {
  4243. uint8_t tmp[NS_INADDRSZ], *tp;
  4244. int saw_digit = 0;
  4245. int octets = 0;
  4246. *(tp = tmp) = 0;
  4247. int ch;
  4248. while ((ch = *src++) != '\0')
  4249. {
  4250. if (ch >= '0' && ch <= '9')
  4251. {
  4252. uint32_t n = *tp * 10 + (ch - '0');
  4253. if (saw_digit && *tp == 0)
  4254. return 0;
  4255. if (n > 255)
  4256. return 0;
  4257. *tp = n;
  4258. if (!saw_digit)
  4259. {
  4260. if (++octets > 4)
  4261. return 0;
  4262. saw_digit = 1;
  4263. }
  4264. }
  4265. else if (ch == '.' && saw_digit)
  4266. {
  4267. if (octets == 4)
  4268. return 0;
  4269. *++tp = 0;
  4270. saw_digit = 0;
  4271. }
  4272. else
  4273. return 0;
  4274. }
  4275. if (octets < 4)
  4276. return 0;
  4277. memcpy(dst, tmp, NS_INADDRSZ);
  4278. return 1;
  4279. }
  4280. int inet_pton6(const char *src, char *dst) {
  4281. static const char xdigits[] = "0123456789abcdef";
  4282. uint8_t tmp[NS_IN6ADDRSZ];
  4283. uint8_t *tp = (uint8_t*) memset(tmp, '\0', NS_IN6ADDRSZ);
  4284. uint8_t *endp = tp + NS_IN6ADDRSZ;
  4285. uint8_t *colonp = NULL;
  4286. /* Leading :: requires some special handling. */
  4287. if (*src == ':')
  4288. {
  4289. if (*++src != ':')
  4290. return 0;
  4291. }
  4292. const char *curtok = src;
  4293. int saw_xdigit = 0;
  4294. uint32_t val = 0;
  4295. int ch;
  4296. while ((ch = tolower(*src++)) != '\0')
  4297. {
  4298. const char *pch = strchr(xdigits, ch);
  4299. if (pch != NULL)
  4300. {
  4301. val <<= 4;
  4302. val |= (pch - xdigits);
  4303. if (val > 0xffff)
  4304. return 0;
  4305. saw_xdigit = 1;
  4306. continue;
  4307. }
  4308. if (ch == ':')
  4309. {
  4310. curtok = src;
  4311. if (!saw_xdigit)
  4312. {
  4313. if (colonp)
  4314. return 0;
  4315. colonp = tp;
  4316. continue;
  4317. }
  4318. else if (*src == '\0')
  4319. {
  4320. return 0;
  4321. }
  4322. if (tp + NS_INT16SZ > endp)
  4323. return 0;
  4324. *tp++ = (uint8_t) (val >> 8) & 0xff;
  4325. *tp++ = (uint8_t) val & 0xff;
  4326. saw_xdigit = 0;
  4327. val = 0;
  4328. continue;
  4329. }
  4330. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  4331. inet_pton4(curtok, (char*) tp) > 0)
  4332. {
  4333. tp += NS_INADDRSZ;
  4334. saw_xdigit = 0;
  4335. break; /* '\0' was seen by inet_pton4(). */
  4336. }
  4337. return 0;
  4338. }
  4339. if (saw_xdigit)
  4340. {
  4341. if (tp + NS_INT16SZ > endp)
  4342. return 0;
  4343. *tp++ = (uint8_t) (val >> 8) & 0xff;
  4344. *tp++ = (uint8_t) val & 0xff;
  4345. }
  4346. if (colonp != NULL)
  4347. {
  4348. /*
  4349. * Since some memmove()'s erroneously fail to handle
  4350. * overlapping regions, we'll do the shift by hand.
  4351. */
  4352. const int n = tp - colonp;
  4353. if (tp == endp)
  4354. return 0;
  4355. for (int i = 1; i <= n; i++)
  4356. {
  4357. endp[-i] = colonp[n - i];
  4358. colonp[n - i] = 0;
  4359. }
  4360. tp = endp;
  4361. }
  4362. if (tp != endp)
  4363. return 0;
  4364. memcpy(dst, tmp, NS_IN6ADDRSZ);
  4365. return 1;
  4366. }
  4367. int inet_pton(int af, const char *src, struct in6_addr *dst) {
  4368. switch (af)
  4369. {
  4370. case AF_INET:
  4371. return inet_pton4(src, (char *)dst);
  4372. case AF_INET6:
  4373. return inet_pton6(src, (char *)dst);
  4374. default:
  4375. return -1;
  4376. }
  4377. }
  4378. #endif // __MINGW__
  4379. int enet_initialize(void) {
  4380. return 0;
  4381. }
  4382. void enet_deinitialize(void) {}
  4383. enet_uint64 enet_host_random_seed(void) {
  4384. return (enet_uint64) time(NULL);
  4385. }
  4386. int enet_address_set_host_ip_old(ENetAddress *address, const char *name) {
  4387. if (!inet_pton(AF_INET6, name, &address->host)) {
  4388. return -1;
  4389. }
  4390. return 0;
  4391. }
  4392. int enet_address_set_host_old(ENetAddress *address, const char *name) {
  4393. struct addrinfo hints, *resultList = NULL, *result = NULL;
  4394. memset(&hints, 0, sizeof(hints));
  4395. hints.ai_family = AF_UNSPEC;
  4396. if (getaddrinfo(name, NULL, &hints, &resultList) != 0) {
  4397. return -1;
  4398. }
  4399. for (result = resultList; result != NULL; result = result->ai_next) {
  4400. if (result->ai_addr != NULL && result->ai_addrlen >= sizeof(struct sockaddr_in)) {
  4401. if (result->ai_family == AF_INET) {
  4402. struct sockaddr_in * sin = (struct sockaddr_in *) result->ai_addr;
  4403. ((uint32_t *)&address->host.s6_addr)[0] = 0;
  4404. ((uint32_t *)&address->host.s6_addr)[1] = 0;
  4405. ((uint32_t *)&address->host.s6_addr)[2] = htonl(0xffff);
  4406. ((uint32_t *)&address->host.s6_addr)[3] = sin->sin_addr.s_addr;
  4407. freeaddrinfo(resultList);
  4408. return 0;
  4409. }
  4410. else if(result->ai_family == AF_INET6) {
  4411. struct sockaddr_in6 * sin = (struct sockaddr_in6 *)result->ai_addr;
  4412. address->host = sin->sin6_addr;
  4413. address->sin6_scope_id = sin->sin6_scope_id;
  4414. freeaddrinfo(resultList);
  4415. return 0;
  4416. }
  4417. }
  4418. }
  4419. if (resultList != NULL) {
  4420. freeaddrinfo(resultList);
  4421. }
  4422. return enet_address_set_host_ip(address, name);
  4423. } /* enet_address_set_host_old */
  4424. int enet_address_get_host_ip_old(const ENetAddress *address, char *name, size_t nameLength) {
  4425. if (inet_ntop(AF_INET6, &address->host, name, nameLength) == NULL) {
  4426. return -1;
  4427. }
  4428. return 0;
  4429. }
  4430. int enet_address_get_host_old(const ENetAddress *address, char *name, size_t nameLength) {
  4431. struct sockaddr_in6 sin;
  4432. int err;
  4433. memset(&sin, 0, sizeof(struct sockaddr_in6));
  4434. sin.sin6_family = AF_INET6;
  4435. sin.sin6_port = ENET_HOST_TO_NET_16 (address->port);
  4436. sin.sin6_addr = address->host;
  4437. sin.sin6_scope_id = address->sin6_scope_id;
  4438. err = getnameinfo((struct sockaddr *) &sin, sizeof(sin), name, nameLength, NULL, 0, NI_NAMEREQD);
  4439. if (!err) {
  4440. if (name != NULL && nameLength > 0 && !memchr(name, '\0', nameLength)) {
  4441. return -1;
  4442. }
  4443. return 0;
  4444. }
  4445. if (err != EAI_NONAME) {
  4446. return -1;
  4447. }
  4448. return enet_address_get_host_ip(address, name, nameLength);
  4449. } /* enet_address_get_host_old */
  4450. int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
  4451. struct sockaddr_in6 sin;
  4452. memset(&sin, 0, sizeof(struct sockaddr_in6));
  4453. sin.sin6_family = AF_INET6;
  4454. if (address != NULL) {
  4455. sin.sin6_port = ENET_HOST_TO_NET_16(address->port);
  4456. sin.sin6_addr = address->host;
  4457. sin.sin6_scope_id = address->sin6_scope_id;
  4458. } else {
  4459. sin.sin6_port = 0;
  4460. sin.sin6_addr = ENET_HOST_ANY;
  4461. sin.sin6_scope_id = 0;
  4462. }
  4463. return bind(socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in6));
  4464. }
  4465. int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
  4466. struct sockaddr_in6 sin;
  4467. socklen_t sinLength = sizeof(struct sockaddr_in6);
  4468. if (getsockname(socket, (struct sockaddr *) &sin, &sinLength) == -1) {
  4469. return -1;
  4470. }
  4471. address->host = sin.sin6_addr;
  4472. address->port = ENET_NET_TO_HOST_16(sin.sin6_port);
  4473. address->sin6_scope_id = sin.sin6_scope_id;
  4474. return 0;
  4475. }
  4476. int enet_socket_listen(ENetSocket socket, int backlog) {
  4477. return listen(socket, backlog < 0 ? SOMAXCONN : backlog);
  4478. }
  4479. ENetSocket enet_socket_create(ENetSocketType type) {
  4480. return socket(PF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  4481. }
  4482. int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value) {
  4483. int result = -1;
  4484. switch (option) {
  4485. case ENET_SOCKOPT_NONBLOCK:
  4486. result = fcntl(socket, F_SETFL, (value ? O_NONBLOCK : 0) | (fcntl(socket, F_GETFL) & ~O_NONBLOCK));
  4487. break;
  4488. case ENET_SOCKOPT_BROADCAST:
  4489. result = setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&value, sizeof(int));
  4490. break;
  4491. case ENET_SOCKOPT_REUSEADDR:
  4492. result = setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (char *)&value, sizeof(int));
  4493. break;
  4494. case ENET_SOCKOPT_RCVBUF:
  4495. result = setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char *)&value, sizeof(int));
  4496. break;
  4497. case ENET_SOCKOPT_SNDBUF:
  4498. result = setsockopt(socket, SOL_SOCKET, SO_SNDBUF, (char *)&value, sizeof(int));
  4499. break;
  4500. case ENET_SOCKOPT_RCVTIMEO: {
  4501. struct timeval timeVal;
  4502. timeVal.tv_sec = value / 1000;
  4503. timeVal.tv_usec = (value % 1000) * 1000;
  4504. result = setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeVal, sizeof(struct timeval));
  4505. break;
  4506. }
  4507. case ENET_SOCKOPT_SNDTIMEO: {
  4508. struct timeval timeVal;
  4509. timeVal.tv_sec = value / 1000;
  4510. timeVal.tv_usec = (value % 1000) * 1000;
  4511. result = setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeVal, sizeof(struct timeval));
  4512. break;
  4513. }
  4514. case ENET_SOCKOPT_NODELAY:
  4515. result = setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *)&value, sizeof(int));
  4516. break;
  4517. case ENET_SOCKOPT_IPV6_V6ONLY:
  4518. result = setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&value, sizeof(int));
  4519. break;
  4520. default:
  4521. break;
  4522. }
  4523. return result == -1 ? -1 : 0;
  4524. } /* enet_socket_set_option */
  4525. int enet_socket_get_option(ENetSocket socket, ENetSocketOption option, int *value) {
  4526. int result = -1;
  4527. socklen_t len;
  4528. switch (option) {
  4529. case ENET_SOCKOPT_ERROR:
  4530. len = sizeof(int);
  4531. result = getsockopt(socket, SOL_SOCKET, SO_ERROR, value, &len);
  4532. break;
  4533. default:
  4534. break;
  4535. }
  4536. return result == -1 ? -1 : 0;
  4537. }
  4538. int enet_socket_connect(ENetSocket socket, const ENetAddress *address) {
  4539. struct sockaddr_in6 sin;
  4540. int result;
  4541. memset(&sin, 0, sizeof(struct sockaddr_in6));
  4542. sin.sin6_family = AF_INET6;
  4543. sin.sin6_port = ENET_HOST_TO_NET_16(address->port);
  4544. sin.sin6_addr = address->host;
  4545. sin.sin6_scope_id = address->sin6_scope_id;
  4546. result = connect(socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in6));
  4547. if (result == -1 && errno == EINPROGRESS) {
  4548. return 0;
  4549. }
  4550. return result;
  4551. }
  4552. ENetSocket enet_socket_accept(ENetSocket socket, ENetAddress *address) {
  4553. int result;
  4554. struct sockaddr_in6 sin;
  4555. socklen_t sinLength = sizeof(struct sockaddr_in6);
  4556. result = accept(socket,address != NULL ? (struct sockaddr *) &sin : NULL, address != NULL ? &sinLength : NULL);
  4557. if (result == -1) {
  4558. return ENET_SOCKET_NULL;
  4559. }
  4560. if (address != NULL) {
  4561. address->host = sin.sin6_addr;
  4562. address->port = ENET_NET_TO_HOST_16 (sin.sin6_port);
  4563. address->sin6_scope_id = sin.sin6_scope_id;
  4564. }
  4565. return result;
  4566. }
  4567. int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how) {
  4568. return shutdown(socket, (int) how);
  4569. }
  4570. void enet_socket_destroy(ENetSocket socket) {
  4571. if (socket != -1) {
  4572. close(socket);
  4573. }
  4574. }
  4575. int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBuffer *buffers, size_t bufferCount) {
  4576. struct msghdr msgHdr;
  4577. struct sockaddr_in6 sin;
  4578. int sentLength;
  4579. memset(&msgHdr, 0, sizeof(struct msghdr));
  4580. if (address != NULL) {
  4581. memset(&sin, 0, sizeof(struct sockaddr_in6));
  4582. sin.sin6_family = AF_INET6;
  4583. sin.sin6_port = ENET_HOST_TO_NET_16(address->port);
  4584. sin.sin6_addr = address->host;
  4585. sin.sin6_scope_id = address->sin6_scope_id;
  4586. msgHdr.msg_name = &sin;
  4587. msgHdr.msg_namelen = sizeof(struct sockaddr_in6);
  4588. }
  4589. msgHdr.msg_iov = (struct iovec *) buffers;
  4590. msgHdr.msg_iovlen = bufferCount;
  4591. sentLength = sendmsg(socket, &msgHdr, MSG_NOSIGNAL);
  4592. if (sentLength == -1) {
  4593. if (errno == EWOULDBLOCK) {
  4594. return 0;
  4595. }
  4596. return -1;
  4597. }
  4598. return sentLength;
  4599. } /* enet_socket_send */
  4600. int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
  4601. struct msghdr msgHdr;
  4602. struct sockaddr_in6 sin;
  4603. int recvLength;
  4604. memset(&msgHdr, 0, sizeof(struct msghdr));
  4605. if (address != NULL) {
  4606. msgHdr.msg_name = &sin;
  4607. msgHdr.msg_namelen = sizeof(struct sockaddr_in6);
  4608. }
  4609. msgHdr.msg_iov = (struct iovec *) buffers;
  4610. msgHdr.msg_iovlen = bufferCount;
  4611. recvLength = recvmsg(socket, &msgHdr, MSG_NOSIGNAL);
  4612. if (recvLength == -1) {
  4613. if (errno == EWOULDBLOCK) {
  4614. return 0;
  4615. }
  4616. return -1;
  4617. }
  4618. if (msgHdr.msg_flags & MSG_TRUNC) {
  4619. return -1;
  4620. }
  4621. if (address != NULL) {
  4622. address->host = sin.sin6_addr;
  4623. address->port = ENET_NET_TO_HOST_16(sin.sin6_port);
  4624. address->sin6_scope_id = sin.sin6_scope_id;
  4625. }
  4626. return recvLength;
  4627. } /* enet_socket_receive */
  4628. int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
  4629. struct timeval timeVal;
  4630. timeVal.tv_sec = timeout / 1000;
  4631. timeVal.tv_usec = (timeout % 1000) * 1000;
  4632. return select(maxSocket + 1, readSet, writeSet, NULL, &timeVal);
  4633. }
  4634. int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint64 timeout) {
  4635. struct pollfd pollSocket;
  4636. int pollCount;
  4637. pollSocket.fd = socket;
  4638. pollSocket.events = 0;
  4639. if (*condition & ENET_SOCKET_WAIT_SEND) {
  4640. pollSocket.events |= POLLOUT;
  4641. }
  4642. if (*condition & ENET_SOCKET_WAIT_RECEIVE) {
  4643. pollSocket.events |= POLLIN;
  4644. }
  4645. pollCount = poll(&pollSocket, 1, timeout);
  4646. if (pollCount < 0) {
  4647. if (errno == EINTR && *condition & ENET_SOCKET_WAIT_INTERRUPT) {
  4648. *condition = ENET_SOCKET_WAIT_INTERRUPT;
  4649. return 0;
  4650. }
  4651. return -1;
  4652. }
  4653. *condition = ENET_SOCKET_WAIT_NONE;
  4654. if (pollCount == 0) {
  4655. return 0;
  4656. }
  4657. if (pollSocket.revents & POLLOUT) {
  4658. *condition |= ENET_SOCKET_WAIT_SEND;
  4659. }
  4660. if (pollSocket.revents & POLLIN) {
  4661. *condition |= ENET_SOCKET_WAIT_RECEIVE;
  4662. }
  4663. return 0;
  4664. } /* enet_socket_wait */
  4665. #endif // !_WIN32
  4666. // =======================================================================//
  4667. // !
  4668. // ! Platform Specific (Win)
  4669. // !
  4670. // =======================================================================//
  4671. #ifdef _WIN32
  4672. int enet_initialize(void) {
  4673. WORD versionRequested = MAKEWORD(1, 1);
  4674. WSADATA wsaData;
  4675. if (WSAStartup(versionRequested, &wsaData)) {
  4676. return -1;
  4677. }
  4678. if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) {
  4679. WSACleanup();
  4680. return -1;
  4681. }
  4682. timeBeginPeriod(1);
  4683. return 0;
  4684. }
  4685. void enet_deinitialize(void) {
  4686. timeEndPeriod(1);
  4687. WSACleanup();
  4688. }
  4689. enet_uint64 enet_host_random_seed(void) {
  4690. return (enet_uint64) timeGetTime();
  4691. }
  4692. int enet_address_set_host_ip_old(ENetAddress *address, const char *name) {
  4693. enet_uint8 vals[4] = { 0, 0, 0, 0 };
  4694. int i;
  4695. for (i = 0; i < 4; ++i) {
  4696. const char *next = name + 1;
  4697. if (*name != '0') {
  4698. long val = strtol(name, (char **) &next, 10);
  4699. if (val < 0 || val > 255 || next == name || next - name > 3) {
  4700. return -1;
  4701. }
  4702. vals[i] = (enet_uint8) val;
  4703. }
  4704. if (*next != (i < 3 ? '.' : '\0')) {
  4705. return -1;
  4706. }
  4707. name = next + 1;
  4708. }
  4709. memcpy(&address->host, vals, sizeof(enet_uint32));
  4710. return 0;
  4711. }
  4712. int enet_address_set_host_old(ENetAddress *address, const char *name) {
  4713. struct hostent *hostEntry = NULL;
  4714. hostEntry = gethostbyname(name);
  4715. if (hostEntry == NULL || hostEntry->h_addrtype != AF_INET) {
  4716. if (!inet_pton(AF_INET6, name, &address->host)) {
  4717. return -1;
  4718. }
  4719. return 0;
  4720. }
  4721. ((enet_uint32 *)&address->host.s6_addr)[0] = 0;
  4722. ((enet_uint32 *)&address->host.s6_addr)[1] = 0;
  4723. ((enet_uint32 *)&address->host.s6_addr)[2] = htonl(0xffff);
  4724. ((enet_uint32 *)&address->host.s6_addr)[3] = *(enet_uint32 *)hostEntry->h_addr_list[0];
  4725. return 0;
  4726. }
  4727. int enet_address_get_host_ip_old(const ENetAddress *address, char *name, size_t nameLength) {
  4728. if (inet_ntop(AF_INET6, (PVOID)&address->host, name, nameLength) == NULL) {
  4729. return -1;
  4730. }
  4731. return 0;
  4732. }
  4733. int enet_address_get_host_old(const ENetAddress *address, char *name, size_t nameLength) {
  4734. struct in6_addr in;
  4735. struct hostent *hostEntry = NULL;
  4736. in = address->host;
  4737. hostEntry = gethostbyaddr((char *)&in, sizeof(struct in6_addr), AF_INET6);
  4738. if (hostEntry == NULL) {
  4739. return enet_address_get_host_ip(address, name, nameLength);
  4740. } else {
  4741. size_t hostLen = strlen(hostEntry->h_name);
  4742. if (hostLen >= nameLength) {
  4743. return -1;
  4744. }
  4745. memcpy(name, hostEntry->h_name, hostLen + 1);
  4746. }
  4747. return 0;
  4748. }
  4749. int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
  4750. struct sockaddr_in6 sin;
  4751. memset(&sin, 0, sizeof(struct sockaddr_in6));
  4752. sin.sin6_family = AF_INET6;
  4753. if (address != NULL) {
  4754. sin.sin6_port = ENET_HOST_TO_NET_16 (address->port);
  4755. sin.sin6_addr = address->host;
  4756. sin.sin6_scope_id = address->sin6_scope_id;
  4757. } else {
  4758. sin.sin6_port = 0;
  4759. sin.sin6_addr = in6addr_any;
  4760. sin.sin6_scope_id = 0;
  4761. }
  4762. return bind(socket, (struct sockaddr *) &sin, sizeof(struct sockaddr_in6)) == SOCKET_ERROR ? -1 : 0;
  4763. }
  4764. int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
  4765. struct sockaddr_in6 sin;
  4766. int sinLength = sizeof(struct sockaddr_in6);
  4767. if (getsockname(socket, (struct sockaddr *) &sin, &sinLength) == -1) {
  4768. return -1;
  4769. }
  4770. address->host = sin.sin6_addr;
  4771. address->port = ENET_NET_TO_HOST_16(sin.sin6_port);
  4772. address->sin6_scope_id = sin.sin6_scope_id;
  4773. return 0;
  4774. }
  4775. int enet_socket_listen(ENetSocket socket, int backlog) {
  4776. return listen(socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0;
  4777. }
  4778. ENetSocket enet_socket_create(ENetSocketType type) {
  4779. return socket(PF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  4780. }
  4781. int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value) {
  4782. int result = SOCKET_ERROR;
  4783. switch (option) {
  4784. case ENET_SOCKOPT_NONBLOCK: {
  4785. u_long nonBlocking = (u_long) value;
  4786. result = ioctlsocket(socket, FIONBIO, &nonBlocking);
  4787. break;
  4788. }
  4789. case ENET_SOCKOPT_BROADCAST:
  4790. result = setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&value, sizeof(int));
  4791. break;
  4792. case ENET_SOCKOPT_REUSEADDR:
  4793. result = setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (char *)&value, sizeof(int));
  4794. break;
  4795. case ENET_SOCKOPT_RCVBUF:
  4796. result = setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char *)&value, sizeof(int));
  4797. break;
  4798. case ENET_SOCKOPT_SNDBUF:
  4799. result = setsockopt(socket, SOL_SOCKET, SO_SNDBUF, (char *)&value, sizeof(int));
  4800. break;
  4801. case ENET_SOCKOPT_RCVTIMEO:
  4802. result = setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&value, sizeof(int));
  4803. break;
  4804. case ENET_SOCKOPT_SNDTIMEO:
  4805. result = setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&value, sizeof(int));
  4806. break;
  4807. case ENET_SOCKOPT_NODELAY:
  4808. result = setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *)&value, sizeof(int));
  4809. break;
  4810. case ENET_SOCKOPT_IPV6_V6ONLY:
  4811. result = setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&value, sizeof(int));
  4812. break;
  4813. default:
  4814. break;
  4815. }
  4816. return result == SOCKET_ERROR ? -1 : 0;
  4817. } /* enet_socket_set_option */
  4818. int enet_socket_get_option(ENetSocket socket, ENetSocketOption option, int *value) {
  4819. int result = SOCKET_ERROR, len;
  4820. switch (option) {
  4821. case ENET_SOCKOPT_ERROR:
  4822. len = sizeof(int);
  4823. result = getsockopt(socket, SOL_SOCKET, SO_ERROR, (char *)value, &len);
  4824. break;
  4825. default:
  4826. break;
  4827. }
  4828. return result == SOCKET_ERROR ? -1 : 0;
  4829. }
  4830. int enet_socket_connect(ENetSocket socket, const ENetAddress *address) {
  4831. struct sockaddr_in6 sin;
  4832. int result;
  4833. memset(&sin, 0, sizeof(struct sockaddr_in6));
  4834. sin.sin6_family = AF_INET6;
  4835. sin.sin6_port = ENET_HOST_TO_NET_16(address->port);
  4836. sin.sin6_addr = address->host;
  4837. sin.sin6_scope_id = address->sin6_scope_id;
  4838. result = connect(socket, (struct sockaddr *) &sin, sizeof(struct sockaddr_in6));
  4839. if (result == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) {
  4840. return -1;
  4841. }
  4842. return 0;
  4843. }
  4844. ENetSocket enet_socket_accept(ENetSocket socket, ENetAddress *address) {
  4845. SOCKET result;
  4846. struct sockaddr_in6 sin;
  4847. int sinLength = sizeof(struct sockaddr_in6);
  4848. result = accept(socket, address != NULL ? (struct sockaddr *)&sin : NULL, address != NULL ? &sinLength : NULL);
  4849. if (result == INVALID_SOCKET) {
  4850. return ENET_SOCKET_NULL;
  4851. }
  4852. if (address != NULL) {
  4853. address->host = sin.sin6_addr;
  4854. address->port = ENET_NET_TO_HOST_16(sin.sin6_port);
  4855. address->sin6_scope_id = sin.sin6_scope_id;
  4856. }
  4857. return result;
  4858. }
  4859. int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how) {
  4860. return shutdown(socket, (int) how) == SOCKET_ERROR ? -1 : 0;
  4861. }
  4862. void enet_socket_destroy(ENetSocket socket) {
  4863. if (socket != INVALID_SOCKET) {
  4864. closesocket(socket);
  4865. }
  4866. }
  4867. int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBuffer *buffers, size_t bufferCount) {
  4868. struct sockaddr_in6 sin;
  4869. DWORD sentLength;
  4870. if (address != NULL) {
  4871. memset(&sin, 0, sizeof(struct sockaddr_in6));
  4872. sin.sin6_family = AF_INET6;
  4873. sin.sin6_port = ENET_HOST_TO_NET_16(address->port);
  4874. sin.sin6_addr = address->host;
  4875. sin.sin6_scope_id = address->sin6_scope_id;
  4876. }
  4877. if (WSASendTo(socket,
  4878. (LPWSABUF) buffers,
  4879. (DWORD) bufferCount,
  4880. &sentLength,
  4881. 0,
  4882. address != NULL ? (struct sockaddr *) &sin : NULL,
  4883. address != NULL ? sizeof(struct sockaddr_in6) : 0,
  4884. NULL,
  4885. NULL) == SOCKET_ERROR
  4886. ) {
  4887. return (WSAGetLastError() == WSAEWOULDBLOCK) ? 0 : -1;
  4888. }
  4889. return (int) sentLength;
  4890. }
  4891. int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
  4892. INT sinLength = sizeof(struct sockaddr_in6);
  4893. DWORD flags = 0, recvLength;
  4894. struct sockaddr_in6 sin;
  4895. if (WSARecvFrom(socket,
  4896. (LPWSABUF) buffers,
  4897. (DWORD) bufferCount,
  4898. &recvLength,
  4899. &flags,
  4900. address != NULL ? (struct sockaddr *) &sin : NULL,
  4901. address != NULL ? &sinLength : NULL,
  4902. NULL,
  4903. NULL) == SOCKET_ERROR
  4904. ) {
  4905. switch (WSAGetLastError()) {
  4906. case WSAEWOULDBLOCK:
  4907. case WSAECONNRESET:
  4908. return 0;
  4909. }
  4910. return -1;
  4911. }
  4912. if (flags & MSG_PARTIAL) {
  4913. return -1;
  4914. }
  4915. if (address != NULL) {
  4916. address->host = sin.sin6_addr;
  4917. address->port = ENET_NET_TO_HOST_16(sin.sin6_port);
  4918. address->sin6_scope_id = sin.sin6_scope_id;
  4919. }
  4920. return (int) recvLength;
  4921. } /* enet_socket_receive */
  4922. int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
  4923. struct timeval timeVal;
  4924. timeVal.tv_sec = timeout / 1000;
  4925. timeVal.tv_usec = (timeout % 1000) * 1000;
  4926. return select(maxSocket + 1, readSet, writeSet, NULL, &timeVal);
  4927. }
  4928. int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint64 timeout) {
  4929. fd_set readSet, writeSet;
  4930. struct timeval timeVal;
  4931. int selectCount;
  4932. timeVal.tv_sec = timeout / 1000;
  4933. timeVal.tv_usec = (timeout % 1000) * 1000;
  4934. FD_ZERO(&readSet);
  4935. FD_ZERO(&writeSet);
  4936. if (*condition & ENET_SOCKET_WAIT_SEND) {
  4937. FD_SET(socket, &writeSet);
  4938. }
  4939. if (*condition & ENET_SOCKET_WAIT_RECEIVE) {
  4940. FD_SET(socket, &readSet);
  4941. }
  4942. selectCount = select(socket + 1, &readSet, &writeSet, NULL, &timeVal);
  4943. if (selectCount < 0) {
  4944. return -1;
  4945. }
  4946. *condition = ENET_SOCKET_WAIT_NONE;
  4947. if (selectCount == 0) {
  4948. return 0;
  4949. }
  4950. if (FD_ISSET(socket, &writeSet)) {
  4951. *condition |= ENET_SOCKET_WAIT_SEND;
  4952. }
  4953. if (FD_ISSET(socket, &readSet)) {
  4954. *condition |= ENET_SOCKET_WAIT_RECEIVE;
  4955. }
  4956. return 0;
  4957. } /* enet_socket_wait */
  4958. #endif // _WIN32
  4959. #ifdef __cplusplus
  4960. }
  4961. #endif
  4962. #endif // ENET_IMPLEMENTATION
  4963. #endif // ENET_INCLUDE_H