enet.h 229 KB

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