NetPacket.cpp 183 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. ////////// NetPacket.cpp ///////////////////////////
  24. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  25. #include "GameNetwork/NetPacket.h"
  26. #include "GameNetwork/NetCommandMsg.h"
  27. #include "GameNetwork/NetworkDefs.h"
  28. #include "GameNetwork/NetworkUtil.h"
  29. #include "GameNetwork/GameMessageParser.h"
  30. #ifdef _INTERNAL
  31. // for occasional debugging...
  32. //#pragma optimize("", off)
  33. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  34. #endif
  35. // This function assumes that all of the fields are either of default value or are
  36. // present in the raw data.
  37. NetCommandRef * NetPacket::ConstructNetCommandMsgFromRawData(UnsignedByte *data, UnsignedShort dataLength) {
  38. NetCommandType commandType = NETCOMMANDTYPE_GAMECOMMAND;
  39. UnsignedShort commandID = 0;
  40. UnsignedInt frame = 0;
  41. UnsignedByte playerID = 0;
  42. UnsignedByte relay = 0;
  43. Int offset = 0;
  44. Bool notDone = TRUE;
  45. NetCommandRef *ref = NULL;
  46. NetCommandMsg *msg = NULL;
  47. while ((offset < (Int)dataLength) && notDone) {
  48. if (data[offset] == 'T') {
  49. ++offset;
  50. memcpy(&commandType, data + offset, sizeof(UnsignedByte));
  51. offset += sizeof(UnsignedByte);
  52. } else if (data[offset] == 'R') {
  53. ++offset;
  54. memcpy(&relay, data + offset, sizeof(UnsignedByte));
  55. offset += sizeof(UnsignedByte);
  56. } else if (data[offset] == 'P') {
  57. ++offset;
  58. memcpy(&playerID, data + offset, sizeof(UnsignedByte));
  59. offset += sizeof(UnsignedByte);
  60. } else if (data[offset] == 'C') {
  61. ++offset;
  62. memcpy(&commandID, data + offset, sizeof(UnsignedShort));
  63. offset += sizeof(UnsignedShort);
  64. } else if (data[offset] == 'F') {
  65. ++offset;
  66. memcpy(&frame, data + offset, sizeof(UnsignedInt));
  67. offset += sizeof(UnsignedInt);
  68. } else if (data[offset] == 'D') {
  69. ++offset;
  70. if (commandType == NETCOMMANDTYPE_GAMECOMMAND) {
  71. msg = readGameMessage(data, offset);
  72. } else if (commandType == NETCOMMANDTYPE_ACKBOTH) {
  73. msg = readAckBothMessage(data, offset);
  74. } else if (commandType == NETCOMMANDTYPE_ACKSTAGE1) {
  75. msg = readAckStage1Message(data, offset);
  76. } else if (commandType == NETCOMMANDTYPE_ACKSTAGE2) {
  77. msg = readAckStage2Message(data, offset);
  78. } else if (commandType == NETCOMMANDTYPE_FRAMEINFO) {
  79. msg = readFrameMessage(data, offset);
  80. } else if (commandType == NETCOMMANDTYPE_PLAYERLEAVE) {
  81. msg = readPlayerLeaveMessage(data, offset);
  82. } else if (commandType == NETCOMMANDTYPE_RUNAHEADMETRICS) {
  83. msg = readRunAheadMetricsMessage(data, offset);
  84. } else if (commandType == NETCOMMANDTYPE_RUNAHEAD) {
  85. msg = readRunAheadMessage(data, offset);
  86. } else if (commandType == NETCOMMANDTYPE_DESTROYPLAYER) {
  87. msg = readDestroyPlayerMessage(data, offset);
  88. } else if (commandType == NETCOMMANDTYPE_KEEPALIVE) {
  89. msg = readKeepAliveMessage(data, offset);
  90. } else if (commandType == NETCOMMANDTYPE_DISCONNECTKEEPALIVE) {
  91. msg = readDisconnectKeepAliveMessage(data, offset);
  92. } else if (commandType == NETCOMMANDTYPE_DISCONNECTPLAYER) {
  93. msg = readDisconnectPlayerMessage(data, offset);
  94. } else if (commandType == NETCOMMANDTYPE_PACKETROUTERQUERY) {
  95. msg = readPacketRouterQueryMessage(data, offset);
  96. } else if (commandType == NETCOMMANDTYPE_PACKETROUTERACK) {
  97. msg = readPacketRouterAckMessage(data, offset);
  98. } else if (commandType == NETCOMMANDTYPE_DISCONNECTCHAT) {
  99. msg = readDisconnectChatMessage(data, offset);
  100. } else if (commandType == NETCOMMANDTYPE_DISCONNECTVOTE) {
  101. msg = readDisconnectVoteMessage(data, offset);
  102. } else if (commandType == NETCOMMANDTYPE_CHAT) {
  103. msg = readChatMessage(data, offset);
  104. } else if (commandType == NETCOMMANDTYPE_PROGRESS) {
  105. msg = readProgressMessage(data, offset);
  106. } else if (commandType == NETCOMMANDTYPE_LOADCOMPLETE) {
  107. msg = readLoadCompleteMessage(data, offset);
  108. } else if (commandType == NETCOMMANDTYPE_TIMEOUTSTART) {
  109. msg = readTimeOutGameStartMessage(data, offset);
  110. } else if (commandType == NETCOMMANDTYPE_WRAPPER) {
  111. msg = readWrapperMessage(data, offset);
  112. } else if (commandType == NETCOMMANDTYPE_FILE) {
  113. msg = readFileMessage(data, offset);
  114. } else if (commandType == NETCOMMANDTYPE_FILEANNOUNCE) {
  115. msg = readFileAnnounceMessage(data, offset);
  116. } else if (commandType == NETCOMMANDTYPE_FILEPROGRESS) {
  117. msg = readFileProgressMessage(data, offset);
  118. } else if (commandType == NETCOMMANDTYPE_DISCONNECTFRAME) {
  119. msg = readDisconnectFrameMessage(data, offset);
  120. } else if (commandType == NETCOMMANDTYPE_DISCONNECTSCREENOFF) {
  121. msg = readDisconnectScreenOffMessage(data, offset);
  122. } else if (commandType == NETCOMMANDTYPE_FRAMERESENDREQUEST) {
  123. msg = readFrameResendRequestMessage(data, offset);
  124. }
  125. msg->setExecutionFrame(frame);
  126. msg->setID(commandID);
  127. msg->setPlayerID(playerID);
  128. msg->setNetCommandType(commandType);
  129. ref = NEW_NETCOMMANDREF(msg);
  130. ref->setRelay(relay);
  131. msg->detach();
  132. msg = NULL;
  133. notDone = FALSE;
  134. }
  135. }
  136. return ref;
  137. }
  138. NetPacketList NetPacket::ConstructBigCommandPacketList(NetCommandRef *ref) {
  139. // if we don't have a unique command ID, then the wrapped command cannot
  140. // be identified. Therefore don't allow commands without a unique ID to
  141. // be wrapped.
  142. NetCommandMsg *msg = ref->getCommand();
  143. if (!DoesCommandRequireACommandID(msg->getNetCommandType())) {
  144. DEBUG_CRASH(("Trying to wrap a command that doesn't have a unique command ID"));
  145. return NULL;
  146. }
  147. UnsignedInt bufferSize = GetBufferSizeNeededForCommand(msg); // need to implement. I have a drinking problem.
  148. UnsignedByte *bigPacketData = NULL;
  149. NetPacketList packetList;
  150. // create the buffer for the huge message and fill the buffer with that message.
  151. UnsignedInt bigPacketCurrentOffset = 0;
  152. bigPacketData = NEW UnsignedByte[bufferSize];
  153. FillBufferWithCommand(bigPacketData, ref);
  154. // create the wrapper command message we'll be using.
  155. NetWrapperCommandMsg *wrapperMsg = newInstance(NetWrapperCommandMsg);
  156. // get the amount of space needed for the wrapper message, not including the wrapped command data.
  157. UnsignedInt wrapperSize = GetBufferSizeNeededForCommand(wrapperMsg);
  158. UnsignedInt commandSizePerPacket = MAX_PACKET_SIZE - wrapperSize;
  159. UnsignedInt numChunks = bufferSize / commandSizePerPacket;
  160. if ((bufferSize % commandSizePerPacket) > 0) {
  161. ++numChunks;
  162. }
  163. UnsignedInt currentChunk = 0;
  164. // create the packets and the wrapper messages.
  165. while (currentChunk < numChunks) {
  166. NetPacket *packet = newInstance(NetPacket);
  167. UnsignedShort dataSizeThisPacket = commandSizePerPacket;
  168. if ((bufferSize - bigPacketCurrentOffset) < dataSizeThisPacket) {
  169. dataSizeThisPacket = bufferSize - bigPacketCurrentOffset;
  170. }
  171. if (DoesCommandRequireACommandID(wrapperMsg->getNetCommandType())) {
  172. wrapperMsg->setID(GenerateNextCommandID());
  173. }
  174. wrapperMsg->setPlayerID(msg->getPlayerID());
  175. wrapperMsg->setExecutionFrame(msg->getExecutionFrame());
  176. wrapperMsg->setChunkNumber(currentChunk);
  177. wrapperMsg->setNumChunks(numChunks);
  178. wrapperMsg->setDataOffset(bigPacketCurrentOffset);
  179. wrapperMsg->setData(bigPacketData + bigPacketCurrentOffset, dataSizeThisPacket);
  180. wrapperMsg->setTotalDataLength(bufferSize);
  181. wrapperMsg->setWrappedCommandID(msg->getID());
  182. bigPacketCurrentOffset += dataSizeThisPacket;
  183. NetCommandRef * ref = NEW_NETCOMMANDREF(wrapperMsg);
  184. ref->setRelay(ref->getRelay());
  185. if (packet->addCommand(ref) == FALSE) {
  186. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::BeginBigCommandPacketList - failed to add a wrapper command to the packet\n")); // I still have a drinking problem.
  187. }
  188. packetList.push_back(packet);
  189. ref->deleteInstance();
  190. ref = NULL;
  191. ++currentChunk;
  192. }
  193. wrapperMsg->detach();
  194. wrapperMsg = NULL;
  195. delete bigPacketData;
  196. bigPacketData = NULL;
  197. return packetList;
  198. }
  199. UnsignedInt NetPacket::GetBufferSizeNeededForCommand(NetCommandMsg *msg) {
  200. // This is where the fun begins...
  201. if (msg == NULL) {
  202. return TRUE; // There was nothing to add, so it was successful.
  203. }
  204. switch(msg->getNetCommandType())
  205. {
  206. case NETCOMMANDTYPE_GAMECOMMAND:
  207. return GetGameCommandSize(msg);
  208. case NETCOMMANDTYPE_ACKSTAGE1:
  209. case NETCOMMANDTYPE_ACKSTAGE2:
  210. case NETCOMMANDTYPE_ACKBOTH:
  211. return GetAckCommandSize(msg);
  212. case NETCOMMANDTYPE_FRAMEINFO:
  213. return GetFrameCommandSize(msg);
  214. case NETCOMMANDTYPE_PLAYERLEAVE:
  215. return GetPlayerLeaveCommandSize(msg);
  216. case NETCOMMANDTYPE_RUNAHEADMETRICS:
  217. return GetRunAheadMetricsCommandSize(msg);
  218. case NETCOMMANDTYPE_RUNAHEAD:
  219. return GetRunAheadCommandSize(msg);
  220. case NETCOMMANDTYPE_DESTROYPLAYER:
  221. return GetDestroyPlayerCommandSize(msg);
  222. case NETCOMMANDTYPE_KEEPALIVE:
  223. return GetKeepAliveCommandSize(msg);
  224. case NETCOMMANDTYPE_DISCONNECTKEEPALIVE:
  225. return GetDisconnectKeepAliveCommandSize(msg);
  226. case NETCOMMANDTYPE_DISCONNECTPLAYER:
  227. return GetDisconnectPlayerCommandSize(msg);
  228. case NETCOMMANDTYPE_PACKETROUTERQUERY:
  229. return GetPacketRouterQueryCommandSize(msg);
  230. case NETCOMMANDTYPE_PACKETROUTERACK:
  231. return GetPacketRouterAckCommandSize(msg);
  232. case NETCOMMANDTYPE_DISCONNECTCHAT:
  233. return GetDisconnectChatCommandSize(msg);
  234. case NETCOMMANDTYPE_DISCONNECTVOTE:
  235. return GetDisconnectVoteCommandSize(msg);
  236. case NETCOMMANDTYPE_CHAT:
  237. return GetChatCommandSize(msg);
  238. case NETCOMMANDTYPE_PROGRESS:
  239. return GetProgressMessageSize(msg);
  240. case NETCOMMANDTYPE_LOADCOMPLETE:
  241. return GetLoadCompleteMessageSize(msg);
  242. case NETCOMMANDTYPE_TIMEOUTSTART:
  243. return GetTimeOutGameStartMessageSize(msg);
  244. case NETCOMMANDTYPE_WRAPPER:
  245. return GetWrapperCommandSize(msg);
  246. case NETCOMMANDTYPE_FILE:
  247. return GetFileCommandSize(msg);
  248. case NETCOMMANDTYPE_FILEANNOUNCE:
  249. return GetFileAnnounceCommandSize(msg);
  250. case NETCOMMANDTYPE_FILEPROGRESS:
  251. return GetFileProgressCommandSize(msg);
  252. case NETCOMMANDTYPE_DISCONNECTFRAME:
  253. return GetDisconnectFrameCommandSize(msg);
  254. case NETCOMMANDTYPE_DISCONNECTSCREENOFF:
  255. return GetDisconnectScreenOffCommandSize(msg);
  256. case NETCOMMANDTYPE_FRAMERESENDREQUEST:
  257. return GetFrameResendRequestCommandSize(msg);
  258. default:
  259. DEBUG_CRASH(("Unknown NETCOMMANDTYPE %d", msg->getNetCommandType()));
  260. break;
  261. }
  262. return 0;
  263. }
  264. UnsignedInt NetPacket::GetGameCommandSize(NetCommandMsg *msg) {
  265. NetGameCommandMsg *cmdMsg = (NetGameCommandMsg *)msg;
  266. UnsignedShort msglen = 0;
  267. msglen += sizeof(UnsignedInt) + sizeof(UnsignedByte); // frame number
  268. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // player ID
  269. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // relay
  270. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // command type
  271. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte); // command ID
  272. msglen += sizeof(UnsignedByte); // the 'D' for the data section.
  273. GameMessage *gmsg = cmdMsg->constructGameMessage();
  274. GameMessageParser *parser = newInstance(GameMessageParser)(gmsg);
  275. msglen += sizeof(GameMessage::Type);
  276. msglen += sizeof(UnsignedByte);
  277. // Int numTypes = parser->getNumTypes();
  278. GameMessageParserArgumentType *arg = parser->getFirstArgumentType();
  279. while (arg != NULL) {
  280. msglen += 2 * sizeof(UnsignedByte); // for the type and number of args of that type declaration.
  281. GameMessageArgumentDataType type = arg->getType();
  282. if (type == ARGUMENTDATATYPE_INTEGER) {
  283. msglen += arg->getArgCount() * sizeof(Int);
  284. } else if (type == ARGUMENTDATATYPE_REAL) {
  285. msglen += arg->getArgCount() * sizeof(Real);
  286. } else if (type == ARGUMENTDATATYPE_BOOLEAN) {
  287. msglen += arg->getArgCount() * sizeof(Bool);
  288. } else if (type == ARGUMENTDATATYPE_OBJECTID) {
  289. msglen += arg->getArgCount() * sizeof(ObjectID);
  290. } else if (type == ARGUMENTDATATYPE_DRAWABLEID) {
  291. msglen += arg->getArgCount() * sizeof(DrawableID);
  292. } else if (type == ARGUMENTDATATYPE_TEAMID) {
  293. msglen += arg->getArgCount() * sizeof(UnsignedInt);
  294. } else if (type == ARGUMENTDATATYPE_LOCATION) {
  295. msglen += arg->getArgCount() * sizeof(Coord3D);
  296. } else if (type == ARGUMENTDATATYPE_PIXEL) {
  297. msglen += arg->getArgCount() * sizeof(ICoord2D);
  298. } else if (type == ARGUMENTDATATYPE_PIXELREGION) {
  299. msglen += arg->getArgCount() * sizeof(IRegion2D);
  300. } else if (type == ARGUMENTDATATYPE_TIMESTAMP) {
  301. msglen += arg->getArgCount() * sizeof(UnsignedInt);
  302. } else if (type == ARGUMENTDATATYPE_WIDECHAR) {
  303. msglen += arg->getArgCount() * sizeof(WideChar);
  304. }
  305. arg = arg->getNext();
  306. }
  307. parser->deleteInstance();
  308. parser = NULL;
  309. gmsg->deleteInstance();
  310. gmsg = NULL;
  311. return msglen;
  312. }
  313. UnsignedInt NetPacket::GetAckCommandSize(NetCommandMsg *msg) {
  314. Int msglen = 0;
  315. ++msglen;
  316. msglen += sizeof(UnsignedByte);
  317. ++msglen;
  318. msglen += sizeof(UnsignedByte);
  319. ++msglen;
  320. msglen += sizeof(UnsignedShort);
  321. msglen += sizeof(UnsignedByte);
  322. return msglen;
  323. }
  324. UnsignedInt NetPacket::GetFrameCommandSize(NetCommandMsg *msg) {
  325. Int msglen = 0;
  326. ++msglen;
  327. msglen += sizeof(UnsignedByte);
  328. msglen += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  329. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  330. ++msglen;
  331. msglen += sizeof(UnsignedByte);
  332. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  333. ++msglen;
  334. msglen += sizeof(UnsignedShort);
  335. return msglen;
  336. }
  337. UnsignedInt NetPacket::GetPlayerLeaveCommandSize(NetCommandMsg *msg) {
  338. Int msglen = 0;
  339. ++msglen;
  340. msglen += sizeof(UnsignedByte);
  341. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  342. msglen += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  343. ++msglen;
  344. msglen += sizeof(UnsignedByte);
  345. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  346. ++msglen;
  347. msglen += sizeof(UnsignedByte);
  348. return msglen;
  349. }
  350. UnsignedInt NetPacket::GetRunAheadMetricsCommandSize(NetCommandMsg *msg) {
  351. Int msglen = 0;
  352. ++msglen;
  353. msglen += sizeof(UnsignedByte);
  354. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  355. ++msglen;
  356. msglen += sizeof(UnsignedByte);
  357. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  358. ++msglen;
  359. msglen += sizeof(UnsignedByte);
  360. msglen += sizeof(Real);
  361. return msglen;
  362. }
  363. UnsignedInt NetPacket::GetRunAheadCommandSize(NetCommandMsg *msg) {
  364. Int msglen = 0;
  365. ++msglen;
  366. msglen += sizeof(UnsignedByte);
  367. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  368. msglen += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  369. ++msglen;
  370. msglen += sizeof(UnsignedByte);
  371. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  372. ++msglen;
  373. msglen += sizeof(UnsignedShort);
  374. msglen += sizeof(UnsignedByte);
  375. return msglen;
  376. }
  377. UnsignedInt NetPacket::GetDestroyPlayerCommandSize(NetCommandMsg *msg) {
  378. Int msglen = 0;
  379. ++msglen;
  380. msglen += sizeof(UnsignedByte);
  381. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  382. msglen += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  383. ++msglen;
  384. msglen += sizeof(UnsignedByte);
  385. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  386. ++msglen;
  387. msglen += sizeof(UnsignedInt);
  388. return msglen;
  389. }
  390. UnsignedInt NetPacket::GetKeepAliveCommandSize(NetCommandMsg *msg) {
  391. Int msglen = 0;
  392. ++msglen;
  393. msglen += sizeof(UnsignedByte);
  394. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  395. ++msglen;
  396. msglen += sizeof(UnsignedByte);
  397. ++msglen; // For the 'D'
  398. return msglen;
  399. }
  400. UnsignedInt NetPacket::GetDisconnectKeepAliveCommandSize(NetCommandMsg *msg) {
  401. Int msglen = 0;
  402. ++msglen;
  403. msglen += sizeof(UnsignedByte);
  404. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  405. ++msglen;
  406. msglen += sizeof(UnsignedByte);
  407. ++msglen; // For the 'D'
  408. return msglen;
  409. }
  410. UnsignedInt NetPacket::GetDisconnectPlayerCommandSize(NetCommandMsg *msg) {
  411. Int msglen = 0;
  412. ++msglen;
  413. msglen += sizeof(UnsignedByte);
  414. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  415. ++msglen;
  416. msglen += sizeof(UnsignedByte);
  417. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  418. ++msglen; // the 'D'
  419. msglen += sizeof(UnsignedByte); // slot number
  420. msglen += sizeof(UnsignedInt); // disconnect frame
  421. return msglen;
  422. }
  423. UnsignedInt NetPacket::GetPacketRouterQueryCommandSize(NetCommandMsg *msg) {
  424. Int msglen = 0;
  425. ++msglen;
  426. msglen += sizeof(UnsignedByte);
  427. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  428. ++msglen;
  429. msglen += sizeof(UnsignedByte);
  430. ++msglen; // the 'D'
  431. return msglen;
  432. }
  433. UnsignedInt NetPacket::GetPacketRouterAckCommandSize(NetCommandMsg *msg) {
  434. Int msglen = 0;
  435. ++msglen;
  436. msglen += sizeof(UnsignedByte);
  437. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  438. ++msglen;
  439. msglen += sizeof(UnsignedByte);
  440. ++msglen; // the 'D'
  441. return msglen;
  442. }
  443. UnsignedInt NetPacket::GetDisconnectChatCommandSize(NetCommandMsg *msg) {
  444. Int msglen = 0;
  445. NetDisconnectChatCommandMsg *cmdMsg = (NetDisconnectChatCommandMsg *)(msg);
  446. ++msglen;
  447. msglen += sizeof(UnsignedByte);
  448. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  449. ++msglen;
  450. msglen += sizeof(UnsignedByte);
  451. ++msglen; // the 'D'
  452. msglen += sizeof(UnsignedByte); // string msglength
  453. UnsignedByte textmsglen = cmdMsg->getText().getLength();
  454. msglen += textmsglen * sizeof(UnsignedShort);
  455. return msglen;
  456. }
  457. UnsignedInt NetPacket::GetDisconnectVoteCommandSize(NetCommandMsg *msg) {
  458. Int msglen = 0;
  459. ++msglen;
  460. msglen += sizeof(UnsignedByte);
  461. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  462. ++msglen;
  463. msglen += sizeof(UnsignedByte);
  464. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  465. ++msglen; // the 'D'
  466. msglen += sizeof(UnsignedByte); // slot number
  467. msglen += sizeof(UnsignedInt); // vote frame.
  468. return msglen;
  469. }
  470. UnsignedInt NetPacket::GetChatCommandSize(NetCommandMsg *msg) {
  471. Int msglen = 0;
  472. NetChatCommandMsg *cmdMsg = (NetChatCommandMsg *)(msg);
  473. ++msglen;
  474. msglen += sizeof(UnsignedByte);
  475. msglen += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  476. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  477. ++msglen;
  478. msglen += sizeof(UnsignedByte);
  479. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  480. ++msglen; // the 'D'
  481. msglen += sizeof(UnsignedByte); // string msglength
  482. UnsignedByte textmsglen = cmdMsg->getText().getLength();
  483. msglen += textmsglen * sizeof(UnsignedShort);
  484. msglen += sizeof(Int); // playerMask
  485. return msglen;
  486. }
  487. UnsignedInt NetPacket::GetProgressMessageSize(NetCommandMsg *msg) {
  488. Int msglen = 0;
  489. ++msglen;
  490. msglen += sizeof(UnsignedByte);
  491. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  492. ++msglen;
  493. msglen += sizeof(UnsignedByte);
  494. ++msglen; // For the 'D'
  495. ++msglen; // percentage
  496. return msglen;
  497. }
  498. UnsignedInt NetPacket::GetLoadCompleteMessageSize(NetCommandMsg *msg) {
  499. Int msglen = 0;
  500. ++msglen;
  501. msglen += sizeof(UnsignedByte);
  502. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  503. ++msglen;
  504. msglen += sizeof(UnsignedByte);
  505. ++msglen; // For the 'D'
  506. return msglen;
  507. }
  508. UnsignedInt NetPacket::GetTimeOutGameStartMessageSize(NetCommandMsg *msg) {
  509. Int msglen = 0;
  510. ++msglen;
  511. msglen += sizeof(UnsignedByte);
  512. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  513. ++msglen;
  514. msglen += sizeof(UnsignedByte);
  515. ++msglen; // For the 'D'
  516. return msglen;
  517. }
  518. // type, player, ID, relay, Data
  519. UnsignedInt NetPacket::GetWrapperCommandSize(NetCommandMsg *msg) {
  520. UnsignedInt msglen = 0;
  521. ++msglen; // 'T'
  522. msglen += sizeof(UnsignedByte); // command type
  523. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'P' and player ID
  524. msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // 'C' and command ID
  525. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'R' and relay
  526. ++msglen; // 'D'
  527. msglen += sizeof(UnsignedShort); // m_wrappedCommandID
  528. msglen += sizeof(UnsignedInt); // m_chunkNumber
  529. msglen += sizeof(UnsignedInt); // m_numChunks
  530. msglen += sizeof(UnsignedInt); // m_totalDataLength
  531. msglen += sizeof(UnsignedInt); // m_dataLength
  532. msglen += sizeof(UnsignedInt); // m_dataOffset
  533. return msglen;
  534. }
  535. UnsignedInt NetPacket::GetFileCommandSize(NetCommandMsg *msg) {
  536. NetFileCommandMsg *filemsg = (NetFileCommandMsg *)msg;
  537. UnsignedInt msglen = 0;
  538. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'T' and command type
  539. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'P' and player ID
  540. msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // 'C' and command ID
  541. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'R' and relay
  542. ++msglen; // 'D'
  543. msglen += filemsg->getPortableFilename().getLength() + 1; // PORTABLE filename and the terminating 0
  544. msglen += sizeof(UnsignedInt); // file data length
  545. msglen += filemsg->getFileLength(); // the file data
  546. return msglen;
  547. }
  548. UnsignedInt NetPacket::GetFileAnnounceCommandSize(NetCommandMsg *msg) {
  549. NetFileAnnounceCommandMsg *filemsg = (NetFileAnnounceCommandMsg *)msg;
  550. UnsignedInt msglen = 0;
  551. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'T' and command type
  552. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'P' and player ID
  553. msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // 'C' and command ID
  554. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'R' and relay
  555. ++msglen; // 'D'
  556. msglen += filemsg->getPortableFilename().getLength() + 1; // PORTABLE filename and the terminating 0
  557. msglen += sizeof(UnsignedShort); // m_fileID
  558. msglen += sizeof(UnsignedByte); // m_playerMask
  559. return msglen;
  560. }
  561. UnsignedInt NetPacket::GetFileProgressCommandSize(NetCommandMsg *msg) {
  562. UnsignedInt msglen = 0;
  563. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'T' and command type
  564. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'P' and player ID
  565. msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // 'C' and command ID
  566. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'R' and relay
  567. ++msglen; // 'D'
  568. msglen += sizeof(UnsignedShort); // m_fileID
  569. msglen += sizeof(Int); // m_progress
  570. return msglen;
  571. }
  572. UnsignedInt NetPacket::GetDisconnectFrameCommandSize(NetCommandMsg *msg) {
  573. UnsignedInt msglen = 0;
  574. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'T' and command type
  575. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'P' and player ID
  576. msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // 'C' and command ID
  577. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'R' and relay
  578. ++msglen; // 'D'
  579. msglen += sizeof(UnsignedInt); // disconnect frame
  580. return msglen;
  581. }
  582. UnsignedInt NetPacket::GetDisconnectScreenOffCommandSize(NetCommandMsg *msg) {
  583. UnsignedInt msglen = 0;
  584. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'T' and command type
  585. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'P' and player ID
  586. msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // 'C' and command ID
  587. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'R' and relay
  588. ++msglen; // 'D'
  589. msglen += sizeof(UnsignedInt); // new frame
  590. return msglen;
  591. }
  592. UnsignedInt NetPacket::GetFrameResendRequestCommandSize(NetCommandMsg *msg) {
  593. UnsignedInt msglen = 0;
  594. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'T' and command type
  595. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'P' and player ID
  596. msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // 'C' and command ID
  597. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // 'R' and relay
  598. ++msglen; // 'D'
  599. msglen += sizeof(UnsignedInt); // frame to resend
  600. return msglen;
  601. }
  602. // this function assumes that buffer is already the correct size.
  603. void NetPacket::FillBufferWithCommand(UnsignedByte *buffer, NetCommandRef *ref) {
  604. NetCommandMsg *msg = ref->getCommand();
  605. switch(msg->getNetCommandType())
  606. {
  607. case NETCOMMANDTYPE_GAMECOMMAND:
  608. FillBufferWithGameCommand(buffer, ref);
  609. break;
  610. case NETCOMMANDTYPE_ACKSTAGE1:
  611. case NETCOMMANDTYPE_ACKSTAGE2:
  612. case NETCOMMANDTYPE_ACKBOTH:
  613. FillBufferWithAckCommand(buffer, ref);
  614. break;
  615. case NETCOMMANDTYPE_FRAMEINFO:
  616. FillBufferWithFrameCommand(buffer, ref);
  617. break;
  618. case NETCOMMANDTYPE_PLAYERLEAVE:
  619. FillBufferWithPlayerLeaveCommand(buffer, ref);
  620. break;
  621. case NETCOMMANDTYPE_RUNAHEADMETRICS:
  622. FillBufferWithRunAheadMetricsCommand(buffer, ref);
  623. break;
  624. case NETCOMMANDTYPE_RUNAHEAD:
  625. FillBufferWithRunAheadCommand(buffer, ref);
  626. break;
  627. case NETCOMMANDTYPE_DESTROYPLAYER:
  628. FillBufferWithDestroyPlayerCommand(buffer, ref);
  629. break;
  630. case NETCOMMANDTYPE_KEEPALIVE:
  631. FillBufferWithKeepAliveCommand(buffer, ref);
  632. break;
  633. case NETCOMMANDTYPE_DISCONNECTKEEPALIVE:
  634. FillBufferWithDisconnectKeepAliveCommand(buffer, ref);
  635. break;
  636. case NETCOMMANDTYPE_DISCONNECTPLAYER:
  637. FillBufferWithDisconnectPlayerCommand(buffer, ref);
  638. break;
  639. case NETCOMMANDTYPE_PACKETROUTERQUERY:
  640. FillBufferWithPacketRouterQueryCommand(buffer, ref);
  641. break;
  642. case NETCOMMANDTYPE_PACKETROUTERACK:
  643. FillBufferWithPacketRouterAckCommand(buffer, ref);
  644. break;
  645. case NETCOMMANDTYPE_DISCONNECTCHAT:
  646. FillBufferWithDisconnectChatCommand(buffer, ref);
  647. break;
  648. case NETCOMMANDTYPE_DISCONNECTVOTE:
  649. FillBufferWithDisconnectVoteCommand(buffer, ref);
  650. break;
  651. case NETCOMMANDTYPE_CHAT:
  652. FillBufferWithChatCommand(buffer, ref);
  653. break;
  654. case NETCOMMANDTYPE_PROGRESS:
  655. FillBufferWithProgressMessage(buffer, ref);
  656. break;
  657. case NETCOMMANDTYPE_LOADCOMPLETE:
  658. FillBufferWithLoadCompleteMessage(buffer, ref);
  659. break;
  660. case NETCOMMANDTYPE_TIMEOUTSTART:
  661. FillBufferWithTimeOutGameStartMessage(buffer, ref);
  662. break;
  663. case NETCOMMANDTYPE_FILE:
  664. FillBufferWithFileMessage(buffer, ref);
  665. break;
  666. case NETCOMMANDTYPE_FILEANNOUNCE:
  667. FillBufferWithFileAnnounceMessage(buffer, ref);
  668. break;
  669. case NETCOMMANDTYPE_FILEPROGRESS:
  670. FillBufferWithFileProgressMessage(buffer, ref);
  671. break;
  672. case NETCOMMANDTYPE_DISCONNECTFRAME:
  673. FillBufferWithDisconnectFrameMessage(buffer, ref);
  674. break;
  675. case NETCOMMANDTYPE_DISCONNECTSCREENOFF:
  676. FillBufferWithDisconnectScreenOffMessage(buffer, ref);
  677. break;
  678. case NETCOMMANDTYPE_FRAMERESENDREQUEST:
  679. FillBufferWithFrameResendRequestMessage(buffer, ref);
  680. break;
  681. default:
  682. DEBUG_CRASH(("Unknown NETCOMMANDTYPE %d", msg->getNetCommandType()));
  683. break;
  684. }
  685. }
  686. void NetPacket::FillBufferWithGameCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  687. NetGameCommandMsg *cmdMsg = (NetGameCommandMsg *)(msg->getCommand());
  688. UnsignedShort offset = 0;
  689. // get the game message from the NetCommandMsg
  690. GameMessage *gmsg = cmdMsg->constructGameMessage();
  691. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::FillBufferWithGameCommand for command ID %d\n", cmdMsg->getID()));
  692. // If necessary, put the NetCommandType into the packet.
  693. buffer[offset] = 'T';
  694. ++offset;
  695. buffer[offset] = cmdMsg->getNetCommandType();
  696. offset += sizeof(UnsignedByte);
  697. // If necessary, put the execution frame into the packet.
  698. buffer[offset] = 'F';
  699. ++offset;
  700. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  701. memcpy(buffer+offset, &newframe, sizeof(UnsignedInt));
  702. offset += sizeof(UnsignedInt);
  703. // If necessary, put the relay into the packet.
  704. buffer[offset] = 'R';
  705. ++offset;
  706. UnsignedByte newRelay = msg->getRelay();
  707. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  708. offset += sizeof(UnsignedByte);
  709. // If necessary, put the playerID into the packet.
  710. buffer[offset] = 'P';
  711. ++offset;
  712. buffer[offset] = cmdMsg->getPlayerID();
  713. offset += sizeof(UnsignedByte);
  714. // If necessary, specify the command ID of this command.
  715. buffer[offset] = 'C';
  716. ++offset;
  717. UnsignedShort newID = cmdMsg->getID();
  718. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  719. offset += sizeof(UnsignedShort);
  720. buffer[offset] = 'D';
  721. ++offset;
  722. // Now copy the GameMessage type into the packet.
  723. GameMessage::Type newType = gmsg->getType();
  724. memcpy(buffer + offset, &newType, sizeof(GameMessage::Type));
  725. offset += sizeof(GameMessage::Type);
  726. GameMessageParser *parser = newInstance(GameMessageParser)(gmsg);
  727. UnsignedByte numTypes = parser->getNumTypes();
  728. memcpy(buffer + offset, &numTypes, sizeof(numTypes));
  729. offset += sizeof(numTypes);
  730. GameMessageParserArgumentType *argType = parser->getFirstArgumentType();
  731. while (argType != NULL) {
  732. UnsignedByte type = (UnsignedByte)(argType->getType());
  733. memcpy(buffer + offset, &type, sizeof(type));
  734. offset += sizeof(type);
  735. UnsignedByte argTypeCount = argType->getArgCount();
  736. memcpy(buffer + offset, &argTypeCount, sizeof(argTypeCount));
  737. offset += sizeof(argTypeCount);
  738. argType = argType->getNext();
  739. }
  740. Int numArgs = gmsg->getArgumentCount();
  741. for (Int i = 0; i < numArgs; ++i) {
  742. GameMessageArgumentDataType type = gmsg->getArgumentDataType(i);
  743. GameMessageArgumentType arg = *(gmsg->getArgument(i));
  744. // writeGameMessageArgumentToPacket(type, arg);
  745. if (type == ARGUMENTDATATYPE_INTEGER) {
  746. memcpy(buffer + offset, &(arg.integer), sizeof(arg.integer));
  747. offset += sizeof(arg.integer);
  748. } else if (type == ARGUMENTDATATYPE_REAL) {
  749. memcpy(buffer + offset, &(arg.real), sizeof(arg.real));
  750. offset += sizeof(arg.real);
  751. } else if (type == ARGUMENTDATATYPE_BOOLEAN) {
  752. memcpy(buffer + offset, &(arg.boolean), sizeof(arg.boolean));
  753. offset += sizeof(arg.boolean);
  754. } else if (type == ARGUMENTDATATYPE_OBJECTID) {
  755. memcpy(buffer + offset, &(arg.objectID), sizeof(arg.objectID));
  756. offset += sizeof(arg.objectID);
  757. } else if (type == ARGUMENTDATATYPE_DRAWABLEID) {
  758. memcpy(buffer + offset, &(arg.drawableID), sizeof(arg.drawableID));
  759. offset += sizeof(arg.drawableID);
  760. } else if (type == ARGUMENTDATATYPE_TEAMID) {
  761. memcpy(buffer + offset, &(arg.teamID), sizeof(arg.teamID));
  762. offset += sizeof(arg.teamID);
  763. } else if (type == ARGUMENTDATATYPE_LOCATION) {
  764. memcpy(buffer + offset, &(arg.location), sizeof(arg.location));
  765. offset += sizeof(arg.location);
  766. } else if (type == ARGUMENTDATATYPE_PIXEL) {
  767. memcpy(buffer + offset, &(arg.pixel), sizeof(arg.pixel));
  768. offset += sizeof(arg.pixel);
  769. } else if (type == ARGUMENTDATATYPE_PIXELREGION) {
  770. memcpy(buffer + offset, &(arg.pixelRegion), sizeof(arg.pixelRegion));
  771. offset += sizeof(arg.pixelRegion);
  772. } else if (type == ARGUMENTDATATYPE_TIMESTAMP) {
  773. memcpy(buffer + offset, &(arg.timestamp), sizeof(arg.timestamp));
  774. offset += sizeof(arg.timestamp);
  775. } else if (type == ARGUMENTDATATYPE_WIDECHAR) {
  776. memcpy(buffer + offset, &(arg.wChar), sizeof(arg.wChar));
  777. offset += sizeof(arg.wChar);
  778. }
  779. }
  780. parser->deleteInstance();
  781. parser = NULL;
  782. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addGameMessage - added game message, frame %d, player %d, command ID %d\n", m_lastFrame, m_lastPlayerID, m_lastCommandID));
  783. if (gmsg)
  784. gmsg->deleteInstance();
  785. gmsg = NULL;
  786. }
  787. void NetPacket::FillBufferWithAckCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  788. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::FillBufferWithAckCommand - adding ack for command %d for player %d\n", cmdMsg->getCommandID(), msg->getCommand()->getPlayerID()));
  789. NetCommandMsg *cmdMsg = msg->getCommand();
  790. UnsignedShort offset = 0;
  791. UnsignedShort commandID = 0;
  792. UnsignedByte originalPlayerID = 0;
  793. if (cmdMsg->getNetCommandType() == NETCOMMANDTYPE_ACKBOTH) {
  794. NetAckBothCommandMsg *ackmsg = (NetAckBothCommandMsg *)msg;
  795. commandID = ackmsg->getCommandID();
  796. originalPlayerID = ackmsg->getOriginalPlayerID();
  797. } else if (cmdMsg->getNetCommandType() == NETCOMMANDTYPE_ACKSTAGE1) {
  798. NetAckStage1CommandMsg *ackmsg = (NetAckStage1CommandMsg *)msg;
  799. commandID = ackmsg->getCommandID();
  800. originalPlayerID = ackmsg->getOriginalPlayerID();
  801. } else if (cmdMsg->getNetCommandType() == NETCOMMANDTYPE_ACKSTAGE2) {
  802. NetAckStage2CommandMsg *ackmsg = (NetAckStage2CommandMsg *)msg;
  803. commandID = ackmsg->getCommandID();
  804. originalPlayerID = ackmsg->getOriginalPlayerID();
  805. }
  806. buffer[offset] = 'T';
  807. ++offset;
  808. buffer[offset] = cmdMsg->getNetCommandType();
  809. offset += sizeof(UnsignedByte);
  810. buffer[offset] = 'P';
  811. ++offset;
  812. buffer[offset] = cmdMsg->getPlayerID();
  813. offset += sizeof(UnsignedByte);
  814. // Put in the command id of the command we are acking.
  815. buffer[offset] = 'D';
  816. ++offset;
  817. memcpy(buffer + offset, &commandID, sizeof(UnsignedShort));
  818. offset += sizeof(UnsignedShort);
  819. memcpy(buffer + offset, &originalPlayerID, sizeof(UnsignedByte));
  820. offset += sizeof(UnsignedByte);
  821. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added ACK, original player %d, command id %d\n", origPlayerID, cmdID));
  822. }
  823. void NetPacket::FillBufferWithFrameCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  824. NetFrameCommandMsg *cmdMsg = (NetFrameCommandMsg *)(msg->getCommand());
  825. UnsignedShort offset = 0;
  826. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addFrameCommand - adding frame command for frame %d, command count = %d, command id = %d\n", cmdMsg->getExecutionFrame(), cmdMsg->getCommandCount(), cmdMsg->getID()));
  827. // If necessary, put the NetCommandType into the packet.
  828. buffer[offset] = 'T';
  829. ++offset;
  830. buffer[offset] = cmdMsg->getNetCommandType();
  831. offset += sizeof(UnsignedByte);
  832. // If necessary, put the execution frame into the packet.
  833. buffer[offset] = 'F';
  834. ++offset;
  835. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  836. memcpy(buffer+offset, &newframe, sizeof(UnsignedInt));
  837. offset += sizeof(UnsignedInt);
  838. // If necessary, put the relay into the packet.
  839. buffer[offset] = 'R';
  840. ++offset;
  841. UnsignedByte newRelay = msg->getRelay();
  842. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  843. offset += sizeof(UnsignedByte);
  844. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  845. buffer[offset] = 'P';
  846. ++offset;
  847. buffer[offset] = cmdMsg->getPlayerID();
  848. offset += sizeof(UnsignedByte);
  849. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  850. // If necessary, specify the command ID of this command.
  851. buffer[offset] = 'C';
  852. ++offset;
  853. UnsignedShort newID = cmdMsg->getID();
  854. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  855. offset += sizeof(UnsignedShort);
  856. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  857. buffer[offset] = 'D';
  858. ++offset;
  859. UnsignedShort cmdCount = cmdMsg->getCommandCount();
  860. memcpy(buffer + offset, &cmdCount, sizeof(UnsignedShort));
  861. offset += sizeof(UnsignedShort);
  862. // frameinfodebug
  863. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added frame %d, player %d, command count = %d, command id = %d\n", cmdMsg->getExecutionFrame(), cmdMsg->getPlayerID(), cmdMsg->getCommandCount(), cmdMsg->getID()));
  864. }
  865. void NetPacket::FillBufferWithPlayerLeaveCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  866. NetPlayerLeaveCommandMsg *cmdMsg = (NetPlayerLeaveCommandMsg *)(msg->getCommand());
  867. UnsignedShort offset = 0;
  868. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPlayerLeaveCommand - adding player leave command for player %d\n", cmdMsg->getLeavingPlayerID()));
  869. // If necessary, put the NetCommandType into the packet.
  870. buffer[offset] = 'T';
  871. ++offset;
  872. buffer[offset] = cmdMsg->getNetCommandType();
  873. offset += sizeof(UnsignedByte);
  874. // If necessary, put the relay into the packet.
  875. buffer[offset] = 'R';
  876. ++offset;
  877. UnsignedByte newRelay = msg->getRelay();
  878. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  879. offset += sizeof(UnsignedByte);
  880. // If necessary, put the execution frame into the packet.
  881. buffer[offset] = 'F';
  882. ++offset;
  883. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  884. memcpy(buffer+offset, &newframe, sizeof(UnsignedInt));
  885. offset += sizeof(UnsignedInt);
  886. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  887. buffer[offset] = 'P';
  888. ++offset;
  889. buffer[offset] = cmdMsg->getPlayerID();
  890. offset += sizeof(UnsignedByte);
  891. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  892. // If necessary, specify the command ID of this command.
  893. buffer[offset] = 'C';
  894. ++offset;
  895. UnsignedShort newID = cmdMsg->getID();
  896. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  897. offset += sizeof(UnsignedShort);
  898. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  899. buffer[offset] = 'D';
  900. ++offset;
  901. UnsignedByte leavingPlayerID = cmdMsg->getLeavingPlayerID();
  902. memcpy(buffer + offset, &leavingPlayerID, sizeof(UnsignedByte));
  903. offset += sizeof(UnsignedByte);
  904. }
  905. void NetPacket::FillBufferWithRunAheadMetricsCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  906. NetRunAheadMetricsCommandMsg *cmdMsg = (NetRunAheadMetricsCommandMsg *)(msg->getCommand());
  907. UnsignedShort offset = 0;
  908. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addRunAheadMetricsCommand - adding run ahead metrics for player %d, fps = %d, latency = %f\n", cmdMsg->getPlayerID(), cmdMsg->getAverageFps(), cmdMsg->getAverageLatency()));
  909. // If necessary, put the NetCommandType into the packet.
  910. buffer[offset] = 'T';
  911. ++offset;
  912. buffer[offset] = cmdMsg->getNetCommandType();
  913. offset += sizeof(UnsignedByte);
  914. // If necessary, put the relay into the packet.
  915. buffer[offset] = 'R';
  916. ++offset;
  917. UnsignedByte newRelay = msg->getRelay();
  918. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  919. offset += sizeof(UnsignedByte);
  920. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  921. buffer[offset] = 'P';
  922. ++offset;
  923. buffer[offset] = cmdMsg->getPlayerID();
  924. offset += sizeof(UnsignedByte);
  925. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  926. // If necessary, specify the command ID of this command.
  927. buffer[offset] = 'C';
  928. ++offset;
  929. UnsignedShort newID = cmdMsg->getID();
  930. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  931. offset += sizeof(UnsignedShort);
  932. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  933. buffer[offset] = 'D';
  934. ++offset;
  935. // write the average latency
  936. Real averageLatency = cmdMsg->getAverageLatency();
  937. memcpy(buffer + offset, &averageLatency, sizeof(averageLatency));
  938. offset += sizeof(averageLatency);
  939. // write the average fps
  940. UnsignedShort averageFps = (UnsignedShort)(cmdMsg->getAverageFps());
  941. memcpy(buffer + offset, &averageFps, sizeof(averageFps));
  942. offset += sizeof(averageFps);
  943. }
  944. void NetPacket::FillBufferWithRunAheadCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  945. NetRunAheadCommandMsg *cmdMsg = (NetRunAheadCommandMsg *)(msg->getCommand());
  946. UnsignedShort offset = 0;
  947. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::FillBufferWithRunAheadCommand - adding run ahead command\n"));
  948. // If necessary, put the NetCommandType into the packet.
  949. buffer[offset] = 'T';
  950. ++offset;
  951. buffer[offset] = cmdMsg->getNetCommandType();
  952. offset += sizeof(UnsignedByte);
  953. // If necessary, put the relay into the packet.
  954. buffer[offset] = 'R';
  955. ++offset;
  956. UnsignedByte newRelay = msg->getRelay();
  957. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  958. offset += sizeof(UnsignedByte);
  959. // If necessary, put the execution frame into the packet.
  960. buffer[offset] = 'F';
  961. ++offset;
  962. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  963. memcpy(buffer+offset, &newframe, sizeof(UnsignedInt));
  964. offset += sizeof(UnsignedInt);
  965. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  966. buffer[offset] = 'P';
  967. ++offset;
  968. buffer[offset] = cmdMsg->getPlayerID();
  969. offset += sizeof(UnsignedByte);
  970. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  971. // If necessary, specify the command ID of this command.
  972. buffer[offset] = 'C';
  973. ++offset;
  974. UnsignedShort newID = cmdMsg->getID();
  975. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  976. offset += sizeof(UnsignedShort);
  977. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  978. buffer[offset] = 'D';
  979. ++offset;
  980. UnsignedShort newRunAhead = cmdMsg->getRunAhead();
  981. memcpy(buffer + offset, &newRunAhead, sizeof(UnsignedShort));
  982. offset += sizeof(UnsignedShort);
  983. UnsignedByte newFrameRate = cmdMsg->getFrameRate();
  984. memcpy(buffer + offset, &newFrameRate, sizeof(UnsignedByte));
  985. offset += sizeof(UnsignedByte);
  986. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added run ahead command, frame %d, player id %d command id %d\n", m_lastFrame, m_lastPlayerID, m_lastCommandID));
  987. }
  988. void NetPacket::FillBufferWithDestroyPlayerCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  989. NetDestroyPlayerCommandMsg *cmdMsg = (NetDestroyPlayerCommandMsg *)(msg->getCommand());
  990. UnsignedShort offset = 0;
  991. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addRunAheadCommand - adding run ahead command\n"));
  992. // If necessary, put the NetCommandType into the packet.
  993. buffer[offset] = 'T';
  994. ++offset;
  995. buffer[offset] = cmdMsg->getNetCommandType();
  996. offset += sizeof(UnsignedByte);
  997. // If necessary, put the relay into the packet.
  998. buffer[offset] = 'R';
  999. ++offset;
  1000. UnsignedByte newRelay = msg->getRelay();
  1001. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1002. offset += sizeof(UnsignedByte);
  1003. // If necessary, put the execution frame into the packet.
  1004. buffer[offset] = 'F';
  1005. ++offset;
  1006. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  1007. memcpy(buffer+offset, &newframe, sizeof(UnsignedInt));
  1008. offset += sizeof(UnsignedInt);
  1009. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  1010. buffer[offset] = 'P';
  1011. ++offset;
  1012. buffer[offset] = cmdMsg->getPlayerID();
  1013. offset += sizeof(UnsignedByte);
  1014. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  1015. // If necessary, specify the command ID of this command.
  1016. buffer[offset] = 'C';
  1017. ++offset;
  1018. UnsignedShort newID = cmdMsg->getID();
  1019. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  1020. offset += sizeof(UnsignedShort);
  1021. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  1022. buffer[offset] = 'D';
  1023. ++offset;
  1024. UnsignedInt newVal = cmdMsg->getPlayerIndex();
  1025. memcpy(buffer + offset, &newVal, sizeof(UnsignedInt));
  1026. offset += sizeof(UnsignedInt);
  1027. }
  1028. void NetPacket::FillBufferWithKeepAliveCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  1029. NetKeepAliveCommandMsg *cmdMsg = (NetKeepAliveCommandMsg *)(msg->getCommand());
  1030. UnsignedShort offset = 0;
  1031. // If necessary, put the NetCommandType into the packet.
  1032. buffer[offset] = 'T';
  1033. ++offset;
  1034. buffer[offset] = cmdMsg->getNetCommandType();
  1035. offset += sizeof(UnsignedByte);
  1036. // If necessary, put the relay into the packet.
  1037. buffer[offset] = 'R';
  1038. ++offset;
  1039. UnsignedByte newRelay = msg->getRelay();
  1040. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1041. offset += sizeof(UnsignedByte);
  1042. buffer[offset] = 'P';
  1043. ++offset;
  1044. buffer[offset] = cmdMsg->getPlayerID();
  1045. offset += sizeof(UnsignedByte);
  1046. buffer[offset] = 'D';
  1047. ++offset;
  1048. }
  1049. void NetPacket::FillBufferWithDisconnectKeepAliveCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  1050. NetDisconnectKeepAliveCommandMsg *cmdMsg = (NetDisconnectKeepAliveCommandMsg *)(msg->getCommand());
  1051. UnsignedShort offset = 0;
  1052. // Put the NetCommandType into the packet.
  1053. buffer[offset] = 'T';
  1054. ++offset;
  1055. buffer[offset] = cmdMsg->getNetCommandType();
  1056. offset += sizeof(UnsignedByte);
  1057. // Put the relay into the packet.
  1058. buffer[offset] = 'R';
  1059. ++offset;
  1060. UnsignedByte newRelay = msg->getRelay();
  1061. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1062. offset += sizeof(UnsignedByte);
  1063. // Put the player ID into the packet.
  1064. buffer[offset] = 'P';
  1065. ++offset;
  1066. buffer[offset] = cmdMsg->getPlayerID();
  1067. offset += sizeof(UnsignedByte);
  1068. buffer[offset] = 'D';
  1069. ++offset;
  1070. }
  1071. void NetPacket::FillBufferWithDisconnectPlayerCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  1072. NetDisconnectPlayerCommandMsg *cmdMsg = (NetDisconnectPlayerCommandMsg *)(msg->getCommand());
  1073. UnsignedShort offset = 0;
  1074. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectPlayerCommand - adding run ahead command\n"));
  1075. // If necessary, put the NetCommandType into the packet.
  1076. buffer[offset] = 'T';
  1077. ++offset;
  1078. buffer[offset] = cmdMsg->getNetCommandType();
  1079. offset += sizeof(UnsignedByte);
  1080. // If necessary, put the relay into the packet.
  1081. buffer[offset] = 'R';
  1082. ++offset;
  1083. UnsignedByte newRelay = msg->getRelay();
  1084. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1085. offset += sizeof(UnsignedByte);
  1086. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  1087. buffer[offset] = 'P';
  1088. ++offset;
  1089. buffer[offset] = cmdMsg->getPlayerID();
  1090. offset += sizeof(UnsignedByte);
  1091. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  1092. // If necessary, specify the command ID of this command.
  1093. buffer[offset] = 'C';
  1094. ++offset;
  1095. UnsignedShort newID = cmdMsg->getID();
  1096. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  1097. offset += sizeof(UnsignedShort);
  1098. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  1099. buffer[offset] = 'D';
  1100. ++offset;
  1101. UnsignedByte slot = cmdMsg->getDisconnectSlot();
  1102. memcpy(buffer + offset, &slot, sizeof(slot));
  1103. offset += sizeof(slot);
  1104. UnsignedInt disconnectFrame = cmdMsg->getDisconnectFrame();
  1105. memcpy(buffer + offset, &disconnectFrame, sizeof(disconnectFrame));
  1106. offset += sizeof(disconnectFrame);
  1107. }
  1108. void NetPacket::FillBufferWithPacketRouterQueryCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  1109. NetPacketRouterQueryCommandMsg *cmdMsg = (NetPacketRouterQueryCommandMsg *)(msg->getCommand());
  1110. UnsignedShort offset = 0;
  1111. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPacketRouterQueryCommand - adding packet router query command\n"));
  1112. // If necessary, put the NetCommandType into the packet.
  1113. buffer[offset] = 'T';
  1114. ++offset;
  1115. buffer[offset] = cmdMsg->getNetCommandType();
  1116. offset += sizeof(UnsignedByte);
  1117. // If necessary, put the relay into the packet.
  1118. buffer[offset] = 'R';
  1119. ++offset;
  1120. UnsignedByte newRelay = msg->getRelay();
  1121. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1122. offset += sizeof(UnsignedByte);
  1123. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  1124. buffer[offset] = 'P';
  1125. ++offset;
  1126. buffer[offset] = cmdMsg->getPlayerID();
  1127. offset += sizeof(UnsignedByte);
  1128. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  1129. buffer[offset] = 'D';
  1130. ++offset;
  1131. }
  1132. void NetPacket::FillBufferWithPacketRouterAckCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  1133. NetPacketRouterAckCommandMsg *cmdMsg = (NetPacketRouterAckCommandMsg *)(msg->getCommand());
  1134. UnsignedShort offset = 0;
  1135. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPacketRouterAckCommand - adding packet router query command\n"));
  1136. // If necessary, put the NetCommandType into the packet.
  1137. buffer[offset] = 'T';
  1138. ++offset;
  1139. buffer[offset] = cmdMsg->getNetCommandType();
  1140. offset += sizeof(UnsignedByte);
  1141. // If necessary, put the relay into the packet.
  1142. buffer[offset] = 'R';
  1143. ++offset;
  1144. UnsignedByte newRelay = msg->getRelay();
  1145. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1146. offset += sizeof(UnsignedByte);
  1147. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  1148. buffer[offset] = 'P';
  1149. ++offset;
  1150. buffer[offset] = cmdMsg->getPlayerID();
  1151. offset += sizeof(UnsignedByte);
  1152. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  1153. buffer[offset] = 'D';
  1154. ++offset;
  1155. }
  1156. void NetPacket::FillBufferWithDisconnectChatCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  1157. NetDisconnectChatCommandMsg *cmdMsg = (NetDisconnectChatCommandMsg *)(msg->getCommand());
  1158. UnsignedShort offset = 0;
  1159. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - adding run ahead command\n"));
  1160. // If necessary, put the NetCommandType into the packet.
  1161. buffer[offset] = 'T';
  1162. ++offset;
  1163. buffer[offset] = cmdMsg->getNetCommandType();
  1164. offset += sizeof(UnsignedByte);
  1165. // If necessary, put the relay into the packet.
  1166. buffer[offset] = 'R';
  1167. ++offset;
  1168. UnsignedByte newRelay = msg->getRelay();
  1169. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1170. offset += sizeof(UnsignedByte);
  1171. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  1172. buffer[offset] = 'P';
  1173. ++offset;
  1174. buffer[offset] = cmdMsg->getPlayerID();
  1175. offset += sizeof(UnsignedByte);
  1176. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  1177. buffer[offset] = 'D';
  1178. ++offset;
  1179. UnicodeString unitext = cmdMsg->getText();
  1180. UnsignedByte length = unitext.getLength();
  1181. memcpy(buffer + offset, &length, sizeof(UnsignedByte));
  1182. offset += sizeof(UnsignedByte);
  1183. memcpy(buffer + offset, unitext.str(), length * sizeof(UnsignedShort));
  1184. offset += length * sizeof(UnsignedShort);
  1185. }
  1186. void NetPacket::FillBufferWithDisconnectVoteCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  1187. NetDisconnectVoteCommandMsg *cmdMsg = (NetDisconnectVoteCommandMsg *)(msg->getCommand());
  1188. UnsignedShort offset = 0;
  1189. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectVoteCommand - adding run ahead command\n"));
  1190. // If necessary, put the NetCommandType into the packet.
  1191. buffer[offset] = 'T';
  1192. ++offset;
  1193. buffer[offset] = cmdMsg->getNetCommandType();
  1194. offset += sizeof(UnsignedByte);
  1195. // If necessary, put the relay into the packet.
  1196. buffer[offset] = 'R';
  1197. ++offset;
  1198. UnsignedByte newRelay = msg->getRelay();
  1199. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1200. offset += sizeof(UnsignedByte);
  1201. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  1202. buffer[offset] = 'P';
  1203. ++offset;
  1204. buffer[offset] = cmdMsg->getPlayerID();
  1205. offset += sizeof(UnsignedByte);
  1206. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  1207. // If necessary, specify the command ID of this command.
  1208. buffer[offset] = 'C';
  1209. ++offset;
  1210. UnsignedShort newID = cmdMsg->getID();
  1211. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  1212. offset += sizeof(UnsignedShort);
  1213. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  1214. buffer[offset] = 'D';
  1215. ++offset;
  1216. UnsignedByte slot = cmdMsg->getSlot();
  1217. memcpy(buffer + offset, &slot, sizeof(slot));
  1218. offset += sizeof(slot);
  1219. UnsignedInt voteFrame = cmdMsg->getVoteFrame();
  1220. memcpy(buffer + offset, &voteFrame, sizeof(voteFrame));
  1221. offset += sizeof(voteFrame);
  1222. }
  1223. void NetPacket::FillBufferWithChatCommand(UnsignedByte *buffer, NetCommandRef *msg) {
  1224. NetChatCommandMsg *cmdMsg = (NetChatCommandMsg *)(msg->getCommand());
  1225. UnsignedShort offset = 0;
  1226. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - adding run ahead command\n"));
  1227. // If necessary, put the NetCommandType into the packet.
  1228. buffer[offset] = 'T';
  1229. ++offset;
  1230. buffer[offset] = cmdMsg->getNetCommandType();
  1231. offset += sizeof(UnsignedByte);
  1232. // If necessary, put the execution frame into the packet.
  1233. buffer[offset] = 'F';
  1234. ++offset;
  1235. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  1236. memcpy(buffer+offset, &newframe, sizeof(UnsignedInt));
  1237. offset += sizeof(UnsignedInt);
  1238. // If necessary, put the relay into the packet.
  1239. buffer[offset] = 'R';
  1240. ++offset;
  1241. UnsignedByte newRelay = msg->getRelay();
  1242. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1243. offset += sizeof(UnsignedByte);
  1244. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  1245. buffer[offset] = 'P';
  1246. ++offset;
  1247. buffer[offset] = cmdMsg->getPlayerID();
  1248. offset += sizeof(UnsignedByte);
  1249. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  1250. // If necessary, specify the command ID of this command.
  1251. buffer[offset] = 'C';
  1252. ++offset;
  1253. UnsignedShort newID = cmdMsg->getID();
  1254. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  1255. offset += sizeof(UnsignedShort);
  1256. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  1257. buffer[offset] = 'D';
  1258. ++offset;
  1259. UnicodeString unitext = cmdMsg->getText();
  1260. UnsignedByte length = unitext.getLength();
  1261. Int playerMask = cmdMsg->getPlayerMask();
  1262. memcpy(buffer + offset, &length, sizeof(UnsignedByte));
  1263. offset += sizeof(UnsignedByte);
  1264. memcpy(buffer + offset, unitext.str(), length * sizeof(UnsignedShort));
  1265. offset += length * sizeof(UnsignedShort);
  1266. memcpy(buffer + offset, &playerMask, sizeof(Int));
  1267. offset += sizeof(Int);
  1268. }
  1269. void NetPacket::FillBufferWithProgressMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1270. NetProgressCommandMsg *cmdMsg = (NetProgressCommandMsg *)(msg->getCommand());
  1271. UnsignedShort offset = 0;
  1272. // If necessary, put the NetCommandType into the packet.
  1273. buffer[offset] = 'T';
  1274. ++offset;
  1275. buffer[offset] = cmdMsg->getNetCommandType();
  1276. offset += sizeof(UnsignedByte);
  1277. // If necessary, put the relay into the packet.
  1278. buffer[offset] = 'R';
  1279. ++offset;
  1280. UnsignedByte newRelay = msg->getRelay();
  1281. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1282. offset += sizeof(UnsignedByte);
  1283. // Put the player ID into the packet.
  1284. buffer[offset] = 'P';
  1285. ++offset;
  1286. buffer[offset] = cmdMsg->getPlayerID();
  1287. offset += sizeof(UnsignedByte);
  1288. buffer[offset] = 'D';
  1289. ++offset;
  1290. buffer[offset] = cmdMsg->getPercentage();
  1291. ++offset;
  1292. }
  1293. void NetPacket::FillBufferWithLoadCompleteMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1294. NetCommandMsg *cmdMsg = (NetCommandMsg *)(msg->getCommand());
  1295. UnsignedShort offset = 0;
  1296. // If necessary, put the NetCommandType into the packet.
  1297. buffer[offset] = 'T';
  1298. ++offset;
  1299. buffer[offset] = cmdMsg->getNetCommandType();
  1300. offset += sizeof(UnsignedByte);
  1301. // If necessary, put the relay into the packet.
  1302. buffer[offset] = 'R';
  1303. ++offset;
  1304. UnsignedByte newRelay = msg->getRelay();
  1305. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1306. offset += sizeof(UnsignedByte);
  1307. buffer[offset] = 'P';
  1308. ++offset;
  1309. buffer[offset] = cmdMsg->getPlayerID();
  1310. offset += sizeof(UnsignedByte);
  1311. // If necessary, specify the command ID of this command.
  1312. buffer[offset] = 'C';
  1313. ++offset;
  1314. UnsignedShort newID = cmdMsg->getID();
  1315. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  1316. offset += sizeof(UnsignedShort);
  1317. buffer[offset] = 'D';
  1318. ++offset;
  1319. }
  1320. void NetPacket::FillBufferWithTimeOutGameStartMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1321. NetCommandMsg *cmdMsg = (NetCommandMsg *)(msg->getCommand());
  1322. UnsignedShort offset = 0;
  1323. // If necessary, put the NetCommandType into the packet.
  1324. buffer[offset] = 'T';
  1325. ++offset;
  1326. buffer[offset] = cmdMsg->getNetCommandType();
  1327. offset += sizeof(UnsignedByte);
  1328. // If necessary, put the relay into the packet.
  1329. buffer[offset] = 'R';
  1330. ++offset;
  1331. UnsignedByte newRelay = msg->getRelay();
  1332. memcpy(buffer+offset, &newRelay, sizeof(UnsignedByte));
  1333. offset += sizeof(UnsignedByte);
  1334. buffer[offset] = 'P';
  1335. ++offset;
  1336. buffer[offset] = cmdMsg->getPlayerID();
  1337. offset += sizeof(UnsignedByte);
  1338. // If necessary, specify the command ID of this command.
  1339. buffer[offset] = 'C';
  1340. ++offset;
  1341. UnsignedShort newID = cmdMsg->getID();
  1342. memcpy(buffer + offset, &newID, sizeof(UnsignedShort));
  1343. offset += sizeof(UnsignedShort);
  1344. buffer[offset] = 'D';
  1345. ++offset;
  1346. }
  1347. void NetPacket::FillBufferWithFileMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1348. NetFileCommandMsg *cmdMsg = (NetFileCommandMsg *)(msg->getCommand());
  1349. UnsignedInt offset = 0;
  1350. // command type
  1351. buffer[offset] = 'T';
  1352. ++offset;
  1353. buffer[offset] = cmdMsg->getNetCommandType();
  1354. offset += sizeof(UnsignedByte);
  1355. // relay
  1356. buffer[offset] = 'R';
  1357. ++offset;
  1358. buffer[offset] = msg->getRelay();
  1359. offset += sizeof(UnsignedByte);
  1360. // player ID
  1361. buffer[offset] = 'P';
  1362. ++offset;
  1363. buffer[offset] = cmdMsg->getPlayerID();
  1364. offset += sizeof(UnsignedByte);
  1365. // command ID
  1366. buffer[offset] = 'C';
  1367. ++offset;
  1368. UnsignedShort newID = cmdMsg->getID();
  1369. memcpy(buffer + offset, &newID, sizeof(newID));
  1370. offset += sizeof(newID);
  1371. // data
  1372. buffer[offset] = 'D';
  1373. ++offset;
  1374. AsciiString filename = cmdMsg->getPortableFilename(); // PORTABLE
  1375. for (Int i = 0; i < filename.getLength(); ++i) {
  1376. buffer[offset] = filename.getCharAt(i);
  1377. ++offset;
  1378. }
  1379. buffer[offset] = 0;
  1380. ++offset;
  1381. UnsignedInt newInt = cmdMsg->getFileLength();
  1382. memcpy(buffer + offset, &newInt, sizeof(newInt));
  1383. offset += sizeof(newInt);
  1384. memcpy(buffer + offset, cmdMsg->getFileData(), cmdMsg->getFileLength());
  1385. offset += cmdMsg->getFileLength();
  1386. }
  1387. void NetPacket::FillBufferWithFileAnnounceMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1388. NetFileAnnounceCommandMsg *cmdMsg = (NetFileAnnounceCommandMsg *)(msg->getCommand());
  1389. UnsignedInt offset = 0;
  1390. // command type
  1391. buffer[offset] = 'T';
  1392. ++offset;
  1393. buffer[offset] = cmdMsg->getNetCommandType();
  1394. offset += sizeof(UnsignedByte);
  1395. // relay
  1396. buffer[offset] = 'R';
  1397. ++offset;
  1398. buffer[offset] = msg->getRelay();
  1399. offset += sizeof(UnsignedByte);
  1400. // player ID
  1401. buffer[offset] = 'P';
  1402. ++offset;
  1403. buffer[offset] = cmdMsg->getPlayerID();
  1404. offset += sizeof(UnsignedByte);
  1405. // command ID
  1406. buffer[offset] = 'C';
  1407. ++offset;
  1408. UnsignedShort newID = cmdMsg->getID();
  1409. memcpy(buffer + offset, &newID, sizeof(newID));
  1410. offset += sizeof(newID);
  1411. // data
  1412. buffer[offset] = 'D';
  1413. ++offset;
  1414. AsciiString filename = cmdMsg->getPortableFilename(); // PORTABLE
  1415. for (Int i = 0; i < filename.getLength(); ++i) {
  1416. buffer[offset] = filename.getCharAt(i);
  1417. ++offset;
  1418. }
  1419. buffer[offset] = 0;
  1420. ++offset;
  1421. UnsignedShort fileID = cmdMsg->getFileID();
  1422. memcpy(buffer + offset, &fileID, sizeof(fileID));
  1423. offset += sizeof(fileID);
  1424. UnsignedByte playerMask = cmdMsg->getPlayerMask();
  1425. memcpy(buffer + offset, &playerMask, sizeof(playerMask));
  1426. offset += sizeof(playerMask);
  1427. }
  1428. void NetPacket::FillBufferWithFileProgressMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1429. NetFileProgressCommandMsg *cmdMsg = (NetFileProgressCommandMsg *)(msg->getCommand());
  1430. UnsignedInt offset = 0;
  1431. // command type
  1432. buffer[offset] = 'T';
  1433. ++offset;
  1434. buffer[offset] = cmdMsg->getNetCommandType();
  1435. offset += sizeof(UnsignedByte);
  1436. // relay
  1437. buffer[offset] = 'R';
  1438. ++offset;
  1439. buffer[offset] = msg->getRelay();
  1440. offset += sizeof(UnsignedByte);
  1441. // player ID
  1442. buffer[offset] = 'P';
  1443. ++offset;
  1444. buffer[offset] = cmdMsg->getPlayerID();
  1445. offset += sizeof(UnsignedByte);
  1446. // command ID
  1447. buffer[offset] = 'C';
  1448. ++offset;
  1449. UnsignedShort newID = cmdMsg->getID();
  1450. memcpy(buffer + offset, &newID, sizeof(newID));
  1451. offset += sizeof(newID);
  1452. // data
  1453. buffer[offset] = 'D';
  1454. ++offset;
  1455. UnsignedShort fileID = cmdMsg->getFileID();
  1456. memcpy(buffer + offset, &fileID, sizeof(fileID));
  1457. offset += sizeof(fileID);
  1458. Int progress = cmdMsg->getProgress();
  1459. memcpy(buffer + offset, &progress, sizeof(progress));
  1460. offset += sizeof(progress);
  1461. }
  1462. void NetPacket::FillBufferWithDisconnectFrameMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1463. NetDisconnectFrameCommandMsg *cmdMsg = (NetDisconnectFrameCommandMsg *)(msg->getCommand());
  1464. UnsignedInt offset = 0;
  1465. // command type
  1466. buffer[offset] = 'T';
  1467. ++offset;
  1468. buffer[offset] = cmdMsg->getNetCommandType();
  1469. offset += sizeof(UnsignedByte);
  1470. // relay
  1471. buffer[offset] = 'R';
  1472. ++offset;
  1473. buffer[offset] = msg->getRelay();
  1474. offset += sizeof(UnsignedByte);
  1475. // player ID
  1476. buffer[offset] = 'P';
  1477. ++offset;
  1478. buffer[offset] = cmdMsg->getPlayerID();
  1479. offset += sizeof(UnsignedByte);
  1480. // command ID
  1481. buffer[offset] = 'C';
  1482. ++offset;
  1483. UnsignedShort newID = cmdMsg->getID();
  1484. memcpy(buffer + offset, &newID, sizeof(newID));
  1485. offset += sizeof(newID);
  1486. // data
  1487. buffer[offset] = 'D';
  1488. ++offset;
  1489. UnsignedInt disconnectFrame = cmdMsg->getDisconnectFrame();
  1490. memcpy(buffer + offset, &disconnectFrame, sizeof(disconnectFrame));
  1491. offset += sizeof(disconnectFrame);
  1492. }
  1493. void NetPacket::FillBufferWithDisconnectScreenOffMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1494. NetDisconnectScreenOffCommandMsg *cmdMsg = (NetDisconnectScreenOffCommandMsg *)(msg->getCommand());
  1495. UnsignedInt offset = 0;
  1496. // command type
  1497. buffer[offset] = 'T';
  1498. ++offset;
  1499. buffer[offset] = cmdMsg->getNetCommandType();
  1500. offset += sizeof(UnsignedByte);
  1501. // relay
  1502. buffer[offset] = 'R';
  1503. ++offset;
  1504. buffer[offset] = msg->getRelay();
  1505. offset += sizeof(UnsignedByte);
  1506. // player ID
  1507. buffer[offset] = 'P';
  1508. ++offset;
  1509. buffer[offset] = cmdMsg->getPlayerID();
  1510. offset += sizeof(UnsignedByte);
  1511. // command ID
  1512. buffer[offset] = 'C';
  1513. ++offset;
  1514. UnsignedShort newID = cmdMsg->getID();
  1515. memcpy(buffer + offset, &newID, sizeof(newID));
  1516. offset += sizeof(newID);
  1517. // data
  1518. buffer[offset] = 'D';
  1519. ++offset;
  1520. UnsignedInt newFrame = cmdMsg->getNewFrame();
  1521. memcpy(buffer + offset, &newFrame, sizeof(newFrame));
  1522. offset += sizeof(newFrame);
  1523. }
  1524. void NetPacket::FillBufferWithFrameResendRequestMessage(UnsignedByte *buffer, NetCommandRef *msg) {
  1525. NetFrameResendRequestCommandMsg *cmdMsg = (NetFrameResendRequestCommandMsg *)(msg->getCommand());
  1526. UnsignedInt offset = 0;
  1527. // command type
  1528. buffer[offset] = 'T';
  1529. ++offset;
  1530. buffer[offset] = cmdMsg->getNetCommandType();
  1531. offset += sizeof(UnsignedByte);
  1532. // relay
  1533. buffer[offset] = 'R';
  1534. ++offset;
  1535. buffer[offset] = msg->getRelay();
  1536. offset += sizeof(UnsignedByte);
  1537. // player ID
  1538. buffer[offset] = 'P';
  1539. ++offset;
  1540. buffer[offset] = cmdMsg->getPlayerID();
  1541. offset += sizeof(UnsignedByte);
  1542. // command ID
  1543. buffer[offset] = 'C';
  1544. ++offset;
  1545. UnsignedShort newID = cmdMsg->getID();
  1546. memcpy(buffer + offset, &newID, sizeof(newID));
  1547. offset += sizeof(newID);
  1548. // data
  1549. buffer[offset] = 'D';
  1550. ++offset;
  1551. UnsignedInt frameToResend = cmdMsg->getFrameToResend();
  1552. memcpy(buffer + offset, &frameToResend, sizeof(frameToResend));
  1553. offset += sizeof(frameToResend);
  1554. }
  1555. /**
  1556. * Constructor
  1557. */
  1558. NetPacket::NetPacket() {
  1559. init();
  1560. }
  1561. /**
  1562. * Constructor given raw transport data.
  1563. */
  1564. NetPacket::NetPacket(TransportMessage *msg) {
  1565. init();
  1566. m_packetLen = msg->length;
  1567. memcpy(m_packet, msg->data, MAX_PACKET_SIZE);
  1568. m_numCommands = -1;
  1569. m_addr = msg->addr;
  1570. m_port = msg->port;
  1571. }
  1572. /**
  1573. * Destructor
  1574. */
  1575. NetPacket::~NetPacket() {
  1576. if (m_lastCommand != NULL) {
  1577. m_lastCommand->deleteInstance();
  1578. m_lastCommand = NULL;
  1579. }
  1580. }
  1581. /**
  1582. * Initialize all the member variable values.
  1583. */
  1584. void NetPacket::init() {
  1585. m_addr = 0;
  1586. m_port = 0;
  1587. m_numCommands = 0;
  1588. m_packetLen = 0;
  1589. m_packet[0] = 0;
  1590. m_lastPlayerID = 0;
  1591. m_lastFrame = 0;
  1592. m_lastCommandID = 0;
  1593. m_lastCommandType = 0;
  1594. m_lastRelay = 0;
  1595. m_lastCommand = NULL;
  1596. }
  1597. void NetPacket::reset() {
  1598. if (m_lastCommand != NULL) {
  1599. m_lastCommand->deleteInstance();
  1600. m_lastCommand = NULL;
  1601. }
  1602. init();
  1603. }
  1604. /**
  1605. * Set the address to which this packet is to be sent.
  1606. */
  1607. void NetPacket::setAddress(Int addr, Int port) {
  1608. m_addr = addr;
  1609. m_port = port;
  1610. }
  1611. /**
  1612. * Adds this command to the packet. Returns false if there wasn't enough room
  1613. * in the packet for this message, true otherwise.
  1614. */
  1615. Bool NetPacket::addCommand(NetCommandRef *msg) {
  1616. // This is where the fun begins...
  1617. NetCommandMsg *cmdMsg = msg->getCommand();
  1618. if (msg == NULL) {
  1619. return TRUE; // There was nothing to add, so it was successful.
  1620. }
  1621. switch(cmdMsg->getNetCommandType())
  1622. {
  1623. case NETCOMMANDTYPE_GAMECOMMAND:
  1624. return addGameCommand(msg);
  1625. case NETCOMMANDTYPE_ACKSTAGE1:
  1626. return addAckStage1Command(msg);
  1627. case NETCOMMANDTYPE_ACKSTAGE2:
  1628. return addAckStage2Command(msg);
  1629. case NETCOMMANDTYPE_ACKBOTH:
  1630. return addAckBothCommand(msg);
  1631. case NETCOMMANDTYPE_FRAMEINFO:
  1632. return addFrameCommand(msg);
  1633. case NETCOMMANDTYPE_PLAYERLEAVE:
  1634. return addPlayerLeaveCommand(msg);
  1635. case NETCOMMANDTYPE_RUNAHEADMETRICS:
  1636. return addRunAheadMetricsCommand(msg);
  1637. case NETCOMMANDTYPE_RUNAHEAD:
  1638. return addRunAheadCommand(msg);
  1639. case NETCOMMANDTYPE_DESTROYPLAYER:
  1640. return addDestroyPlayerCommand(msg);
  1641. case NETCOMMANDTYPE_KEEPALIVE:
  1642. return addKeepAliveCommand(msg);
  1643. case NETCOMMANDTYPE_DISCONNECTKEEPALIVE:
  1644. return addDisconnectKeepAliveCommand(msg);
  1645. case NETCOMMANDTYPE_DISCONNECTPLAYER:
  1646. return addDisconnectPlayerCommand(msg);
  1647. case NETCOMMANDTYPE_PACKETROUTERQUERY:
  1648. return addPacketRouterQueryCommand(msg);
  1649. case NETCOMMANDTYPE_PACKETROUTERACK:
  1650. return addPacketRouterAckCommand(msg);
  1651. case NETCOMMANDTYPE_DISCONNECTCHAT:
  1652. return addDisconnectChatCommand(msg);
  1653. case NETCOMMANDTYPE_DISCONNECTVOTE:
  1654. return addDisconnectVoteCommand(msg);
  1655. case NETCOMMANDTYPE_CHAT:
  1656. return addChatCommand(msg);
  1657. case NETCOMMANDTYPE_PROGRESS:
  1658. return addProgressMessage(msg);
  1659. case NETCOMMANDTYPE_LOADCOMPLETE:
  1660. return addLoadCompleteMessage(msg);
  1661. case NETCOMMANDTYPE_TIMEOUTSTART:
  1662. return addTimeOutGameStartMessage(msg);
  1663. case NETCOMMANDTYPE_WRAPPER:
  1664. return addWrapperCommand(msg);
  1665. case NETCOMMANDTYPE_FILE:
  1666. return addFileCommand(msg);
  1667. case NETCOMMANDTYPE_FILEANNOUNCE:
  1668. return addFileAnnounceCommand(msg);
  1669. case NETCOMMANDTYPE_FILEPROGRESS:
  1670. return addFileProgressCommand(msg);
  1671. case NETCOMMANDTYPE_DISCONNECTFRAME:
  1672. return addDisconnectFrameCommand(msg);
  1673. case NETCOMMANDTYPE_DISCONNECTSCREENOFF:
  1674. return addDisconnectScreenOffCommand(msg);
  1675. case NETCOMMANDTYPE_FRAMERESENDREQUEST:
  1676. return addFrameResendRequestCommand(msg);
  1677. default:
  1678. DEBUG_CRASH(("Unknown NETCOMMANDTYPE %d", cmdMsg->getNetCommandType()));
  1679. break;
  1680. }
  1681. return TRUE;
  1682. }
  1683. /*
  1684. T = Net command type
  1685. F = Execution frame
  1686. P = Player ID
  1687. C = Command ID
  1688. R = Relay
  1689. D = Command Data
  1690. Z = Repeat last command
  1691. */
  1692. Bool NetPacket::addFrameResendRequestCommand(NetCommandRef *msg) {
  1693. Bool needNewCommandID = FALSE;
  1694. if (isRoomForFrameResendRequestMessage(msg)) {
  1695. NetFrameResendRequestCommandMsg *cmdMsg = (NetFrameResendRequestCommandMsg *)(msg->getCommand());
  1696. // If necessary, put the NetCommandType into the packet.
  1697. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  1698. m_packet[m_packetLen] = 'T';
  1699. ++m_packetLen;
  1700. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  1701. m_packetLen += sizeof(UnsignedByte);
  1702. m_lastCommandType = cmdMsg->getNetCommandType();
  1703. }
  1704. // If necessary, put the execution frame into the packet.
  1705. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  1706. m_packet[m_packetLen] = 'F';
  1707. ++m_packetLen;
  1708. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  1709. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  1710. m_packetLen += sizeof(UnsignedInt);
  1711. m_lastFrame = newframe;
  1712. }
  1713. // If necessary, put the relay into the packet.
  1714. if (m_lastRelay != msg->getRelay()) {
  1715. m_packet[m_packetLen] = 'R';
  1716. ++m_packetLen;
  1717. UnsignedByte newRelay = msg->getRelay();
  1718. memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte));
  1719. m_packetLen += sizeof(UnsignedByte);
  1720. m_lastRelay = newRelay;
  1721. }
  1722. // If necessary put the player ID into the packet.
  1723. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  1724. m_packet[m_packetLen] = 'P';
  1725. ++m_packetLen;
  1726. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  1727. m_packetLen += sizeof(UnsignedByte);
  1728. m_lastPlayerID = cmdMsg->getPlayerID();
  1729. needNewCommandID = TRUE;
  1730. }
  1731. // If necessary, specify the command ID of this command.
  1732. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  1733. m_packet[m_packetLen] = 'C';
  1734. ++m_packetLen;
  1735. UnsignedShort newID = cmdMsg->getID();
  1736. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  1737. m_packetLen += sizeof(UnsignedShort);
  1738. }
  1739. m_lastCommandID = cmdMsg->getID();
  1740. m_packet[m_packetLen] = 'D';
  1741. ++m_packetLen;
  1742. UnsignedInt frameToResend = cmdMsg->getFrameToResend();
  1743. memcpy(m_packet + m_packetLen, &frameToResend, sizeof(frameToResend));
  1744. m_packetLen += sizeof(frameToResend);
  1745. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addFrameResendRequest - added frame resend request command from player %d for frame %d, command id = %d\n", m_lastPlayerID, frameToResend, m_lastCommandID));
  1746. ++m_numCommands;
  1747. if (m_lastCommand != NULL) {
  1748. m_lastCommand->deleteInstance();
  1749. m_lastCommand = NULL;
  1750. }
  1751. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  1752. m_lastCommand->setRelay(msg->getRelay());
  1753. return TRUE;
  1754. }
  1755. return FALSE;
  1756. }
  1757. Bool NetPacket::isRoomForFrameResendRequestMessage(NetCommandRef *msg) {
  1758. Int len = 0;
  1759. Bool needNewCommandID = FALSE;
  1760. NetFrameResendRequestCommandMsg *cmdMsg = (NetFrameResendRequestCommandMsg *)(msg->getCommand());
  1761. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  1762. ++len;
  1763. len += sizeof(UnsignedByte);
  1764. }
  1765. if (m_lastRelay != msg->getRelay()) {
  1766. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  1767. }
  1768. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  1769. len += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  1770. }
  1771. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  1772. ++len;
  1773. len += sizeof(UnsignedByte);
  1774. needNewCommandID = TRUE;
  1775. }
  1776. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  1777. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  1778. }
  1779. ++len; // for 'D'
  1780. len += sizeof(UnsignedInt); // for the frame to be resent
  1781. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  1782. return FALSE;
  1783. }
  1784. return TRUE;
  1785. }
  1786. Bool NetPacket::addDisconnectScreenOffCommand(NetCommandRef *msg) {
  1787. Bool needNewCommandID = FALSE;
  1788. if (isRoomForDisconnectScreenOffMessage(msg)) {
  1789. NetDisconnectScreenOffCommandMsg *cmdMsg = (NetDisconnectScreenOffCommandMsg *)(msg->getCommand());
  1790. // If necessary, put the NetCommandType into the packet.
  1791. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  1792. m_packet[m_packetLen] = 'T';
  1793. ++m_packetLen;
  1794. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  1795. m_packetLen += sizeof(UnsignedByte);
  1796. m_lastCommandType = cmdMsg->getNetCommandType();
  1797. }
  1798. // If necessary, put the execution frame into the packet.
  1799. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  1800. m_packet[m_packetLen] = 'F';
  1801. ++m_packetLen;
  1802. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  1803. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  1804. m_packetLen += sizeof(UnsignedInt);
  1805. m_lastFrame = newframe;
  1806. }
  1807. // If necessary, put the relay into the packet.
  1808. if (m_lastRelay != msg->getRelay()) {
  1809. m_packet[m_packetLen] = 'R';
  1810. ++m_packetLen;
  1811. UnsignedByte newRelay = msg->getRelay();
  1812. memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte));
  1813. m_packetLen += sizeof(UnsignedByte);
  1814. m_lastRelay = newRelay;
  1815. }
  1816. // If necessary put the player ID into the packet.
  1817. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  1818. m_packet[m_packetLen] = 'P';
  1819. ++m_packetLen;
  1820. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  1821. m_packetLen += sizeof(UnsignedByte);
  1822. m_lastPlayerID = cmdMsg->getPlayerID();
  1823. needNewCommandID = TRUE;
  1824. }
  1825. // If necessary, specify the command ID of this command.
  1826. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  1827. m_packet[m_packetLen] = 'C';
  1828. ++m_packetLen;
  1829. UnsignedShort newID = cmdMsg->getID();
  1830. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  1831. m_packetLen += sizeof(UnsignedShort);
  1832. }
  1833. m_lastCommandID = cmdMsg->getID();
  1834. m_packet[m_packetLen] = 'D';
  1835. ++m_packetLen;
  1836. UnsignedInt newFrame = cmdMsg->getNewFrame();
  1837. memcpy(m_packet + m_packetLen, &newFrame, sizeof(newFrame));
  1838. m_packetLen += sizeof(newFrame);
  1839. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectScreenOff - added disconnect screen off command from player %d for frame %d, command id = %d\n", m_lastPlayerID, newFrame, m_lastCommandID));
  1840. ++m_numCommands;
  1841. if (m_lastCommand != NULL) {
  1842. m_lastCommand->deleteInstance();
  1843. m_lastCommand = NULL;
  1844. }
  1845. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  1846. m_lastCommand->setRelay(msg->getRelay());
  1847. return TRUE;
  1848. }
  1849. return FALSE;
  1850. }
  1851. Bool NetPacket::isRoomForDisconnectScreenOffMessage(NetCommandRef *msg) {
  1852. Int len = 0;
  1853. Bool needNewCommandID = FALSE;
  1854. NetDisconnectScreenOffCommandMsg *cmdMsg = (NetDisconnectScreenOffCommandMsg *)(msg->getCommand());
  1855. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  1856. ++len;
  1857. len += sizeof(UnsignedByte);
  1858. }
  1859. if (m_lastRelay != msg->getRelay()) {
  1860. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  1861. }
  1862. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  1863. len += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  1864. }
  1865. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  1866. ++len;
  1867. len += sizeof(UnsignedByte);
  1868. needNewCommandID = TRUE;
  1869. }
  1870. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  1871. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  1872. }
  1873. ++len; // for 'D'
  1874. len += sizeof(UnsignedInt); // for the disconnect frame
  1875. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  1876. return FALSE;
  1877. }
  1878. return TRUE;
  1879. }
  1880. Bool NetPacket::addDisconnectFrameCommand(NetCommandRef *msg) {
  1881. Bool needNewCommandID = FALSE;
  1882. if (isRoomForDisconnectFrameMessage(msg)) {
  1883. NetDisconnectFrameCommandMsg *cmdMsg = (NetDisconnectFrameCommandMsg *)(msg->getCommand());
  1884. // If necessary, put the NetCommandType into the packet.
  1885. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  1886. m_packet[m_packetLen] = 'T';
  1887. ++m_packetLen;
  1888. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  1889. m_packetLen += sizeof(UnsignedByte);
  1890. m_lastCommandType = cmdMsg->getNetCommandType();
  1891. }
  1892. // If necessary, put the execution frame into the packet.
  1893. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  1894. m_packet[m_packetLen] = 'F';
  1895. ++m_packetLen;
  1896. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  1897. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  1898. m_packetLen += sizeof(UnsignedInt);
  1899. m_lastFrame = newframe;
  1900. }
  1901. // If necessary, put the relay into the packet.
  1902. if (m_lastRelay != msg->getRelay()) {
  1903. m_packet[m_packetLen] = 'R';
  1904. ++m_packetLen;
  1905. UnsignedByte newRelay = msg->getRelay();
  1906. memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte));
  1907. m_packetLen += sizeof(UnsignedByte);
  1908. m_lastRelay = newRelay;
  1909. }
  1910. // If necessary put the player ID into the packet.
  1911. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  1912. m_packet[m_packetLen] = 'P';
  1913. ++m_packetLen;
  1914. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  1915. m_packetLen += sizeof(UnsignedByte);
  1916. m_lastPlayerID = cmdMsg->getPlayerID();
  1917. needNewCommandID = TRUE;
  1918. }
  1919. // If necessary, specify the command ID of this command.
  1920. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  1921. m_packet[m_packetLen] = 'C';
  1922. ++m_packetLen;
  1923. UnsignedShort newID = cmdMsg->getID();
  1924. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  1925. m_packetLen += sizeof(UnsignedShort);
  1926. }
  1927. m_lastCommandID = cmdMsg->getID();
  1928. m_packet[m_packetLen] = 'D';
  1929. ++m_packetLen;
  1930. UnsignedInt disconnectFrame = cmdMsg->getDisconnectFrame();
  1931. memcpy(m_packet + m_packetLen, &disconnectFrame, sizeof(disconnectFrame));
  1932. m_packetLen += sizeof(disconnectFrame);
  1933. ++m_numCommands;
  1934. if (m_lastCommand != NULL) {
  1935. m_lastCommand->deleteInstance();
  1936. m_lastCommand = NULL;
  1937. }
  1938. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  1939. m_lastCommand->setRelay(msg->getRelay());
  1940. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectFrame - added disconnect frame command from player %d for frame %d, command id = %d\n", m_lastPlayerID, disconnectFrame, m_lastCommandID));
  1941. return TRUE;
  1942. }
  1943. return FALSE;
  1944. }
  1945. Bool NetPacket::isRoomForDisconnectFrameMessage(NetCommandRef *msg) {
  1946. Int len = 0;
  1947. Bool needNewCommandID = FALSE;
  1948. NetDisconnectFrameCommandMsg *cmdMsg = (NetDisconnectFrameCommandMsg *)(msg->getCommand());
  1949. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  1950. ++len;
  1951. len += sizeof(UnsignedByte);
  1952. }
  1953. if (m_lastRelay != msg->getRelay()) {
  1954. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  1955. }
  1956. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  1957. len += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  1958. }
  1959. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  1960. ++len;
  1961. len += sizeof(UnsignedByte);
  1962. needNewCommandID = TRUE;
  1963. }
  1964. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  1965. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  1966. }
  1967. ++len; // for 'D'
  1968. len += sizeof(UnsignedInt); // for the disconnect frame
  1969. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  1970. return FALSE;
  1971. }
  1972. return TRUE;
  1973. }
  1974. Bool NetPacket::addFileCommand(NetCommandRef *msg) {
  1975. Bool needNewCommandID = FALSE;
  1976. if (isRoomForFileMessage(msg)) {
  1977. NetFileCommandMsg *cmdMsg = (NetFileCommandMsg *)(msg->getCommand());
  1978. // If necessary, put the NetCommandType into the packet.
  1979. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  1980. m_packet[m_packetLen] = 'T';
  1981. ++m_packetLen;
  1982. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  1983. m_packetLen += sizeof(UnsignedByte);
  1984. m_lastCommandType = cmdMsg->getNetCommandType();
  1985. }
  1986. // If necessary, put the relay into the packet.
  1987. if (m_lastRelay != msg->getRelay()) {
  1988. m_packet[m_packetLen] = 'R';
  1989. ++m_packetLen;
  1990. UnsignedByte newRelay = msg->getRelay();
  1991. memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte));
  1992. m_packetLen += sizeof(UnsignedByte);
  1993. m_lastRelay = newRelay;
  1994. }
  1995. // If necessary put the player ID into the packet.
  1996. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  1997. m_packet[m_packetLen] = 'P';
  1998. ++m_packetLen;
  1999. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2000. m_packetLen += sizeof(UnsignedByte);
  2001. m_lastPlayerID = cmdMsg->getPlayerID();
  2002. needNewCommandID = TRUE;
  2003. }
  2004. // If necessary, specify the command ID of this command.
  2005. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2006. m_packet[m_packetLen] = 'C';
  2007. ++m_packetLen;
  2008. UnsignedShort newID = cmdMsg->getID();
  2009. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  2010. m_packetLen += sizeof(UnsignedShort);
  2011. }
  2012. m_lastCommandID = cmdMsg->getID();
  2013. m_packet[m_packetLen] = 'D';
  2014. ++m_packetLen;
  2015. AsciiString filename = cmdMsg->getPortableFilename(); // PORTABLE
  2016. strcpy((char *)(m_packet + m_packetLen), filename.str());
  2017. m_packetLen += filename.getLength() + 1;
  2018. UnsignedInt fileLength = cmdMsg->getFileLength();
  2019. memcpy(m_packet + m_packetLen, &fileLength, sizeof(fileLength));
  2020. m_packetLen += sizeof(fileLength);
  2021. memcpy(m_packet + m_packetLen, cmdMsg->getFileData(), fileLength);
  2022. m_packetLen += fileLength;
  2023. ++m_numCommands;
  2024. if (m_lastCommand != NULL) {
  2025. m_lastCommand->deleteInstance();
  2026. m_lastCommand = NULL;
  2027. }
  2028. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2029. m_lastCommand->setRelay(msg->getRelay());
  2030. return TRUE;
  2031. }
  2032. return FALSE;
  2033. }
  2034. Bool NetPacket::isRoomForFileMessage(NetCommandRef *msg) {
  2035. Int len = 0;
  2036. Bool needNewCommandID = FALSE;
  2037. NetFileCommandMsg *cmdMsg = (NetFileCommandMsg *)(msg->getCommand());
  2038. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2039. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2040. }
  2041. if (m_lastRelay != msg->getRelay()) {
  2042. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2043. }
  2044. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2045. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2046. needNewCommandID = TRUE;
  2047. }
  2048. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2049. len += sizeof(UnsignedByte) + sizeof(UnsignedShort);
  2050. }
  2051. ++len; // 'D'
  2052. len += cmdMsg->getPortableFilename().getLength() + 1; // PORTABLE filename + the terminating 0
  2053. len += sizeof(UnsignedInt); // filedata length
  2054. len += cmdMsg->getFileLength();
  2055. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2056. return FALSE;
  2057. }
  2058. return TRUE;
  2059. }
  2060. Bool NetPacket::addFileAnnounceCommand(NetCommandRef *msg) {
  2061. Bool needNewCommandID = FALSE;
  2062. if (isRoomForFileAnnounceMessage(msg)) {
  2063. NetFileAnnounceCommandMsg *cmdMsg = (NetFileAnnounceCommandMsg *)(msg->getCommand());
  2064. // If necessary, put the NetCommandType into the packet.
  2065. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2066. m_packet[m_packetLen] = 'T';
  2067. ++m_packetLen;
  2068. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2069. m_packetLen += sizeof(UnsignedByte);
  2070. m_lastCommandType = cmdMsg->getNetCommandType();
  2071. }
  2072. // If necessary, put the relay into the packet.
  2073. if (m_lastRelay != msg->getRelay()) {
  2074. m_packet[m_packetLen] = 'R';
  2075. ++m_packetLen;
  2076. UnsignedByte newRelay = msg->getRelay();
  2077. memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte));
  2078. m_packetLen += sizeof(UnsignedByte);
  2079. m_lastRelay = newRelay;
  2080. }
  2081. // If necessary put the player ID into the packet.
  2082. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2083. m_packet[m_packetLen] = 'P';
  2084. ++m_packetLen;
  2085. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2086. m_packetLen += sizeof(UnsignedByte);
  2087. m_lastPlayerID = cmdMsg->getPlayerID();
  2088. needNewCommandID = TRUE;
  2089. }
  2090. // If necessary, specify the command ID of this command.
  2091. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2092. m_packet[m_packetLen] = 'C';
  2093. ++m_packetLen;
  2094. UnsignedShort newID = cmdMsg->getID();
  2095. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  2096. m_packetLen += sizeof(UnsignedShort);
  2097. }
  2098. m_lastCommandID = cmdMsg->getID();
  2099. m_packet[m_packetLen] = 'D';
  2100. ++m_packetLen;
  2101. AsciiString filename = cmdMsg->getPortableFilename(); // PORTABLE
  2102. strcpy((char *)(m_packet + m_packetLen), filename.str());
  2103. m_packetLen += filename.getLength() + 1;
  2104. UnsignedShort fileID = cmdMsg->getFileID();
  2105. memcpy(m_packet + m_packetLen, &fileID, sizeof(fileID));
  2106. m_packetLen += sizeof(fileID);
  2107. UnsignedByte playerMask = cmdMsg->getPlayerMask();
  2108. memcpy(m_packet + m_packetLen, &playerMask, sizeof(playerMask));
  2109. m_packetLen += sizeof(playerMask);
  2110. ++m_numCommands;
  2111. if (m_lastCommand != NULL) {
  2112. m_lastCommand->deleteInstance();
  2113. m_lastCommand = NULL;
  2114. }
  2115. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2116. m_lastCommand->setRelay(msg->getRelay());
  2117. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Adding file announce message for fileID %d, ID %d to packet\n",
  2118. cmdMsg->getFileID(), cmdMsg->getID()));
  2119. return TRUE;
  2120. }
  2121. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("No room to add file announce message to packet\n"));
  2122. return FALSE;
  2123. }
  2124. Bool NetPacket::isRoomForFileAnnounceMessage(NetCommandRef *msg) {
  2125. Int len = 0;
  2126. Bool needNewCommandID = FALSE;
  2127. NetFileAnnounceCommandMsg *cmdMsg = (NetFileAnnounceCommandMsg *)(msg->getCommand());
  2128. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2129. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2130. }
  2131. if (m_lastRelay != msg->getRelay()) {
  2132. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2133. }
  2134. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2135. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2136. needNewCommandID = TRUE;
  2137. }
  2138. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2139. len += sizeof(UnsignedByte) + sizeof(UnsignedShort);
  2140. }
  2141. ++len; // 'D'
  2142. len += cmdMsg->getPortableFilename().getLength() + 1; // PORTABLE filename + the terminating 0
  2143. len += sizeof(UnsignedShort); // m_fileID
  2144. len += sizeof(UnsignedByte); // m_playerMask
  2145. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2146. return FALSE;
  2147. }
  2148. return TRUE;
  2149. }
  2150. Bool NetPacket::addFileProgressCommand(NetCommandRef *msg) {
  2151. Bool needNewCommandID = FALSE;
  2152. if (isRoomForFileProgressMessage(msg)) {
  2153. NetFileProgressCommandMsg *cmdMsg = (NetFileProgressCommandMsg *)(msg->getCommand());
  2154. // If necessary, put the NetCommandType into the packet.
  2155. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2156. m_packet[m_packetLen] = 'T';
  2157. ++m_packetLen;
  2158. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2159. m_packetLen += sizeof(UnsignedByte);
  2160. m_lastCommandType = cmdMsg->getNetCommandType();
  2161. }
  2162. // If necessary, put the relay into the packet.
  2163. if (m_lastRelay != msg->getRelay()) {
  2164. m_packet[m_packetLen] = 'R';
  2165. ++m_packetLen;
  2166. UnsignedByte newRelay = msg->getRelay();
  2167. memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte));
  2168. m_packetLen += sizeof(UnsignedByte);
  2169. m_lastRelay = newRelay;
  2170. }
  2171. // If necessary put the player ID into the packet.
  2172. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2173. m_packet[m_packetLen] = 'P';
  2174. ++m_packetLen;
  2175. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2176. m_packetLen += sizeof(UnsignedByte);
  2177. m_lastPlayerID = cmdMsg->getPlayerID();
  2178. needNewCommandID = TRUE;
  2179. }
  2180. // If necessary, specify the command ID of this command.
  2181. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2182. m_packet[m_packetLen] = 'C';
  2183. ++m_packetLen;
  2184. UnsignedShort newID = cmdMsg->getID();
  2185. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  2186. m_packetLen += sizeof(UnsignedShort);
  2187. }
  2188. m_lastCommandID = cmdMsg->getID();
  2189. m_packet[m_packetLen] = 'D';
  2190. ++m_packetLen;
  2191. UnsignedShort fileID = cmdMsg->getFileID();
  2192. memcpy(m_packet + m_packetLen, &fileID, sizeof(fileID));
  2193. m_packetLen += sizeof(fileID);
  2194. Int progress = cmdMsg->getProgress();
  2195. memcpy(m_packet + m_packetLen, &progress, sizeof(progress));
  2196. m_packetLen += sizeof(progress);
  2197. ++m_numCommands;
  2198. if (m_lastCommand != NULL) {
  2199. m_lastCommand->deleteInstance();
  2200. m_lastCommand = NULL;
  2201. }
  2202. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2203. m_lastCommand->setRelay(msg->getRelay());
  2204. return TRUE;
  2205. }
  2206. return FALSE;
  2207. }
  2208. Bool NetPacket::isRoomForFileProgressMessage(NetCommandRef *msg) {
  2209. Int len = 0;
  2210. Bool needNewCommandID = FALSE;
  2211. NetFileProgressCommandMsg *cmdMsg = (NetFileProgressCommandMsg *)(msg->getCommand());
  2212. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2213. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2214. }
  2215. if (m_lastRelay != msg->getRelay()) {
  2216. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2217. }
  2218. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2219. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2220. needNewCommandID = TRUE;
  2221. }
  2222. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2223. len += sizeof(UnsignedByte) + sizeof(UnsignedShort);
  2224. }
  2225. ++len; // 'D'
  2226. len += sizeof(UnsignedShort); // m_fileID
  2227. len += sizeof(Int); // m_progress
  2228. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2229. return FALSE;
  2230. }
  2231. return TRUE;
  2232. }
  2233. Bool NetPacket::addWrapperCommand(NetCommandRef *msg) {
  2234. Bool needNewCommandID = FALSE;
  2235. if (isRoomForWrapperMessage(msg)) {
  2236. NetWrapperCommandMsg *cmdMsg = (NetWrapperCommandMsg *)(msg->getCommand());
  2237. // If necessary, put the NetCommandType into the packet.
  2238. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2239. m_packet[m_packetLen] = 'T';
  2240. ++m_packetLen;
  2241. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2242. m_packetLen += sizeof(UnsignedByte);
  2243. m_lastCommandType = cmdMsg->getNetCommandType();
  2244. }
  2245. // If necessary, put the relay into the packet.
  2246. if (m_lastRelay != msg->getRelay()) {
  2247. m_packet[m_packetLen] = 'R';
  2248. ++m_packetLen;
  2249. UnsignedByte newRelay = msg->getRelay();
  2250. memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte));
  2251. m_packetLen += sizeof(UnsignedByte);
  2252. m_lastRelay = newRelay;
  2253. }
  2254. // If necessary put the player ID into the packet.
  2255. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2256. m_packet[m_packetLen] = 'P';
  2257. ++m_packetLen;
  2258. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2259. m_packetLen += sizeof(UnsignedByte);
  2260. m_lastPlayerID = cmdMsg->getPlayerID();
  2261. needNewCommandID = TRUE;
  2262. }
  2263. // If necessary, specify the command ID of this command.
  2264. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2265. m_packet[m_packetLen] = 'C';
  2266. ++m_packetLen;
  2267. UnsignedShort newID = cmdMsg->getID();
  2268. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  2269. m_packetLen += sizeof(UnsignedShort);
  2270. }
  2271. m_lastCommandID = cmdMsg->getID();
  2272. m_packet[m_packetLen] = 'D';
  2273. ++m_packetLen;
  2274. // wrapped command ID
  2275. UnsignedShort wrappedCommandID = cmdMsg->getWrappedCommandID();
  2276. memcpy(m_packet + m_packetLen, &wrappedCommandID, sizeof(wrappedCommandID));
  2277. m_packetLen += sizeof(wrappedCommandID);
  2278. // chunk number
  2279. // m_packet[m_packetLen] = cmdMsg->getChunkNumber();
  2280. // ++m_packetLen;
  2281. UnsignedInt chunkNumber = cmdMsg->getChunkNumber();
  2282. memcpy(m_packet + m_packetLen, &chunkNumber, sizeof(chunkNumber));
  2283. m_packetLen += sizeof(chunkNumber);
  2284. // number of chunks
  2285. // m_packet[m_packetLen] = cmdMsg->getNumChunks();
  2286. // ++m_packetLen;
  2287. UnsignedInt numChunks = cmdMsg->getNumChunks();
  2288. memcpy(m_packet + m_packetLen, &numChunks, sizeof(numChunks));
  2289. m_packetLen += sizeof(numChunks);
  2290. // total length of data for all chunks
  2291. UnsignedInt totalDataLength = cmdMsg->getTotalDataLength();
  2292. memcpy(m_packet + m_packetLen, &totalDataLength, sizeof(totalDataLength));
  2293. m_packetLen += sizeof(totalDataLength);
  2294. // data length for this chunk
  2295. UnsignedInt dataLength = cmdMsg->getDataLength();
  2296. memcpy(m_packet + m_packetLen, &dataLength, sizeof(dataLength));
  2297. m_packetLen += sizeof(dataLength);
  2298. // the offset into the data of this chunk
  2299. UnsignedInt dataOffset = cmdMsg->getDataOffset();
  2300. memcpy(m_packet + m_packetLen, &dataOffset, sizeof(dataOffset));
  2301. m_packetLen += sizeof(dataOffset);
  2302. // the data for this chunk
  2303. UnsignedByte *data = cmdMsg->getData();
  2304. memcpy(m_packet + m_packetLen, data, dataLength);
  2305. m_packetLen += dataLength;
  2306. ++m_numCommands;
  2307. if (m_lastCommand != NULL) {
  2308. m_lastCommand->deleteInstance();
  2309. m_lastCommand = NULL;
  2310. }
  2311. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2312. m_lastCommand->setRelay(msg->getRelay());
  2313. return TRUE;
  2314. }
  2315. return FALSE;
  2316. }
  2317. Bool NetPacket::isRoomForWrapperMessage(NetCommandRef *msg) {
  2318. Int len = 0;
  2319. Bool needNewCommandID = FALSE;
  2320. NetWrapperCommandMsg *cmdMsg = (NetWrapperCommandMsg *)(msg->getCommand());
  2321. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2322. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2323. }
  2324. if (m_lastRelay != msg->getRelay()) {
  2325. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2326. }
  2327. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2328. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2329. needNewCommandID = TRUE;
  2330. }
  2331. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2332. len += sizeof(UnsignedByte) + sizeof(UnsignedShort);
  2333. }
  2334. ++len; // 'D'
  2335. len += sizeof(UnsignedShort); // wrapped command ID
  2336. len += sizeof(UnsignedInt); // chunk number
  2337. len += sizeof(UnsignedInt); // number of chunks
  2338. len += sizeof(UnsignedInt); // total data length
  2339. len += sizeof(UnsignedInt); // data length of this chunk
  2340. len += sizeof(UnsignedInt); // offset of this chunk
  2341. len += cmdMsg->getDataLength(); // for the data of this chunk
  2342. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2343. return FALSE;
  2344. }
  2345. return TRUE;
  2346. }
  2347. /**
  2348. * Add a TimeOutGameStart to the packet. Returns true if successful.
  2349. */
  2350. Bool NetPacket::addTimeOutGameStartMessage(NetCommandRef *msg) {
  2351. Bool needNewCommandID = FALSE;
  2352. if (isRoomForLoadCompleteMessage(msg)) {
  2353. NetCommandMsg *cmdMsg = (NetCommandMsg *)(msg->getCommand());
  2354. // If necessary, put the NetCommandType into the packet.
  2355. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2356. m_packet[m_packetLen] = 'T';
  2357. ++m_packetLen;
  2358. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2359. m_packetLen += sizeof(UnsignedByte);
  2360. m_lastCommandType = cmdMsg->getNetCommandType();
  2361. }
  2362. // If necessary, put the relay into the packet.
  2363. if (m_lastRelay != msg->getRelay()) {
  2364. m_packet[m_packetLen] = 'R';
  2365. ++m_packetLen;
  2366. UnsignedByte newRelay = msg->getRelay();
  2367. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  2368. m_packetLen += sizeof(UnsignedByte);
  2369. m_lastRelay = newRelay;
  2370. }
  2371. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2372. m_packet[m_packetLen] = 'P';
  2373. ++m_packetLen;
  2374. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2375. m_packetLen += sizeof(UnsignedByte);
  2376. m_lastPlayerID = cmdMsg->getPlayerID();
  2377. needNewCommandID = TRUE;
  2378. }
  2379. // If necessary, specify the command ID of this command.
  2380. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2381. m_packet[m_packetLen] = 'C';
  2382. ++m_packetLen;
  2383. UnsignedShort newID = cmdMsg->getID();
  2384. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  2385. m_packetLen += sizeof(UnsignedShort);
  2386. }
  2387. m_lastCommandID = cmdMsg->getID();
  2388. m_packet[m_packetLen] = 'D';
  2389. ++m_packetLen;
  2390. ++m_numCommands;
  2391. if (m_lastCommand != NULL) {
  2392. m_lastCommand->deleteInstance();
  2393. m_lastCommand = NULL;
  2394. }
  2395. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2396. m_lastCommand->setRelay(msg->getRelay());
  2397. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.\n"));
  2398. return TRUE;
  2399. }
  2400. return FALSE;
  2401. }
  2402. /**
  2403. * Returns true if there is room in the packet for this command.
  2404. */
  2405. Bool NetPacket::isRoomForTimeOutGameStartMessage(NetCommandRef *msg) {
  2406. Int len = 0;
  2407. NetCommandMsg *cmdMsg = (NetCommandMsg *)(msg->getCommand());
  2408. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2409. ++len;
  2410. len += sizeof(UnsignedByte);
  2411. }
  2412. if (m_lastRelay != msg->getRelay()) {
  2413. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2414. }
  2415. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2416. ++len;
  2417. len += sizeof(UnsignedByte);
  2418. }
  2419. ++len; // For the 'D'
  2420. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2421. return FALSE;
  2422. }
  2423. return TRUE;
  2424. }
  2425. /**
  2426. * Add a Progress command to the packet. Returns true if successful.
  2427. */
  2428. Bool NetPacket::addLoadCompleteMessage(NetCommandRef *msg) {
  2429. Bool needNewCommandID = FALSE;
  2430. if (isRoomForLoadCompleteMessage(msg)) {
  2431. NetCommandMsg *cmdMsg = (NetCommandMsg *)(msg->getCommand());
  2432. // If necessary, put the NetCommandType into the packet.
  2433. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2434. m_packet[m_packetLen] = 'T';
  2435. ++m_packetLen;
  2436. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2437. m_packetLen += sizeof(UnsignedByte);
  2438. m_lastCommandType = cmdMsg->getNetCommandType();
  2439. }
  2440. // If necessary, put the relay into the packet.
  2441. if (m_lastRelay != msg->getRelay()) {
  2442. m_packet[m_packetLen] = 'R';
  2443. ++m_packetLen;
  2444. UnsignedByte newRelay = msg->getRelay();
  2445. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  2446. m_packetLen += sizeof(UnsignedByte);
  2447. m_lastRelay = newRelay;
  2448. }
  2449. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2450. m_packet[m_packetLen] = 'P';
  2451. ++m_packetLen;
  2452. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2453. m_packetLen += sizeof(UnsignedByte);
  2454. m_lastPlayerID = cmdMsg->getPlayerID();
  2455. needNewCommandID = TRUE;
  2456. }
  2457. // If necessary, specify the command ID of this command.
  2458. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2459. m_packet[m_packetLen] = 'C';
  2460. ++m_packetLen;
  2461. UnsignedShort newID = cmdMsg->getID();
  2462. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  2463. m_packetLen += sizeof(UnsignedShort);
  2464. }
  2465. m_lastCommandID = cmdMsg->getID();
  2466. m_packet[m_packetLen] = 'D';
  2467. ++m_packetLen;
  2468. ++m_numCommands;
  2469. if (m_lastCommand != NULL) {
  2470. m_lastCommand->deleteInstance();
  2471. m_lastCommand = NULL;
  2472. }
  2473. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2474. m_lastCommand->setRelay(msg->getRelay());
  2475. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.\n"));
  2476. return TRUE;
  2477. }
  2478. return FALSE;
  2479. }
  2480. /**
  2481. * Returns true if there is room in the packet for this command.
  2482. */
  2483. Bool NetPacket::isRoomForLoadCompleteMessage(NetCommandRef *msg) {
  2484. Int len = 0;
  2485. NetCommandMsg *cmdMsg = (NetCommandMsg *)(msg->getCommand());
  2486. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2487. ++len;
  2488. len += sizeof(UnsignedByte);
  2489. }
  2490. if (m_lastRelay != msg->getRelay()) {
  2491. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2492. }
  2493. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2494. ++len;
  2495. len += sizeof(UnsignedByte);
  2496. }
  2497. ++len; // For the 'D'
  2498. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2499. return FALSE;
  2500. }
  2501. return TRUE;
  2502. }
  2503. /**
  2504. * Add a Progress command to the packet. Returns true if successful.
  2505. */
  2506. Bool NetPacket::addProgressMessage(NetCommandRef *msg) {
  2507. if (isRoomForProgressMessage(msg)) {
  2508. NetProgressCommandMsg *cmdMsg = (NetProgressCommandMsg *)(msg->getCommand());
  2509. // If necessary, put the NetCommandType into the packet.
  2510. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2511. m_packet[m_packetLen] = 'T';
  2512. ++m_packetLen;
  2513. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2514. m_packetLen += sizeof(UnsignedByte);
  2515. m_lastCommandType = cmdMsg->getNetCommandType();
  2516. }
  2517. // If necessary, put the relay into the packet.
  2518. if (m_lastRelay != msg->getRelay()) {
  2519. m_packet[m_packetLen] = 'R';
  2520. ++m_packetLen;
  2521. UnsignedByte newRelay = msg->getRelay();
  2522. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  2523. m_packetLen += sizeof(UnsignedByte);
  2524. m_lastRelay = newRelay;
  2525. }
  2526. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2527. m_packet[m_packetLen] = 'P';
  2528. ++m_packetLen;
  2529. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2530. m_packetLen += sizeof(UnsignedByte);
  2531. m_lastPlayerID = cmdMsg->getPlayerID();
  2532. }
  2533. m_packet[m_packetLen] = 'D';
  2534. ++m_packetLen;
  2535. m_packet[m_packetLen] = cmdMsg->getPercentage();
  2536. ++m_packetLen;
  2537. ++m_numCommands;
  2538. if (m_lastCommand != NULL) {
  2539. m_lastCommand->deleteInstance();
  2540. m_lastCommand = NULL;
  2541. }
  2542. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2543. m_lastCommand->setRelay(msg->getRelay());
  2544. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.\n"));
  2545. return TRUE;
  2546. }
  2547. return FALSE;
  2548. }
  2549. /**
  2550. * Returns true if there is room in the packet for this command.
  2551. */
  2552. Bool NetPacket::isRoomForProgressMessage(NetCommandRef *msg) {
  2553. Int len = 0;
  2554. NetProgressCommandMsg *cmdMsg = (NetProgressCommandMsg *)(msg->getCommand());
  2555. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2556. ++len;
  2557. len += sizeof(UnsignedByte);
  2558. }
  2559. if (m_lastRelay != msg->getRelay()) {
  2560. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2561. }
  2562. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2563. ++len;
  2564. len += sizeof(UnsignedByte);
  2565. }
  2566. ++len; // For the 'D'
  2567. ++len; // percentage
  2568. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2569. return FALSE;
  2570. }
  2571. return TRUE;
  2572. }
  2573. Bool NetPacket::addDisconnectVoteCommand(NetCommandRef *msg) {
  2574. Bool needNewCommandID = FALSE;
  2575. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectVoteCommand - entering...\n"));
  2576. // need type, player id, relay, command id, slot number
  2577. if (isRoomForDisconnectVoteMessage(msg)) {
  2578. NetDisconnectVoteCommandMsg *cmdMsg = (NetDisconnectVoteCommandMsg *)(msg->getCommand());
  2579. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectVoteCommand - adding run ahead command\n"));
  2580. // If necessary, put the NetCommandType into the packet.
  2581. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2582. m_packet[m_packetLen] = 'T';
  2583. ++m_packetLen;
  2584. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2585. m_packetLen += sizeof(UnsignedByte);
  2586. m_lastCommandType = cmdMsg->getNetCommandType();
  2587. }
  2588. // If necessary, put the relay into the packet.
  2589. if (m_lastRelay != msg->getRelay()) {
  2590. m_packet[m_packetLen] = 'R';
  2591. ++m_packetLen;
  2592. UnsignedByte newRelay = msg->getRelay();
  2593. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  2594. m_packetLen += sizeof(UnsignedByte);
  2595. m_lastRelay = newRelay;
  2596. }
  2597. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  2598. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2599. m_packet[m_packetLen] = 'P';
  2600. ++m_packetLen;
  2601. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2602. m_packetLen += sizeof(UnsignedByte);
  2603. m_lastPlayerID = cmdMsg->getPlayerID();
  2604. needNewCommandID = TRUE;
  2605. }
  2606. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  2607. // If necessary, specify the command ID of this command.
  2608. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2609. m_packet[m_packetLen] = 'C';
  2610. ++m_packetLen;
  2611. UnsignedShort newID = cmdMsg->getID();
  2612. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  2613. m_packetLen += sizeof(UnsignedShort);
  2614. }
  2615. m_lastCommandID = cmdMsg->getID();
  2616. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  2617. m_packet[m_packetLen] = 'D';
  2618. ++m_packetLen;
  2619. UnsignedByte slot = cmdMsg->getSlot();
  2620. memcpy(m_packet + m_packetLen, &slot, sizeof(slot));
  2621. m_packetLen += sizeof(slot);
  2622. UnsignedInt voteFrame = cmdMsg->getVoteFrame();
  2623. memcpy(m_packet + m_packetLen, &voteFrame, sizeof(voteFrame));
  2624. m_packetLen += sizeof(voteFrame);
  2625. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectVoteCommand - added disconnect vote command, player id %d command id %d, voted slot %d\n", m_lastPlayerID, m_lastCommandID, slot));
  2626. ++m_numCommands;
  2627. if (m_lastCommand != NULL) {
  2628. m_lastCommand->deleteInstance();
  2629. m_lastCommand = NULL;
  2630. }
  2631. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2632. m_lastCommand->setRelay(msg->getRelay());
  2633. return TRUE;
  2634. }
  2635. return FALSE;
  2636. }
  2637. /**
  2638. * Returns true if there is room for this player disconnect command in this packet.
  2639. */
  2640. Bool NetPacket::isRoomForDisconnectVoteMessage(NetCommandRef *msg) {
  2641. Int len = 0;
  2642. Bool needNewCommandID = FALSE;
  2643. NetDisconnectVoteCommandMsg *cmdMsg = (NetDisconnectVoteCommandMsg *)(msg->getCommand());
  2644. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2645. ++len;
  2646. len += sizeof(UnsignedByte);
  2647. }
  2648. if (m_lastRelay != msg->getRelay()) {
  2649. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2650. }
  2651. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2652. ++len;
  2653. len += sizeof(UnsignedByte);
  2654. needNewCommandID = TRUE;
  2655. }
  2656. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2657. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  2658. }
  2659. ++len; // the 'D'
  2660. len += sizeof(UnsignedByte); // slot number
  2661. len += sizeof(UnsignedInt); // vote frame
  2662. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2663. return FALSE;
  2664. }
  2665. return TRUE;
  2666. }
  2667. Bool NetPacket::addDisconnectChatCommand(NetCommandRef *msg) {
  2668. // type, player, id, relay, data
  2669. // data format: 1 byte string length, string (two bytes per character)
  2670. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - Entering...\n"));
  2671. if (isRoomForDisconnectChatMessage(msg)) {
  2672. NetDisconnectChatCommandMsg *cmdMsg = (NetDisconnectChatCommandMsg *)(msg->getCommand());
  2673. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - adding run ahead command\n"));
  2674. // If necessary, put the NetCommandType into the packet.
  2675. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2676. m_packet[m_packetLen] = 'T';
  2677. ++m_packetLen;
  2678. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2679. m_packetLen += sizeof(UnsignedByte);
  2680. m_lastCommandType = cmdMsg->getNetCommandType();
  2681. }
  2682. // If necessary, put the relay into the packet.
  2683. if (m_lastRelay != msg->getRelay()) {
  2684. m_packet[m_packetLen] = 'R';
  2685. ++m_packetLen;
  2686. UnsignedByte newRelay = msg->getRelay();
  2687. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  2688. m_packetLen += sizeof(UnsignedByte);
  2689. m_lastRelay = newRelay;
  2690. }
  2691. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  2692. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2693. m_packet[m_packetLen] = 'P';
  2694. ++m_packetLen;
  2695. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2696. m_packetLen += sizeof(UnsignedByte);
  2697. m_lastPlayerID = cmdMsg->getPlayerID();
  2698. }
  2699. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  2700. m_packet[m_packetLen] = 'D';
  2701. ++m_packetLen;
  2702. UnicodeString unitext = cmdMsg->getText();
  2703. UnsignedByte length = unitext.getLength();
  2704. memcpy(m_packet + m_packetLen, &length, sizeof(UnsignedByte));
  2705. m_packetLen += sizeof(UnsignedByte);
  2706. memcpy(m_packet + m_packetLen, unitext.str(), length * sizeof(UnsignedShort));
  2707. m_packetLen += length * sizeof(UnsignedShort);
  2708. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added disconnect chat command\n"));
  2709. ++m_numCommands;
  2710. if (m_lastCommand != NULL) {
  2711. m_lastCommand->deleteInstance();
  2712. m_lastCommand = NULL;
  2713. }
  2714. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2715. m_lastCommand->setRelay(msg->getRelay());
  2716. return TRUE;
  2717. }
  2718. return FALSE;
  2719. }
  2720. Bool NetPacket::isRoomForDisconnectChatMessage(NetCommandRef *msg) {
  2721. Int len = 0;
  2722. NetDisconnectChatCommandMsg *cmdMsg = (NetDisconnectChatCommandMsg *)(msg->getCommand());
  2723. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2724. ++len;
  2725. len += sizeof(UnsignedByte);
  2726. }
  2727. if (m_lastRelay != msg->getRelay()) {
  2728. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2729. }
  2730. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2731. ++len;
  2732. len += sizeof(UnsignedByte);
  2733. }
  2734. ++len; // the 'D'
  2735. len += sizeof(UnsignedByte); // string length
  2736. UnsignedByte textLen = cmdMsg->getText().getLength();
  2737. len += textLen * sizeof(UnsignedShort);
  2738. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2739. return FALSE;
  2740. }
  2741. return TRUE;
  2742. }
  2743. Bool NetPacket::addChatCommand(NetCommandRef *msg) {
  2744. Bool needNewCommandID = FALSE;
  2745. if (isRoomForChatMessage(msg)) {
  2746. NetChatCommandMsg *cmdMsg = (NetChatCommandMsg *)(msg->getCommand());
  2747. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - adding run ahead command\n"));
  2748. // If necessary, put the NetCommandType into the packet.
  2749. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2750. m_packet[m_packetLen] = 'T';
  2751. ++m_packetLen;
  2752. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2753. m_packetLen += sizeof(UnsignedByte);
  2754. m_lastCommandType = cmdMsg->getNetCommandType();
  2755. }
  2756. // If necessary, put the execution frame into the packet.
  2757. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  2758. m_packet[m_packetLen] = 'F';
  2759. ++m_packetLen;
  2760. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  2761. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  2762. m_packetLen += sizeof(UnsignedInt);
  2763. m_lastFrame = newframe;
  2764. }
  2765. // If necessary, put the relay into the packet.
  2766. if (m_lastRelay != msg->getRelay()) {
  2767. m_packet[m_packetLen] = 'R';
  2768. ++m_packetLen;
  2769. UnsignedByte newRelay = msg->getRelay();
  2770. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  2771. m_packetLen += sizeof(UnsignedByte);
  2772. m_lastRelay = newRelay;
  2773. }
  2774. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  2775. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2776. m_packet[m_packetLen] = 'P';
  2777. ++m_packetLen;
  2778. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2779. m_packetLen += sizeof(UnsignedByte);
  2780. m_lastPlayerID = cmdMsg->getPlayerID();
  2781. needNewCommandID = TRUE;
  2782. }
  2783. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  2784. // If necessary, specify the command ID of this command.
  2785. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2786. m_packet[m_packetLen] = 'C';
  2787. ++m_packetLen;
  2788. UnsignedShort newID = cmdMsg->getID();
  2789. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  2790. m_packetLen += sizeof(UnsignedShort);
  2791. }
  2792. m_lastCommandID = cmdMsg->getID();
  2793. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  2794. m_packet[m_packetLen] = 'D';
  2795. ++m_packetLen;
  2796. UnicodeString unitext = cmdMsg->getText();
  2797. UnsignedByte length = unitext.getLength();
  2798. Int playerMask = cmdMsg->getPlayerMask();
  2799. memcpy(m_packet + m_packetLen, &length, sizeof(UnsignedByte));
  2800. m_packetLen += sizeof(UnsignedByte);
  2801. memcpy(m_packet + m_packetLen, unitext.str(), length * sizeof(UnsignedShort));
  2802. m_packetLen += length * sizeof(UnsignedShort);
  2803. memcpy(m_packet + m_packetLen, &playerMask, sizeof(Int));
  2804. m_packetLen += sizeof(Int);
  2805. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added chat command\n"));
  2806. ++m_numCommands;
  2807. if (m_lastCommand != NULL) {
  2808. m_lastCommand->deleteInstance();
  2809. m_lastCommand = NULL;
  2810. }
  2811. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2812. m_lastCommand->setRelay(msg->getRelay());
  2813. return TRUE;
  2814. }
  2815. return FALSE;
  2816. }
  2817. Bool NetPacket::isRoomForChatMessage(NetCommandRef *msg) {
  2818. Bool needNewCommandID = FALSE;
  2819. Int len = 0;
  2820. NetChatCommandMsg *cmdMsg = (NetChatCommandMsg *)(msg->getCommand());
  2821. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2822. ++len;
  2823. len += sizeof(UnsignedByte);
  2824. }
  2825. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  2826. len += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  2827. }
  2828. if (m_lastRelay != msg->getRelay()) {
  2829. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2830. }
  2831. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2832. ++len;
  2833. len += sizeof(UnsignedByte);
  2834. needNewCommandID = TRUE;
  2835. }
  2836. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  2837. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  2838. }
  2839. ++len; // the 'D'
  2840. len += sizeof(UnsignedByte); // string length
  2841. UnsignedByte textLen = cmdMsg->getText().getLength();
  2842. len += textLen * sizeof(UnsignedShort);
  2843. len += sizeof(Int); // playerMask
  2844. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2845. return FALSE;
  2846. }
  2847. return TRUE;
  2848. }
  2849. Bool NetPacket::addPacketRouterAckCommand(NetCommandRef *msg) {
  2850. // need type, player id, relay, command id, slot number
  2851. if (isRoomForPacketRouterAckMessage(msg)) {
  2852. NetPacketRouterAckCommandMsg *cmdMsg = (NetPacketRouterAckCommandMsg *)(msg->getCommand());
  2853. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPacketRouterAckCommand - adding packet router query command\n"));
  2854. // If necessary, put the NetCommandType into the packet.
  2855. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2856. m_packet[m_packetLen] = 'T';
  2857. ++m_packetLen;
  2858. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2859. m_packetLen += sizeof(UnsignedByte);
  2860. m_lastCommandType = cmdMsg->getNetCommandType();
  2861. }
  2862. // If necessary, put the relay into the packet.
  2863. if (m_lastRelay != msg->getRelay()) {
  2864. m_packet[m_packetLen] = 'R';
  2865. ++m_packetLen;
  2866. UnsignedByte newRelay = msg->getRelay();
  2867. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  2868. m_packetLen += sizeof(UnsignedByte);
  2869. m_lastRelay = newRelay;
  2870. }
  2871. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  2872. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2873. m_packet[m_packetLen] = 'P';
  2874. ++m_packetLen;
  2875. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2876. m_packetLen += sizeof(UnsignedByte);
  2877. m_lastPlayerID = cmdMsg->getPlayerID();
  2878. }
  2879. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  2880. m_packet[m_packetLen] = 'D';
  2881. ++m_packetLen;
  2882. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added packet router ack command, player id %d\n", m_lastPlayerID));
  2883. ++m_numCommands;
  2884. if (m_lastCommand != NULL) {
  2885. m_lastCommand->deleteInstance();
  2886. m_lastCommand = NULL;
  2887. }
  2888. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2889. m_lastCommand->setRelay(msg->getRelay());
  2890. return TRUE;
  2891. }
  2892. return FALSE;
  2893. }
  2894. /**
  2895. * Returns true if there is room for this packet router ack command in this packet.
  2896. */
  2897. Bool NetPacket::isRoomForPacketRouterAckMessage(NetCommandRef *msg) {
  2898. Int len = 0;
  2899. NetPacketRouterAckCommandMsg *cmdMsg = (NetPacketRouterAckCommandMsg *)(msg->getCommand());
  2900. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2901. ++len;
  2902. len += sizeof(UnsignedByte);
  2903. }
  2904. if (m_lastRelay != msg->getRelay()) {
  2905. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2906. }
  2907. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2908. ++len;
  2909. len += sizeof(UnsignedByte);
  2910. }
  2911. ++len; // the 'D'
  2912. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2913. return FALSE;
  2914. }
  2915. return TRUE;
  2916. }
  2917. Bool NetPacket::addPacketRouterQueryCommand(NetCommandRef *msg) {
  2918. // need type, player id, relay, command id, slot number
  2919. if (isRoomForPacketRouterQueryMessage(msg)) {
  2920. NetPacketRouterQueryCommandMsg *cmdMsg = (NetPacketRouterQueryCommandMsg *)(msg->getCommand());
  2921. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPacketRouterQueryCommand - adding packet router query command\n"));
  2922. // If necessary, put the NetCommandType into the packet.
  2923. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2924. m_packet[m_packetLen] = 'T';
  2925. ++m_packetLen;
  2926. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2927. m_packetLen += sizeof(UnsignedByte);
  2928. m_lastCommandType = cmdMsg->getNetCommandType();
  2929. }
  2930. // If necessary, put the relay into the packet.
  2931. if (m_lastRelay != msg->getRelay()) {
  2932. m_packet[m_packetLen] = 'R';
  2933. ++m_packetLen;
  2934. UnsignedByte newRelay = msg->getRelay();
  2935. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  2936. m_packetLen += sizeof(UnsignedByte);
  2937. m_lastRelay = newRelay;
  2938. }
  2939. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  2940. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2941. m_packet[m_packetLen] = 'P';
  2942. ++m_packetLen;
  2943. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  2944. m_packetLen += sizeof(UnsignedByte);
  2945. m_lastPlayerID = cmdMsg->getPlayerID();
  2946. }
  2947. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  2948. m_packet[m_packetLen] = 'D';
  2949. ++m_packetLen;
  2950. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added packet router query command, player id %d\n", m_lastPlayerID));
  2951. ++m_numCommands;
  2952. if (m_lastCommand != NULL) {
  2953. m_lastCommand->deleteInstance();
  2954. m_lastCommand = NULL;
  2955. }
  2956. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  2957. m_lastCommand->setRelay(msg->getRelay());
  2958. return TRUE;
  2959. }
  2960. return FALSE;
  2961. }
  2962. /**
  2963. * Returns true if there is room for this packet router query command in this packet.
  2964. */
  2965. Bool NetPacket::isRoomForPacketRouterQueryMessage(NetCommandRef *msg) {
  2966. Int len = 0;
  2967. NetPacketRouterQueryCommandMsg *cmdMsg = (NetPacketRouterQueryCommandMsg *)(msg->getCommand());
  2968. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2969. ++len;
  2970. len += sizeof(UnsignedByte);
  2971. }
  2972. if (m_lastRelay != msg->getRelay()) {
  2973. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  2974. }
  2975. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  2976. ++len;
  2977. len += sizeof(UnsignedByte);
  2978. }
  2979. ++len; // the 'D'
  2980. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  2981. return FALSE;
  2982. }
  2983. return TRUE;
  2984. }
  2985. Bool NetPacket::addDisconnectPlayerCommand(NetCommandRef *msg) {
  2986. Bool needNewCommandID = FALSE;
  2987. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectPlayerCommand - entering...\n"));
  2988. // need type, player id, relay, command id, slot number
  2989. if (isRoomForDisconnectPlayerMessage(msg)) {
  2990. NetDisconnectPlayerCommandMsg *cmdMsg = (NetDisconnectPlayerCommandMsg *)(msg->getCommand());
  2991. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectPlayerCommand - adding run ahead command\n"));
  2992. // If necessary, put the NetCommandType into the packet.
  2993. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  2994. m_packet[m_packetLen] = 'T';
  2995. ++m_packetLen;
  2996. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  2997. m_packetLen += sizeof(UnsignedByte);
  2998. m_lastCommandType = cmdMsg->getNetCommandType();
  2999. }
  3000. // If necessary, put the relay into the packet.
  3001. if (m_lastRelay != msg->getRelay()) {
  3002. m_packet[m_packetLen] = 'R';
  3003. ++m_packetLen;
  3004. UnsignedByte newRelay = msg->getRelay();
  3005. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3006. m_packetLen += sizeof(UnsignedByte);
  3007. m_lastRelay = newRelay;
  3008. }
  3009. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  3010. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3011. m_packet[m_packetLen] = 'P';
  3012. ++m_packetLen;
  3013. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3014. m_packetLen += sizeof(UnsignedByte);
  3015. m_lastPlayerID = cmdMsg->getPlayerID();
  3016. needNewCommandID = TRUE;
  3017. }
  3018. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  3019. // If necessary, specify the command ID of this command.
  3020. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3021. m_packet[m_packetLen] = 'C';
  3022. ++m_packetLen;
  3023. UnsignedShort newID = cmdMsg->getID();
  3024. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  3025. m_packetLen += sizeof(UnsignedShort);
  3026. }
  3027. m_lastCommandID = cmdMsg->getID();
  3028. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  3029. m_packet[m_packetLen] = 'D';
  3030. ++m_packetLen;
  3031. UnsignedByte slot = cmdMsg->getDisconnectSlot();
  3032. memcpy(m_packet + m_packetLen, &slot, sizeof(slot));
  3033. m_packetLen += sizeof(slot);
  3034. UnsignedInt disconnectFrame = cmdMsg->getDisconnectFrame();
  3035. memcpy(m_packet + m_packetLen, &disconnectFrame, sizeof(disconnectFrame));
  3036. m_packetLen += sizeof(disconnectFrame);
  3037. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectPlayerCommand - added disconnect player command, player id %d command id %d, disconnecting slot %d\n", m_lastPlayerID, m_lastCommandID, slot));
  3038. ++m_numCommands;
  3039. if (m_lastCommand != NULL) {
  3040. m_lastCommand->deleteInstance();
  3041. m_lastCommand = NULL;
  3042. }
  3043. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3044. m_lastCommand->setRelay(msg->getRelay());
  3045. return TRUE;
  3046. }
  3047. return FALSE;
  3048. }
  3049. /**
  3050. * Returns true if there is room for this player disconnect command in this packet.
  3051. */
  3052. Bool NetPacket::isRoomForDisconnectPlayerMessage(NetCommandRef *msg) {
  3053. Int len = 0;
  3054. Bool needNewCommandID = FALSE;
  3055. NetDisconnectPlayerCommandMsg *cmdMsg = (NetDisconnectPlayerCommandMsg *)(msg->getCommand());
  3056. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3057. ++len;
  3058. len += sizeof(UnsignedByte);
  3059. }
  3060. if (m_lastRelay != msg->getRelay()) {
  3061. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  3062. }
  3063. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3064. ++len;
  3065. len += sizeof(UnsignedByte);
  3066. needNewCommandID = TRUE;
  3067. }
  3068. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3069. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  3070. }
  3071. ++len; // the 'D'
  3072. len += sizeof(UnsignedByte); // slot number
  3073. len += sizeof(UnsignedInt); // disconnectFrame
  3074. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3075. return FALSE;
  3076. }
  3077. return TRUE;
  3078. }
  3079. /**
  3080. * Add a keep alive command to the packet. Returns true if successful.
  3081. */
  3082. Bool NetPacket::addDisconnectKeepAliveCommand(NetCommandRef *msg) {
  3083. if (isRoomForDisconnectKeepAliveMessage(msg)) {
  3084. NetDisconnectKeepAliveCommandMsg *cmdMsg = (NetDisconnectKeepAliveCommandMsg *)(msg->getCommand());
  3085. // If necessary, put the NetCommandType into the packet.
  3086. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3087. m_packet[m_packetLen] = 'T';
  3088. ++m_packetLen;
  3089. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3090. m_packetLen += sizeof(UnsignedByte);
  3091. m_lastCommandType = cmdMsg->getNetCommandType();
  3092. }
  3093. // If necessary, put the relay into the packet.
  3094. if (m_lastRelay != msg->getRelay()) {
  3095. m_packet[m_packetLen] = 'R';
  3096. ++m_packetLen;
  3097. UnsignedByte newRelay = msg->getRelay();
  3098. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3099. m_packetLen += sizeof(UnsignedByte);
  3100. m_lastRelay = newRelay;
  3101. }
  3102. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3103. m_packet[m_packetLen] = 'P';
  3104. ++m_packetLen;
  3105. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3106. m_packetLen += sizeof(UnsignedByte);
  3107. m_lastPlayerID = cmdMsg->getPlayerID();
  3108. }
  3109. m_packet[m_packetLen] = 'D';
  3110. ++m_packetLen;
  3111. ++m_numCommands;
  3112. if (m_lastCommand != NULL) {
  3113. m_lastCommand->deleteInstance();
  3114. m_lastCommand = NULL;
  3115. }
  3116. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3117. m_lastCommand->setRelay(msg->getRelay());
  3118. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.\n"));
  3119. return TRUE;
  3120. }
  3121. return FALSE;
  3122. }
  3123. /**
  3124. * Returns true if there is room in the packet for this command.
  3125. */
  3126. Bool NetPacket::isRoomForDisconnectKeepAliveMessage(NetCommandRef *msg) {
  3127. Int len = 0;
  3128. NetDisconnectKeepAliveCommandMsg *cmdMsg = (NetDisconnectKeepAliveCommandMsg *)(msg->getCommand());
  3129. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3130. ++len;
  3131. len += sizeof(UnsignedByte);
  3132. }
  3133. if (m_lastRelay != msg->getRelay()) {
  3134. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  3135. }
  3136. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3137. ++len;
  3138. len += sizeof(UnsignedByte);
  3139. }
  3140. ++len; // For the 'D'
  3141. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3142. return FALSE;
  3143. }
  3144. return TRUE;
  3145. }
  3146. /**
  3147. * Add a keep alive command to the packet. Returns true if successful.
  3148. */
  3149. Bool NetPacket::addKeepAliveCommand(NetCommandRef *msg) {
  3150. if (isRoomForKeepAliveMessage(msg)) {
  3151. NetKeepAliveCommandMsg *cmdMsg = (NetKeepAliveCommandMsg *)(msg->getCommand());
  3152. // If necessary, put the NetCommandType into the packet.
  3153. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3154. m_packet[m_packetLen] = 'T';
  3155. ++m_packetLen;
  3156. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3157. m_packetLen += sizeof(UnsignedByte);
  3158. m_lastCommandType = cmdMsg->getNetCommandType();
  3159. }
  3160. // If necessary, put the relay into the packet.
  3161. if (m_lastRelay != msg->getRelay()) {
  3162. m_packet[m_packetLen] = 'R';
  3163. ++m_packetLen;
  3164. UnsignedByte newRelay = msg->getRelay();
  3165. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3166. m_packetLen += sizeof(UnsignedByte);
  3167. m_lastRelay = newRelay;
  3168. }
  3169. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3170. m_packet[m_packetLen] = 'P';
  3171. ++m_packetLen;
  3172. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3173. m_packetLen += sizeof(UnsignedByte);
  3174. m_lastPlayerID = cmdMsg->getPlayerID();
  3175. }
  3176. m_packet[m_packetLen] = 'D';
  3177. ++m_packetLen;
  3178. ++m_numCommands;
  3179. if (m_lastCommand != NULL) {
  3180. m_lastCommand->deleteInstance();
  3181. m_lastCommand = NULL;
  3182. }
  3183. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3184. m_lastCommand->setRelay(msg->getRelay());
  3185. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.\n"));
  3186. return TRUE;
  3187. }
  3188. return FALSE;
  3189. }
  3190. /**
  3191. * Returns true if there is room in the packet for this command.
  3192. */
  3193. Bool NetPacket::isRoomForKeepAliveMessage(NetCommandRef *msg) {
  3194. Int len = 0;
  3195. NetKeepAliveCommandMsg *cmdMsg = (NetKeepAliveCommandMsg *)(msg->getCommand());
  3196. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3197. ++len;
  3198. len += sizeof(UnsignedByte);
  3199. }
  3200. if (m_lastRelay != msg->getRelay()) {
  3201. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  3202. }
  3203. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3204. ++len;
  3205. len += sizeof(UnsignedByte);
  3206. }
  3207. ++len; // For the 'D'
  3208. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3209. return FALSE;
  3210. }
  3211. return TRUE;
  3212. }
  3213. /**
  3214. * Add a run ahead command to the packet. Returns true if successful.
  3215. */
  3216. Bool NetPacket::addRunAheadCommand(NetCommandRef *msg) {
  3217. Bool needNewCommandID = FALSE;
  3218. if (isRoomForRunAheadMessage(msg)) {
  3219. NetRunAheadCommandMsg *cmdMsg = (NetRunAheadCommandMsg *)(msg->getCommand());
  3220. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addRunAheadCommand - adding run ahead command\n"));
  3221. // If necessary, put the NetCommandType into the packet.
  3222. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3223. m_packet[m_packetLen] = 'T';
  3224. ++m_packetLen;
  3225. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3226. m_packetLen += sizeof(UnsignedByte);
  3227. m_lastCommandType = cmdMsg->getNetCommandType();
  3228. }
  3229. // If necessary, put the relay into the packet.
  3230. if (m_lastRelay != msg->getRelay()) {
  3231. m_packet[m_packetLen] = 'R';
  3232. ++m_packetLen;
  3233. UnsignedByte newRelay = msg->getRelay();
  3234. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3235. m_packetLen += sizeof(UnsignedByte);
  3236. m_lastRelay = newRelay;
  3237. }
  3238. // If necessary, put the execution frame into the packet.
  3239. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3240. m_packet[m_packetLen] = 'F';
  3241. ++m_packetLen;
  3242. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  3243. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  3244. m_packetLen += sizeof(UnsignedInt);
  3245. m_lastFrame = newframe;
  3246. }
  3247. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  3248. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3249. m_packet[m_packetLen] = 'P';
  3250. ++m_packetLen;
  3251. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3252. m_packetLen += sizeof(UnsignedByte);
  3253. m_lastPlayerID = cmdMsg->getPlayerID();
  3254. needNewCommandID = TRUE;
  3255. }
  3256. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  3257. // If necessary, specify the command ID of this command.
  3258. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3259. m_packet[m_packetLen] = 'C';
  3260. ++m_packetLen;
  3261. UnsignedShort newID = cmdMsg->getID();
  3262. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  3263. m_packetLen += sizeof(UnsignedShort);
  3264. }
  3265. m_lastCommandID = cmdMsg->getID();
  3266. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  3267. m_packet[m_packetLen] = 'D';
  3268. ++m_packetLen;
  3269. UnsignedShort newRunAhead = cmdMsg->getRunAhead();
  3270. memcpy(m_packet + m_packetLen, &newRunAhead, sizeof(UnsignedShort));
  3271. m_packetLen += sizeof(UnsignedShort);
  3272. UnsignedByte newFrameRate = cmdMsg->getFrameRate();
  3273. memcpy(m_packet + m_packetLen, &newFrameRate, sizeof(UnsignedByte));
  3274. m_packetLen += sizeof(UnsignedByte);
  3275. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added run ahead command, frame %d, player id %d command id %d\n", m_lastFrame, m_lastPlayerID, m_lastCommandID));
  3276. ++m_numCommands;
  3277. if (m_lastCommand != NULL) {
  3278. m_lastCommand->deleteInstance();
  3279. m_lastCommand = NULL;
  3280. }
  3281. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3282. m_lastCommand->setRelay(msg->getRelay());
  3283. return TRUE;
  3284. }
  3285. return FALSE;
  3286. }
  3287. /**
  3288. * Returns true if there is room for this run ahead command in this packet.
  3289. */
  3290. Bool NetPacket::isRoomForRunAheadMessage(NetCommandRef *msg) {
  3291. Int len = 0;
  3292. Bool needNewCommandID = FALSE;
  3293. NetRunAheadCommandMsg *cmdMsg = (NetRunAheadCommandMsg *)(msg->getCommand());
  3294. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3295. ++len;
  3296. len += sizeof(UnsignedByte);
  3297. }
  3298. if (m_lastRelay != msg->getRelay()) {
  3299. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  3300. }
  3301. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3302. len += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  3303. }
  3304. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3305. ++len;
  3306. len += sizeof(UnsignedByte);
  3307. needNewCommandID = TRUE;
  3308. }
  3309. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3310. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  3311. }
  3312. ++len; // for 'D'
  3313. len += sizeof(UnsignedShort);
  3314. len += sizeof(UnsignedByte);
  3315. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3316. return FALSE;
  3317. }
  3318. return TRUE;
  3319. }
  3320. /**
  3321. * Add a DestroyPlayer command to the packet. Returns true if successful.
  3322. */
  3323. Bool NetPacket::addDestroyPlayerCommand(NetCommandRef *msg) {
  3324. Bool needNewCommandID = FALSE;
  3325. if (isRoomForDestroyPlayerMessage(msg)) {
  3326. NetDestroyPlayerCommandMsg *cmdMsg = (NetDestroyPlayerCommandMsg *)(msg->getCommand());
  3327. // If necessary, put the NetCommandType into the packet.
  3328. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3329. m_packet[m_packetLen] = 'T';
  3330. ++m_packetLen;
  3331. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3332. m_packetLen += sizeof(UnsignedByte);
  3333. m_lastCommandType = cmdMsg->getNetCommandType();
  3334. }
  3335. // If necessary, put the relay into the packet.
  3336. if (m_lastRelay != msg->getRelay()) {
  3337. m_packet[m_packetLen] = 'R';
  3338. ++m_packetLen;
  3339. UnsignedByte newRelay = msg->getRelay();
  3340. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3341. m_packetLen += sizeof(UnsignedByte);
  3342. m_lastRelay = newRelay;
  3343. }
  3344. // If necessary, put the execution frame into the packet.
  3345. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3346. m_packet[m_packetLen] = 'F';
  3347. ++m_packetLen;
  3348. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  3349. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  3350. m_packetLen += sizeof(UnsignedInt);
  3351. m_lastFrame = newframe;
  3352. }
  3353. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  3354. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3355. m_packet[m_packetLen] = 'P';
  3356. ++m_packetLen;
  3357. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3358. m_packetLen += sizeof(UnsignedByte);
  3359. m_lastPlayerID = cmdMsg->getPlayerID();
  3360. needNewCommandID = TRUE;
  3361. }
  3362. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  3363. // If necessary, specify the command ID of this command.
  3364. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3365. m_packet[m_packetLen] = 'C';
  3366. ++m_packetLen;
  3367. UnsignedShort newID = cmdMsg->getID();
  3368. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  3369. m_packetLen += sizeof(UnsignedShort);
  3370. }
  3371. m_lastCommandID = cmdMsg->getID();
  3372. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  3373. m_packet[m_packetLen] = 'D';
  3374. ++m_packetLen;
  3375. UnsignedInt newVal = cmdMsg->getPlayerIndex();
  3376. memcpy(m_packet + m_packetLen, &newVal, sizeof(UnsignedInt));
  3377. m_packetLen += sizeof(UnsignedInt);
  3378. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added CRC:0x%8.8X info command, frame %d, player id %d command id %d\n", newCRC, m_lastFrame, m_lastPlayerID, m_lastCommandID));
  3379. ++m_numCommands;
  3380. if (m_lastCommand != NULL) {
  3381. m_lastCommand->deleteInstance();
  3382. m_lastCommand = NULL;
  3383. }
  3384. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3385. m_lastCommand->setRelay(msg->getRelay());
  3386. return TRUE;
  3387. }
  3388. return FALSE;
  3389. }
  3390. /**
  3391. * Returns true if there is room for this DestroyPlayer command in this packet.
  3392. */
  3393. Bool NetPacket::isRoomForDestroyPlayerMessage(NetCommandRef *msg) {
  3394. Int len = 0;
  3395. Bool needNewCommandID = FALSE;
  3396. NetDestroyPlayerCommandMsg *cmdMsg = (NetDestroyPlayerCommandMsg *)(msg->getCommand());
  3397. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3398. ++len;
  3399. len += sizeof(UnsignedByte);
  3400. }
  3401. if (m_lastRelay != msg->getRelay()) {
  3402. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  3403. }
  3404. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3405. len += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  3406. }
  3407. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3408. ++len;
  3409. len += sizeof(UnsignedByte);
  3410. needNewCommandID = TRUE;
  3411. }
  3412. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3413. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  3414. }
  3415. ++len; // for 'D'
  3416. len += sizeof(UnsignedInt);
  3417. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3418. return FALSE;
  3419. }
  3420. return TRUE;
  3421. }
  3422. /**
  3423. * Add a run ahead metrics command to the packet. Returns true if successful.
  3424. */
  3425. Bool NetPacket::addRunAheadMetricsCommand(NetCommandRef *msg) {
  3426. Bool needNewCommandID = FALSE;
  3427. if (isRoomForRunAheadMetricsMessage(msg)) {
  3428. NetRunAheadMetricsCommandMsg *cmdMsg = (NetRunAheadMetricsCommandMsg *)(msg->getCommand());
  3429. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addRunAheadMetricsCommand - adding run ahead metrics for player %d, fps = %d, latency = %f\n", cmdMsg->getPlayerID(), cmdMsg->getAverageFps(), cmdMsg->getAverageLatency()));
  3430. // If necessary, put the NetCommandType into the packet.
  3431. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3432. m_packet[m_packetLen] = 'T';
  3433. ++m_packetLen;
  3434. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3435. m_packetLen += sizeof(UnsignedByte);
  3436. m_lastCommandType = cmdMsg->getNetCommandType();
  3437. }
  3438. // If necessary, put the relay into the packet.
  3439. if (m_lastRelay != msg->getRelay()) {
  3440. m_packet[m_packetLen] = 'R';
  3441. ++m_packetLen;
  3442. UnsignedByte newRelay = msg->getRelay();
  3443. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3444. m_packetLen += sizeof(UnsignedByte);
  3445. m_lastRelay = newRelay;
  3446. }
  3447. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  3448. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3449. m_packet[m_packetLen] = 'P';
  3450. ++m_packetLen;
  3451. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3452. m_packetLen += sizeof(UnsignedByte);
  3453. m_lastPlayerID = cmdMsg->getPlayerID();
  3454. needNewCommandID = TRUE;
  3455. }
  3456. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  3457. // If necessary, specify the command ID of this command.
  3458. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3459. m_packet[m_packetLen] = 'C';
  3460. ++m_packetLen;
  3461. UnsignedShort newID = cmdMsg->getID();
  3462. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  3463. m_packetLen += sizeof(UnsignedShort);
  3464. }
  3465. m_lastCommandID = cmdMsg->getID();
  3466. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  3467. m_packet[m_packetLen] = 'D';
  3468. ++m_packetLen;
  3469. // write the average latency
  3470. Real averageLatency = cmdMsg->getAverageLatency();
  3471. memcpy(m_packet + m_packetLen, &averageLatency, sizeof(averageLatency));
  3472. m_packetLen += sizeof(averageLatency);
  3473. // write the average fps
  3474. UnsignedShort averageFps = (UnsignedShort)(cmdMsg->getAverageFps());
  3475. memcpy(m_packet + m_packetLen, &averageFps, sizeof(averageFps));
  3476. m_packetLen += sizeof(averageFps);
  3477. if (m_lastCommand != NULL) {
  3478. m_lastCommand->deleteInstance();
  3479. m_lastCommand = NULL;
  3480. }
  3481. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3482. m_lastCommand->setRelay(msg->getRelay());
  3483. ++m_numCommands;
  3484. return TRUE;
  3485. }
  3486. return FALSE;
  3487. }
  3488. /**
  3489. * Returns true if there is enough room in the packet to fit this message.
  3490. */
  3491. Bool NetPacket::isRoomForRunAheadMetricsMessage(NetCommandRef *msg) {
  3492. Int len = 0;
  3493. Bool needNewCommandID = FALSE;
  3494. NetRunAheadMetricsCommandMsg *cmdMsg = (NetRunAheadMetricsCommandMsg *)(msg->getCommand());
  3495. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3496. ++len;
  3497. len += sizeof(UnsignedByte);
  3498. }
  3499. if (m_lastRelay != msg->getRelay()) {
  3500. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  3501. }
  3502. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3503. ++len;
  3504. len += sizeof(UnsignedByte);
  3505. needNewCommandID = TRUE;
  3506. }
  3507. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3508. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  3509. }
  3510. ++len; // 'D'
  3511. len += sizeof(UnsignedShort);
  3512. len += sizeof(Real);
  3513. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3514. return FALSE;
  3515. }
  3516. return TRUE;
  3517. }
  3518. /**
  3519. * Add a player leave command to the packet. Returns true if successful.
  3520. */
  3521. Bool NetPacket::addPlayerLeaveCommand(NetCommandRef *msg) {
  3522. Bool needNewCommandID = FALSE;
  3523. if (isRoomForPlayerLeaveMessage(msg)) {
  3524. NetPlayerLeaveCommandMsg *cmdMsg = (NetPlayerLeaveCommandMsg *)(msg->getCommand());
  3525. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPlayerLeaveCommand - adding player leave command for player %d\n", cmdMsg->getLeavingPlayerID()));
  3526. // If necessary, put the NetCommandType into the packet.
  3527. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3528. m_packet[m_packetLen] = 'T';
  3529. ++m_packetLen;
  3530. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3531. m_packetLen += sizeof(UnsignedByte);
  3532. m_lastCommandType = cmdMsg->getNetCommandType();
  3533. }
  3534. // If necessary, put the relay into the packet.
  3535. if (m_lastRelay != msg->getRelay()) {
  3536. m_packet[m_packetLen] = 'R';
  3537. ++m_packetLen;
  3538. UnsignedByte newRelay = msg->getRelay();
  3539. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3540. m_packetLen += sizeof(UnsignedByte);
  3541. m_lastRelay = newRelay;
  3542. }
  3543. // If necessary, put the execution frame into the packet.
  3544. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3545. m_packet[m_packetLen] = 'F';
  3546. ++m_packetLen;
  3547. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  3548. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  3549. m_packetLen += sizeof(UnsignedInt);
  3550. m_lastFrame = newframe;
  3551. }
  3552. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  3553. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3554. m_packet[m_packetLen] = 'P';
  3555. ++m_packetLen;
  3556. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3557. m_packetLen += sizeof(UnsignedByte);
  3558. m_lastPlayerID = cmdMsg->getPlayerID();
  3559. needNewCommandID = TRUE;
  3560. }
  3561. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  3562. // If necessary, specify the command ID of this command.
  3563. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3564. m_packet[m_packetLen] = 'C';
  3565. ++m_packetLen;
  3566. UnsignedShort newID = cmdMsg->getID();
  3567. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  3568. m_packetLen += sizeof(UnsignedShort);
  3569. }
  3570. m_lastCommandID = cmdMsg->getID();
  3571. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  3572. m_packet[m_packetLen] = 'D';
  3573. ++m_packetLen;
  3574. UnsignedByte leavingPlayerID = cmdMsg->getLeavingPlayerID();
  3575. memcpy(m_packet + m_packetLen, &leavingPlayerID, sizeof(UnsignedByte));
  3576. m_packetLen += sizeof(UnsignedByte);
  3577. if (m_lastCommand != NULL) {
  3578. m_lastCommand->deleteInstance();
  3579. m_lastCommand = NULL;
  3580. }
  3581. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3582. m_lastCommand->setRelay(msg->getRelay());
  3583. ++m_numCommands;
  3584. return TRUE;
  3585. }
  3586. return FALSE;
  3587. }
  3588. /**
  3589. * Returns true if there is enough room in the packet to fit this message.
  3590. */
  3591. Bool NetPacket::isRoomForPlayerLeaveMessage(NetCommandRef *msg) {
  3592. Int len = 0;
  3593. Bool needNewCommandID = FALSE;
  3594. NetPlayerLeaveCommandMsg *cmdMsg = (NetPlayerLeaveCommandMsg *)(msg->getCommand());
  3595. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3596. ++len;
  3597. len += sizeof(UnsignedByte);
  3598. }
  3599. if (m_lastRelay != msg->getRelay()) {
  3600. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  3601. }
  3602. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3603. len += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  3604. }
  3605. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3606. ++len;
  3607. len += sizeof(UnsignedByte);
  3608. needNewCommandID = TRUE;
  3609. }
  3610. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3611. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  3612. }
  3613. ++len; // for 'D'
  3614. len += sizeof(UnsignedByte);
  3615. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3616. return FALSE;
  3617. }
  3618. return TRUE;
  3619. }
  3620. /**
  3621. * Add this frame command message. Returns true if successful.
  3622. */
  3623. Bool NetPacket::addFrameCommand(NetCommandRef *msg) {
  3624. Bool needNewCommandID = FALSE;
  3625. if (isFrameRepeat(msg)) {
  3626. if (m_packetLen >= MAX_PACKET_SIZE) {
  3627. return FALSE;
  3628. }
  3629. m_packet[m_packetLen] = 'Z';
  3630. ++m_packetLen;
  3631. m_lastCommandID = msg->getCommand()->getID();
  3632. m_lastCommand->deleteInstance();
  3633. m_lastCommand = newInstance(NetCommandRef)(msg->getCommand());
  3634. m_lastCommand->setRelay(msg->getRelay());
  3635. ++m_lastFrame; // need this cause we're actually advancing to the next frame by adding this command.
  3636. ++m_numCommands;
  3637. // frameinfodebug
  3638. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added frame %d, player %d, command count = %d, command id = %d, repeat\n", m_lastFrame, m_lastPlayerID, 0, m_lastCommandID));
  3639. return TRUE;
  3640. }
  3641. if (isRoomForFrameMessage(msg)) {
  3642. NetFrameCommandMsg *cmdMsg = (NetFrameCommandMsg *)(msg->getCommand());
  3643. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addFrameCommand - adding frame command for frame %d, command count = %d, command id = %d\n", cmdMsg->getExecutionFrame(), cmdMsg->getCommandCount(), cmdMsg->getID()));
  3644. // If necessary, put the NetCommandType into the packet.
  3645. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3646. m_packet[m_packetLen] = 'T';
  3647. ++m_packetLen;
  3648. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3649. m_packetLen += sizeof(UnsignedByte);
  3650. m_lastCommandType = cmdMsg->getNetCommandType();
  3651. }
  3652. // If necessary, put the execution frame into the packet.
  3653. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3654. m_packet[m_packetLen] = 'F';
  3655. ++m_packetLen;
  3656. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  3657. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  3658. m_packetLen += sizeof(UnsignedInt);
  3659. m_lastFrame = newframe;
  3660. }
  3661. // If necessary, put the relay into the packet.
  3662. if (m_lastRelay != msg->getRelay()) {
  3663. m_packet[m_packetLen] = 'R';
  3664. ++m_packetLen;
  3665. UnsignedByte newRelay = msg->getRelay();
  3666. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3667. m_packetLen += sizeof(UnsignedByte);
  3668. m_lastRelay = newRelay;
  3669. }
  3670. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay));
  3671. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3672. m_packet[m_packetLen] = 'P';
  3673. ++m_packetLen;
  3674. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3675. m_packetLen += sizeof(UnsignedByte);
  3676. m_lastPlayerID = cmdMsg->getPlayerID();
  3677. needNewCommandID = TRUE;
  3678. }
  3679. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID));
  3680. // If necessary, specify the command ID of this command.
  3681. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3682. m_packet[m_packetLen] = 'C';
  3683. ++m_packetLen;
  3684. UnsignedShort newID = cmdMsg->getID();
  3685. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  3686. m_packetLen += sizeof(UnsignedShort);
  3687. }
  3688. m_lastCommandID = cmdMsg->getID();
  3689. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d\n", m_lastCommandID));
  3690. m_packet[m_packetLen] = 'D';
  3691. ++m_packetLen;
  3692. UnsignedShort cmdCount = cmdMsg->getCommandCount();
  3693. memcpy(m_packet + m_packetLen, &cmdCount, sizeof(UnsignedShort));
  3694. m_packetLen += sizeof(UnsignedShort);
  3695. // frameinfodebug
  3696. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added frame %d, player %d, command count = %d, command id = %d\n", cmdMsg->getExecutionFrame(), cmdMsg->getPlayerID(), cmdMsg->getCommandCount(), cmdMsg->getID()));
  3697. if (m_lastCommand != NULL) {
  3698. m_lastCommand->deleteInstance();
  3699. m_lastCommand = NULL;
  3700. }
  3701. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3702. m_lastCommand->setRelay(msg->getRelay());
  3703. ++m_numCommands;
  3704. return TRUE;
  3705. }
  3706. return FALSE;
  3707. }
  3708. /**
  3709. * Returns true if there is enough room in this packet for this frame message.
  3710. */
  3711. Bool NetPacket::isRoomForFrameMessage(NetCommandRef *msg) {
  3712. Int len = 0;
  3713. Bool needNewCommandID = FALSE;
  3714. NetFrameCommandMsg *cmdMsg = (NetFrameCommandMsg *)(msg->getCommand());
  3715. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3716. ++len;
  3717. len += sizeof(UnsignedByte);
  3718. }
  3719. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3720. len += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  3721. }
  3722. if (m_lastRelay != msg->getRelay()) {
  3723. len += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  3724. }
  3725. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3726. ++len;
  3727. len += sizeof(UnsignedByte);
  3728. needNewCommandID = TRUE;
  3729. }
  3730. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3731. len += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  3732. }
  3733. ++len; // for 'D'
  3734. len += sizeof(UnsignedShort);
  3735. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3736. return FALSE;
  3737. }
  3738. return TRUE;
  3739. }
  3740. Bool NetPacket::isFrameRepeat(NetCommandRef *msg) {
  3741. if (m_lastCommand == NULL) {
  3742. return FALSE;
  3743. }
  3744. if (m_lastCommand->getCommand()->getNetCommandType() != NETCOMMANDTYPE_FRAMEINFO) {
  3745. return FALSE;
  3746. }
  3747. NetFrameCommandMsg *framemsg = (NetFrameCommandMsg *)(msg->getCommand());
  3748. NetFrameCommandMsg *lastmsg = (NetFrameCommandMsg *)(m_lastCommand->getCommand());
  3749. if (framemsg->getCommandCount() != 0) {
  3750. return FALSE;
  3751. }
  3752. if (framemsg->getExecutionFrame() != (lastmsg->getExecutionFrame() + 1)) {
  3753. return FALSE;
  3754. }
  3755. if (msg->getRelay() != m_lastCommand->getRelay()) {
  3756. return FALSE;
  3757. }
  3758. if (framemsg->getID() != (lastmsg->getID() + 1)) {
  3759. return FALSE;
  3760. }
  3761. return TRUE;
  3762. }
  3763. /**
  3764. * Add an ack "both" command.
  3765. */
  3766. Bool NetPacket::addAckBothCommand(NetCommandRef *msg) {
  3767. NetAckBothCommandMsg *ackmsg = (NetAckBothCommandMsg *)(msg->getCommand());
  3768. return addAckCommand(msg, ackmsg->getCommandID(), ackmsg->getOriginalPlayerID());
  3769. }
  3770. /**
  3771. * Add an ack stage 1 command.
  3772. */
  3773. Bool NetPacket::addAckStage1Command(NetCommandRef *msg) {
  3774. NetAckStage1CommandMsg *ackmsg = (NetAckStage1CommandMsg *)(msg->getCommand());
  3775. return addAckCommand(msg, ackmsg->getCommandID(), ackmsg->getOriginalPlayerID());
  3776. }
  3777. /**
  3778. * Add an ack stage 2 command.
  3779. */
  3780. Bool NetPacket::addAckStage2Command(NetCommandRef *msg) {
  3781. NetAckStage2CommandMsg *ackmsg = (NetAckStage2CommandMsg *)(msg->getCommand());
  3782. return addAckCommand(msg, ackmsg->getCommandID(), ackmsg->getOriginalPlayerID());
  3783. }
  3784. /**
  3785. * Add this ack command to the packet. Returns true if successful.
  3786. */
  3787. Bool NetPacket::addAckCommand(NetCommandRef *msg, UnsignedShort commandID, UnsignedByte originalPlayerID) {
  3788. if (isAckRepeat(msg)) {
  3789. if (m_packetLen >= MAX_PACKET_SIZE) {
  3790. return FALSE;
  3791. }
  3792. m_packet[m_packetLen] = 'Z';
  3793. ++m_packetLen;
  3794. ++m_numCommands;
  3795. m_lastCommand->deleteInstance();
  3796. m_lastCommand = NULL;
  3797. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3798. m_lastCommand->setRelay(msg->getRelay());
  3799. return TRUE;
  3800. }
  3801. if (isRoomForAckMessage(msg)) {
  3802. NetCommandMsg *cmdMsg = msg->getCommand();
  3803. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addAckCommand - adding ack for command %d for player %d\n", cmdMsg->getCommandID(), msg->getCommand()->getPlayerID()));
  3804. // If necessary, put the NetCommandType into the packet.
  3805. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3806. m_packet[m_packetLen] = 'T';
  3807. ++m_packetLen;
  3808. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3809. m_packetLen += sizeof(UnsignedByte);
  3810. m_lastCommandType = cmdMsg->getNetCommandType();
  3811. }
  3812. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3813. m_packet[m_packetLen] = 'P';
  3814. ++m_packetLen;
  3815. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3816. m_packetLen += sizeof(UnsignedByte);
  3817. m_lastPlayerID = cmdMsg->getPlayerID();
  3818. }
  3819. // Put in the command id of the command we are acking.
  3820. m_packet[m_packetLen] = 'D';
  3821. ++m_packetLen;
  3822. memcpy(m_packet + m_packetLen, &commandID, sizeof(UnsignedShort));
  3823. m_packetLen += sizeof(UnsignedShort);
  3824. memcpy(m_packet + m_packetLen, &originalPlayerID, sizeof(UnsignedByte));
  3825. m_packetLen += sizeof(UnsignedByte);
  3826. if (m_lastCommand != NULL) {
  3827. m_lastCommand->deleteInstance();
  3828. m_lastCommand = NULL;
  3829. }
  3830. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  3831. m_lastCommand->setRelay(msg->getRelay());
  3832. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added ACK, original player %d, command id %d\n", origPlayerID, cmdID));
  3833. ++m_numCommands;
  3834. return TRUE;
  3835. }
  3836. return FALSE;
  3837. }
  3838. /**
  3839. * Returns true if there is enough room in the packet for this ack message.
  3840. */
  3841. Bool NetPacket::isRoomForAckMessage(NetCommandRef *msg) {
  3842. Int len = 0;
  3843. NetCommandMsg *cmdMsg = msg->getCommand();
  3844. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3845. ++len;
  3846. len += sizeof(UnsignedByte);
  3847. }
  3848. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3849. ++len;
  3850. len += sizeof(UnsignedByte);
  3851. }
  3852. ++len; // for 'D'
  3853. len += sizeof(UnsignedShort);
  3854. len += sizeof(UnsignedByte);
  3855. if ((len + m_packetLen) > MAX_PACKET_SIZE) {
  3856. return FALSE;
  3857. }
  3858. return TRUE;
  3859. }
  3860. Bool NetPacket::isAckRepeat(NetCommandRef *msg) {
  3861. if (m_lastCommand == NULL) {
  3862. return FALSE;
  3863. }
  3864. if (m_lastCommand->getCommand()->getNetCommandType() != msg->getCommand()->getNetCommandType()) {
  3865. return FALSE;
  3866. }
  3867. if (msg->getCommand()->getNetCommandType() == NETCOMMANDTYPE_ACKBOTH) {
  3868. return isAckBothRepeat(msg);
  3869. }
  3870. if (msg->getCommand()->getNetCommandType() == NETCOMMANDTYPE_ACKSTAGE1) {
  3871. return isAckStage1Repeat(msg);
  3872. }
  3873. if (msg->getCommand()->getNetCommandType() == NETCOMMANDTYPE_ACKSTAGE2) {
  3874. return isAckStage2Repeat(msg);
  3875. }
  3876. return FALSE;
  3877. }
  3878. Bool NetPacket::isAckBothRepeat(NetCommandRef *msg) {
  3879. NetAckBothCommandMsg *ack = (NetAckBothCommandMsg *)(msg->getCommand());
  3880. NetAckBothCommandMsg *lastAck = (NetAckBothCommandMsg *)(m_lastCommand->getCommand());
  3881. if (lastAck->getCommandID() != (ack->getCommandID() - 1)) {
  3882. return FALSE;
  3883. }
  3884. if (lastAck->getOriginalPlayerID() != ack->getOriginalPlayerID()) {
  3885. return FALSE;
  3886. }
  3887. if (msg->getRelay() != m_lastCommand->getRelay()) {
  3888. return FALSE;
  3889. }
  3890. return TRUE;
  3891. }
  3892. Bool NetPacket::isAckStage1Repeat(NetCommandRef *msg) {
  3893. NetAckStage2CommandMsg *ack = (NetAckStage2CommandMsg *)(msg->getCommand());
  3894. NetAckStage2CommandMsg *lastAck = (NetAckStage2CommandMsg *)(m_lastCommand->getCommand());
  3895. if (lastAck->getCommandID() != (ack->getCommandID() - 1)) {
  3896. return FALSE;
  3897. }
  3898. if (lastAck->getOriginalPlayerID() != ack->getOriginalPlayerID()) {
  3899. return FALSE;
  3900. }
  3901. if (msg->getRelay() != m_lastCommand->getRelay()) {
  3902. return FALSE;
  3903. }
  3904. return TRUE;
  3905. }
  3906. Bool NetPacket::isAckStage2Repeat(NetCommandRef *msg) {
  3907. NetAckStage2CommandMsg *ack = (NetAckStage2CommandMsg *)(msg->getCommand());
  3908. NetAckStage2CommandMsg *lastAck = (NetAckStage2CommandMsg *)(m_lastCommand->getCommand());
  3909. if (lastAck->getCommandID() != (ack->getCommandID() - 1)) {
  3910. return FALSE;
  3911. }
  3912. if (lastAck->getOriginalPlayerID() != ack->getOriginalPlayerID()) {
  3913. return FALSE;
  3914. }
  3915. if (msg->getRelay() != m_lastCommand->getRelay()) {
  3916. return FALSE;
  3917. }
  3918. return TRUE;
  3919. }
  3920. /**
  3921. * Adds this game command to the packet. Returns true if successful.
  3922. */
  3923. Bool NetPacket::addGameCommand(NetCommandRef *msg) {
  3924. Bool retval = FALSE;
  3925. NetGameCommandMsg *cmdMsg = (NetGameCommandMsg *)(msg->getCommand());
  3926. // get the game message from the NetCommandMsg
  3927. GameMessage *gmsg = cmdMsg->constructGameMessage();
  3928. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addGameCommand for command ID %d\n", cmdMsg->getID()));
  3929. if (isRoomForGameMessage(msg, gmsg)) {
  3930. // Now we know there is enough room, put the new game message into the packet.
  3931. Bool needNewCommandID = FALSE; // this is to allow us to force the starting command ID to be respecified with this command.
  3932. // If necessary, put the NetCommandType into the packet.
  3933. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  3934. m_packet[m_packetLen] = 'T';
  3935. ++m_packetLen;
  3936. m_packet[m_packetLen] = cmdMsg->getNetCommandType();
  3937. m_packetLen += sizeof(UnsignedByte);
  3938. m_lastCommandType = cmdMsg->getNetCommandType();
  3939. }
  3940. // If necessary, put the execution frame into the packet.
  3941. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  3942. m_packet[m_packetLen] = 'F';
  3943. ++m_packetLen;
  3944. UnsignedInt newframe = cmdMsg->getExecutionFrame();
  3945. memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt));
  3946. m_packetLen += sizeof(UnsignedInt);
  3947. m_lastFrame = newframe;
  3948. }
  3949. // If necessary, put the relay into the packet.
  3950. if (m_lastRelay != msg->getRelay()) {
  3951. m_packet[m_packetLen] = 'R';
  3952. ++m_packetLen;
  3953. UnsignedByte newRelay = msg->getRelay();
  3954. memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte));
  3955. m_packetLen += sizeof(UnsignedByte);
  3956. m_lastRelay = newRelay;
  3957. }
  3958. // If necessary, put the playerID into the packet.
  3959. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  3960. m_packet[m_packetLen] = 'P';
  3961. ++m_packetLen;
  3962. m_packet[m_packetLen] = cmdMsg->getPlayerID();
  3963. m_packetLen += sizeof(UnsignedByte);
  3964. //since if we have a new player we need to respecify the starting command ID, lets force the command id to be different.
  3965. needNewCommandID = TRUE;
  3966. m_lastPlayerID = cmdMsg->getPlayerID();
  3967. }
  3968. // If necessary, specify the command ID of this command.
  3969. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  3970. m_packet[m_packetLen] = 'C';
  3971. ++m_packetLen;
  3972. UnsignedShort newID = cmdMsg->getID();
  3973. memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort));
  3974. m_packetLen += sizeof(UnsignedShort);
  3975. }
  3976. m_lastCommandID = cmdMsg->getID();
  3977. m_packet[m_packetLen] = 'D';
  3978. ++m_packetLen;
  3979. // Now copy the GameMessage type into the packet.
  3980. GameMessage::Type newType = gmsg->getType();
  3981. memcpy(m_packet + m_packetLen, &newType, sizeof(GameMessage::Type));
  3982. m_packetLen += sizeof(GameMessage::Type);
  3983. GameMessageParser *parser = newInstance(GameMessageParser)(gmsg);
  3984. UnsignedByte numTypes = parser->getNumTypes();
  3985. memcpy(m_packet + m_packetLen, &numTypes, sizeof(numTypes));
  3986. m_packetLen += sizeof(numTypes);
  3987. GameMessageParserArgumentType *argType = parser->getFirstArgumentType();
  3988. while (argType != NULL) {
  3989. UnsignedByte type = (UnsignedByte)(argType->getType());
  3990. memcpy(m_packet + m_packetLen, &type, sizeof(type));
  3991. m_packetLen += sizeof(type);
  3992. UnsignedByte argTypeCount = argType->getArgCount();
  3993. memcpy(m_packet + m_packetLen, &argTypeCount, sizeof(argTypeCount));
  3994. m_packetLen += sizeof(argTypeCount);
  3995. argType = argType->getNext();
  3996. }
  3997. Int numArgs = gmsg->getArgumentCount();
  3998. for (Int i = 0; i < numArgs; ++i) {
  3999. GameMessageArgumentDataType type = gmsg->getArgumentDataType(i);
  4000. GameMessageArgumentType arg = *(gmsg->getArgument(i));
  4001. writeGameMessageArgumentToPacket(type, arg);
  4002. }
  4003. parser->deleteInstance();
  4004. parser = NULL;
  4005. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addGameMessage - added game message, frame %d, player %d, command ID %d\n", m_lastFrame, m_lastPlayerID, m_lastCommandID));
  4006. ++m_numCommands;
  4007. if (m_lastCommand != NULL) {
  4008. m_lastCommand->deleteInstance();
  4009. m_lastCommand = NULL;
  4010. }
  4011. m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand());
  4012. m_lastCommand->setRelay(msg->getRelay());
  4013. retval = TRUE;
  4014. }
  4015. if (gmsg) {
  4016. gmsg->deleteInstance();
  4017. gmsg = NULL;
  4018. }
  4019. return retval;
  4020. }
  4021. void NetPacket::writeGameMessageArgumentToPacket(GameMessageArgumentDataType type, GameMessageArgumentType arg) {
  4022. if (type == ARGUMENTDATATYPE_INTEGER) {
  4023. memcpy(m_packet + m_packetLen, &(arg.integer), sizeof(arg.integer));
  4024. m_packetLen += sizeof(arg.integer);
  4025. } else if (type == ARGUMENTDATATYPE_REAL) {
  4026. memcpy(m_packet + m_packetLen, &(arg.real), sizeof(arg.real));
  4027. m_packetLen += sizeof(arg.real);
  4028. } else if (type == ARGUMENTDATATYPE_BOOLEAN) {
  4029. memcpy(m_packet + m_packetLen, &(arg.boolean), sizeof(arg.boolean));
  4030. m_packetLen += sizeof(arg.boolean);
  4031. } else if (type == ARGUMENTDATATYPE_OBJECTID) {
  4032. memcpy(m_packet + m_packetLen, &(arg.objectID), sizeof(arg.objectID));
  4033. m_packetLen += sizeof(arg.objectID);
  4034. } else if (type == ARGUMENTDATATYPE_DRAWABLEID) {
  4035. memcpy(m_packet + m_packetLen, &(arg.drawableID), sizeof(arg.drawableID));
  4036. m_packetLen += sizeof(arg.drawableID);
  4037. } else if (type == ARGUMENTDATATYPE_TEAMID) {
  4038. memcpy(m_packet + m_packetLen, &(arg.teamID), sizeof(arg.teamID));
  4039. m_packetLen += sizeof(arg.teamID);
  4040. } else if (type == ARGUMENTDATATYPE_LOCATION) {
  4041. memcpy(m_packet + m_packetLen, &(arg.location), sizeof(arg.location));
  4042. m_packetLen += sizeof(arg.location);
  4043. } else if (type == ARGUMENTDATATYPE_PIXEL) {
  4044. memcpy(m_packet + m_packetLen, &(arg.pixel), sizeof(arg.pixel));
  4045. m_packetLen += sizeof(arg.pixel);
  4046. } else if (type == ARGUMENTDATATYPE_PIXELREGION) {
  4047. memcpy(m_packet + m_packetLen, &(arg.pixelRegion), sizeof(arg.pixelRegion));
  4048. m_packetLen += sizeof(arg.pixelRegion);
  4049. } else if (type == ARGUMENTDATATYPE_TIMESTAMP) {
  4050. memcpy(m_packet + m_packetLen, &(arg.timestamp), sizeof(arg.timestamp));
  4051. m_packetLen += sizeof(arg.timestamp);
  4052. } else if (type == ARGUMENTDATATYPE_WIDECHAR) {
  4053. memcpy(m_packet + m_packetLen, &(arg.wChar), sizeof(arg.wChar));
  4054. m_packetLen += sizeof(arg.wChar);
  4055. }
  4056. }
  4057. /**
  4058. * Returns true if there is enough room in this packet for this message.
  4059. */
  4060. Bool NetPacket::isRoomForGameMessage(NetCommandRef *msg, GameMessage *gmsg) {
  4061. // Calculate how much space the NetCommandMsg will take in this packet.
  4062. Int msglen = 0;
  4063. NetGameCommandMsg *cmdMsg = (NetGameCommandMsg *)(msg->getCommand());
  4064. Bool needNewCommandID = FALSE;
  4065. if (m_lastFrame != cmdMsg->getExecutionFrame()) {
  4066. msglen += sizeof(UnsignedInt) + sizeof(UnsignedByte);
  4067. }
  4068. if (m_lastPlayerID != cmdMsg->getPlayerID()) {
  4069. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  4070. needNewCommandID = TRUE;
  4071. }
  4072. if (m_lastRelay != msg->getRelay()) {
  4073. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  4074. }
  4075. if (m_lastCommandType != cmdMsg->getNetCommandType()) {
  4076. msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
  4077. }
  4078. if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) {
  4079. msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
  4080. }
  4081. GameMessageParser *parser = newInstance(GameMessageParser)(gmsg);
  4082. ++msglen; // for 'D'
  4083. msglen += sizeof(GameMessage::Type);
  4084. msglen += sizeof(UnsignedByte);
  4085. // Int numTypes = parser->getNumTypes();
  4086. GameMessageParserArgumentType *arg = parser->getFirstArgumentType();
  4087. while (arg != NULL) {
  4088. msglen += 2 * sizeof(UnsignedByte); // for the type and number of args of that type declaration.
  4089. GameMessageArgumentDataType type = arg->getType();
  4090. if (type == ARGUMENTDATATYPE_INTEGER) {
  4091. msglen += arg->getArgCount() * sizeof(Int);
  4092. } else if (type == ARGUMENTDATATYPE_REAL) {
  4093. msglen += arg->getArgCount() * sizeof(Real);
  4094. } else if (type == ARGUMENTDATATYPE_BOOLEAN) {
  4095. msglen += arg->getArgCount() * sizeof(Bool);
  4096. } else if (type == ARGUMENTDATATYPE_OBJECTID) {
  4097. msglen += arg->getArgCount() * sizeof(ObjectID);
  4098. } else if (type == ARGUMENTDATATYPE_DRAWABLEID) {
  4099. msglen += arg->getArgCount() * sizeof(DrawableID);
  4100. } else if (type == ARGUMENTDATATYPE_TEAMID) {
  4101. msglen += arg->getArgCount() * sizeof(UnsignedInt);
  4102. } else if (type == ARGUMENTDATATYPE_LOCATION) {
  4103. msglen += arg->getArgCount() * sizeof(Coord3D);
  4104. } else if (type == ARGUMENTDATATYPE_PIXEL) {
  4105. msglen += arg->getArgCount() * sizeof(ICoord2D);
  4106. } else if (type == ARGUMENTDATATYPE_PIXELREGION) {
  4107. msglen += arg->getArgCount() * sizeof(IRegion2D);
  4108. } else if (type == ARGUMENTDATATYPE_TIMESTAMP) {
  4109. msglen += arg->getArgCount() * sizeof(UnsignedInt);
  4110. } else if (type == ARGUMENTDATATYPE_WIDECHAR) {
  4111. msglen += arg->getArgCount() * sizeof(WideChar);
  4112. }
  4113. arg = arg->getNext();
  4114. }
  4115. parser->deleteInstance();
  4116. parser = NULL;
  4117. // Is there enough room in the packet for this message?
  4118. if (msglen > (MAX_PACKET_SIZE - m_packetLen)) {
  4119. return FALSE; // there isn't enough room in this packet for this new message.
  4120. }
  4121. return TRUE;
  4122. }
  4123. /**
  4124. * Returns the list of commands that are in this packet.
  4125. */
  4126. NetCommandList * NetPacket::getCommandList() {
  4127. NetCommandList *retval = newInstance(NetCommandList);
  4128. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::getCommandList, packet length = %d\n", m_packetLen));
  4129. retval->init();
  4130. // These need to be the same as the default values for m_lastPlayerID, m_lastFrame, etc.
  4131. UnsignedByte playerID = 0;
  4132. UnsignedInt frame = 0;
  4133. UnsignedShort commandID = 1; // The first command is going to be
  4134. UnsignedByte commandType = 0;
  4135. UnsignedByte relay = 0;
  4136. NetCommandRef *lastCommand = NULL;
  4137. Int i = 0;
  4138. while (i < m_packetLen) {
  4139. if (m_packet[i] == 'T') {
  4140. ++i;
  4141. memcpy(&commandType, m_packet + i, sizeof(UnsignedByte));
  4142. i += sizeof(UnsignedByte);
  4143. } else if (m_packet[i] == 'F') {
  4144. ++i;
  4145. memcpy(&frame, m_packet + i, sizeof(UnsignedInt));
  4146. i += sizeof(UnsignedInt);
  4147. } else if (m_packet[i] == 'P') {
  4148. ++i;
  4149. memcpy(&playerID, m_packet + i, sizeof(UnsignedByte));
  4150. i += sizeof(UnsignedByte);
  4151. } else if (m_packet[i] == 'R') {
  4152. ++i;
  4153. memcpy(&relay, m_packet + i, sizeof(UnsignedByte));
  4154. i += sizeof(UnsignedByte);
  4155. } else if (m_packet[i] == 'C') {
  4156. ++i;
  4157. memcpy(&commandID, m_packet + i, sizeof(UnsignedShort));
  4158. i += sizeof(UnsignedShort);
  4159. } else if (m_packet[i] == 'D') {
  4160. ++i;
  4161. NetCommandMsg *msg = NULL;
  4162. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::getCommandList() - command of type %d(%s)\n", commandType, GetAsciiNetCommandType((NetCommandType)commandType).str()));
  4163. switch((NetCommandType)commandType)
  4164. {
  4165. case NETCOMMANDTYPE_GAMECOMMAND:
  4166. msg = readGameMessage(m_packet, i);
  4167. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read game command from player %d for frame %d\n", playerID, frame));
  4168. break;
  4169. case NETCOMMANDTYPE_ACKBOTH:
  4170. msg = readAckBothMessage(m_packet, i);
  4171. break;
  4172. case NETCOMMANDTYPE_ACKSTAGE1:
  4173. msg = readAckStage1Message(m_packet, i);
  4174. break;
  4175. case NETCOMMANDTYPE_ACKSTAGE2:
  4176. msg = readAckStage2Message(m_packet, i);
  4177. break;
  4178. case NETCOMMANDTYPE_FRAMEINFO:
  4179. msg = readFrameMessage(m_packet, i);
  4180. // frameinfodebug
  4181. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read frame %d from player %d, command count = %d, relay = 0x%X\n", frame, playerID, ((NetFrameCommandMsg *)msg)->getCommandCount(), relay));
  4182. break;
  4183. case NETCOMMANDTYPE_PLAYERLEAVE:
  4184. msg = readPlayerLeaveMessage(m_packet, i);
  4185. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read player leave message from player %d for execution on frame %d\n", playerID, frame));
  4186. break;
  4187. case NETCOMMANDTYPE_RUNAHEADMETRICS:
  4188. msg = readRunAheadMetricsMessage(m_packet, i);
  4189. break;
  4190. case NETCOMMANDTYPE_RUNAHEAD:
  4191. msg = readRunAheadMessage(m_packet, i);
  4192. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read run ahead message from player %d for execution on frame %d\n", playerID, frame));
  4193. break;
  4194. case NETCOMMANDTYPE_DESTROYPLAYER:
  4195. msg = readDestroyPlayerMessage(m_packet, i);
  4196. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read CRC info message from player %d for execution on frame %d\n", playerID, frame));
  4197. break;
  4198. case NETCOMMANDTYPE_KEEPALIVE:
  4199. msg = readKeepAliveMessage(m_packet, i);
  4200. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read keep alive message from player %d\n", playerID));
  4201. break;
  4202. case NETCOMMANDTYPE_DISCONNECTKEEPALIVE:
  4203. msg = readDisconnectKeepAliveMessage(m_packet, i);
  4204. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read keep alive message from player %d\n", playerID));
  4205. break;
  4206. case NETCOMMANDTYPE_DISCONNECTPLAYER:
  4207. msg = readDisconnectPlayerMessage(m_packet, i);
  4208. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read disconnect player message from player %d\n", playerID));
  4209. break;
  4210. case NETCOMMANDTYPE_PACKETROUTERQUERY:
  4211. msg = readPacketRouterQueryMessage(m_packet, i);
  4212. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read packet router query message from player %d\n", playerID));
  4213. break;
  4214. case NETCOMMANDTYPE_PACKETROUTERACK:
  4215. msg = readPacketRouterAckMessage(m_packet, i);
  4216. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read packet router ack message from player %d\n", playerID));
  4217. break;
  4218. case NETCOMMANDTYPE_DISCONNECTCHAT:
  4219. msg = readDisconnectChatMessage(m_packet, i);
  4220. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read disconnect chat message from player %d\n", playerID));
  4221. break;
  4222. case NETCOMMANDTYPE_DISCONNECTVOTE:
  4223. msg = readDisconnectVoteMessage(m_packet, i);
  4224. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read disconnect vote message from player %d\n", playerID));
  4225. break;
  4226. case NETCOMMANDTYPE_CHAT:
  4227. msg = readChatMessage(m_packet, i);
  4228. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read chat message from player %d\n", playerID));
  4229. break;
  4230. case NETCOMMANDTYPE_PROGRESS:
  4231. msg = readProgressMessage(m_packet, i);
  4232. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read Progress message from player %d\n", playerID));
  4233. break;
  4234. case NETCOMMANDTYPE_LOADCOMPLETE:
  4235. msg = readLoadCompleteMessage(m_packet, i);
  4236. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read LoadComplete message from player %d\n", playerID));
  4237. break;
  4238. case NETCOMMANDTYPE_TIMEOUTSTART:
  4239. msg = readTimeOutGameStartMessage(m_packet, i);
  4240. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read TimeOutGameStart message from player %d\n", playerID));
  4241. break;
  4242. case NETCOMMANDTYPE_WRAPPER:
  4243. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read Wrapper message from player %d\n", playerID));
  4244. msg = readWrapperMessage(m_packet, i);
  4245. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Done reading Wrapper message from player %d - wrapped command was %d\n", playerID,
  4246. ((NetWrapperCommandMsg *)msg)->getWrappedCommandID()));
  4247. break;
  4248. case NETCOMMANDTYPE_FILE:
  4249. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read file message from player %d\n", playerID));
  4250. msg = readFileMessage(m_packet, i);
  4251. break;
  4252. case NETCOMMANDTYPE_FILEANNOUNCE:
  4253. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read file announce message from player %d\n", playerID));
  4254. msg = readFileAnnounceMessage(m_packet, i);
  4255. break;
  4256. case NETCOMMANDTYPE_FILEPROGRESS:
  4257. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read file progress message from player %d\n", playerID));
  4258. msg = readFileProgressMessage(m_packet, i);
  4259. break;
  4260. case NETCOMMANDTYPE_DISCONNECTFRAME:
  4261. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read disconnect frame message from player %d\n", playerID));
  4262. msg = readDisconnectFrameMessage(m_packet, i);
  4263. break;
  4264. case NETCOMMANDTYPE_DISCONNECTSCREENOFF:
  4265. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read disconnect screen off message from player %d\n", playerID));
  4266. msg = readDisconnectScreenOffMessage(m_packet, i);
  4267. break;
  4268. case NETCOMMANDTYPE_FRAMERESENDREQUEST:
  4269. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("read frame resend request message from player %d\n", playerID));
  4270. msg = readFrameResendRequestMessage(m_packet, i);
  4271. break;
  4272. }
  4273. if (msg == NULL) {
  4274. DEBUG_CRASH(("Didn't read a message from the packet. Things are about to go wrong."));
  4275. continue;
  4276. }
  4277. // set the info
  4278. msg->setExecutionFrame(frame);
  4279. msg->setPlayerID(playerID);
  4280. msg->setNetCommandType((NetCommandType)commandType);
  4281. msg->setID(commandID);
  4282. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("frame = %d, player = %d, command type = %d, id = %d\n", frame, playerID, commandType, commandID));
  4283. // increment to the next command ID.
  4284. if (DoesCommandRequireACommandID((NetCommandType)commandType)) {
  4285. ++commandID;
  4286. }
  4287. // add the message to the list.
  4288. NetCommandRef *ref = retval->addMessage(msg);
  4289. if (ref != NULL) {
  4290. ref->setRelay(relay);
  4291. } else {
  4292. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::getCommandList - failed to set relay for message %d\n", msg->getID()));
  4293. }
  4294. if (lastCommand != NULL) {
  4295. lastCommand->deleteInstance();
  4296. lastCommand = NULL;
  4297. }
  4298. lastCommand = newInstance(NetCommandRef)(msg);
  4299. msg->detach(); // Need to detach from new NetCommandMsg created by the "readXMessage" above.
  4300. // since the message is part of the list now, we don't have to keep track of it. So we'll just set it to NULL.
  4301. msg = NULL;
  4302. } else if (m_packet[i] == 'Z') {
  4303. ++i;
  4304. // Repeat the last command, doing some funky cool byte-saving stuff
  4305. if (lastCommand == NULL) {
  4306. DEBUG_CRASH(("Got a repeat command with no command to repeat."));
  4307. }
  4308. NetCommandMsg *msg = NULL;
  4309. if (commandType == NETCOMMANDTYPE_ACKSTAGE1) {
  4310. msg = newInstance(NetAckStage1CommandMsg)();
  4311. NetAckStage1CommandMsg *last = (NetAckStage1CommandMsg *)(lastCommand->getCommand());
  4312. ((NetAckStage1CommandMsg *)msg)->setCommandID(last->getCommandID() + 1);
  4313. ((NetAckStage1CommandMsg *)msg)->setOriginalPlayerID(last->getOriginalPlayerID());
  4314. } else if (commandType == NETCOMMANDTYPE_ACKSTAGE2) {
  4315. msg = newInstance(NetAckStage2CommandMsg)();
  4316. NetAckStage2CommandMsg *last = (NetAckStage2CommandMsg *)(lastCommand->getCommand());
  4317. ((NetAckStage2CommandMsg *)msg)->setCommandID(last->getCommandID() + 1);
  4318. ((NetAckStage2CommandMsg *)msg)->setOriginalPlayerID(last->getOriginalPlayerID());
  4319. } else if (commandType == NETCOMMANDTYPE_ACKBOTH) {
  4320. msg = newInstance(NetAckBothCommandMsg)();
  4321. NetAckBothCommandMsg *last = (NetAckBothCommandMsg *)(lastCommand->getCommand());
  4322. ((NetAckBothCommandMsg *)msg)->setCommandID(last->getCommandID() + 1);
  4323. ((NetAckBothCommandMsg *)msg)->setOriginalPlayerID(last->getOriginalPlayerID());
  4324. } else if (commandType == NETCOMMANDTYPE_FRAMEINFO) {
  4325. msg = newInstance(NetFrameCommandMsg)();
  4326. ++frame; // this is set below.
  4327. ((NetFrameCommandMsg *)msg)->setCommandCount(0);
  4328. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Read a repeated frame command, frame = %d, player = %d, commandID = %d\n", frame, playerID, commandID));
  4329. } else {
  4330. DEBUG_CRASH(("Trying to repeat a command that shouldn't be repeated."));
  4331. continue;
  4332. }
  4333. msg->setExecutionFrame(frame);
  4334. msg->setPlayerID(playerID);
  4335. msg->setNetCommandType((NetCommandType)commandType);
  4336. msg->setID(commandID);
  4337. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("frame = %d, player = %d, command type = %d, id = %d\n", frame, playerID, commandType, commandID));
  4338. // increment to the next command ID.
  4339. if (DoesCommandRequireACommandID((NetCommandType)commandType)) {
  4340. ++commandID;
  4341. }
  4342. // add the message to the list.
  4343. NetCommandRef *ref = retval->addMessage(msg);
  4344. if (ref != NULL) {
  4345. ref->setRelay(relay);
  4346. }
  4347. lastCommand->deleteInstance();
  4348. lastCommand = NULL;
  4349. // lastCommand = newInstance(NetCommandRef)(msg);
  4350. lastCommand = NEW_NETCOMMANDREF(msg);
  4351. msg->detach(); // Need to detach from new NetCommandMsg created by the "readXMessage" above.
  4352. // since the message is part of the list now, we don't have to keep track of it. So we'll just set it to NULL.
  4353. msg = NULL;
  4354. } else {
  4355. // we don't recognize this command, but we have to increment i so we don't fall into an infinite loop.
  4356. DEBUG_CRASH(("Unrecognized packet entry, ignoring."));
  4357. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::getCommandList - Unrecognized packet entry at index %d\n", i));
  4358. dumpPacketToLog();
  4359. ++i;
  4360. }
  4361. }
  4362. if (lastCommand != NULL) {
  4363. lastCommand->deleteInstance();
  4364. lastCommand = NULL;
  4365. }
  4366. return retval;
  4367. }
  4368. /**
  4369. * Reads the data portion of a game message from the given position in the packet.
  4370. */
  4371. NetCommandMsg * NetPacket::readGameMessage(UnsignedByte *data, Int &i)
  4372. {
  4373. NetGameCommandMsg *msg = newInstance(NetGameCommandMsg);
  4374. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readGameMessage\n"));
  4375. // Get the GameMessage command type.
  4376. GameMessage::Type newType;
  4377. memcpy(&newType, data + i, sizeof(GameMessage::Type));
  4378. i += sizeof(GameMessage::Type);
  4379. msg->setGameMessageType(newType);
  4380. // Get the number of argument types
  4381. UnsignedByte numArgTypes = 0;
  4382. memcpy(&numArgTypes, data + i, sizeof(numArgTypes));
  4383. i += sizeof(numArgTypes);
  4384. // Get the types and the number of arguments of those types.
  4385. Int totalArgCount = 0;
  4386. GameMessageParser *parser = newInstance(GameMessageParser)();
  4387. for (Int j = 0; j < numArgTypes; ++j) {
  4388. UnsignedByte type = (UnsignedByte)ARGUMENTDATATYPE_UNKNOWN;
  4389. memcpy(&type, data + i, sizeof(type));
  4390. i += sizeof(type);
  4391. UnsignedByte argCount = 0;
  4392. memcpy(&argCount, data + i, sizeof(argCount));
  4393. i += sizeof(argCount);
  4394. parser->addArgType((GameMessageArgumentDataType)type, argCount);
  4395. totalArgCount += argCount;
  4396. }
  4397. GameMessageParserArgumentType *parserArgType = parser->getFirstArgumentType();
  4398. GameMessageArgumentDataType lasttype = ARGUMENTDATATYPE_UNKNOWN;
  4399. Int argsLeftForType = 0;
  4400. if (parserArgType != NULL) {
  4401. lasttype = parserArgType->getType();
  4402. argsLeftForType = parserArgType->getArgCount();
  4403. }
  4404. for (j = 0; j < totalArgCount; ++j) {
  4405. readGameMessageArgumentFromPacket(lasttype, msg, data, i);
  4406. --argsLeftForType;
  4407. if (argsLeftForType == 0) {
  4408. DEBUG_ASSERTCRASH(parserArgType != NULL, ("parserArgType was NULL when it shouldn't have been."));
  4409. if (parserArgType == NULL) {
  4410. return NULL;
  4411. }
  4412. parserArgType = parserArgType->getNext();
  4413. // parserArgType is allowed to be NULL here
  4414. if (parserArgType != NULL) {
  4415. argsLeftForType = parserArgType->getArgCount();
  4416. lasttype = parserArgType->getType();
  4417. }
  4418. }
  4419. }
  4420. parser->deleteInstance();
  4421. parser = NULL;
  4422. return (NetCommandMsg *)msg;
  4423. }
  4424. void NetPacket::readGameMessageArgumentFromPacket(GameMessageArgumentDataType type, NetGameCommandMsg *msg, UnsignedByte *data, Int &i) {
  4425. if (type == ARGUMENTDATATYPE_INTEGER) {
  4426. GameMessageArgumentType arg;
  4427. Int theint;
  4428. memcpy(&theint, data + i, sizeof(theint));
  4429. i += sizeof(theint);
  4430. arg.integer = theint;
  4431. msg->addArgument(type, arg);
  4432. } else if (type == ARGUMENTDATATYPE_REAL) {
  4433. GameMessageArgumentType arg;
  4434. Real thereal;
  4435. memcpy(&thereal, data + i, sizeof(thereal));
  4436. i += sizeof(thereal);
  4437. arg.real = thereal;
  4438. msg->addArgument(type, arg);
  4439. } else if (type == ARGUMENTDATATYPE_BOOLEAN) {
  4440. GameMessageArgumentType arg;
  4441. Bool thebool;
  4442. memcpy(&thebool, data + i, sizeof(thebool));
  4443. i += sizeof(thebool);
  4444. arg.boolean = thebool;
  4445. msg->addArgument(type, arg);
  4446. } else if (type == ARGUMENTDATATYPE_OBJECTID) {
  4447. GameMessageArgumentType arg;
  4448. ObjectID theint;
  4449. memcpy(&theint, data + i, sizeof(theint));
  4450. i += sizeof(theint);
  4451. arg.objectID = theint;
  4452. msg->addArgument(type, arg);
  4453. } else if (type == ARGUMENTDATATYPE_DRAWABLEID) {
  4454. GameMessageArgumentType arg;
  4455. DrawableID theint;
  4456. memcpy(&theint, data + i, sizeof(theint));
  4457. i += sizeof(theint);
  4458. arg.drawableID = theint;
  4459. msg->addArgument(type, arg);
  4460. } else if (type == ARGUMENTDATATYPE_TEAMID) {
  4461. GameMessageArgumentType arg;
  4462. UnsignedInt theint;
  4463. memcpy(&theint, data + i, sizeof(theint));
  4464. i += sizeof(theint);
  4465. arg.teamID = theint;
  4466. msg->addArgument(type, arg);
  4467. } else if (type == ARGUMENTDATATYPE_LOCATION) {
  4468. GameMessageArgumentType arg;
  4469. Coord3D coord;
  4470. memcpy(&coord, data + i, sizeof(coord));
  4471. i += sizeof(coord);
  4472. arg.location = coord;
  4473. msg->addArgument(type, arg);
  4474. } else if (type == ARGUMENTDATATYPE_PIXEL) {
  4475. GameMessageArgumentType arg;
  4476. ICoord2D pixel;
  4477. memcpy(&pixel, data + i, sizeof(pixel));
  4478. i += sizeof(pixel);
  4479. arg.pixel = pixel;
  4480. msg->addArgument(type, arg);
  4481. } else if (type == ARGUMENTDATATYPE_PIXELREGION) {
  4482. GameMessageArgumentType arg;
  4483. IRegion2D reg;
  4484. memcpy(&reg, data + i, sizeof(reg));
  4485. i += sizeof(reg);
  4486. arg.pixelRegion = reg;
  4487. msg->addArgument(type, arg);
  4488. } else if (type == ARGUMENTDATATYPE_TIMESTAMP) {
  4489. GameMessageArgumentType arg;
  4490. UnsignedInt stamp;
  4491. memcpy(&stamp, data + i, sizeof(stamp));
  4492. i += sizeof(stamp);
  4493. arg.timestamp = stamp;
  4494. msg->addArgument(type, arg);
  4495. } else if (type == ARGUMENTDATATYPE_WIDECHAR) {
  4496. GameMessageArgumentType arg;
  4497. WideChar c;
  4498. memcpy(&c, data + i, sizeof(c));
  4499. i += sizeof(c);
  4500. arg.wChar = c;
  4501. msg->addArgument(type, arg);
  4502. }
  4503. }
  4504. /**
  4505. * Reads the data portion of the ack message at this position in the packet.
  4506. */
  4507. NetCommandMsg * NetPacket::readAckBothMessage(UnsignedByte *data, Int &i) {
  4508. NetAckBothCommandMsg *msg = newInstance(NetAckBothCommandMsg);
  4509. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readAckMessage, "));
  4510. UnsignedShort cmdID = 0;
  4511. memcpy(&cmdID, data + i, sizeof(UnsignedShort));
  4512. i += sizeof(UnsignedShort);
  4513. msg->setCommandID(cmdID);
  4514. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("commandID = %d, ", cmdID));
  4515. UnsignedByte origPlayerID = 0;
  4516. memcpy(&origPlayerID, data + i, sizeof(UnsignedByte));
  4517. i += sizeof(UnsignedByte);
  4518. msg->setOriginalPlayerID(origPlayerID);
  4519. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("original player id = %d\n", origPlayerID));
  4520. return msg;
  4521. }
  4522. /**
  4523. * Reads the data portion of the ack message at this position in the packet.
  4524. */
  4525. NetCommandMsg * NetPacket::readAckStage1Message(UnsignedByte *data, Int &i) {
  4526. NetAckStage1CommandMsg *msg = newInstance(NetAckStage1CommandMsg);
  4527. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readAckMessage, "));
  4528. UnsignedShort cmdID = 0;
  4529. memcpy(&cmdID, data + i, sizeof(UnsignedShort));
  4530. i += sizeof(UnsignedShort);
  4531. msg->setCommandID(cmdID);
  4532. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("commandID = %d, ", cmdID));
  4533. UnsignedByte origPlayerID = 0;
  4534. memcpy(&origPlayerID, data + i, sizeof(UnsignedByte));
  4535. i += sizeof(UnsignedByte);
  4536. msg->setOriginalPlayerID(origPlayerID);
  4537. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("original player id = %d\n", origPlayerID));
  4538. return msg;
  4539. }
  4540. /**
  4541. * Reads the data portion of the ack message at this position in the packet.
  4542. */
  4543. NetCommandMsg * NetPacket::readAckStage2Message(UnsignedByte *data, Int &i) {
  4544. NetAckStage2CommandMsg *msg = newInstance(NetAckStage2CommandMsg);
  4545. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readAckMessage, "));
  4546. UnsignedShort cmdID = 0;
  4547. memcpy(&cmdID, data + i, sizeof(UnsignedShort));
  4548. i += sizeof(UnsignedShort);
  4549. msg->setCommandID(cmdID);
  4550. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("commandID = %d, ", cmdID));
  4551. UnsignedByte origPlayerID = 0;
  4552. memcpy(&origPlayerID, data + i, sizeof(UnsignedByte));
  4553. i += sizeof(UnsignedByte);
  4554. msg->setOriginalPlayerID(origPlayerID);
  4555. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("original player id = %d\n", origPlayerID));
  4556. return msg;
  4557. }
  4558. /**
  4559. * Reads the data portion of the frame message at this position in the packet.
  4560. */
  4561. NetCommandMsg * NetPacket::readFrameMessage(UnsignedByte *data, Int &i) {
  4562. NetFrameCommandMsg *msg = newInstance(NetFrameCommandMsg);
  4563. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readFrameMessage, "));
  4564. UnsignedShort cmdCount = 0;
  4565. memcpy(&cmdCount, data + i, sizeof(UnsignedShort));
  4566. i += sizeof(UnsignedShort);
  4567. msg->setCommandCount(cmdCount);
  4568. // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command count = %d, ", cmdCount));
  4569. return msg;
  4570. }
  4571. /**
  4572. * Reads the player leave message at this position in the packet.
  4573. */
  4574. NetCommandMsg * NetPacket::readPlayerLeaveMessage(UnsignedByte *data, Int &i) {
  4575. NetPlayerLeaveCommandMsg *msg = newInstance(NetPlayerLeaveCommandMsg);
  4576. UnsignedByte leavingPlayerID = 0;
  4577. memcpy(&leavingPlayerID, data + i, sizeof(UnsignedByte));
  4578. i += sizeof(UnsignedByte);
  4579. msg->setLeavingPlayerID(leavingPlayerID);
  4580. return msg;
  4581. }
  4582. /**
  4583. * Reads the run ahead metrics message at this position in the packet.
  4584. */
  4585. NetCommandMsg * NetPacket::readRunAheadMetricsMessage(UnsignedByte *data, Int &i) {
  4586. NetRunAheadMetricsCommandMsg *msg = newInstance(NetRunAheadMetricsCommandMsg);
  4587. Real averageLatency = (Real)0.2;
  4588. UnsignedShort averageFps = 30;
  4589. memcpy(&averageLatency, data + i, sizeof(Real));
  4590. i += sizeof(Real);
  4591. msg->setAverageLatency(averageLatency);
  4592. memcpy(&averageFps, data + i, sizeof(UnsignedShort));
  4593. i += sizeof(UnsignedShort);
  4594. msg->setAverageFps((Int)averageFps);
  4595. return msg;
  4596. }
  4597. /**
  4598. * Reads the run ahead message at this position in the packet.
  4599. */
  4600. NetCommandMsg * NetPacket::readRunAheadMessage(UnsignedByte *data, Int &i) {
  4601. NetRunAheadCommandMsg *msg = newInstance(NetRunAheadCommandMsg);
  4602. UnsignedShort newRunAhead = 20;
  4603. memcpy(&newRunAhead, data + i, sizeof(UnsignedShort));
  4604. i += sizeof(UnsignedShort);
  4605. msg->setRunAhead(newRunAhead);
  4606. UnsignedByte newFrameRate = 30;
  4607. memcpy(&newFrameRate, data + i, sizeof(UnsignedByte));
  4608. i += sizeof(UnsignedByte);
  4609. msg->setFrameRate(newFrameRate);
  4610. return msg;
  4611. }
  4612. /**
  4613. * Reads the CRC info message at this position in the packet.
  4614. */
  4615. NetCommandMsg * NetPacket::readDestroyPlayerMessage(UnsignedByte *data, Int &i) {
  4616. NetDestroyPlayerCommandMsg *msg = newInstance(NetDestroyPlayerCommandMsg);
  4617. UnsignedInt newVal = 0;
  4618. memcpy(&newVal, data + i, sizeof(UnsignedInt));
  4619. i += sizeof(UnsignedInt);
  4620. msg->setPlayerIndex(newVal);
  4621. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Saw CRC of 0x%8.8X\n", newCRC));
  4622. return msg;
  4623. }
  4624. /**
  4625. * Reads the keep alive data, of which there is none.
  4626. */
  4627. NetCommandMsg * NetPacket::readKeepAliveMessage(UnsignedByte *data, Int &i) {
  4628. NetKeepAliveCommandMsg *msg = newInstance(NetKeepAliveCommandMsg);
  4629. return msg;
  4630. }
  4631. /**
  4632. * Reads the disconnect keep alive data, of which there is none.
  4633. */
  4634. NetCommandMsg * NetPacket::readDisconnectKeepAliveMessage(UnsignedByte *data, Int &i) {
  4635. NetDisconnectKeepAliveCommandMsg *msg = newInstance(NetDisconnectKeepAliveCommandMsg);
  4636. return msg;
  4637. }
  4638. /**
  4639. * Reads the disconnect player data. Which is the slot number of the player being disconnected.
  4640. */
  4641. NetCommandMsg * NetPacket::readDisconnectPlayerMessage(UnsignedByte *data, Int &i) {
  4642. NetDisconnectPlayerCommandMsg *msg = newInstance(NetDisconnectPlayerCommandMsg);
  4643. UnsignedByte slot = 0;
  4644. memcpy(&slot, data + i, sizeof(slot));
  4645. i += sizeof(slot);
  4646. msg->setDisconnectSlot(slot);
  4647. UnsignedInt disconnectFrame = 0;
  4648. memcpy(&disconnectFrame, data + i, sizeof(disconnectFrame));
  4649. i += sizeof(disconnectFrame);
  4650. msg->setDisconnectFrame(disconnectFrame);
  4651. return msg;
  4652. }
  4653. /**
  4654. * Reads the packet router query data, of which there is none.
  4655. */
  4656. NetCommandMsg * NetPacket::readPacketRouterQueryMessage(UnsignedByte *data, Int &i) {
  4657. NetPacketRouterQueryCommandMsg *msg = newInstance(NetPacketRouterQueryCommandMsg);
  4658. return msg;
  4659. }
  4660. /**
  4661. * Reads the packet router ack data, of which there is none.
  4662. */
  4663. NetCommandMsg * NetPacket::readPacketRouterAckMessage(UnsignedByte *data, Int &i) {
  4664. NetPacketRouterAckCommandMsg *msg = newInstance(NetPacketRouterAckCommandMsg);
  4665. return msg;
  4666. }
  4667. /**
  4668. * Reads the disconnect chat data, which is just the string.
  4669. */
  4670. NetCommandMsg * NetPacket::readDisconnectChatMessage(UnsignedByte *data, Int &i) {
  4671. NetDisconnectChatCommandMsg *msg = newInstance(NetDisconnectChatCommandMsg);
  4672. UnsignedShort text[256];
  4673. UnsignedByte length;
  4674. memcpy(&length, data + i, sizeof(UnsignedByte));
  4675. ++i;
  4676. memcpy(text, data + i, length * sizeof(UnsignedShort));
  4677. i += length * sizeof(UnsignedShort);
  4678. text[length] = 0;
  4679. UnicodeString unitext;
  4680. unitext.set(text);
  4681. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readDisconnectChatMessage - read message, message is %ls\n", unitext.str()));
  4682. msg->setText(unitext);
  4683. return msg;
  4684. }
  4685. /**
  4686. * Reads the chat data, which is just the string.
  4687. */
  4688. NetCommandMsg * NetPacket::readChatMessage(UnsignedByte *data, Int &i) {
  4689. NetChatCommandMsg *msg = newInstance(NetChatCommandMsg);
  4690. UnsignedShort text[256];
  4691. UnsignedByte length;
  4692. Int playerMask;
  4693. memcpy(&length, data + i, sizeof(UnsignedByte));
  4694. ++i;
  4695. memcpy(text, data + i, length * sizeof(UnsignedShort));
  4696. i += length * sizeof(UnsignedShort);
  4697. text[length] = 0;
  4698. memcpy(&playerMask, data + i, sizeof(Int));
  4699. i += sizeof(Int);
  4700. UnicodeString unitext;
  4701. unitext.set(text);
  4702. //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readChatMessage - read message, message is %ls\n", unitext.str()));
  4703. msg->setText(unitext);
  4704. msg->setPlayerMask(playerMask);
  4705. return msg;
  4706. }
  4707. /**
  4708. * Reads the disconnect vote data. Which is the slot number of the player being disconnected.
  4709. */
  4710. NetCommandMsg * NetPacket::readDisconnectVoteMessage(UnsignedByte *data, Int &i) {
  4711. NetDisconnectVoteCommandMsg *msg = newInstance(NetDisconnectVoteCommandMsg);
  4712. UnsignedByte slot = 0;
  4713. memcpy(&slot, data + i, sizeof(slot));
  4714. i += sizeof(slot);
  4715. msg->setSlot(slot);
  4716. UnsignedInt voteFrame = 0;
  4717. memcpy(&voteFrame, data + i, sizeof(voteFrame));
  4718. i += sizeof(voteFrame);
  4719. msg->setVoteFrame(voteFrame);
  4720. return msg;
  4721. }
  4722. /**
  4723. * Reads the Progress data. Which is the slot number of the player being disconnected.
  4724. */
  4725. NetCommandMsg * NetPacket::readProgressMessage(UnsignedByte *data, Int &i) {
  4726. NetProgressCommandMsg *msg = newInstance(NetProgressCommandMsg);
  4727. UnsignedByte percentage = 0;
  4728. memcpy(&percentage, data + i, sizeof(UnsignedByte));
  4729. i += sizeof(UnsignedByte);
  4730. msg->setPercentage(percentage);
  4731. return msg;
  4732. }
  4733. NetCommandMsg * NetPacket::readLoadCompleteMessage(UnsignedByte *data, Int &i) {
  4734. NetCommandMsg *msg = newInstance(NetCommandMsg);
  4735. return msg;
  4736. }
  4737. NetCommandMsg * NetPacket::readTimeOutGameStartMessage(UnsignedByte *data, Int &i) {
  4738. NetCommandMsg *msg = newInstance(NetCommandMsg);
  4739. return msg;
  4740. }
  4741. NetCommandMsg * NetPacket::readWrapperMessage(UnsignedByte *data, Int &i) {
  4742. NetWrapperCommandMsg *msg = newInstance(NetWrapperCommandMsg);
  4743. // get the wrapped command ID
  4744. UnsignedShort wrappedCommandID = 0;
  4745. memcpy(&wrappedCommandID, data + i, sizeof(wrappedCommandID));
  4746. msg->setWrappedCommandID(wrappedCommandID);
  4747. i += sizeof(wrappedCommandID);
  4748. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readWrapperMessage - wrapped command ID == %d\n", wrappedCommandID));
  4749. // get the chunk number.
  4750. UnsignedInt chunkNumber = 0;
  4751. memcpy(&chunkNumber, data + i, sizeof(chunkNumber));
  4752. msg->setChunkNumber(chunkNumber);
  4753. i += sizeof(chunkNumber);
  4754. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readWrapperMessage - chunk number = %d\n", chunkNumber));
  4755. // get the number of chunks
  4756. UnsignedInt numChunks = 0;
  4757. memcpy(&numChunks, data + i, sizeof(numChunks));
  4758. msg->setNumChunks(numChunks);
  4759. i += sizeof(numChunks);
  4760. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readWrapperMessage - number of chunks = %d\n", numChunks));
  4761. // get the total data length
  4762. UnsignedInt totalDataLength = 0;
  4763. memcpy(&totalDataLength, data + i, sizeof(totalDataLength));
  4764. msg->setTotalDataLength(totalDataLength);
  4765. i += sizeof(totalDataLength);
  4766. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readWrapperMessage - total data length = %d\n", totalDataLength));
  4767. // get the data length for this chunk
  4768. UnsignedInt dataLength = 0;
  4769. memcpy(&dataLength, data + i, sizeof(dataLength));
  4770. i += sizeof(dataLength);
  4771. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readWrapperMessage - data length = %d\n", dataLength));
  4772. UnsignedInt dataOffset = 0;
  4773. memcpy(&dataOffset, data + i, sizeof(dataOffset));
  4774. msg->setDataOffset(dataOffset);
  4775. i += sizeof(dataOffset);
  4776. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readWrapperMessage - data offset = %d\n", dataOffset));
  4777. msg->setData(data + i, dataLength);
  4778. i += dataLength;
  4779. return msg;
  4780. }
  4781. NetCommandMsg * NetPacket::readFileMessage(UnsignedByte *data, Int &i) {
  4782. NetFileCommandMsg *msg = newInstance(NetFileCommandMsg);
  4783. char filename[_MAX_PATH];
  4784. char *c = filename;
  4785. while (data[i] != 0) {
  4786. *c = data[i];
  4787. ++c;
  4788. ++i;
  4789. }
  4790. *c = 0;
  4791. ++i;
  4792. msg->setPortableFilename(AsciiString(filename)); // it's transferred as a portable filename
  4793. UnsignedInt dataLength = 0;
  4794. memcpy(&dataLength, data + i, sizeof(dataLength));
  4795. i += sizeof(dataLength);
  4796. UnsignedByte *buf = NEW UnsignedByte[dataLength];
  4797. memcpy(buf, data + i, dataLength);
  4798. i += dataLength;
  4799. msg->setFileData(buf, dataLength);
  4800. return msg;
  4801. }
  4802. NetCommandMsg * NetPacket::readFileAnnounceMessage(UnsignedByte *data, Int &i) {
  4803. NetFileAnnounceCommandMsg *msg = newInstance(NetFileAnnounceCommandMsg);
  4804. char filename[_MAX_PATH];
  4805. char *c = filename;
  4806. while (data[i] != 0) {
  4807. *c = data[i];
  4808. ++c;
  4809. ++i;
  4810. }
  4811. *c = 0;
  4812. ++i;
  4813. msg->setPortableFilename(AsciiString(filename)); // it's transferred as a portable filename
  4814. UnsignedShort fileID = 0;
  4815. memcpy(&fileID, data + i, sizeof(fileID));
  4816. i += sizeof(fileID);
  4817. msg->setFileID(fileID);
  4818. UnsignedByte playerMask = 0;
  4819. memcpy(&playerMask, data + i, sizeof(playerMask));
  4820. i += sizeof(playerMask);
  4821. msg->setPlayerMask(playerMask);
  4822. return msg;
  4823. }
  4824. NetCommandMsg * NetPacket::readFileProgressMessage(UnsignedByte *data, Int &i) {
  4825. NetFileProgressCommandMsg *msg = newInstance(NetFileProgressCommandMsg);
  4826. UnsignedShort fileID = 0;
  4827. memcpy(&fileID, data + i, sizeof(fileID));
  4828. i += sizeof(fileID);
  4829. msg->setFileID(fileID);
  4830. Int progress = 0;
  4831. memcpy(&progress, data + i, sizeof(progress));
  4832. i += sizeof(progress);
  4833. msg->setProgress(progress);
  4834. return msg;
  4835. }
  4836. NetCommandMsg * NetPacket::readDisconnectFrameMessage(UnsignedByte *data, Int &i) {
  4837. NetDisconnectFrameCommandMsg *msg = newInstance(NetDisconnectFrameCommandMsg);
  4838. UnsignedInt disconnectFrame = 0;
  4839. memcpy(&disconnectFrame, data + i, sizeof(disconnectFrame));
  4840. i += sizeof(disconnectFrame);
  4841. msg->setDisconnectFrame(disconnectFrame);
  4842. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::readDisconnectFrameMessage - read disconnect frame for frame %d\n", disconnectFrame));
  4843. return msg;
  4844. }
  4845. NetCommandMsg * NetPacket::readDisconnectScreenOffMessage(UnsignedByte *data, Int &i) {
  4846. NetDisconnectScreenOffCommandMsg *msg = newInstance(NetDisconnectScreenOffCommandMsg);
  4847. UnsignedInt newFrame = 0;
  4848. memcpy(&newFrame, data + i, sizeof(newFrame));
  4849. i += sizeof(newFrame);
  4850. msg->setNewFrame(newFrame);
  4851. return msg;
  4852. }
  4853. NetCommandMsg * NetPacket::readFrameResendRequestMessage(UnsignedByte *data, Int &i) {
  4854. NetFrameResendRequestCommandMsg *msg = newInstance(NetFrameResendRequestCommandMsg);
  4855. UnsignedInt frameToResend = 0;
  4856. memcpy(&frameToResend, data + i, sizeof(frameToResend));
  4857. i += sizeof(frameToResend);
  4858. msg->setFrameToResend(frameToResend);
  4859. return msg;
  4860. }
  4861. /**
  4862. * Returns the number of commands in this packet. Only valid if the packet is locally constructed.
  4863. */
  4864. Int NetPacket::getNumCommands() {
  4865. return m_numCommands;
  4866. }
  4867. /**
  4868. * Returns the address that this packet is to be sent to. Only valid if the packet is locally constructed.
  4869. */
  4870. UnsignedInt NetPacket::getAddr() {
  4871. return m_addr;
  4872. }
  4873. /**
  4874. * Returns the port that this packet is to be sent to. Only valid if the packet is locally constructed.
  4875. */
  4876. UnsignedShort NetPacket::getPort() {
  4877. return m_port;
  4878. }
  4879. /**
  4880. * Returns the data of this packet.
  4881. */
  4882. UnsignedByte * NetPacket::getData() {
  4883. return m_packet;
  4884. }
  4885. /**
  4886. * Returns the length of the packet.
  4887. */
  4888. Int NetPacket::getLength() {
  4889. return m_packetLen;
  4890. }
  4891. /**
  4892. * Dumps the packet to the debug log file
  4893. */
  4894. void NetPacket::dumpPacketToLog() {
  4895. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::dumpPacketToLog() - packet is %d bytes\n", m_packetLen));
  4896. Int numLines = m_packetLen / 8;
  4897. if ((m_packetLen % 8) != 0) {
  4898. ++numLines;
  4899. }
  4900. for (Int dumpindex = 0; dumpindex < numLines; ++dumpindex) {
  4901. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("\t%d\t", dumpindex*8));
  4902. for (Int dumpindex2 = 0; (dumpindex2 < 8) && ((dumpindex*8 + dumpindex2) < m_packetLen); ++dumpindex2) {
  4903. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("%02x '%c' ", m_packet[dumpindex*8 + dumpindex2], m_packet[dumpindex*8 + dumpindex2]));
  4904. }
  4905. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("\n"));
  4906. }
  4907. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("End of packet dump\n"));
  4908. }