enet.h 228 KB

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