ReliabilityLayer.cpp 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. ///
  12. #include "ReliabilityLayer.h"
  13. #include "GetTime.h"
  14. #include "SocketLayer.h"
  15. #include "PluginInterface2.h"
  16. #include "RakAssert.h"
  17. #include "Rand.h"
  18. #include "MessageIdentifiers.h"
  19. #ifdef USE_THREADED_SEND
  20. #include "SendToThread.h"
  21. #endif
  22. #include <math.h>
  23. using namespace RakNet;
  24. // Can't figure out which library has this function on the PS3
  25. double Ceil(double d) {if (((double)((int)d))==d) return d; return (int) (d+1.0);}
  26. // #if defined(new)
  27. // #pragma push_macro("new")
  28. // #undef new
  29. // #define RELIABILITY_LAYER_NEW_UNDEF_ALLOCATING_QUEUE
  30. // #endif
  31. //#define _DEBUG_LOGGER
  32. #if CC_TIME_TYPE_BYTES==4
  33. static const CCTimeType MAX_TIME_BETWEEN_PACKETS= 350; // 350 milliseconds
  34. static const CCTimeType HISTOGRAM_RESTART_CYCLE=10000; // Every 10 seconds reset the histogram
  35. #else
  36. static const CCTimeType MAX_TIME_BETWEEN_PACKETS= 350000; // 350 milliseconds
  37. //static const CCTimeType HISTOGRAM_RESTART_CYCLE=10000000; // Every 10 seconds reset the histogram
  38. #endif
  39. static const int DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE=512;
  40. static const CCTimeType STARTING_TIME_BETWEEN_PACKETS=MAX_TIME_BETWEEN_PACKETS;
  41. //static const long double TIME_BETWEEN_PACKETS_INCREASE_MULTIPLIER_DEFAULT=.02;
  42. //static const long double TIME_BETWEEN_PACKETS_DECREASE_MULTIPLIER_DEFAULT=1.0 / 9.0;
  43. typedef uint32_t BitstreamLengthEncoding;
  44. #ifdef _MSC_VER
  45. #pragma warning( push )
  46. #endif
  47. //#define PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  48. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  49. static unsigned int packetNumber=0;
  50. static FILE *fp=0;
  51. #endif
  52. //#define FLIP_SEND_ORDER_TEST
  53. //#define LOG_TRIVIAL_NOTIFICATIONS
  54. BPSTracker::TimeAndValue2::TimeAndValue2() {}
  55. BPSTracker::TimeAndValue2::~TimeAndValue2() {}
  56. BPSTracker::TimeAndValue2::TimeAndValue2(RakNet::TimeUS t, uint64_t v1) : value1(v1), time(t) {}
  57. //BPSTracker::TimeAndValue2::TimeAndValue2(RakNet::TimeUS t, uint64_t v1, uint64_t v2) : time(t), value1(v1), value2(v2) {}
  58. BPSTracker::BPSTracker() {Reset(_FILE_AND_LINE_);}
  59. BPSTracker::~BPSTracker() {}
  60. //void BPSTracker::Reset(const char *file, unsigned int line) {total1=total2=lastSec1=lastSec2=0; dataQueue.Clear(file,line);}
  61. void BPSTracker::Reset(const char *file, unsigned int line) {total1=lastSec1=0; dataQueue.Clear(file,line);}
  62. //void BPSTracker::Push2(RakNetTimeUS time, uint64_t value1, uint64_t value2) {dataQueue.Push(TimeAndValue2(time,value1,value2),_FILE_AND_LINE_); total1+=value1; lastSec1+=value1; total2+=value2; lastSec2+=value2;}
  63. //uint64_t BPSTracker::GetBPS2(RakNetTimeUS time) {ClearExpired2(time); return lastSec2;}
  64. //void BPSTracker::GetBPS1And2(RakNetTimeUS time, uint64_t &out1, uint64_t &out2) {ClearExpired2(time); out1=lastSec1; out2=lastSec2;}
  65. uint64_t BPSTracker::GetTotal1(void) const {return total1;}
  66. //uint64_t BPSTracker::GetTotal2(void) const {return total2;}
  67. // void BPSTracker::ClearExpired2(RakNet::TimeUS time) {
  68. // RakNet::TimeUS threshold=time;
  69. // if (threshold < 1000000)
  70. // return;
  71. // threshold-=1000000;
  72. // while (dataQueue.IsEmpty()==false && dataQueue.Peek().time < threshold)
  73. // {
  74. // lastSec1-=dataQueue.Peek().value1;
  75. // lastSec2-=dataQueue.Peek().value2;
  76. // dataQueue.Pop();
  77. // }
  78. // }
  79. void BPSTracker::ClearExpired1(RakNet::TimeUS time)
  80. {
  81. while (dataQueue.IsEmpty()==false &&
  82. #if CC_TIME_TYPE_BYTES==8
  83. dataQueue.Peek().time+1000000 < time
  84. #else
  85. dataQueue.Peek().time+1000 < time
  86. #endif
  87. )
  88. {
  89. lastSec1-=dataQueue.Peek().value1;
  90. dataQueue.Pop();
  91. }
  92. }
  93. struct DatagramHeaderFormat
  94. {
  95. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  96. CCTimeType sourceSystemTime;
  97. #endif
  98. DatagramSequenceNumberType datagramNumber;
  99. // Use floats to save bandwidth
  100. // float B; // Link capacity
  101. float AS; // Data arrival rate
  102. bool isACK;
  103. bool isNAK;
  104. bool isPacketPair;
  105. bool hasBAndAS;
  106. bool isContinuousSend;
  107. bool needsBAndAs;
  108. bool isValid; // To differentiate between what I serialized, and offline data
  109. static BitSize_t GetDataHeaderBitLength()
  110. {
  111. return BYTES_TO_BITS(GetDataHeaderByteLength());
  112. }
  113. static unsigned int GetDataHeaderByteLength()
  114. {
  115. //return 2 + 3 + sizeof(RakNet::TimeMS) + sizeof(float)*2;
  116. return 2 + 3 +
  117. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  118. sizeof(RakNetTimeMS) +
  119. #endif
  120. sizeof(float)*1;
  121. }
  122. void Serialize(RakNet::BitStream *b)
  123. {
  124. // Not endian safe
  125. // RakAssert(GetDataHeaderByteLength()==sizeof(DatagramHeaderFormat));
  126. // b->WriteAlignedBytes((const unsigned char*) this, sizeof(DatagramHeaderFormat));
  127. // return;
  128. b->Write(true); // IsValid
  129. if (isACK)
  130. {
  131. b->Write(true);
  132. b->Write(hasBAndAS);
  133. b->AlignWriteToByteBoundary();
  134. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  135. RakNet::TimeMS timeMSLow=(RakNet::TimeMS) sourceSystemTime&0xFFFFFFFF; b->Write(timeMSLow);
  136. #endif
  137. if (hasBAndAS)
  138. {
  139. // b->Write(B);
  140. b->Write(AS);
  141. }
  142. }
  143. else if (isNAK)
  144. {
  145. b->Write(false);
  146. b->Write(true);
  147. }
  148. else
  149. {
  150. b->Write(false);
  151. b->Write(false);
  152. b->Write(isPacketPair);
  153. b->Write(isContinuousSend);
  154. b->Write(needsBAndAs);
  155. b->AlignWriteToByteBoundary();
  156. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  157. RakNet::TimeMS timeMSLow=(RakNet::TimeMS) sourceSystemTime&0xFFFFFFFF; b->Write(timeMSLow);
  158. #endif
  159. b->Write(datagramNumber);
  160. }
  161. }
  162. void Deserialize(RakNet::BitStream *b)
  163. {
  164. // Not endian safe
  165. // b->ReadAlignedBytes((unsigned char*) this, sizeof(DatagramHeaderFormat));
  166. // return;
  167. b->Read(isValid);
  168. b->Read(isACK);
  169. if (isACK)
  170. {
  171. isNAK=false;
  172. isPacketPair=false;
  173. b->Read(hasBAndAS);
  174. b->AlignReadToByteBoundary();
  175. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  176. RakNet::TimeMS timeMS; b->Read(timeMS); sourceSystemTime=(CCTimeType) timeMS;
  177. #endif
  178. if (hasBAndAS)
  179. {
  180. // b->Read(B);
  181. b->Read(AS);
  182. }
  183. }
  184. else
  185. {
  186. b->Read(isNAK);
  187. if (isNAK)
  188. {
  189. isPacketPair=false;
  190. }
  191. else
  192. {
  193. b->Read(isPacketPair);
  194. b->Read(isContinuousSend);
  195. b->Read(needsBAndAs);
  196. b->AlignReadToByteBoundary();
  197. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  198. RakNet::TimeMS timeMS; b->Read(timeMS); sourceSystemTime=(CCTimeType) timeMS;
  199. #endif
  200. b->Read(datagramNumber);
  201. }
  202. }
  203. }
  204. };
  205. #if !defined(__GNUC__) && !defined(__ARMCC)
  206. #pragma warning(disable:4702) // unreachable code
  207. #endif
  208. #ifdef _WIN32
  209. //#define _DEBUG_LOGGER
  210. #ifdef _DEBUG_LOGGER
  211. #include "WindowsIncludes.h"
  212. #endif
  213. #endif
  214. //#define DEBUG_SPLIT_PACKET_PROBLEMS
  215. #if defined (DEBUG_SPLIT_PACKET_PROBLEMS)
  216. static int waitFlag=-1;
  217. #endif
  218. using namespace RakNet;
  219. int RakNet::SplitPacketChannelComp( SplitPacketIdType const &key, SplitPacketChannel* const &data )
  220. {
  221. #if PREALLOCATE_LARGE_MESSAGES==1
  222. if (key < data->returnedPacket->splitPacketId)
  223. return -1;
  224. if (key == data->returnedPacket->splitPacketId)
  225. return 0;
  226. #else
  227. if (key < data->splitPacketList[0]->splitPacketId)
  228. return -1;
  229. if (key == data->splitPacketList[0]->splitPacketId)
  230. return 0;
  231. #endif
  232. return 1;
  233. }
  234. // DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS( InternalPacket, SplitPacketIndexType, splitPacketIndex )
  235. /*
  236. bool operator<( const DataStructures::MLKeyRef<SplitPacketIndexType> &inputKey, const InternalPacket *cls )
  237. {
  238. return inputKey.Get() < cls->splitPacketIndex;
  239. }
  240. bool operator>( const DataStructures::MLKeyRef<SplitPacketIndexType> &inputKey, const InternalPacket *cls )
  241. {
  242. return inputKey.Get() > cls->splitPacketIndex;
  243. }
  244. bool operator==( const DataStructures::MLKeyRef<SplitPacketIndexType> &inputKey, const InternalPacket *cls )
  245. {
  246. return inputKey.Get() == cls->splitPacketIndex;
  247. }
  248. /// Semi-hack: This is necessary to call Sort()
  249. bool operator<( const DataStructures::MLKeyRef<InternalPacket *> &inputKey, const InternalPacket *cls )
  250. {
  251. return inputKey.Get()->splitPacketIndex < cls->splitPacketIndex;
  252. }
  253. bool operator>( const DataStructures::MLKeyRef<InternalPacket *> &inputKey, const InternalPacket *cls )
  254. {
  255. return inputKey.Get()->splitPacketIndex > cls->splitPacketIndex;
  256. }
  257. bool operator==( const DataStructures::MLKeyRef<InternalPacket *> &inputKey, const InternalPacket *cls )
  258. {
  259. return inputKey.Get()->splitPacketIndex == cls->splitPacketIndex;
  260. }
  261. */
  262. int SplitPacketIndexComp( SplitPacketIndexType const &key, InternalPacket* const &data )
  263. {
  264. if (key < data->splitPacketIndex)
  265. return -1;
  266. if (key == data->splitPacketIndex)
  267. return 0;
  268. return 1;
  269. }
  270. //-------------------------------------------------------------------------------------------------------
  271. // Constructor
  272. //-------------------------------------------------------------------------------------------------------
  273. // Add 21 to the default MTU so if we encrypt it can hold potentially 21 more bytes of extra data + padding.
  274. ReliabilityLayer::ReliabilityLayer()
  275. {
  276. #ifdef _DEBUG
  277. // Wait longer to disconnect in debug so I don't get disconnected while tracing
  278. timeoutTime=30000;
  279. #else
  280. timeoutTime=10000;
  281. #endif
  282. #ifdef _DEBUG
  283. minExtraPing=extraPingVariance=0;
  284. packetloss=(double) minExtraPing;
  285. #endif
  286. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  287. if (fp==0 && 0)
  288. {
  289. fp = fopen("reliableorderedoutput.txt", "wt");
  290. }
  291. #endif
  292. InitializeVariables();
  293. //int i = sizeof(InternalPacket);
  294. datagramHistoryMessagePool.SetPageSize(sizeof(MessageNumberNode)*128);
  295. internalPacketPool.SetPageSize(sizeof(InternalPacket)*INTERNAL_PACKET_PAGE_SIZE);
  296. refCountedDataPool.SetPageSize(sizeof(InternalPacketRefCountedData)*32);
  297. }
  298. //-------------------------------------------------------------------------------------------------------
  299. // Destructor
  300. //-------------------------------------------------------------------------------------------------------
  301. ReliabilityLayer::~ReliabilityLayer()
  302. {
  303. FreeMemory( true ); // Free all memory immediately
  304. }
  305. //-------------------------------------------------------------------------------------------------------
  306. // Resets the layer for reuse
  307. //-------------------------------------------------------------------------------------------------------
  308. void ReliabilityLayer::Reset( bool resetVariables, int MTUSize, bool _useSecurity )
  309. {
  310. FreeMemory( true ); // true because making a memory reset pending in the update cycle causes resets after reconnects. Instead, just call Reset from a single thread
  311. if (resetVariables)
  312. {
  313. InitializeVariables();
  314. #if LIBCAT_SECURITY==1
  315. useSecurity = _useSecurity;
  316. if (_useSecurity)
  317. MTUSize -= cat::AuthenticatedEncryption::OVERHEAD_BYTES;
  318. #else
  319. (void) _useSecurity;
  320. #endif // LIBCAT_SECURITY
  321. congestionManager.Init(RakNet::GetTimeUS(), MTUSize - UDP_HEADER_SIZE);
  322. }
  323. }
  324. //-------------------------------------------------------------------------------------------------------
  325. // Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable packet
  326. //-------------------------------------------------------------------------------------------------------
  327. void ReliabilityLayer::SetTimeoutTime( RakNet::TimeMS time )
  328. {
  329. timeoutTime=time;
  330. }
  331. //-------------------------------------------------------------------------------------------------------
  332. // Returns the value passed to SetTimeoutTime. or the default if it was never called
  333. //-------------------------------------------------------------------------------------------------------
  334. RakNet::TimeMS ReliabilityLayer::GetTimeoutTime(void)
  335. {
  336. return timeoutTime;
  337. }
  338. //-------------------------------------------------------------------------------------------------------
  339. // Initialize the variables
  340. //-------------------------------------------------------------------------------------------------------
  341. void ReliabilityLayer::InitializeVariables( void )
  342. {
  343. memset( orderedWriteIndex, 0, NUMBER_OF_ORDERED_STREAMS * sizeof(OrderingIndexType));
  344. memset( sequencedWriteIndex, 0, NUMBER_OF_ORDERED_STREAMS * sizeof(OrderingIndexType) );
  345. memset( orderedReadIndex, 0, NUMBER_OF_ORDERED_STREAMS * sizeof(OrderingIndexType) );
  346. memset( highestSequencedReadIndex, 0, NUMBER_OF_ORDERED_STREAMS * sizeof(OrderingIndexType) );
  347. memset( &statistics, 0, sizeof( statistics ) );
  348. memset( &heapIndexOffsets, 0, sizeof( heapIndexOffsets ) );
  349. statistics.connectionStartTime = RakNet::GetTimeUS();
  350. splitPacketId = 0;
  351. elapsedTimeSinceLastUpdate=0;
  352. throughputCapCountdown=0;
  353. sendReliableMessageNumberIndex = 0;
  354. internalOrderIndex=0;
  355. timeToNextUnreliableCull=0;
  356. unreliableLinkedListHead=0;
  357. lastUpdateTime= RakNet::GetTimeUS();
  358. bandwidthExceededStatistic=false;
  359. remoteSystemTime=0;
  360. unreliableTimeout=0;
  361. lastBpsClear=0;
  362. // Disable packet pairs
  363. countdownToNextPacketPair=15;
  364. nextAllowedThroughputSample=0;
  365. deadConnection = cheater = false;
  366. timeOfLastContinualSend=0;
  367. // timeResendQueueNonEmpty = 0;
  368. timeLastDatagramArrived=RakNet::GetTimeMS();
  369. // packetlossThisSample=false;
  370. // backoffThisSample=0;
  371. // packetlossThisSampleResendCount=0;
  372. // lastPacketlossTime=0;
  373. statistics.messagesInResendBuffer=0;
  374. statistics.bytesInResendBuffer=0;
  375. receivedPacketsBaseIndex=0;
  376. resetReceivedPackets=true;
  377. receivePacketCount=0;
  378. // SetPing( 1000 );
  379. timeBetweenPackets=STARTING_TIME_BETWEEN_PACKETS;
  380. ackPingIndex=0;
  381. ackPingSum=(CCTimeType)0;
  382. nextSendTime=lastUpdateTime;
  383. //nextLowestPingReset=(CCTimeType)0;
  384. // continuousSend=false;
  385. // histogramStart=(CCTimeType)0;
  386. // histogramBitsSent=0;
  387. unacknowledgedBytes=0;
  388. resendLinkedListHead=0;
  389. totalUserDataBytesAcked=0;
  390. datagramHistoryPopCount=0;
  391. InitHeapWeights();
  392. for (int i=0; i < NUMBER_OF_PRIORITIES; i++)
  393. {
  394. statistics.messageInSendBuffer[i]=0;
  395. statistics.bytesInSendBuffer[i]=0.0;
  396. }
  397. for (int i=0; i < RNS_PER_SECOND_METRICS_COUNT; i++)
  398. {
  399. bpsMetrics[i].Reset(_FILE_AND_LINE_);
  400. }
  401. }
  402. //-------------------------------------------------------------------------------------------------------
  403. // Frees all allocated memory
  404. //-------------------------------------------------------------------------------------------------------
  405. void ReliabilityLayer::FreeMemory( bool freeAllImmediately )
  406. {
  407. (void) freeAllImmediately;
  408. FreeThreadSafeMemory();
  409. }
  410. void ReliabilityLayer::FreeThreadSafeMemory( void )
  411. {
  412. unsigned i,j;
  413. InternalPacket *internalPacket;
  414. ClearPacketsAndDatagrams();
  415. for (i=0; i < splitPacketChannelList.Size(); i++)
  416. {
  417. for (j=0; j < splitPacketChannelList[i]->splitPacketList.Size(); j++)
  418. {
  419. FreeInternalPacketData(splitPacketChannelList[i]->splitPacketList[j], _FILE_AND_LINE_ );
  420. ReleaseToInternalPacketPool( splitPacketChannelList[i]->splitPacketList[j] );
  421. }
  422. #if PREALLOCATE_LARGE_MESSAGES==1
  423. if (splitPacketChannelList[i]->returnedPacket)
  424. {
  425. FreeInternalPacketData(splitPacketChannelList[i]->returnedPacket, __FILE__, __LINE__ );
  426. ReleaseToInternalPacketPool( splitPacketChannelList[i]->returnedPacket );
  427. }
  428. #endif
  429. RakNet::OP_DELETE(splitPacketChannelList[i], __FILE__, __LINE__);
  430. }
  431. splitPacketChannelList.Clear(false, _FILE_AND_LINE_);
  432. while ( outputQueue.Size() > 0 )
  433. {
  434. internalPacket = outputQueue.Pop();
  435. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  436. ReleaseToInternalPacketPool( internalPacket );
  437. }
  438. outputQueue.ClearAndForceAllocation( 32, _FILE_AND_LINE_ );
  439. /*
  440. for ( i = 0; i < orderingList.Size(); i++ )
  441. {
  442. if ( orderingList[ i ] )
  443. {
  444. DataStructures::LinkedList<InternalPacket*>* theList = orderingList[ i ];
  445. if ( theList )
  446. {
  447. while ( theList->Size() )
  448. {
  449. internalPacket = orderingList[ i ]->Pop();
  450. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  451. ReleaseToInternalPacketPool( internalPacket );
  452. }
  453. RakNet::OP_DELETE(theList, _FILE_AND_LINE_);
  454. }
  455. }
  456. }
  457. orderingList.Clear(false, _FILE_AND_LINE_);
  458. */
  459. for (i=0; i < NUMBER_OF_ORDERED_STREAMS; i++)
  460. {
  461. for (j=0; j < orderingHeaps[i].Size(); j++)
  462. {
  463. FreeInternalPacketData(orderingHeaps[i][j], _FILE_AND_LINE_ );
  464. ReleaseToInternalPacketPool( orderingHeaps[i][j] );
  465. }
  466. orderingHeaps[i].Clear(true, _FILE_AND_LINE_);
  467. }
  468. //resendList.ForEachData(DeleteInternalPacket);
  469. // resendTree.Clear(_FILE_AND_LINE_);
  470. memset(resendBuffer, 0, sizeof(resendBuffer));
  471. statistics.messagesInResendBuffer=0;
  472. statistics.bytesInResendBuffer=0;
  473. if (resendLinkedListHead)
  474. {
  475. InternalPacket *prev;
  476. InternalPacket *iter = resendLinkedListHead;
  477. #ifdef _MSC_VER
  478. #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant
  479. #endif
  480. while (1)
  481. {
  482. if (iter->data)
  483. FreeInternalPacketData(iter, _FILE_AND_LINE_ );
  484. prev=iter;
  485. iter=iter->resendNext;
  486. if (iter==resendLinkedListHead)
  487. {
  488. ReleaseToInternalPacketPool(prev);
  489. break;
  490. }
  491. ReleaseToInternalPacketPool(prev);
  492. }
  493. resendLinkedListHead=0;
  494. }
  495. unacknowledgedBytes=0;
  496. // acknowlegements.Clear(_FILE_AND_LINE_);
  497. for ( j=0 ; j < outgoingPacketBuffer.Size(); j++ )
  498. {
  499. if ( outgoingPacketBuffer[ j ]->data)
  500. FreeInternalPacketData( outgoingPacketBuffer[ j ], _FILE_AND_LINE_ );
  501. ReleaseToInternalPacketPool( outgoingPacketBuffer[ j ] );
  502. }
  503. outgoingPacketBuffer.Clear(true, _FILE_AND_LINE_);
  504. #ifdef _DEBUG
  505. for (unsigned i = 0; i < delayList.Size(); i++ )
  506. RakNet::OP_DELETE(delayList[ i ], __FILE__, __LINE__);
  507. delayList.Clear(__FILE__, __LINE__);
  508. #endif
  509. unreliableWithAckReceiptHistory.Clear(false, _FILE_AND_LINE_);
  510. packetsToSendThisUpdate.Clear(false, _FILE_AND_LINE_);
  511. packetsToSendThisUpdate.Preallocate(512, _FILE_AND_LINE_);
  512. packetsToDeallocThisUpdate.Clear(false, _FILE_AND_LINE_);
  513. packetsToDeallocThisUpdate.Preallocate(512, _FILE_AND_LINE_);
  514. packetsToSendThisUpdateDatagramBoundaries.Clear(false, _FILE_AND_LINE_);
  515. packetsToSendThisUpdateDatagramBoundaries.Preallocate(128, _FILE_AND_LINE_);
  516. datagramSizesInBytes.Clear(false, _FILE_AND_LINE_);
  517. datagramSizesInBytes.Preallocate(128, _FILE_AND_LINE_);
  518. internalPacketPool.Clear(_FILE_AND_LINE_);
  519. refCountedDataPool.Clear(_FILE_AND_LINE_);
  520. /*
  521. DataStructures::Page<DatagramSequenceNumberType, DatagramMessageIDList*, RESEND_TREE_ORDER> *cur = datagramMessageIDTree.GetListHead();
  522. while (cur)
  523. {
  524. int treeIndex;
  525. for (treeIndex=0; treeIndex < cur->size; treeIndex++)
  526. ReleaseToDatagramMessageIDPool(cur->data[treeIndex]);
  527. cur=cur->resendNext;
  528. }
  529. datagramMessageIDTree.Clear(_FILE_AND_LINE_);
  530. datagramMessageIDPool.Clear(_FILE_AND_LINE_);
  531. */
  532. while (datagramHistory.Size())
  533. {
  534. RemoveFromDatagramHistory(datagramHistoryPopCount);
  535. datagramHistory.Pop();
  536. datagramHistoryPopCount++;
  537. }
  538. datagramHistoryMessagePool.Clear(_FILE_AND_LINE_);
  539. datagramHistoryPopCount=0;
  540. acknowlegements.Clear();
  541. NAKs.Clear();
  542. unreliableLinkedListHead=0;
  543. }
  544. //-------------------------------------------------------------------------------------------------------
  545. // Packets are read directly from the socket layer and skip the reliability
  546. //layer because unconnected players do not use the reliability layer
  547. // This function takes packet data after a player has been confirmed as
  548. //connected. The game should not use that data directly
  549. // because some data is used internally, such as packet acknowledgment and
  550. //split packets
  551. //-------------------------------------------------------------------------------------------------------
  552. bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer(
  553. const char *buffer, unsigned int length, SystemAddress &systemAddress, DataStructures::List<PluginInterface2*> &messageHandlerList, int MTUSize,
  554. RakNetSocket2 *s, RakNetRandom *rnr, CCTimeType timeRead,
  555. BitStream &updateBitStream)
  556. {
  557. #ifdef _DEBUG
  558. RakAssert( !( buffer == 0 ) );
  559. #endif
  560. #if CC_TIME_TYPE_BYTES==4
  561. timeRead/=1000;
  562. #endif
  563. bpsMetrics[(int) ACTUAL_BYTES_RECEIVED].Push1(timeRead,length);
  564. (void) MTUSize;
  565. if ( length <= 2 || buffer == 0 ) // Length of 1 is a connection request resend that we just ignore
  566. {
  567. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  568. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("length <= 2 || buffer == 0", BYTES_TO_BITS(length), systemAddress, true);
  569. return true;
  570. }
  571. timeLastDatagramArrived=RakNet::GetTimeMS();
  572. // CCTimeType time;
  573. // bool indexFound;
  574. // int count, size;
  575. DatagramSequenceNumberType holeCount;
  576. unsigned i;
  577. #if LIBCAT_SECURITY==1
  578. if (useSecurity)
  579. {
  580. unsigned int received = length;
  581. if (!auth_enc.Decrypt((cat::u8*)buffer, received))
  582. return false;
  583. length = received;
  584. }
  585. #endif
  586. RakNet::BitStream socketData( (unsigned char*) buffer, length, false ); // Convert the incoming data to a bitstream for easy parsing
  587. // time = RakNet::GetTimeUS();
  588. // Set to the current time if it is not zero, and we get incoming data
  589. // if (timeResendQueueNonEmpty!=0)
  590. // timeResendQueueNonEmpty=timeRead;
  591. DatagramHeaderFormat dhf;
  592. dhf.Deserialize(&socketData);
  593. if (dhf.isValid==false)
  594. {
  595. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  596. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("dhf.isValid==false", BYTES_TO_BITS(length), systemAddress, true);
  597. return true;
  598. }
  599. if (dhf.isACK)
  600. {
  601. DatagramSequenceNumberType datagramNumber;
  602. // datagramNumber=dhf.datagramNumber;
  603. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  604. RakNet::TimeMS timeMSLow=(RakNet::TimeMS) timeRead&0xFFFFFFFF;
  605. CCTimeType rtt = timeMSLow-dhf.sourceSystemTime;
  606. #if CC_TIME_TYPE_BYTES==4
  607. if (rtt > 10000)
  608. #else
  609. if (rtt > 10000000)
  610. #endif
  611. {
  612. // Sanity check. This could happen due to type overflow, especially since I only send the low 4 bytes to reduce bandwidth
  613. rtt=(CCTimeType) congestionManager.GetRTT();
  614. }
  615. // RakAssert(rtt < 500000);
  616. // printf("%i ", (RakNet::TimeMS)(rtt/1000));
  617. ackPing=rtt;
  618. #endif
  619. #ifdef _DEBUG
  620. if (dhf.hasBAndAS==false)
  621. {
  622. // dhf.B=0;
  623. dhf.AS=0;
  624. }
  625. #endif
  626. // congestionManager.OnAck(timeRead, rtt, dhf.hasBAndAS, dhf.B, dhf.AS, totalUserDataBytesAcked );
  627. incomingAcks.Clear();
  628. if (incomingAcks.Deserialize(&socketData)==false)
  629. {
  630. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  631. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("incomingAcks.Deserialize failed", BYTES_TO_BITS(length), systemAddress, true);
  632. return false;
  633. }
  634. for (i=0; i<incomingAcks.ranges.Size();i++)
  635. {
  636. if (incomingAcks.ranges[i].minIndex>incomingAcks.ranges[i].maxIndex || (incomingAcks.ranges[i].maxIndex == (uint24_t)(0xFFFFFFFF)))
  637. {
  638. RakAssert(incomingAcks.ranges[i].minIndex<=incomingAcks.ranges[i].maxIndex);
  639. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  640. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("incomingAcks minIndex > maxIndex or maxIndex is max value", BYTES_TO_BITS(length), systemAddress, true);
  641. return false;
  642. }
  643. for (datagramNumber=incomingAcks.ranges[i].minIndex; datagramNumber >= incomingAcks.ranges[i].minIndex && datagramNumber <= incomingAcks.ranges[i].maxIndex; datagramNumber++)
  644. {
  645. CCTimeType whenSent;
  646. if (unreliableWithAckReceiptHistory.Size()>0)
  647. {
  648. unsigned int k=0;
  649. while (k < unreliableWithAckReceiptHistory.Size())
  650. {
  651. if (unreliableWithAckReceiptHistory[k].datagramNumber == datagramNumber)
  652. {
  653. InternalPacket *ackReceipt = AllocateFromInternalPacketPool();
  654. AllocInternalPacketData(ackReceipt, 5, false, _FILE_AND_LINE_ );
  655. ackReceipt->dataBitLength=BYTES_TO_BITS(5);
  656. ackReceipt->data[0]=(MessageID)ID_SND_RECEIPT_ACKED;
  657. memcpy(ackReceipt->data+sizeof(MessageID), &unreliableWithAckReceiptHistory[k].sendReceiptSerial, sizeof(uint32_t));
  658. outputQueue.Push(ackReceipt, _FILE_AND_LINE_ );
  659. // Remove, swap with last
  660. unreliableWithAckReceiptHistory.RemoveAtIndex(k);
  661. }
  662. else
  663. k++;
  664. }
  665. }
  666. MessageNumberNode *messageNumberNode = GetMessageNumberNodeByDatagramIndex(datagramNumber, &whenSent);
  667. if (messageNumberNode)
  668. {
  669. // printf("%p Got ack for %i\n", this, datagramNumber.val);
  670. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  671. congestionManager.OnAck(timeRead, rtt, dhf.hasBAndAS, 0, dhf.AS, totalUserDataBytesAcked, bandwidthExceededStatistic, datagramNumber );
  672. #else
  673. CCTimeType ping;
  674. if (timeRead>whenSent)
  675. ping=timeRead-whenSent;
  676. else
  677. ping=0;
  678. congestionManager.OnAck(timeRead, ping, dhf.hasBAndAS, 0, dhf.AS, totalUserDataBytesAcked, bandwidthExceededStatistic, datagramNumber );
  679. #endif
  680. while (messageNumberNode)
  681. {
  682. // TESTING1
  683. // printf("Remove %i on ack for datagramNumber=%i.\n", messageNumberNode->messageNumber.val, datagramNumber.val);
  684. RemovePacketFromResendListAndDeleteOlderReliableSequenced( messageNumberNode->messageNumber, timeRead, messageHandlerList, systemAddress );
  685. messageNumberNode=messageNumberNode->next;
  686. }
  687. RemoveFromDatagramHistory(datagramNumber);
  688. }
  689. // else if (isReliable)
  690. // {
  691. // // Previously used slot, rather than empty unreliable slot
  692. // printf("%p Ack %i is duplicate\n", this, datagramNumber.val);
  693. //
  694. // congestionManager.OnDuplicateAck(timeRead, datagramNumber);
  695. // }
  696. }
  697. }
  698. }
  699. else if (dhf.isNAK)
  700. {
  701. DatagramSequenceNumberType messageNumber;
  702. DataStructures::RangeList<DatagramSequenceNumberType> incomingNAKs;
  703. if (incomingNAKs.Deserialize(&socketData)==false)
  704. {
  705. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  706. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("incomingNAKs.Deserialize failed", BYTES_TO_BITS(length), systemAddress, true);
  707. return false;
  708. }
  709. for (i=0; i<incomingNAKs.ranges.Size();i++)
  710. {
  711. if (incomingNAKs.ranges[i].minIndex>incomingNAKs.ranges[i].maxIndex)
  712. {
  713. RakAssert(incomingNAKs.ranges[i].minIndex<=incomingNAKs.ranges[i].maxIndex);
  714. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  715. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("incomingNAKs minIndex>maxIndex", BYTES_TO_BITS(length), systemAddress, true);
  716. return false;
  717. }
  718. // Sanity check
  719. //RakAssert(incomingNAKs.ranges[i].maxIndex.val-incomingNAKs.ranges[i].minIndex.val<1000);
  720. for (messageNumber=incomingNAKs.ranges[i].minIndex; messageNumber >= incomingNAKs.ranges[i].minIndex && messageNumber <= incomingNAKs.ranges[i].maxIndex; messageNumber++)
  721. {
  722. congestionManager.OnNAK(timeRead, messageNumber);
  723. // REMOVEME
  724. // printf("%p NAK %i\n", this, dhf.datagramNumber.val);
  725. CCTimeType timeSent;
  726. MessageNumberNode *messageNumberNode = GetMessageNumberNodeByDatagramIndex(messageNumber, &timeSent);
  727. while (messageNumberNode)
  728. {
  729. // Update timers so resends occur immediately
  730. InternalPacket *internalPacket = resendBuffer[messageNumberNode->messageNumber & (uint32_t) RESEND_BUFFER_ARRAY_MASK];
  731. if (internalPacket)
  732. {
  733. if (internalPacket->nextActionTime!=0)
  734. {
  735. internalPacket->nextActionTime=timeRead;
  736. }
  737. }
  738. messageNumberNode=messageNumberNode->next;
  739. }
  740. }
  741. }
  742. }
  743. else
  744. {
  745. uint32_t skippedMessageCount;
  746. if (!congestionManager.OnGotPacket(dhf.datagramNumber, dhf.isContinuousSend, timeRead, length, &skippedMessageCount))
  747. {
  748. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  749. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("congestionManager.OnGotPacket failed", BYTES_TO_BITS(length), systemAddress, true);
  750. return true;
  751. }
  752. if (dhf.isPacketPair)
  753. congestionManager.OnGotPacketPair(dhf.datagramNumber, length, timeRead);
  754. DatagramHeaderFormat dhfNAK;
  755. dhfNAK.isNAK=true;
  756. uint32_t skippedMessageOffset;
  757. for (skippedMessageOffset=skippedMessageCount; skippedMessageOffset > 0; skippedMessageOffset--)
  758. {
  759. NAKs.Insert(dhf.datagramNumber-skippedMessageOffset);
  760. }
  761. remoteSystemNeedsBAndAS=dhf.needsBAndAs;
  762. // Ack dhf.datagramNumber
  763. // Ack even unreliable messages for congestion control, just don't resend them on no ack
  764. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  765. SendAcknowledgementPacket( dhf.datagramNumber, dhf.sourceSystemTime);
  766. #else
  767. SendAcknowledgementPacket( dhf.datagramNumber, 0);
  768. #endif
  769. InternalPacket* internalPacket = CreateInternalPacketFromBitStream( &socketData, timeRead );
  770. if (internalPacket==0)
  771. {
  772. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  773. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("CreateInternalPacketFromBitStream failed", BYTES_TO_BITS(length), systemAddress, true);
  774. return true;
  775. }
  776. while ( internalPacket )
  777. {
  778. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  779. {
  780. #if CC_TIME_TYPE_BYTES==4
  781. messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, receivePacketCount, systemAddress, timeRead, false);
  782. #else
  783. messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, receivePacketCount, systemAddress, (RakNet::TimeMS)(timeRead/(CCTimeType)1000), false);
  784. #endif
  785. }
  786. {
  787. // resetReceivedPackets is set from a non-threadsafe function.
  788. // We do the actual reset in this function so the data is not modified by multiple threads
  789. if (resetReceivedPackets)
  790. {
  791. hasReceivedPacketQueue.ClearAndForceAllocation(DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE, _FILE_AND_LINE_);
  792. receivedPacketsBaseIndex=0;
  793. resetReceivedPackets=false;
  794. }
  795. // Check for corrupt orderingChannel
  796. if (
  797. internalPacket->reliability == RELIABLE_SEQUENCED ||
  798. internalPacket->reliability == UNRELIABLE_SEQUENCED ||
  799. internalPacket->reliability == RELIABLE_ORDERED
  800. )
  801. {
  802. if ( internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS )
  803. {
  804. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  805. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS", BYTES_TO_BITS(length), systemAddress, true);
  806. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  807. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  808. ReleaseToInternalPacketPool( internalPacket );
  809. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  810. }
  811. }
  812. // 8/12/09 was previously not checking if the message was reliable. However, on packetloss this would mean you'd eventually exceed the
  813. // hole count because unreliable messages were never resent, and you'd stop getting messages
  814. if (internalPacket->reliability == RELIABLE || internalPacket->reliability == RELIABLE_SEQUENCED || internalPacket->reliability == RELIABLE_ORDERED )
  815. {
  816. // If the following conditional is true then this either a duplicate packet
  817. // or an older out of order packet
  818. // The subtraction unsigned overflow is intentional
  819. holeCount = (DatagramSequenceNumberType)(internalPacket->reliableMessageNumber-receivedPacketsBaseIndex);
  820. const DatagramSequenceNumberType typeRange = (DatagramSequenceNumberType)(const uint32_t)-1;
  821. // TESTING1
  822. // printf("waiting on reliableMessageNumber=%i holeCount=%i datagramNumber=%i\n", receivedPacketsBaseIndex.val, holeCount.val, dhf.datagramNumber.val);
  823. if (holeCount==(DatagramSequenceNumberType) 0)
  824. {
  825. // Got what we were expecting
  826. if (hasReceivedPacketQueue.Size())
  827. hasReceivedPacketQueue.Pop();
  828. ++receivedPacketsBaseIndex;
  829. }
  830. else if (holeCount > typeRange/(DatagramSequenceNumberType) 2)
  831. {
  832. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  833. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  834. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("holeCount > typeRange/(DatagramSequenceNumberType) 2", BYTES_TO_BITS(length), systemAddress, false);
  835. // Duplicate packet
  836. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  837. ReleaseToInternalPacketPool( internalPacket );
  838. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  839. }
  840. else if ((unsigned int) holeCount<hasReceivedPacketQueue.Size())
  841. {
  842. // Got a higher count out of order packet that was missing in the sequence or we already got
  843. if (hasReceivedPacketQueue[holeCount]!=false) // non-zero means this is a hole
  844. {
  845. #ifdef LOG_TRIVIAL_NOTIFICATIONS
  846. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  847. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Higher count pushed to hasReceivedPacketQueue", BYTES_TO_BITS(length), systemAddress, false);
  848. #endif
  849. // Fill in the hole
  850. hasReceivedPacketQueue[holeCount]=false; // We got the packet at holeCount
  851. }
  852. else
  853. {
  854. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  855. #ifdef LOG_TRIVIAL_NOTIFICATIONS
  856. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  857. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Duplicate packet ignored", BYTES_TO_BITS(length), systemAddress, false);
  858. #endif
  859. // Duplicate packet
  860. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  861. ReleaseToInternalPacketPool( internalPacket );
  862. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  863. }
  864. }
  865. else // holeCount>=receivedPackets.Size()
  866. {
  867. if (holeCount > (DatagramSequenceNumberType) 1000000)
  868. {
  869. RakAssert("Hole count too high. See ReliabilityLayer.h" && 0);
  870. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  871. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("holeCount > 1000000", BYTES_TO_BITS(length), systemAddress, true);
  872. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  873. // Would crash due to out of memory!
  874. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  875. ReleaseToInternalPacketPool( internalPacket );
  876. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  877. }
  878. #ifdef LOG_TRIVIAL_NOTIFICATIONS
  879. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  880. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Adding to hasReceivedPacketQueue later ordered message", BYTES_TO_BITS(length), systemAddress, false);
  881. #endif
  882. // Fix - sending on a higher priority gives us a very very high received packets base index if we formerly had pre-split a lot of messages and
  883. // used that as the message number. Because of this, a lot of time is spent in this linear loop and the timeout time expires because not
  884. // all of the message is sent in time.
  885. // Fixed by late assigning message IDs on the sender
  886. // Add 0 times to the queue until (reliableMessageNumber - baseIndex) < queue size.
  887. while ((unsigned int)(holeCount) > hasReceivedPacketQueue.Size())
  888. hasReceivedPacketQueue.Push(true, _FILE_AND_LINE_ ); // time+(CCTimeType)60 * (CCTimeType)1000 * (CCTimeType)1000); // Didn't get this packet - set the time to give up waiting
  889. hasReceivedPacketQueue.Push(false, _FILE_AND_LINE_ ); // Got the packet
  890. #ifdef _DEBUG
  891. // If this assert hits then DatagramSequenceNumberType has overflowed
  892. RakAssert(hasReceivedPacketQueue.Size() < (unsigned int)((DatagramSequenceNumberType)(const uint32_t)(-1)));
  893. #endif
  894. }
  895. while ( hasReceivedPacketQueue.Size()>0 && hasReceivedPacketQueue.Peek()==false )
  896. {
  897. hasReceivedPacketQueue.Pop();
  898. ++receivedPacketsBaseIndex;
  899. }
  900. }
  901. // If the allocated buffer is > DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE and it is 3x greater than the number of elements actually being used
  902. if (hasReceivedPacketQueue.AllocationSize() > (unsigned int) DEFAULT_HAS_RECEIVED_PACKET_QUEUE_SIZE && hasReceivedPacketQueue.AllocationSize() > hasReceivedPacketQueue.Size() * 3)
  903. hasReceivedPacketQueue.Compress(_FILE_AND_LINE_);
  904. /*
  905. if ( internalPacket->reliability == RELIABLE_SEQUENCED || internalPacket->reliability == UNRELIABLE_SEQUENCED )
  906. {
  907. #ifdef _DEBUG
  908. RakAssert( internalPacket->orderingChannel < NUMBER_OF_ORDERED_STREAMS );
  909. #endif
  910. if ( internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS )
  911. {
  912. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  913. ReleaseToInternalPacketPool( internalPacket );
  914. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  915. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS", BYTES_TO_BITS(length), systemAddress);
  916. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  917. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  918. }
  919. if ( IsOlderOrderedPacket( internalPacket->orderingIndex, waitingForSequencedPacketReadIndex[ internalPacket->orderingChannel ] ) == false )
  920. {
  921. // Is this a split packet?
  922. if ( internalPacket->splitPacketCount > 0 )
  923. {
  924. // Generate the split
  925. // Verify some parameters to make sure we don't get junk data
  926. // Check for a rebuilt packet
  927. InsertIntoSplitPacketList( internalPacket, timeRead );
  928. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  929. // Sequenced
  930. internalPacket = BuildPacketFromSplitPacketList( internalPacket->splitPacketId, timeRead,
  931. s, systemAddress, rnr, remotePortRakNetWasStartedOn_PS3, extraSocketOptions);
  932. if ( internalPacket )
  933. {
  934. // Update our index to the newest packet
  935. waitingForSequencedPacketReadIndex[ internalPacket->orderingChannel ] = internalPacket->orderingIndex + (OrderingIndexType)1;
  936. // If there is a rebuilt packet, add it to the output queue
  937. outputQueue.Push( internalPacket, _FILE_AND_LINE_ );
  938. internalPacket = 0;
  939. }
  940. // else don't have all the parts yet
  941. }
  942. else
  943. {
  944. // Update our index to the newest packet
  945. waitingForSequencedPacketReadIndex[ internalPacket->orderingChannel ] = internalPacket->orderingIndex + (OrderingIndexType)1;
  946. // Not a split packet. Add the packet to the output queue
  947. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  948. outputQueue.Push( internalPacket, _FILE_AND_LINE_ );
  949. internalPacket = 0;
  950. }
  951. }
  952. else
  953. {
  954. // Older sequenced packet. Discard it
  955. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  956. ReleaseToInternalPacketPool( internalPacket );
  957. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  958. }
  959. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  960. }
  961. // Is this an unsequenced split packet?
  962. if ( internalPacket->splitPacketCount > 0 )
  963. {
  964. // Check for a rebuilt packet
  965. if ( internalPacket->reliability != RELIABLE_ORDERED )
  966. internalPacket->orderingChannel = 255; // Use 255 to designate not sequenced and not ordered
  967. InsertIntoSplitPacketList( internalPacket, timeRead );
  968. internalPacket = BuildPacketFromSplitPacketList( internalPacket->splitPacketId, timeRead,
  969. s, systemAddress, rnr, remotePortRakNetWasStartedOn_PS3, extraSocketOptions);
  970. if ( internalPacket == 0 )
  971. {
  972. // Don't have all the parts yet
  973. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  974. }
  975. }
  976. */
  977. /*
  978. if ( internalPacket->reliability == RELIABLE_ORDERED )
  979. {
  980. #ifdef _DEBUG
  981. RakAssert( internalPacket->orderingChannel < NUMBER_OF_ORDERED_STREAMS );
  982. #endif
  983. if ( internalPacket->orderingChannel >= NUMBER_OF_ORDERED_STREAMS )
  984. {
  985. // Invalid packet
  986. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  987. ReleaseToInternalPacketPool( internalPacket );
  988. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_IGNORED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  989. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  990. }
  991. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  992. if ( waitingForOrderedPacketReadIndex[ internalPacket->orderingChannel ] == internalPacket->orderingIndex )
  993. {
  994. // Get the list to hold ordered packets for this stream
  995. DataStructures::LinkedList<InternalPacket*> *orderingListAtOrderingStream;
  996. unsigned char orderingChannelCopy = internalPacket->orderingChannel;
  997. // Push the packet for the user to read
  998. outputQueue.Push( internalPacket, _FILE_AND_LINE_ );
  999. internalPacket = 0; // Don't reference this any longer since other threads access it
  1000. // Wait for the resendNext ordered packet in sequence
  1001. waitingForOrderedPacketReadIndex[ orderingChannelCopy ] ++; // This wraps
  1002. orderingListAtOrderingStream = GetOrderingListAtOrderingStream( orderingChannelCopy );
  1003. if ( orderingListAtOrderingStream != 0)
  1004. {
  1005. while ( orderingListAtOrderingStream->Size() > 0 )
  1006. {
  1007. // Cycle through the list until nothing is found
  1008. orderingListAtOrderingStream->Beginning();
  1009. indexFound=false;
  1010. size=orderingListAtOrderingStream->Size();
  1011. count=0;
  1012. while (count++ < size)
  1013. {
  1014. if ( orderingListAtOrderingStream->Peek()->orderingIndex == waitingForOrderedPacketReadIndex[ orderingChannelCopy ] )
  1015. {
  1016. outputQueue.Push( orderingListAtOrderingStream->Pop(), _FILE_AND_LINE_ );
  1017. waitingForOrderedPacketReadIndex[ orderingChannelCopy ]++;
  1018. indexFound=true;
  1019. }
  1020. else
  1021. (*orderingListAtOrderingStream)++;
  1022. }
  1023. if (indexFound==false)
  1024. break;
  1025. }
  1026. }
  1027. internalPacket = 0;
  1028. }
  1029. else
  1030. {
  1031. // This is a newer ordered packet than we are waiting for. Store it for future use
  1032. AddToOrderingList( internalPacket );
  1033. }
  1034. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  1035. }
  1036. */
  1037. // Is this a split packet? If so then reassemble
  1038. if ( internalPacket->splitPacketCount > 0 )
  1039. {
  1040. // Check for a rebuilt packet
  1041. if ( internalPacket->reliability != RELIABLE_ORDERED && internalPacket->reliability!=RELIABLE_SEQUENCED && internalPacket->reliability!=UNRELIABLE_SEQUENCED)
  1042. internalPacket->orderingChannel = 255; // Use 255 to designate not sequenced and not ordered
  1043. InsertIntoSplitPacketList( internalPacket, timeRead );
  1044. internalPacket = BuildPacketFromSplitPacketList( internalPacket->splitPacketId, timeRead,
  1045. s, systemAddress, rnr, updateBitStream);
  1046. if ( internalPacket == 0 )
  1047. {
  1048. #ifdef LOG_TRIVIAL_NOTIFICATIONS
  1049. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  1050. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("BuildPacketFromSplitPacketList did not return anything.", BYTES_TO_BITS(length), systemAddress, false);
  1051. #endif
  1052. // Don't have all the parts yet
  1053. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  1054. }
  1055. }
  1056. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  1057. unsigned char packetId;
  1058. char *type="UNDEFINED";
  1059. #endif
  1060. if (internalPacket->reliability == RELIABLE_SEQUENCED ||
  1061. internalPacket->reliability == UNRELIABLE_SEQUENCED ||
  1062. internalPacket->reliability == RELIABLE_ORDERED)
  1063. {
  1064. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  1065. // ___________________
  1066. BitStream bitStream(internalPacket->data, BITS_TO_BYTES(internalPacket->dataBitLength), false);
  1067. unsigned int receivedPacketNumber;
  1068. RakNet::Time receivedTime;
  1069. unsigned char streamNumber;
  1070. PacketReliability reliability;
  1071. // ___________________
  1072. bitStream.IgnoreBits(8); // Ignore ID_TIMESTAMP
  1073. bitStream.Read(receivedTime);
  1074. bitStream.Read(packetId);
  1075. bitStream.Read(receivedPacketNumber);
  1076. bitStream.Read(streamNumber);
  1077. bitStream.Read(reliability);
  1078. if (packetId==ID_USER_PACKET_ENUM+1)
  1079. {
  1080. if (reliability==UNRELIABLE_SEQUENCED)
  1081. type="UNRELIABLE_SEQUENCED";
  1082. else if (reliability==RELIABLE_ORDERED)
  1083. type="RELIABLE_ORDERED";
  1084. else
  1085. type="RELIABLE_SEQUENCED";
  1086. }
  1087. // ___________________
  1088. #endif
  1089. if (internalPacket->orderingIndex==orderedReadIndex[internalPacket->orderingChannel])
  1090. {
  1091. // Has current ordering index
  1092. if (internalPacket->reliability == RELIABLE_SEQUENCED ||
  1093. internalPacket->reliability == UNRELIABLE_SEQUENCED)
  1094. {
  1095. // Is sequenced
  1096. if (IsOlderOrderedPacket(internalPacket->sequencingIndex,highestSequencedReadIndex[internalPacket->orderingChannel])==false)
  1097. {
  1098. // Expected or highest known value
  1099. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  1100. if (packetId==ID_USER_PACKET_ENUM+1 && fp)
  1101. {
  1102. fprintf(fp, "Returning %i, %s by fallthrough. OI=%i. SI=%i.\n", receivedPacketNumber, type, internalPacket->orderingIndex.val, internalPacket->sequencingIndex);
  1103. fflush(fp);
  1104. }
  1105. if (packetId==ID_USER_PACKET_ENUM+1)
  1106. {
  1107. if (receivedPacketNumber<packetNumber)
  1108. {
  1109. if (fp)
  1110. {
  1111. fprintf(fp, "Out of order packet from fallthrough! Expecting %i got %i\n", receivedPacketNumber, packetNumber);
  1112. fflush(fp);
  1113. }
  1114. }
  1115. packetNumber=receivedPacketNumber+1;
  1116. }
  1117. #endif
  1118. // Update highest sequence
  1119. // 6/26/2012 - Did not have the +1 in the next statement
  1120. // Means a duplicated RELIABLE_SEQUENCED or UNRELIABLE_SEQUENCED packet would be returned to the user
  1121. highestSequencedReadIndex[internalPacket->orderingChannel] = internalPacket->sequencingIndex+(OrderingIndexType)1;
  1122. // Fallthrough, returned to user below
  1123. }
  1124. else
  1125. {
  1126. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  1127. if (packetId==ID_USER_PACKET_ENUM+1 && fp)
  1128. {
  1129. fprintf(fp, "Discarding %i, %s late sequenced. OI=%i. SI=%i.\n", receivedPacketNumber, type, internalPacket->orderingIndex.val, internalPacket->sequencingIndex);
  1130. fflush(fp);
  1131. }
  1132. #endif
  1133. #ifdef LOG_TRIVIAL_NOTIFICATIONS
  1134. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  1135. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Sequenced rejected: lower than highest known value", BYTES_TO_BITS(length), systemAddress, false);
  1136. #endif
  1137. // Lower than highest known value
  1138. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  1139. ReleaseToInternalPacketPool( internalPacket );
  1140. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  1141. }
  1142. }
  1143. else
  1144. {
  1145. // Push to output buffer immediately
  1146. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  1147. outputQueue.Push( internalPacket, _FILE_AND_LINE_ );
  1148. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  1149. if (packetId==ID_USER_PACKET_ENUM+1 && fp)
  1150. {
  1151. fprintf(fp, "outputting immediate %i, %s. OI=%i. SI=%i.", receivedPacketNumber, type, internalPacket->orderingIndex.val, internalPacket->sequencingIndex);
  1152. if (orderingHeaps[internalPacket->orderingChannel].Size()==0)
  1153. fprintf(fp, "heap empty\n");
  1154. else
  1155. fprintf(fp, "heap head=%i\n", orderingHeaps[internalPacket->orderingChannel].Peek()->orderingIndex.val);
  1156. if (receivedPacketNumber<packetNumber)
  1157. {
  1158. if (packetId==ID_USER_PACKET_ENUM+1 && fp)
  1159. {
  1160. fprintf(fp, "Out of order packet arrived! Expecting %i got %i\n", receivedPacketNumber, packetNumber);
  1161. fflush(fp);
  1162. }
  1163. }
  1164. packetNumber=receivedPacketNumber+1;
  1165. fflush(fp);
  1166. }
  1167. #endif
  1168. orderedReadIndex[internalPacket->orderingChannel]++;
  1169. highestSequencedReadIndex[internalPacket->orderingChannel] = 0;
  1170. // Return off heap until order lost
  1171. while (orderingHeaps[internalPacket->orderingChannel].Size()>0 &&
  1172. orderingHeaps[internalPacket->orderingChannel].Peek()->orderingIndex==orderedReadIndex[internalPacket->orderingChannel])
  1173. {
  1174. internalPacket = orderingHeaps[internalPacket->orderingChannel].Pop(0);
  1175. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  1176. BitStream bitStream2(internalPacket->data, BITS_TO_BYTES(internalPacket->dataBitLength), false);
  1177. bitStream2.IgnoreBits(8); // Ignore ID_TIMESTAMP
  1178. bitStream2.Read(receivedTime);
  1179. bitStream2.IgnoreBits(8); // Ignore ID_USER_ENUM+1
  1180. bitStream2.Read(receivedPacketNumber);
  1181. bitStream2.Read(streamNumber);
  1182. bitStream2.Read(reliability);
  1183. char *type="UNDEFINED";
  1184. if (reliability==UNRELIABLE_SEQUENCED)
  1185. type="UNRELIABLE_SEQUENCED";
  1186. else if (reliability==RELIABLE_ORDERED)
  1187. type="RELIABLE_ORDERED";
  1188. if (packetId==ID_USER_PACKET_ENUM+1 && fp)
  1189. {
  1190. fprintf(fp, "Heap pop %i, %s. OI=%i. SI=%i.\n", receivedPacketNumber, type, internalPacket->orderingIndex.val, internalPacket->sequencingIndex);
  1191. fflush(fp);
  1192. if (receivedPacketNumber<packetNumber)
  1193. {
  1194. if (packetId==ID_USER_PACKET_ENUM+1 && fp)
  1195. {
  1196. fprintf(fp, "Out of order packet from heap! Expecting %i got %i\n", receivedPacketNumber, packetNumber);
  1197. fflush(fp);
  1198. }
  1199. }
  1200. packetNumber=receivedPacketNumber+1;
  1201. }
  1202. #endif
  1203. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  1204. outputQueue.Push( internalPacket, _FILE_AND_LINE_ );
  1205. if (internalPacket->reliability == RELIABLE_ORDERED)
  1206. {
  1207. orderedReadIndex[internalPacket->orderingChannel]++;
  1208. }
  1209. else
  1210. {
  1211. highestSequencedReadIndex[internalPacket->orderingChannel] = internalPacket->sequencingIndex;
  1212. }
  1213. }
  1214. // Done
  1215. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  1216. }
  1217. }
  1218. else if (IsOlderOrderedPacket(internalPacket->orderingIndex,orderedReadIndex[internalPacket->orderingChannel])==false)
  1219. {
  1220. // internalPacket->_orderingIndex is greater
  1221. // If a message has a greater ordering index, and is sequenced or ordered, buffer it
  1222. // Sequenced has a lower heap weight, ordered has max sequenced weight
  1223. // Keep orderedHoleCount count small
  1224. if (orderingHeaps[internalPacket->orderingChannel].Size()==0)
  1225. heapIndexOffsets[internalPacket->orderingChannel]=orderedReadIndex[internalPacket->orderingChannel];
  1226. reliabilityHeapWeightType orderedHoleCount = internalPacket->orderingIndex-heapIndexOffsets[internalPacket->orderingChannel];
  1227. reliabilityHeapWeightType weight = orderedHoleCount*1048576;
  1228. if (internalPacket->reliability == RELIABLE_SEQUENCED ||
  1229. internalPacket->reliability == UNRELIABLE_SEQUENCED)
  1230. weight+=internalPacket->sequencingIndex;
  1231. else
  1232. weight+=(1048576-1);
  1233. orderingHeaps[internalPacket->orderingChannel].Push(weight, internalPacket, _FILE_AND_LINE_);
  1234. #ifdef PRINT_TO_FILE_RELIABLE_ORDERED_TEST
  1235. if (packetId==ID_USER_PACKET_ENUM+1 && fp)
  1236. {
  1237. fprintf(fp, "Heap push %i, %s, weight=%" PRINTF_64_BIT_MODIFIER "u. OI=%i. waiting on %i. SI=%i.\n", receivedPacketNumber, type, weight, internalPacket->orderingIndex.val, orderedReadIndex[internalPacket->orderingChannel].val, internalPacket->sequencingIndex);
  1238. fflush(fp);
  1239. }
  1240. #endif
  1241. #ifdef LOG_TRIVIAL_NOTIFICATIONS
  1242. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  1243. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Larger number ordered packet leaving holes", BYTES_TO_BITS(length), systemAddress, false);
  1244. #endif
  1245. // Buffered, nothing to do
  1246. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  1247. }
  1248. else
  1249. {
  1250. // Out of order
  1251. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  1252. ReleaseToInternalPacketPool( internalPacket );
  1253. #ifdef LOG_TRIVIAL_NOTIFICATIONS
  1254. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  1255. messageHandlerList[messageHandlerIndex]->OnReliabilityLayerNotification("Rejected older resend", BYTES_TO_BITS(length), systemAddress, false);
  1256. #endif
  1257. // Ignored, nothing to do
  1258. goto CONTINUE_SOCKET_DATA_PARSE_LOOP;
  1259. }
  1260. }
  1261. bpsMetrics[(int) USER_MESSAGE_BYTES_RECEIVED_PROCESSED].Push1(timeRead,BITS_TO_BYTES(internalPacket->dataBitLength));
  1262. // Nothing special about this packet. Add it to the output queue
  1263. outputQueue.Push( internalPacket, _FILE_AND_LINE_ );
  1264. internalPacket = 0;
  1265. }
  1266. // Used for a goto to jump to the resendNext packet immediately
  1267. CONTINUE_SOCKET_DATA_PARSE_LOOP:
  1268. // Parse the bitstream to create an internal packet
  1269. internalPacket = CreateInternalPacketFromBitStream( &socketData, timeRead );
  1270. }
  1271. }
  1272. receivePacketCount++;
  1273. return true;
  1274. }
  1275. //-------------------------------------------------------------------------------------------------------
  1276. // This gets an end-user packet already parsed out. Returns number of BITS put into the buffer
  1277. //-------------------------------------------------------------------------------------------------------
  1278. BitSize_t ReliabilityLayer::Receive( unsigned char **data )
  1279. {
  1280. InternalPacket * internalPacket;
  1281. if ( outputQueue.Size() > 0 )
  1282. {
  1283. // #ifdef _DEBUG
  1284. // RakAssert(bitStream->GetNumberOfBitsUsed()==0);
  1285. // #endif
  1286. internalPacket = outputQueue.Pop();
  1287. BitSize_t bitLength;
  1288. *data = internalPacket->data;
  1289. bitLength = internalPacket->dataBitLength;
  1290. ReleaseToInternalPacketPool( internalPacket );
  1291. return bitLength;
  1292. }
  1293. else
  1294. {
  1295. return 0;
  1296. }
  1297. }
  1298. //-------------------------------------------------------------------------------------------------------
  1299. // Puts data on the send queue
  1300. // bitStream contains the data to send
  1301. // priority is what priority to send the data at
  1302. // reliability is what reliability to use
  1303. // ordering channel is from 0 to 255 and specifies what stream to use
  1304. //-------------------------------------------------------------------------------------------------------
  1305. bool ReliabilityLayer::Send( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, unsigned char orderingChannel, bool makeDataCopy, int MTUSize, CCTimeType currentTime, uint32_t receipt )
  1306. {
  1307. #ifdef _DEBUG
  1308. RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) );
  1309. RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) );
  1310. RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) );
  1311. RakAssert( numberOfBitsToSend > 0 );
  1312. #endif
  1313. #if CC_TIME_TYPE_BYTES==4
  1314. currentTime/=1000;
  1315. #endif
  1316. (void) MTUSize;
  1317. // int a = BITS_TO_BYTES(numberOfBitsToSend);
  1318. // Fix any bad parameters
  1319. if ( reliability > RELIABLE_ORDERED_WITH_ACK_RECEIPT || reliability < 0 )
  1320. reliability = RELIABLE;
  1321. if ( priority > NUMBER_OF_PRIORITIES || priority < 0 )
  1322. priority = HIGH_PRIORITY;
  1323. if ( orderingChannel >= NUMBER_OF_ORDERED_STREAMS )
  1324. orderingChannel = 0;
  1325. unsigned int numberOfBytesToSend=(unsigned int) BITS_TO_BYTES(numberOfBitsToSend);
  1326. if ( numberOfBitsToSend == 0 )
  1327. {
  1328. return false;
  1329. }
  1330. InternalPacket * internalPacket = AllocateFromInternalPacketPool();
  1331. if (internalPacket==0)
  1332. {
  1333. notifyOutOfMemory(_FILE_AND_LINE_);
  1334. return false; // Out of memory
  1335. }
  1336. bpsMetrics[(int) USER_MESSAGE_BYTES_PUSHED].Push1(currentTime,numberOfBytesToSend);
  1337. internalPacket->creationTime = currentTime;
  1338. if ( makeDataCopy )
  1339. {
  1340. AllocInternalPacketData(internalPacket, numberOfBytesToSend, true, _FILE_AND_LINE_ );
  1341. //internalPacket->data = (unsigned char*) rakMalloc_Ex( numberOfBytesToSend, _FILE_AND_LINE_ );
  1342. memcpy( internalPacket->data, data, numberOfBytesToSend );
  1343. }
  1344. else
  1345. {
  1346. // Allocated the data elsewhere, delete it in here
  1347. //internalPacket->data = ( unsigned char* ) data;
  1348. AllocInternalPacketData(internalPacket, (unsigned char*) data );
  1349. }
  1350. internalPacket->dataBitLength = numberOfBitsToSend;
  1351. internalPacket->messageInternalOrder = internalOrderIndex++;
  1352. internalPacket->priority = priority;
  1353. internalPacket->reliability = reliability;
  1354. internalPacket->sendReceiptSerial=receipt;
  1355. // Calculate if I need to split the packet
  1356. // int headerLength = BITS_TO_BYTES( GetMessageHeaderLengthBits( internalPacket, true ) );
  1357. unsigned int maxDataSizeBytes = GetMaxDatagramSizeExcludingMessageHeaderBytes() - BITS_TO_BYTES(GetMaxMessageHeaderLengthBits());
  1358. bool splitPacket = numberOfBytesToSend > maxDataSizeBytes;
  1359. // If a split packet, we might have to upgrade the reliability
  1360. if ( splitPacket )
  1361. {
  1362. // Split packets cannot be unreliable, in case that one part doesn't arrive and the whole cannot be reassembled.
  1363. // One part could not arrive either due to packetloss or due to unreliable discard
  1364. if (internalPacket->reliability==UNRELIABLE)
  1365. internalPacket->reliability=RELIABLE;
  1366. else if (internalPacket->reliability==UNRELIABLE_WITH_ACK_RECEIPT)
  1367. internalPacket->reliability=RELIABLE_WITH_ACK_RECEIPT;
  1368. else if (internalPacket->reliability==UNRELIABLE_SEQUENCED)
  1369. internalPacket->reliability=RELIABLE_SEQUENCED;
  1370. // else if (internalPacket->reliability==UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT)
  1371. // internalPacket->reliability=RELIABLE_SEQUENCED_WITH_ACK_RECEIPT;
  1372. }
  1373. // ++sendMessageNumberIndex;
  1374. if ( internalPacket->reliability == RELIABLE_SEQUENCED ||
  1375. internalPacket->reliability == UNRELIABLE_SEQUENCED
  1376. // ||
  1377. // internalPacket->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT ||
  1378. // internalPacket->reliability == UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT
  1379. )
  1380. {
  1381. // Assign the sequence stream and index
  1382. internalPacket->orderingChannel = orderingChannel;
  1383. internalPacket->orderingIndex = orderedWriteIndex[ orderingChannel ];
  1384. internalPacket->sequencingIndex = sequencedWriteIndex[ orderingChannel ]++;
  1385. // This packet supersedes all other sequenced packets on the same ordering channel
  1386. // Delete all packets in all send lists that are sequenced and on the same ordering channel
  1387. // UPDATE:
  1388. // Disabled. We don't have enough info to consistently do this. Sometimes newer data does supercede
  1389. // older data such as with constantly declining health, but not in all cases.
  1390. // For example, with sequenced unreliable sound packets just because you send a newer one doesn't mean you
  1391. // don't need the older ones because the odds are they will still arrive in order
  1392. /*
  1393. for (int i=0; i < NUMBER_OF_PRIORITIES; i++)
  1394. {
  1395. DeleteSequencedPacketsInList(orderingChannel, sendQueue[i]);
  1396. }
  1397. */
  1398. }
  1399. else if ( internalPacket->reliability == RELIABLE_ORDERED || internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT )
  1400. {
  1401. // Assign the ordering channel and index
  1402. internalPacket->orderingChannel = orderingChannel;
  1403. internalPacket->orderingIndex = orderedWriteIndex[ orderingChannel ] ++;
  1404. sequencedWriteIndex[ orderingChannel ]=0;
  1405. }
  1406. if ( splitPacket ) // If it uses a secure header it will be generated here
  1407. {
  1408. // Must split the packet. This will also generate the SHA1 if it is required. It also adds it to the send list.
  1409. //InternalPacket packetCopy;
  1410. //memcpy(&packetCopy, internalPacket, sizeof(InternalPacket));
  1411. //sendPacketSet[priority].CancelWriteLock(internalPacket);
  1412. //SplitPacket( &packetCopy, MTUSize );
  1413. SplitPacket( internalPacket );
  1414. //RakNet::OP_DELETE_ARRAY(packetCopy.data, _FILE_AND_LINE_);
  1415. return true;
  1416. }
  1417. RakAssert(internalPacket->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  1418. AddToUnreliableLinkedList(internalPacket);
  1419. RakAssert(internalPacket->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  1420. RakAssert(internalPacket->messageNumberAssigned==false);
  1421. outgoingPacketBuffer.Push( GetNextWeight(internalPacket->priority), internalPacket, _FILE_AND_LINE_ );
  1422. RakAssert(outgoingPacketBuffer.Size()==0 || outgoingPacketBuffer.Peek()->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  1423. statistics.messageInSendBuffer[(int)internalPacket->priority]++;
  1424. statistics.bytesInSendBuffer[(int)internalPacket->priority]+=(double) BITS_TO_BYTES(internalPacket->dataBitLength);
  1425. // sendPacketSet[priority].WriteUnlock();
  1426. return true;
  1427. }
  1428. //-------------------------------------------------------------------------------------------------------
  1429. // Run this once per game cycle. Handles internal lists and actually does the send
  1430. //-------------------------------------------------------------------------------------------------------
  1431. void ReliabilityLayer::Update( RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time,
  1432. unsigned bitsPerSecondLimit,
  1433. DataStructures::List<PluginInterface2*> &messageHandlerList,
  1434. RakNetRandom *rnr,
  1435. BitStream &updateBitStream)
  1436. {
  1437. (void) MTUSize;
  1438. RakNet::TimeMS timeMs;
  1439. #if CC_TIME_TYPE_BYTES==4
  1440. time/=1000;
  1441. timeMs=time;
  1442. #else
  1443. timeMs=(RakNet::TimeMS) (time/(CCTimeType)1000);
  1444. #endif
  1445. #ifdef _DEBUG
  1446. while (delayList.Size())
  1447. {
  1448. if (delayList.Peek()->sendTime <= timeMs)
  1449. {
  1450. DataAndTime *dat = delayList.Pop();
  1451. // SocketLayer::SendTo( dat->s, dat->data, dat->length, systemAddress, __FILE__, __LINE__ );
  1452. RNS2_SendParameters bsp;
  1453. bsp.data = (char*) dat->data;
  1454. bsp.length = dat->length;
  1455. bsp.systemAddress = systemAddress;
  1456. dat->s->Send(&bsp, _FILE_AND_LINE_);
  1457. RakNet::OP_DELETE(dat,__FILE__,__LINE__);
  1458. }
  1459. else
  1460. {
  1461. break;
  1462. }
  1463. }
  1464. #endif
  1465. // This line is necessary because the timer isn't accurate
  1466. if (time <= lastUpdateTime)
  1467. {
  1468. // Always set the last time in case of overflow
  1469. lastUpdateTime=time;
  1470. return;
  1471. }
  1472. CCTimeType timeSinceLastTick = time - lastUpdateTime;
  1473. lastUpdateTime=time;
  1474. #if CC_TIME_TYPE_BYTES==4
  1475. if (timeSinceLastTick>100)
  1476. timeSinceLastTick=100;
  1477. #else
  1478. if (timeSinceLastTick>100000)
  1479. timeSinceLastTick=100000;
  1480. #endif
  1481. if (unreliableTimeout>0)
  1482. {
  1483. if (timeSinceLastTick>=timeToNextUnreliableCull)
  1484. {
  1485. if (unreliableLinkedListHead)
  1486. {
  1487. // Cull out all unreliable messages that have exceeded the timeout
  1488. InternalPacket *cur = unreliableLinkedListHead;
  1489. InternalPacket *end = unreliableLinkedListHead->unreliablePrev;
  1490. #ifdef _MSC_VER
  1491. #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant
  1492. #endif
  1493. while (1)
  1494. {
  1495. if (time > cur->creationTime+(CCTimeType)unreliableTimeout)
  1496. {
  1497. // Flag invalid, and clear the memory. Still needs to be removed from the sendPacketSet later
  1498. // This fixes a problem where a remote system disconnects, but we don't know it yet, and memory consumption increases to a huge value
  1499. FreeInternalPacketData(cur, _FILE_AND_LINE_ );
  1500. cur->data=0;
  1501. InternalPacket *next = cur->unreliableNext;
  1502. RemoveFromUnreliableLinkedList(cur);
  1503. if (cur==end)
  1504. break;
  1505. cur=next;
  1506. }
  1507. else
  1508. {
  1509. // if (cur==end)
  1510. // break;
  1511. //
  1512. // cur=cur->unreliableNext;
  1513. // They should be inserted in-order, so no need to iterate past the first failure
  1514. break;
  1515. }
  1516. }
  1517. }
  1518. timeToNextUnreliableCull=unreliableTimeout/(CCTimeType)2;
  1519. }
  1520. else
  1521. {
  1522. timeToNextUnreliableCull-=timeSinceLastTick;
  1523. }
  1524. }
  1525. // Due to thread vagarities and the way I store the time to avoid slow calls to RakNet::GetTime
  1526. // time may be less than lastAck
  1527. #if CC_TIME_TYPE_BYTES==4
  1528. if ( statistics.messagesInResendBuffer!=0 && AckTimeout(time) )
  1529. #else
  1530. if ( statistics.messagesInResendBuffer!=0 && AckTimeout(RakNet::TimeMS(time/(CCTimeType)1000)) )
  1531. #endif
  1532. {
  1533. // SHOW - dead connection
  1534. // We've waited a very long time for a reliable packet to get an ack and it never has
  1535. deadConnection = true;
  1536. return;
  1537. }
  1538. if (congestionManager.ShouldSendACKs(time,timeSinceLastTick))
  1539. {
  1540. SendACKs(s, systemAddress, time, rnr, updateBitStream);
  1541. }
  1542. if (NAKs.Size()>0)
  1543. {
  1544. updateBitStream.Reset();
  1545. DatagramHeaderFormat dhfNAK;
  1546. dhfNAK.isNAK=true;
  1547. dhfNAK.isACK=false;
  1548. dhfNAK.isPacketPair=false;
  1549. dhfNAK.Serialize(&updateBitStream);
  1550. NAKs.Serialize(&updateBitStream, GetMaxDatagramSizeExcludingMessageHeaderBits(), true);
  1551. SendBitStream( s, systemAddress, &updateBitStream, rnr, time );
  1552. }
  1553. DatagramHeaderFormat dhf;
  1554. dhf.needsBAndAs=congestionManager.GetIsInSlowStart();
  1555. dhf.isContinuousSend=bandwidthExceededStatistic;
  1556. // bandwidthExceededStatistic=sendPacketSet[0].IsEmpty()==false ||
  1557. // sendPacketSet[1].IsEmpty()==false ||
  1558. // sendPacketSet[2].IsEmpty()==false ||
  1559. // sendPacketSet[3].IsEmpty()==false;
  1560. bandwidthExceededStatistic=outgoingPacketBuffer.Size()>0;
  1561. const bool hasDataToSendOrResend = IsResendQueueEmpty()==false || bandwidthExceededStatistic;
  1562. RakAssert(NUMBER_OF_PRIORITIES==4);
  1563. congestionManager.Update(time, hasDataToSendOrResend);
  1564. statistics.BPSLimitByOutgoingBandwidthLimit = BITS_TO_BYTES(bitsPerSecondLimit);
  1565. statistics.BPSLimitByCongestionControl = congestionManager.GetBytesPerSecondLimitByCongestionControl();
  1566. unsigned int i;
  1567. if (time > lastBpsClear+
  1568. #if CC_TIME_TYPE_BYTES==4
  1569. 100
  1570. #else
  1571. 100000
  1572. #endif
  1573. )
  1574. {
  1575. for (i=0; i < RNS_PER_SECOND_METRICS_COUNT; i++)
  1576. {
  1577. bpsMetrics[i].ClearExpired1(time);
  1578. }
  1579. lastBpsClear=time;
  1580. }
  1581. if (unreliableWithAckReceiptHistory.Size()>0)
  1582. {
  1583. i=0;
  1584. while (i < unreliableWithAckReceiptHistory.Size())
  1585. {
  1586. //if (unreliableWithAckReceiptHistory[i].nextActionTime < time)
  1587. if (time - unreliableWithAckReceiptHistory[i].nextActionTime < (((CCTimeType)-1)/2) )
  1588. {
  1589. InternalPacket *ackReceipt = AllocateFromInternalPacketPool();
  1590. AllocInternalPacketData(ackReceipt, 5, false, _FILE_AND_LINE_ );
  1591. ackReceipt->dataBitLength=BYTES_TO_BITS(5);
  1592. ackReceipt->data[0]=(MessageID)ID_SND_RECEIPT_LOSS;
  1593. memcpy(ackReceipt->data+sizeof(MessageID), &unreliableWithAckReceiptHistory[i].sendReceiptSerial, sizeof(uint32_t));
  1594. outputQueue.Push(ackReceipt, _FILE_AND_LINE_ );
  1595. // Remove, swap with last
  1596. unreliableWithAckReceiptHistory.RemoveAtIndex(i);
  1597. }
  1598. else
  1599. i++;
  1600. }
  1601. }
  1602. if (hasDataToSendOrResend==true)
  1603. {
  1604. InternalPacket *internalPacket;
  1605. // bool forceSend=false;
  1606. bool pushedAnything;
  1607. BitSize_t nextPacketBitLength;
  1608. dhf.isACK=false;
  1609. dhf.isNAK=false;
  1610. dhf.hasBAndAS=false;
  1611. ResetPacketsAndDatagrams();
  1612. int transmissionBandwidth = congestionManager.GetTransmissionBandwidth(time, timeSinceLastTick, unacknowledgedBytes,dhf.isContinuousSend);
  1613. int retransmissionBandwidth = congestionManager.GetRetransmissionBandwidth(time, timeSinceLastTick, unacknowledgedBytes,dhf.isContinuousSend);
  1614. if (retransmissionBandwidth>0 || transmissionBandwidth>0)
  1615. {
  1616. statistics.isLimitedByCongestionControl=false;
  1617. allDatagramSizesSoFar=0;
  1618. // Keep filling datagrams until we exceed retransmission bandwidth
  1619. while ((int)BITS_TO_BYTES(allDatagramSizesSoFar)<retransmissionBandwidth)
  1620. {
  1621. pushedAnything=false;
  1622. // Fill one datagram, then break
  1623. while ( IsResendQueueEmpty()==false )
  1624. {
  1625. internalPacket = resendLinkedListHead;
  1626. RakAssert(internalPacket->messageNumberAssigned==true);
  1627. //if ( internalPacket->nextActionTime < time )
  1628. if ( time - internalPacket->nextActionTime < (((CCTimeType)-1)/2) )
  1629. {
  1630. nextPacketBitLength = internalPacket->headerLength + internalPacket->dataBitLength;
  1631. if ( datagramSizeSoFar + nextPacketBitLength > GetMaxDatagramSizeExcludingMessageHeaderBits() )
  1632. {
  1633. // Gathers all PushPackets()
  1634. PushDatagram();
  1635. break;
  1636. }
  1637. PopListHead(false);
  1638. CC_DEBUG_PRINTF_2("Rs %i ", internalPacket->reliableMessageNumber.val);
  1639. bpsMetrics[(int) USER_MESSAGE_BYTES_RESENT].Push1(time,BITS_TO_BYTES(internalPacket->dataBitLength));
  1640. // Testing1
  1641. // if (internalPacket->reliability==RELIABLE_ORDERED || internalPacket->reliability==RELIABLE_ORDERED_WITH_ACK_RECEIPT)
  1642. // printf("RESEND reliableMessageNumber %i with datagram %i\n", internalPacket->reliableMessageNumber.val, congestionManager.GetNextDatagramSequenceNumber().val);
  1643. PushPacket(time,internalPacket,true); // Affects GetNewTransmissionBandwidth()
  1644. internalPacket->timesSent++;
  1645. congestionManager.OnResend(time, internalPacket->nextActionTime);
  1646. internalPacket->retransmissionTime = congestionManager.GetRTOForRetransmission(internalPacket->timesSent);
  1647. internalPacket->nextActionTime = internalPacket->retransmissionTime+time;
  1648. pushedAnything=true;
  1649. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  1650. {
  1651. #if CC_TIME_TYPE_BYTES==4
  1652. messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, packetsToSendThisUpdateDatagramBoundaries.Size()+congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (RakNet::TimeMS) time, true);
  1653. #else
  1654. messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, packetsToSendThisUpdateDatagramBoundaries.Size()+congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (RakNet::TimeMS)(time/(CCTimeType)1000), true);
  1655. #endif
  1656. }
  1657. // Put the packet back into the resend list at the correct spot
  1658. // Don't make a copy since I'm reinserting an allocated struct
  1659. InsertPacketIntoResendList( internalPacket, time, false, false );
  1660. // Removeme
  1661. // printf("Resend:%i ", internalPacket->reliableMessageNumber);
  1662. }
  1663. else
  1664. {
  1665. // Filled one datagram.
  1666. // If the 2nd and it's time to send a datagram pair, will be marked as a pair
  1667. PushDatagram();
  1668. break;
  1669. }
  1670. }
  1671. if (pushedAnything==false)
  1672. break;
  1673. }
  1674. }
  1675. else
  1676. {
  1677. statistics.isLimitedByCongestionControl=true;
  1678. }
  1679. if ((int)BITS_TO_BYTES(allDatagramSizesSoFar)<transmissionBandwidth)
  1680. {
  1681. // printf("S+ ");
  1682. allDatagramSizesSoFar=0;
  1683. // Keep filling datagrams until we exceed transmission bandwidth
  1684. while (
  1685. ResendBufferOverflow()==false &&
  1686. ((int)BITS_TO_BYTES(allDatagramSizesSoFar)<transmissionBandwidth ||
  1687. // This condition means if we want to send a datagram pair, and only have one datagram buffered, exceed bandwidth to add another
  1688. (countdownToNextPacketPair==0 &&
  1689. datagramsToSendThisUpdateIsPair.Size()==1))
  1690. )
  1691. {
  1692. // Fill with packets until MTU is reached
  1693. // for ( i = 0; i < NUMBER_OF_PRIORITIES; i++ )
  1694. // {
  1695. pushedAnything=false;
  1696. statistics.isLimitedByOutgoingBandwidthLimit=bitsPerSecondLimit!=0 && BITS_TO_BYTES(bitsPerSecondLimit) < bpsMetrics[USER_MESSAGE_BYTES_SENT].GetBPS1(time);
  1697. while (outgoingPacketBuffer.Size() &&
  1698. statistics.isLimitedByOutgoingBandwidthLimit==false)
  1699. //while ( sendPacketSet[ i ].Size() )
  1700. {
  1701. internalPacket=outgoingPacketBuffer.Peek();
  1702. RakAssert(internalPacket->messageNumberAssigned==false);
  1703. RakAssert(outgoingPacketBuffer.Size()==0 || outgoingPacketBuffer.Peek()->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  1704. // internalPacket = sendPacketSet[ i ].Peek();
  1705. if (internalPacket->data==0)
  1706. {
  1707. //sendPacketSet[ i ].Pop();
  1708. outgoingPacketBuffer.Pop(0);
  1709. RakAssert(outgoingPacketBuffer.Size()==0 || outgoingPacketBuffer.Peek()->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  1710. statistics.messageInSendBuffer[(int)internalPacket->priority]--;
  1711. statistics.bytesInSendBuffer[(int)internalPacket->priority]-=(double) BITS_TO_BYTES(internalPacket->dataBitLength);
  1712. ReleaseToInternalPacketPool( internalPacket );
  1713. continue;
  1714. }
  1715. internalPacket->headerLength=GetMessageHeaderLengthBits(internalPacket);
  1716. nextPacketBitLength = internalPacket->headerLength + internalPacket->dataBitLength;
  1717. if ( datagramSizeSoFar + nextPacketBitLength > GetMaxDatagramSizeExcludingMessageHeaderBits() )
  1718. {
  1719. // Hit MTU. May still push packets if smaller ones exist at a lower priority
  1720. RakAssert(datagramSizeSoFar!=0);
  1721. RakAssert(internalPacket->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  1722. break;
  1723. }
  1724. bool isReliable;
  1725. if ( internalPacket->reliability == RELIABLE ||
  1726. internalPacket->reliability == RELIABLE_SEQUENCED ||
  1727. internalPacket->reliability == RELIABLE_ORDERED ||
  1728. internalPacket->reliability == RELIABLE_WITH_ACK_RECEIPT ||
  1729. // internalPacket->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT ||
  1730. internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT
  1731. )
  1732. isReliable = true;
  1733. else
  1734. isReliable = false;
  1735. //sendPacketSet[ i ].Pop();
  1736. outgoingPacketBuffer.Pop(0);
  1737. RakAssert(outgoingPacketBuffer.Size()==0 || outgoingPacketBuffer.Peek()->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  1738. RakAssert(internalPacket->messageNumberAssigned==false);
  1739. statistics.messageInSendBuffer[(int)internalPacket->priority]--;
  1740. statistics.bytesInSendBuffer[(int)internalPacket->priority]-=(double) BITS_TO_BYTES(internalPacket->dataBitLength);
  1741. if (isReliable
  1742. /*
  1743. I thought about this and agree that UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT and RELIABLE_SEQUENCED_WITH_ACK_RECEIPT is not useful unless you also know if the message was discarded.
  1744. The problem is that internally, message numbers are only assigned to reliable messages, because message numbers are only used to discard duplicate message receipt and only reliable messages get sent more than once. However, without message numbers getting assigned and transmitted, there is no way to tell the sender about which messages were discarded. In fact, in looking this over I realized that UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT introduced a bug, because the remote system assumes all message numbers are used (no holes). With that send type, on packetloss, a permanent hole would have been created which eventually would cause the system to discard all further packets.
  1745. So I have two options. Either do not support ack receipts when sending sequenced, or write complex and major new systems. UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT would need to send the message ID number on a special channel which allows for non-delivery. And both of them would need to have a special range list to indicate which message numbers were not delivered, so when acks are sent that can be indicated as well. A further problem is that the ack itself can be lost - it is possible that the message can arrive but be discarded, yet the ack is lost. On resend, the resent message would be ignored as duplicate, and you'd never get the discard message either (unless I made a special buffer for that case too).
  1746. */
  1747. // ||
  1748. // If needs an ack receipt, keep the internal packet around in the list
  1749. // internalPacket->reliability == UNRELIABLE_WITH_ACK_RECEIPT ||
  1750. // internalPacket->reliability == UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT
  1751. )
  1752. {
  1753. internalPacket->messageNumberAssigned=true;
  1754. internalPacket->reliableMessageNumber=sendReliableMessageNumberIndex;
  1755. internalPacket->retransmissionTime = congestionManager.GetRTOForRetransmission(internalPacket->timesSent+1);
  1756. internalPacket->nextActionTime = internalPacket->retransmissionTime+time;
  1757. #if CC_TIME_TYPE_BYTES==4
  1758. const CCTimeType threshhold = 10000;
  1759. #else
  1760. const CCTimeType threshhold = 10000000;
  1761. #endif
  1762. if (internalPacket->nextActionTime-time > threshhold)
  1763. {
  1764. // int a=5;
  1765. RakAssert(time-internalPacket->nextActionTime < threshhold);
  1766. }
  1767. //resendTree.Insert( internalPacket->reliableMessageNumber, internalPacket);
  1768. if (resendBuffer[internalPacket->reliableMessageNumber & (uint32_t) RESEND_BUFFER_ARRAY_MASK]!=0)
  1769. {
  1770. // bool overflow = ResendBufferOverflow();
  1771. RakAssert(0);
  1772. }
  1773. resendBuffer[internalPacket->reliableMessageNumber & (uint32_t) RESEND_BUFFER_ARRAY_MASK] = internalPacket;
  1774. statistics.messagesInResendBuffer++;
  1775. statistics.bytesInResendBuffer+=BITS_TO_BYTES(internalPacket->dataBitLength);
  1776. // printf("pre:%i ", unacknowledgedBytes);
  1777. InsertPacketIntoResendList( internalPacket, time, true, isReliable);
  1778. // printf("post:%i ", unacknowledgedBytes);
  1779. sendReliableMessageNumberIndex++;
  1780. }
  1781. else if (internalPacket->reliability == UNRELIABLE_WITH_ACK_RECEIPT)
  1782. {
  1783. unreliableWithAckReceiptHistory.Push(UnreliableWithAckReceiptNode(
  1784. congestionManager.GetNextDatagramSequenceNumber() + packetsToSendThisUpdateDatagramBoundaries.Size(),
  1785. internalPacket->sendReceiptSerial,
  1786. congestionManager.GetRTOForRetransmission(internalPacket->timesSent+1)+time
  1787. ), _FILE_AND_LINE_);
  1788. }
  1789. // If isReliable is false, the packet and its contents will be added to a list to be freed in ClearPacketsAndDatagrams
  1790. // However, the internalPacket structure will remain allocated and be in the resendBuffer list if it requires a receipt
  1791. bpsMetrics[(int) USER_MESSAGE_BYTES_SENT].Push1(time,BITS_TO_BYTES(internalPacket->dataBitLength));
  1792. // Testing1
  1793. // if (internalPacket->reliability==RELIABLE_ORDERED || internalPacket->reliability==RELIABLE_ORDERED_WITH_ACK_RECEIPT)
  1794. // printf("SEND reliableMessageNumber %i in datagram %i\n", internalPacket->reliableMessageNumber.val, congestionManager.GetNextDatagramSequenceNumber().val);
  1795. PushPacket(time,internalPacket, isReliable);
  1796. internalPacket->timesSent++;
  1797. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  1798. {
  1799. #if CC_TIME_TYPE_BYTES==4
  1800. messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, packetsToSendThisUpdateDatagramBoundaries.Size()+congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (RakNet::TimeMS)time, true);
  1801. #else
  1802. messageHandlerList[messageHandlerIndex]->OnInternalPacket(internalPacket, packetsToSendThisUpdateDatagramBoundaries.Size()+congestionManager.GetNextDatagramSequenceNumber(), systemAddress, (RakNet::TimeMS)(time/(CCTimeType)1000), true);
  1803. #endif
  1804. }
  1805. pushedAnything=true;
  1806. if (ResendBufferOverflow())
  1807. break;
  1808. }
  1809. // if (ResendBufferOverflow())
  1810. // break;
  1811. // }z
  1812. // No datagrams pushed?
  1813. if (datagramSizeSoFar==0)
  1814. break;
  1815. // Filled one datagram.
  1816. // If the 2nd and it's time to send a datagram pair, will be marked as a pair
  1817. PushDatagram();
  1818. }
  1819. }
  1820. for (unsigned int datagramIndex=0; datagramIndex < packetsToSendThisUpdateDatagramBoundaries.Size(); datagramIndex++)
  1821. {
  1822. if (datagramIndex>0)
  1823. dhf.isContinuousSend=true;
  1824. MessageNumberNode* messageNumberNode = 0;
  1825. dhf.datagramNumber=congestionManager.GetAndIncrementNextDatagramSequenceNumber();
  1826. dhf.isPacketPair=datagramsToSendThisUpdateIsPair[datagramIndex];
  1827. //printf("%p pushing datagram %i\n", this, dhf.datagramNumber.val);
  1828. bool isSecondOfPacketPair=dhf.isPacketPair && datagramIndex>0 && datagramsToSendThisUpdateIsPair[datagramIndex-1];
  1829. unsigned int msgIndex, msgTerm;
  1830. if (datagramIndex==0)
  1831. {
  1832. msgIndex=0;
  1833. msgTerm=packetsToSendThisUpdateDatagramBoundaries[0];
  1834. }
  1835. else
  1836. {
  1837. msgIndex=packetsToSendThisUpdateDatagramBoundaries[datagramIndex-1];
  1838. msgTerm=packetsToSendThisUpdateDatagramBoundaries[datagramIndex];
  1839. }
  1840. // More accurate time to reset here
  1841. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  1842. dhf.sourceSystemTime=RakNet::GetTimeUS();
  1843. #endif
  1844. updateBitStream.Reset();
  1845. dhf.Serialize(&updateBitStream);
  1846. CC_DEBUG_PRINTF_2("S%i ",dhf.datagramNumber.val);
  1847. while (msgIndex < msgTerm)
  1848. {
  1849. // If reliable or needs receipt
  1850. if ( packetsToSendThisUpdate[msgIndex]->reliability != UNRELIABLE &&
  1851. packetsToSendThisUpdate[msgIndex]->reliability != UNRELIABLE_SEQUENCED
  1852. )
  1853. {
  1854. if (messageNumberNode==0)
  1855. {
  1856. messageNumberNode = AddFirstToDatagramHistory(dhf.datagramNumber, packetsToSendThisUpdate[msgIndex]->reliableMessageNumber, time);
  1857. }
  1858. else
  1859. {
  1860. messageNumberNode = AddSubsequentToDatagramHistory(messageNumberNode, packetsToSendThisUpdate[msgIndex]->reliableMessageNumber);
  1861. }
  1862. }
  1863. RakAssert(updateBitStream.GetNumberOfBytesUsed()<=MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE);
  1864. WriteToBitStreamFromInternalPacket( &updateBitStream, packetsToSendThisUpdate[msgIndex], time );
  1865. RakAssert(updateBitStream.GetNumberOfBytesUsed()<=MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE);
  1866. msgIndex++;
  1867. }
  1868. if (isSecondOfPacketPair)
  1869. {
  1870. // Pad to size of first datagram
  1871. RakAssert(updateBitStream.GetNumberOfBytesUsed()<=MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE);
  1872. updateBitStream.PadWithZeroToByteLength(datagramSizesInBytes[datagramIndex-1]);
  1873. RakAssert(updateBitStream.GetNumberOfBytesUsed()<=MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE);
  1874. }
  1875. if (messageNumberNode==0)
  1876. {
  1877. // Unreliable, add dummy node
  1878. AddFirstToDatagramHistory(dhf.datagramNumber, time);
  1879. }
  1880. // Store what message ids were sent with this datagram
  1881. // datagramMessageIDTree.Insert(dhf.datagramNumber,idList);
  1882. congestionManager.OnSendBytes(time,UDP_HEADER_SIZE+DatagramHeaderFormat::GetDataHeaderByteLength());
  1883. SendBitStream( s, systemAddress, &updateBitStream, rnr, time );
  1884. bandwidthExceededStatistic=outgoingPacketBuffer.Size()>0;
  1885. // bandwidthExceededStatistic=sendPacketSet[0].IsEmpty()==false ||
  1886. // sendPacketSet[1].IsEmpty()==false ||
  1887. // sendPacketSet[2].IsEmpty()==false ||
  1888. // sendPacketSet[3].IsEmpty()==false;
  1889. if (bandwidthExceededStatistic==true)
  1890. timeOfLastContinualSend=time;
  1891. else
  1892. timeOfLastContinualSend=0;
  1893. }
  1894. ClearPacketsAndDatagrams();
  1895. // Any data waiting to send after attempting to send, then bandwidth is exceeded
  1896. bandwidthExceededStatistic=outgoingPacketBuffer.Size()>0;
  1897. // bandwidthExceededStatistic=sendPacketSet[0].IsEmpty()==false ||
  1898. // sendPacketSet[1].IsEmpty()==false ||
  1899. // sendPacketSet[2].IsEmpty()==false ||
  1900. // sendPacketSet[3].IsEmpty()==false;
  1901. }
  1902. // Keep on top of deleting old unreliable split packets so they don't clog the list.
  1903. //DeleteOldUnreliableSplitPackets( time );
  1904. }
  1905. //-------------------------------------------------------------------------------------------------------
  1906. // Writes a bitstream to the socket
  1907. //-------------------------------------------------------------------------------------------------------
  1908. void ReliabilityLayer::SendBitStream( RakNetSocket2 *s, SystemAddress &systemAddress, RakNet::BitStream *bitStream, RakNetRandom *rnr, CCTimeType currentTime)
  1909. {
  1910. (void) systemAddress;
  1911. (void) rnr;
  1912. unsigned int length;
  1913. length = (unsigned int) bitStream->GetNumberOfBytesUsed();
  1914. #ifdef _DEBUG
  1915. if (packetloss > 0.0)
  1916. {
  1917. if (frandomMT() < packetloss)
  1918. return;
  1919. }
  1920. if (minExtraPing > 0 || extraPingVariance > 0)
  1921. {
  1922. #ifdef FLIP_SEND_ORDER_TEST
  1923. // Flip order of sends without delaying them for testing
  1924. DataAndTime *dat = RakNet::OP_NEW<DataAndTime>(__FILE__,__LINE__);
  1925. memcpy(dat->data, ( char* ) bitStream->GetData(), length );
  1926. dat->s=s;
  1927. dat->length=length;
  1928. dat->sendTime = 0;
  1929. dat->remotePortRakNetWasStartedOn_PS3=remotePortRakNetWasStartedOn_PS3;
  1930. dat->extraSocketOptions=extraSocketOptions;
  1931. delayList.PushAtHead(dat, 0, _FILE_AND_LINE_);
  1932. #else
  1933. RakNet::TimeMS delay = minExtraPing;
  1934. if (extraPingVariance>0)
  1935. delay += (randomMT() % extraPingVariance);
  1936. if (delay > 0)
  1937. {
  1938. DataAndTime *dat = RakNet::OP_NEW<DataAndTime>(__FILE__,__LINE__);
  1939. memcpy(dat->data, ( char* ) bitStream->GetData(), length );
  1940. dat->s=s;
  1941. dat->length=length;
  1942. dat->sendTime = RakNet::GetTimeMS() + delay;
  1943. for (unsigned int i=0; i < delayList.Size(); i++)
  1944. {
  1945. if (dat->sendTime < delayList[i]->sendTime)
  1946. {
  1947. delayList.PushAtHead(dat, i, __FILE__, __LINE__);
  1948. dat=0;
  1949. break;
  1950. }
  1951. }
  1952. if (dat!=0)
  1953. delayList.Push(dat,__FILE__,__LINE__);
  1954. return;
  1955. }
  1956. #endif
  1957. }
  1958. #endif
  1959. #if LIBCAT_SECURITY==1
  1960. if (useSecurity)
  1961. {
  1962. unsigned char *buffer = reinterpret_cast<unsigned char*>( bitStream->GetData() );
  1963. int buffer_size = bitStream->GetNumberOfBitsAllocated() / 8;
  1964. // Verify there is enough room for encrypted output and encrypt
  1965. // Encrypt() will increase length
  1966. bool success = auth_enc.Encrypt(buffer, buffer_size, length);
  1967. RakAssert(success);
  1968. }
  1969. #endif
  1970. bpsMetrics[(int) ACTUAL_BYTES_SENT].Push1(currentTime,length);
  1971. RakAssert(length <= congestionManager.GetMTU());
  1972. #ifdef USE_THREADED_SEND
  1973. SendToThread::SendToThreadBlock *block = SendToThread::AllocateBlock();
  1974. memcpy(block->data, bitStream->GetData(), length);
  1975. block->dataWriteOffset=length;
  1976. block->extraSocketOptions=extraSocketOptions;
  1977. block->remotePortRakNetWasStartedOn_PS3=remotePortRakNetWasStartedOn_PS3;
  1978. block->s=s;
  1979. block->systemAddress=systemAddress;
  1980. SendToThread::ProcessBlock(block);
  1981. #else
  1982. // SocketLayer::SendTo( s, ( char* ) bitStream->GetData(), length, systemAddress, __FILE__, __LINE__ );
  1983. RNS2_SendParameters bsp;
  1984. bsp.data = (char*) bitStream->GetData();
  1985. bsp.length = length;
  1986. bsp.systemAddress = systemAddress;
  1987. s->Send(&bsp, _FILE_AND_LINE_);
  1988. #endif
  1989. }
  1990. //-------------------------------------------------------------------------------------------------------
  1991. // Are we waiting for any data to be sent out or be processed by the player?
  1992. //-------------------------------------------------------------------------------------------------------
  1993. bool ReliabilityLayer::IsOutgoingDataWaiting(void)
  1994. {
  1995. if (outgoingPacketBuffer.Size()>0)
  1996. return true;
  1997. // unsigned i;
  1998. // for ( i = 0; i < NUMBER_OF_PRIORITIES; i++ )
  1999. // {
  2000. // if (sendPacketSet[ i ].Size() > 0)
  2001. // return true;
  2002. // }
  2003. return
  2004. //acknowlegements.Size() > 0 ||
  2005. //resendTree.IsEmpty()==false;// || outputQueue.Size() > 0 || orderingList.Size() > 0 || splitPacketChannelList.Size() > 0;
  2006. statistics.messagesInResendBuffer!=0;
  2007. }
  2008. bool ReliabilityLayer::AreAcksWaiting(void)
  2009. {
  2010. return acknowlegements.Size() > 0;
  2011. }
  2012. //-------------------------------------------------------------------------------------------------------
  2013. void ReliabilityLayer::ApplyNetworkSimulator( double _packetloss, RakNet::TimeMS _minExtraPing, RakNet::TimeMS _extraPingVariance )
  2014. {
  2015. #ifdef _DEBUG
  2016. packetloss=_packetloss;
  2017. minExtraPing=_minExtraPing;
  2018. extraPingVariance=_extraPingVariance;
  2019. // if (ping < (unsigned int)(minExtraPing+extraPingVariance)*2)
  2020. // ping=(minExtraPing+extraPingVariance)*2;
  2021. #endif
  2022. }
  2023. //-------------------------------------------------------------------------------------------------------
  2024. void ReliabilityLayer::SetSplitMessageProgressInterval(int interval)
  2025. {
  2026. splitMessageProgressInterval=interval;
  2027. }
  2028. //-------------------------------------------------------------------------------------------------------
  2029. void ReliabilityLayer::SetUnreliableTimeout(RakNet::TimeMS timeoutMS)
  2030. {
  2031. #if CC_TIME_TYPE_BYTES==4
  2032. unreliableTimeout=timeoutMS;
  2033. #else
  2034. unreliableTimeout=(CCTimeType)timeoutMS*(CCTimeType)1000;
  2035. #endif
  2036. }
  2037. //-------------------------------------------------------------------------------------------------------
  2038. // This will return true if we should not send at this time
  2039. //-------------------------------------------------------------------------------------------------------
  2040. bool ReliabilityLayer::IsSendThrottled( int MTUSize )
  2041. {
  2042. (void) MTUSize;
  2043. return false;
  2044. // return resendList.Size() > windowSize;
  2045. // Disabling this, because it can get stuck here forever
  2046. /*
  2047. unsigned packetsWaiting;
  2048. unsigned resendListDataSize=0;
  2049. unsigned i;
  2050. for (i=0; i < resendList.Size(); i++)
  2051. {
  2052. if (resendList[i])
  2053. resendListDataSize+=resendList[i]->dataBitLength;
  2054. }
  2055. packetsWaiting = 1 + ((BITS_TO_BYTES(resendListDataSize)) / (MTUSize - UDP_HEADER_SIZE - 10)); // 10 to roughly estimate the raknet header
  2056. return packetsWaiting >= windowSize;
  2057. */
  2058. }
  2059. //-------------------------------------------------------------------------------------------------------
  2060. // We lost a packet
  2061. //-------------------------------------------------------------------------------------------------------
  2062. void ReliabilityLayer::UpdateWindowFromPacketloss( CCTimeType time )
  2063. {
  2064. (void) time;
  2065. }
  2066. //-------------------------------------------------------------------------------------------------------
  2067. // Increase the window size
  2068. //-------------------------------------------------------------------------------------------------------
  2069. void ReliabilityLayer::UpdateWindowFromAck( CCTimeType time )
  2070. {
  2071. (void) time;
  2072. }
  2073. //-------------------------------------------------------------------------------------------------------
  2074. // Does what the function name says
  2075. //-------------------------------------------------------------------------------------------------------
  2076. unsigned ReliabilityLayer::RemovePacketFromResendListAndDeleteOlderReliableSequenced( const MessageNumberType messageNumber, CCTimeType time, DataStructures::List<PluginInterface2*> &messageHandlerList, const SystemAddress &systemAddress )
  2077. {
  2078. (void) time;
  2079. (void) messageNumber;
  2080. InternalPacket * internalPacket;
  2081. //InternalPacket *temp;
  2082. // PacketReliability reliability; // What type of reliability algorithm to use with this packet
  2083. // unsigned char orderingChannel; // What ordering channel this packet is on, if the reliability type uses ordering channels
  2084. // OrderingIndexType orderingIndex; // The ID used as identification for ordering channels
  2085. // unsigned j;
  2086. for (unsigned int messageHandlerIndex=0; messageHandlerIndex < messageHandlerList.Size(); messageHandlerIndex++)
  2087. {
  2088. #if CC_TIME_TYPE_BYTES==4
  2089. messageHandlerList[messageHandlerIndex]->OnAck(messageNumber, systemAddress, time);
  2090. #else
  2091. messageHandlerList[messageHandlerIndex]->OnAck(messageNumber, systemAddress, (RakNet::TimeMS)(time/(CCTimeType)1000));
  2092. #endif
  2093. }
  2094. // Testing1
  2095. // if (resendLinkedListHead)
  2096. // {
  2097. // InternalPacket *internalPacket = resendLinkedListHead;
  2098. // do
  2099. // {
  2100. // internalPacket=internalPacket->resendNext;
  2101. // printf("%i ", internalPacket->reliableMessageNumber.val);
  2102. // } while (internalPacket!=resendLinkedListHead);
  2103. // printf("\n");
  2104. // }
  2105. // bool deleted;
  2106. // deleted=resendTree.Delete(messageNumber, internalPacket);
  2107. internalPacket = resendBuffer[messageNumber & RESEND_BUFFER_ARRAY_MASK];
  2108. // May ask to remove twice, for example resend twice, then second ack
  2109. if (internalPacket && internalPacket->reliableMessageNumber==messageNumber)
  2110. {
  2111. // ValidateResendList();
  2112. resendBuffer[messageNumber & RESEND_BUFFER_ARRAY_MASK]=0;
  2113. CC_DEBUG_PRINTF_2("AckRcv %i ", messageNumber);
  2114. statistics.messagesInResendBuffer--;
  2115. statistics.bytesInResendBuffer-=BITS_TO_BYTES(internalPacket->dataBitLength);
  2116. // orderingIndex = internalPacket->orderingIndex;
  2117. totalUserDataBytesAcked+=(double) BITS_TO_BYTES(internalPacket->headerLength+internalPacket->dataBitLength);
  2118. // Return receipt if asked for
  2119. if (internalPacket->reliability>=RELIABLE_WITH_ACK_RECEIPT &&
  2120. (internalPacket->splitPacketCount==0 || internalPacket->splitPacketIndex+1==internalPacket->splitPacketCount)
  2121. )
  2122. {
  2123. InternalPacket *ackReceipt = AllocateFromInternalPacketPool();
  2124. AllocInternalPacketData(ackReceipt, 5, false, _FILE_AND_LINE_ );
  2125. ackReceipt->dataBitLength=BYTES_TO_BITS(5);
  2126. ackReceipt->data[0]=(MessageID)ID_SND_RECEIPT_ACKED;
  2127. memcpy(ackReceipt->data+sizeof(MessageID), &internalPacket->sendReceiptSerial, sizeof(internalPacket->sendReceiptSerial));
  2128. outputQueue.Push(ackReceipt, _FILE_AND_LINE_ );
  2129. }
  2130. bool isReliable;
  2131. if ( internalPacket->reliability == RELIABLE ||
  2132. internalPacket->reliability == RELIABLE_SEQUENCED ||
  2133. internalPacket->reliability == RELIABLE_ORDERED ||
  2134. internalPacket->reliability == RELIABLE_WITH_ACK_RECEIPT ||
  2135. // internalPacket->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT ||
  2136. internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT
  2137. )
  2138. isReliable = true;
  2139. else
  2140. isReliable = false;
  2141. RemoveFromList(internalPacket, isReliable);
  2142. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  2143. ReleaseToInternalPacketPool( internalPacket );
  2144. return 0;
  2145. }
  2146. else
  2147. {
  2148. }
  2149. return (unsigned)-1;
  2150. }
  2151. //-------------------------------------------------------------------------------------------------------
  2152. // Acknowledge receipt of the packet with the specified messageNumber
  2153. //-------------------------------------------------------------------------------------------------------
  2154. void ReliabilityLayer::SendAcknowledgementPacket( const DatagramSequenceNumberType messageNumber, CCTimeType time )
  2155. {
  2156. // REMOVEME
  2157. // printf("%p Send ack %i\n", this, messageNumber.val);
  2158. nextAckTimeToSend=time;
  2159. acknowlegements.Insert(messageNumber);
  2160. //printf("ACK_DG:%i ", messageNumber.val);
  2161. CC_DEBUG_PRINTF_2("AckPush %i ", messageNumber);
  2162. }
  2163. //-------------------------------------------------------------------------------------------------------
  2164. // Parse an internalPacket and figure out how many header bits would be
  2165. // written. Returns that number
  2166. //-------------------------------------------------------------------------------------------------------
  2167. BitSize_t ReliabilityLayer::GetMaxMessageHeaderLengthBits( void )
  2168. {
  2169. InternalPacket ip;
  2170. ip.reliability=RELIABLE_SEQUENCED;
  2171. ip.splitPacketCount=1;
  2172. return GetMessageHeaderLengthBits(&ip);
  2173. }
  2174. //-------------------------------------------------------------------------------------------------------
  2175. BitSize_t ReliabilityLayer::GetMessageHeaderLengthBits( const InternalPacket *const internalPacket )
  2176. {
  2177. BitSize_t bitLength;
  2178. // bitStream->AlignWriteToByteBoundary(); // Potentially unaligned
  2179. // tempChar=(unsigned char)internalPacket->reliability; bitStream->WriteBits( (const unsigned char *)&tempChar, 3, true ); // 3 bits to write reliability.
  2180. // bool hasSplitPacket = internalPacket->splitPacketCount>0; bitStream->Write(hasSplitPacket); // Write 1 bit to indicate if splitPacketCount>0
  2181. bitLength = 8*1;
  2182. // bitStream->AlignWriteToByteBoundary();
  2183. // RakAssert(internalPacket->dataBitLength < 65535);
  2184. // unsigned short s; s = (unsigned short) internalPacket->dataBitLength; bitStream->WriteAlignedVar16((const char*)& s);
  2185. bitLength += 8*2;
  2186. if ( internalPacket->reliability == RELIABLE ||
  2187. internalPacket->reliability == RELIABLE_SEQUENCED ||
  2188. internalPacket->reliability == RELIABLE_ORDERED ||
  2189. internalPacket->reliability == RELIABLE_WITH_ACK_RECEIPT ||
  2190. // internalPacket->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT ||
  2191. internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT
  2192. )
  2193. bitLength += 8*3; // bitStream->Write(internalPacket->reliableMessageNumber); // Message sequence number
  2194. // bitStream->AlignWriteToByteBoundary(); // Potentially nothing else to write
  2195. if ( internalPacket->reliability == UNRELIABLE_SEQUENCED ||
  2196. internalPacket->reliability == RELIABLE_SEQUENCED
  2197. )
  2198. {
  2199. bitLength += 8*3;; // bitStream->Write(internalPacket->_sequencingIndex); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED.
  2200. }
  2201. if ( internalPacket->reliability == UNRELIABLE_SEQUENCED ||
  2202. internalPacket->reliability == RELIABLE_SEQUENCED ||
  2203. internalPacket->reliability == RELIABLE_ORDERED ||
  2204. internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT
  2205. )
  2206. {
  2207. bitLength += 8*3; // bitStream->Write(internalPacket->orderingIndex); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED.
  2208. bitLength += 8*1; // tempChar=internalPacket->orderingChannel; bitStream->WriteAlignedVar8((const char*)& tempChar); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED. 5 bits needed, write one byte
  2209. }
  2210. if (internalPacket->splitPacketCount>0)
  2211. {
  2212. bitLength += 8*4; // bitStream->WriteAlignedVar32((const char*)& internalPacket->splitPacketCount); RakAssert(sizeof(SplitPacketIndexType)==4); // Only needed if splitPacketCount>0. 4 bytes
  2213. bitLength += 8*sizeof(SplitPacketIdType); // bitStream->WriteAlignedVar16((const char*)& internalPacket->splitPacketId); RakAssert(sizeof(SplitPacketIdType)==2); // Only needed if splitPacketCount>0.
  2214. bitLength += 8*4; // bitStream->WriteAlignedVar32((const char*)& internalPacket->splitPacketIndex); // Only needed if splitPacketCount>0. 4 bytes
  2215. }
  2216. return bitLength;
  2217. }
  2218. //-------------------------------------------------------------------------------------------------------
  2219. // Parse an internalPacket and create a bitstream to represent this data
  2220. //-------------------------------------------------------------------------------------------------------
  2221. BitSize_t ReliabilityLayer::WriteToBitStreamFromInternalPacket( RakNet::BitStream *bitStream, const InternalPacket *const internalPacket, CCTimeType curTime )
  2222. {
  2223. (void) curTime;
  2224. BitSize_t start = bitStream->GetNumberOfBitsUsed();
  2225. unsigned char tempChar;
  2226. // (Incoming data may be all zeros due to padding)
  2227. bitStream->AlignWriteToByteBoundary(); // Potentially unaligned
  2228. if (internalPacket->reliability==UNRELIABLE_WITH_ACK_RECEIPT)
  2229. tempChar=UNRELIABLE;
  2230. else if (internalPacket->reliability==RELIABLE_WITH_ACK_RECEIPT)
  2231. tempChar=RELIABLE;
  2232. else if (internalPacket->reliability==RELIABLE_ORDERED_WITH_ACK_RECEIPT)
  2233. tempChar=RELIABLE_ORDERED;
  2234. else
  2235. tempChar=(unsigned char)internalPacket->reliability;
  2236. bitStream->WriteBits( (const unsigned char *)&tempChar, 3, true ); // 3 bits to write reliability.
  2237. bool hasSplitPacket = internalPacket->splitPacketCount>0; bitStream->Write(hasSplitPacket); // Write 1 bit to indicate if splitPacketCount>0
  2238. bitStream->AlignWriteToByteBoundary();
  2239. RakAssert(internalPacket->dataBitLength < 65535);
  2240. unsigned short s; s = (unsigned short) internalPacket->dataBitLength; bitStream->WriteAlignedVar16((const char*)& s);
  2241. if ( internalPacket->reliability == RELIABLE ||
  2242. internalPacket->reliability == RELIABLE_SEQUENCED ||
  2243. internalPacket->reliability == RELIABLE_ORDERED ||
  2244. internalPacket->reliability == RELIABLE_WITH_ACK_RECEIPT ||
  2245. internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT
  2246. )
  2247. bitStream->Write(internalPacket->reliableMessageNumber); // Used for all reliable types
  2248. bitStream->AlignWriteToByteBoundary(); // Potentially nothing else to write
  2249. if ( internalPacket->reliability == UNRELIABLE_SEQUENCED ||
  2250. internalPacket->reliability == RELIABLE_SEQUENCED
  2251. )
  2252. {
  2253. bitStream->Write(internalPacket->sequencingIndex); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED.
  2254. }
  2255. if ( internalPacket->reliability == UNRELIABLE_SEQUENCED ||
  2256. internalPacket->reliability == RELIABLE_SEQUENCED ||
  2257. internalPacket->reliability == RELIABLE_ORDERED ||
  2258. internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT
  2259. )
  2260. {
  2261. bitStream->Write(internalPacket->orderingIndex); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED.
  2262. tempChar=internalPacket->orderingChannel; bitStream->WriteAlignedVar8((const char*)& tempChar); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED. 5 bits needed, write one byte
  2263. }
  2264. if (internalPacket->splitPacketCount>0)
  2265. {
  2266. // printf("Write before\n");
  2267. // bitStream->PrintBits();
  2268. bitStream->WriteAlignedVar32((const char*)& internalPacket->splitPacketCount); RakAssert(sizeof(SplitPacketIndexType)==4); // Only needed if splitPacketCount>0. 4 bytes
  2269. bitStream->WriteAlignedVar16((const char*)& internalPacket->splitPacketId); RakAssert(sizeof(SplitPacketIdType)==2); // Only needed if splitPacketCount>0.
  2270. bitStream->WriteAlignedVar32((const char*)& internalPacket->splitPacketIndex); // Only needed if splitPacketCount>0. 4 bytes
  2271. // printf("Write after\n");
  2272. // bitStream->PrintBits();
  2273. }
  2274. // Write the actual data.
  2275. bitStream->WriteAlignedBytes( ( unsigned char* ) internalPacket->data, BITS_TO_BYTES( internalPacket->dataBitLength ) );
  2276. return bitStream->GetNumberOfBitsUsed() - start;
  2277. }
  2278. //-------------------------------------------------------------------------------------------------------
  2279. // Parse a bitstream and create an internal packet to represent this data
  2280. //-------------------------------------------------------------------------------------------------------
  2281. InternalPacket* ReliabilityLayer::CreateInternalPacketFromBitStream( RakNet::BitStream *bitStream, CCTimeType time )
  2282. {
  2283. bool bitStreamSucceeded;
  2284. InternalPacket* internalPacket;
  2285. unsigned char tempChar;
  2286. bool hasSplitPacket=false;
  2287. bool readSuccess;
  2288. if ( bitStream->GetNumberOfUnreadBits() < (int) sizeof( internalPacket->reliableMessageNumber ) * 8 )
  2289. return 0; // leftover bits
  2290. internalPacket = AllocateFromInternalPacketPool();
  2291. if (internalPacket==0)
  2292. {
  2293. // Out of memory
  2294. RakAssert(0);
  2295. return 0;
  2296. }
  2297. internalPacket->creationTime = time;
  2298. // (Incoming data may be all zeros due to padding)
  2299. bitStream->AlignReadToByteBoundary(); // Potentially unaligned
  2300. bitStream->ReadBits( ( unsigned char* ) ( &( tempChar ) ), 3 );
  2301. internalPacket->reliability = ( const PacketReliability ) tempChar;
  2302. readSuccess=bitStream->Read(hasSplitPacket); // Read 1 bit to indicate if splitPacketCount>0
  2303. bitStream->AlignReadToByteBoundary();
  2304. unsigned short s; bitStream->ReadAlignedVar16((char*)&s); internalPacket->dataBitLength=s; // Length of message (2 bytes)
  2305. if ( internalPacket->reliability == RELIABLE ||
  2306. internalPacket->reliability == RELIABLE_SEQUENCED ||
  2307. internalPacket->reliability == RELIABLE_ORDERED
  2308. // I don't write ACK_RECEIPT to the remote system
  2309. // ||
  2310. // internalPacket->reliability == RELIABLE_WITH_ACK_RECEIPT ||
  2311. // internalPacket->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT ||
  2312. // internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT
  2313. )
  2314. bitStream->Read(internalPacket->reliableMessageNumber); // Message sequence number
  2315. else
  2316. internalPacket->reliableMessageNumber=(MessageNumberType)(const uint32_t)-1;
  2317. bitStream->AlignReadToByteBoundary(); // Potentially nothing else to Read
  2318. if ( internalPacket->reliability == UNRELIABLE_SEQUENCED ||
  2319. internalPacket->reliability == RELIABLE_SEQUENCED
  2320. )
  2321. {
  2322. bitStream->Read(internalPacket->sequencingIndex); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED.
  2323. }
  2324. if ( internalPacket->reliability == UNRELIABLE_SEQUENCED ||
  2325. internalPacket->reliability == RELIABLE_SEQUENCED ||
  2326. internalPacket->reliability == RELIABLE_ORDERED ||
  2327. internalPacket->reliability == RELIABLE_ORDERED_WITH_ACK_RECEIPT
  2328. )
  2329. {
  2330. bitStream->Read(internalPacket->orderingIndex); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED. 4 bytes.
  2331. readSuccess=bitStream->ReadAlignedVar8((char*)& internalPacket->orderingChannel); // Used for UNRELIABLE_SEQUENCED, RELIABLE_SEQUENCED, RELIABLE_ORDERED. 5 bits needed, Read one byte
  2332. }
  2333. else
  2334. internalPacket->orderingChannel=0;
  2335. if (hasSplitPacket)
  2336. {
  2337. // printf("Read before\n");
  2338. // bitStream->PrintBits();
  2339. bitStream->ReadAlignedVar32((char*)& internalPacket->splitPacketCount); // Only needed if splitPacketCount>0. 4 bytes
  2340. bitStream->ReadAlignedVar16((char*)& internalPacket->splitPacketId); // Only needed if splitPacketCount>0.
  2341. readSuccess=bitStream->ReadAlignedVar32((char*)& internalPacket->splitPacketIndex); // Only needed if splitPacketCount>0. 4 bytes
  2342. RakAssert(readSuccess);
  2343. // printf("Read after\n");
  2344. // bitStream->PrintBits();
  2345. }
  2346. else
  2347. {
  2348. internalPacket->splitPacketCount=0;
  2349. }
  2350. if (readSuccess==false ||
  2351. internalPacket->dataBitLength==0 ||
  2352. internalPacket->reliability>=NUMBER_OF_RELIABILITIES ||
  2353. internalPacket->orderingChannel>=32 ||
  2354. (hasSplitPacket && (internalPacket->splitPacketIndex >= internalPacket->splitPacketCount)))
  2355. {
  2356. // If this assert hits, encoding is garbage
  2357. RakAssert("Encoding is garbage" && 0);
  2358. ReleaseToInternalPacketPool( internalPacket );
  2359. return 0;
  2360. }
  2361. // Allocate memory to hold our data
  2362. AllocInternalPacketData(internalPacket, BITS_TO_BYTES( internalPacket->dataBitLength ), false, _FILE_AND_LINE_ );
  2363. RakAssert(BITS_TO_BYTES( internalPacket->dataBitLength )<MAXIMUM_MTU_SIZE);
  2364. if (internalPacket->data == 0)
  2365. {
  2366. RakAssert("Out of memory in ReliabilityLayer::CreateInternalPacketFromBitStream" && 0);
  2367. notifyOutOfMemory(_FILE_AND_LINE_);
  2368. ReleaseToInternalPacketPool( internalPacket );
  2369. return 0;
  2370. }
  2371. // Set the last byte to 0 so if ReadBits does not read a multiple of 8 the last bits are 0'ed out
  2372. internalPacket->data[ BITS_TO_BYTES( internalPacket->dataBitLength ) - 1 ] = 0;
  2373. // Read the data the packet holds
  2374. bitStreamSucceeded = bitStream->ReadAlignedBytes( ( unsigned char* ) internalPacket->data, BITS_TO_BYTES( internalPacket->dataBitLength ) );
  2375. if ( bitStreamSucceeded == false )
  2376. {
  2377. // If this hits, most likely the variable buff is too small in RunUpdateCycle in RakPeer.cpp
  2378. RakAssert("Couldn't read all the data" && 0);
  2379. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  2380. ReleaseToInternalPacketPool( internalPacket );
  2381. return 0;
  2382. }
  2383. return internalPacket;
  2384. }
  2385. //-------------------------------------------------------------------------------------------------------
  2386. // Get the SHA1 code
  2387. //-------------------------------------------------------------------------------------------------------
  2388. void ReliabilityLayer::GetSHA1( unsigned char * const buffer, unsigned int
  2389. nbytes, char code[ SHA1_LENGTH ] )
  2390. {
  2391. CSHA1 sha1;
  2392. sha1.Reset();
  2393. sha1.Update( ( unsigned char* ) buffer, nbytes );
  2394. sha1.Final();
  2395. memcpy( code, sha1.GetHash(), SHA1_LENGTH );
  2396. }
  2397. //-------------------------------------------------------------------------------------------------------
  2398. // Check the SHA1 code
  2399. //-------------------------------------------------------------------------------------------------------
  2400. bool ReliabilityLayer::CheckSHA1( char code[ SHA1_LENGTH ], unsigned char *
  2401. const buffer, unsigned int nbytes )
  2402. {
  2403. char code2[ SHA1_LENGTH ];
  2404. GetSHA1( buffer, nbytes, code2 );
  2405. for ( int i = 0; i < SHA1_LENGTH; i++ )
  2406. if ( code[ i ] != code2[ i ] )
  2407. return false;
  2408. return true;
  2409. }
  2410. /*
  2411. //-------------------------------------------------------------------------------------------------------
  2412. // Search the specified list for sequenced packets on the specified ordering
  2413. // stream, optionally skipping those with splitPacketId, and delete them
  2414. //-------------------------------------------------------------------------------------------------------
  2415. void ReliabilityLayer::DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::List<InternalPacket*>&theList, int splitPacketId )
  2416. {
  2417. unsigned i = 0;
  2418. while ( i < theList.Size() )
  2419. {
  2420. if ( (
  2421. theList[ i ]->reliability == RELIABLE_SEQUENCED ||
  2422. theList[ i ]->reliability == UNRELIABLE_SEQUENCED
  2423. // ||
  2424. // theList[ i ]->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT ||
  2425. // theList[ i ]->reliability == UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT
  2426. ) &&
  2427. theList[ i ]->orderingChannel == orderingChannel && ( splitPacketId == -1 || theList[ i ]->splitPacketId != (unsigned int) splitPacketId ) )
  2428. {
  2429. InternalPacket * internalPacket = theList[ i ];
  2430. theList.RemoveAtIndex( i );
  2431. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  2432. ReleaseToInternalPacketPool( internalPacket );
  2433. }
  2434. else
  2435. i++;
  2436. }
  2437. }
  2438. //-------------------------------------------------------------------------------------------------------
  2439. // Search the specified list for sequenced packets with a value less than orderingIndex and delete them
  2440. // Note - I added functionality so you can use the Queue as a list (in this case for searching) but it is less efficient to do so than a regular list
  2441. //-------------------------------------------------------------------------------------------------------
  2442. void ReliabilityLayer::DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::Queue<InternalPacket*>&theList )
  2443. {
  2444. InternalPacket * internalPacket;
  2445. int listSize = theList.Size();
  2446. int i = 0;
  2447. while ( i < listSize )
  2448. {
  2449. if ( (
  2450. theList[ i ]->reliability == RELIABLE_SEQUENCED ||
  2451. theList[ i ]->reliability == UNRELIABLE_SEQUENCED
  2452. // ||
  2453. // theList[ i ]->reliability == RELIABLE_SEQUENCED_WITH_ACK_RECEIPT ||
  2454. // theList[ i ]->reliability == UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT
  2455. ) && theList[ i ]->orderingChannel == orderingChannel )
  2456. {
  2457. internalPacket = theList[ i ];
  2458. theList.RemoveAtIndex( i );
  2459. FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  2460. ReleaseToInternalPacketPool( internalPacket );
  2461. listSize--;
  2462. }
  2463. else
  2464. i++;
  2465. }
  2466. }
  2467. */
  2468. //-------------------------------------------------------------------------------------------------------
  2469. // Returns true if newPacketOrderingIndex is older than the waitingForPacketOrderingIndex
  2470. //-------------------------------------------------------------------------------------------------------
  2471. bool ReliabilityLayer::IsOlderOrderedPacket( OrderingIndexType newPacketOrderingIndex, OrderingIndexType waitingForPacketOrderingIndex )
  2472. {
  2473. OrderingIndexType maxRange = (OrderingIndexType) (const uint32_t)-1;
  2474. if ( waitingForPacketOrderingIndex > maxRange/(OrderingIndexType)2 )
  2475. {
  2476. if ( newPacketOrderingIndex >= waitingForPacketOrderingIndex - maxRange/(OrderingIndexType)2+(OrderingIndexType)1 && newPacketOrderingIndex < waitingForPacketOrderingIndex )
  2477. {
  2478. return true;
  2479. }
  2480. }
  2481. else
  2482. if ( newPacketOrderingIndex >= ( OrderingIndexType ) ( waitingForPacketOrderingIndex - (( OrderingIndexType ) maxRange/(OrderingIndexType)2+(OrderingIndexType)1) ) ||
  2483. newPacketOrderingIndex < waitingForPacketOrderingIndex )
  2484. {
  2485. return true;
  2486. }
  2487. // Old packet
  2488. return false;
  2489. }
  2490. //-------------------------------------------------------------------------------------------------------
  2491. // Split the passed packet into chunks under MTU_SIZEbytes (including headers) and save those new chunks
  2492. // Optimized version
  2493. //-------------------------------------------------------------------------------------------------------
  2494. void ReliabilityLayer::SplitPacket( InternalPacket *internalPacket )
  2495. {
  2496. // Doing all sizes in bytes in this function so I don't write partial bytes with split packets
  2497. internalPacket->splitPacketCount = 1; // This causes GetMessageHeaderLengthBits to account for the split packet header
  2498. unsigned int headerLength = (unsigned int) BITS_TO_BYTES( GetMessageHeaderLengthBits( internalPacket ) );
  2499. unsigned int dataByteLength = (unsigned int) BITS_TO_BYTES( internalPacket->dataBitLength );
  2500. int maximumSendBlockBytes, byteOffset, bytesToSend;
  2501. SplitPacketIndexType splitPacketIndex;
  2502. int i;
  2503. InternalPacket **internalPacketArray;
  2504. maximumSendBlockBytes = GetMaxDatagramSizeExcludingMessageHeaderBytes() - BITS_TO_BYTES(GetMaxMessageHeaderLengthBits());
  2505. // Calculate how many packets we need to create
  2506. internalPacket->splitPacketCount = ( ( dataByteLength - 1 ) / ( maximumSendBlockBytes ) + 1 );
  2507. // Optimization
  2508. // internalPacketArray = RakNet::OP_NEW<InternalPacket*>(internalPacket->splitPacketCount, _FILE_AND_LINE_ );
  2509. bool usedAlloca=false;
  2510. #if USE_ALLOCA==1
  2511. if (sizeof( InternalPacket* ) * internalPacket->splitPacketCount < MAX_ALLOCA_STACK_ALLOCATION)
  2512. {
  2513. internalPacketArray = ( InternalPacket** ) alloca( sizeof( InternalPacket* ) * internalPacket->splitPacketCount );
  2514. usedAlloca=true;
  2515. }
  2516. else
  2517. #endif
  2518. internalPacketArray = (InternalPacket**) rakMalloc_Ex( sizeof(InternalPacket*) * internalPacket->splitPacketCount, _FILE_AND_LINE_ );
  2519. for ( i = 0; i < ( int ) internalPacket->splitPacketCount; i++ )
  2520. {
  2521. internalPacketArray[ i ] = AllocateFromInternalPacketPool();
  2522. //internalPacketArray[ i ] = (InternalPacket*) alloca( sizeof( InternalPacket ) );
  2523. // internalPacketArray[ i ] = sendPacketSet[internalPacket->priority].WriteLock();
  2524. *internalPacketArray[ i ]=*internalPacket;
  2525. internalPacketArray[ i ]->messageNumberAssigned=false;
  2526. if (i!=0)
  2527. internalPacket->messageInternalOrder = internalOrderIndex++;
  2528. }
  2529. // This identifies which packet this is in the set
  2530. splitPacketIndex = 0;
  2531. InternalPacketRefCountedData *refCounter=0;
  2532. // Do a loop to send out all the packets
  2533. do
  2534. {
  2535. byteOffset = splitPacketIndex * maximumSendBlockBytes;
  2536. bytesToSend = dataByteLength - byteOffset;
  2537. if ( bytesToSend > maximumSendBlockBytes )
  2538. bytesToSend = maximumSendBlockBytes;
  2539. // Copy over our chunk of data
  2540. AllocInternalPacketData(internalPacketArray[ splitPacketIndex ], &refCounter, internalPacket->data, internalPacket->data + byteOffset);
  2541. // internalPacketArray[ splitPacketIndex ]->data = (unsigned char*) rakMalloc_Ex( bytesToSend, _FILE_AND_LINE_ );
  2542. // memcpy( internalPacketArray[ splitPacketIndex ]->data, internalPacket->data + byteOffset, bytesToSend );
  2543. if ( bytesToSend != maximumSendBlockBytes )
  2544. internalPacketArray[ splitPacketIndex ]->dataBitLength = internalPacket->dataBitLength - splitPacketIndex * ( maximumSendBlockBytes << 3 );
  2545. else
  2546. internalPacketArray[ splitPacketIndex ]->dataBitLength = bytesToSend << 3;
  2547. internalPacketArray[ splitPacketIndex ]->splitPacketIndex = splitPacketIndex;
  2548. internalPacketArray[ splitPacketIndex ]->splitPacketId = splitPacketId;
  2549. internalPacketArray[ splitPacketIndex ]->splitPacketCount = internalPacket->splitPacketCount;
  2550. RakAssert(internalPacketArray[ splitPacketIndex ]->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  2551. } while ( ++splitPacketIndex < internalPacket->splitPacketCount );
  2552. splitPacketId++; // It's ok if this wraps to 0
  2553. // InternalPacket *workingPacket;
  2554. // Tell the heap we are going to push a list of elements where each element in the list follows the heap order
  2555. RakAssert(outgoingPacketBuffer.Size()==0 || outgoingPacketBuffer.Peek()->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  2556. outgoingPacketBuffer.StartSeries();
  2557. // Copy all the new packets into the split packet list
  2558. for ( i = 0; i < ( int ) internalPacket->splitPacketCount; i++ )
  2559. {
  2560. internalPacketArray[ i ]->headerLength=headerLength;
  2561. RakAssert(internalPacketArray[ i ]->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  2562. AddToUnreliableLinkedList(internalPacketArray[ i ]);
  2563. // sendPacketSet[ internalPacket->priority ].Push( internalPacketArray[ i ], _FILE_AND_LINE_ );
  2564. RakAssert(internalPacketArray[ i ]->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  2565. RakAssert(internalPacketArray[ i ]->messageNumberAssigned==false);
  2566. outgoingPacketBuffer.PushSeries(GetNextWeight(internalPacketArray[ i ]->priority), internalPacketArray[ i ], _FILE_AND_LINE_);
  2567. RakAssert(outgoingPacketBuffer.Size()==0 || outgoingPacketBuffer.Peek()->dataBitLength<BYTES_TO_BITS(MAXIMUM_MTU_SIZE));
  2568. statistics.messageInSendBuffer[(int)internalPacketArray[ i ]->priority]++;
  2569. statistics.bytesInSendBuffer[(int)(int)internalPacketArray[ i ]->priority]+=(double) BITS_TO_BYTES(internalPacketArray[ i ]->dataBitLength);
  2570. // workingPacket=sendPacketSet[internalPacket->priority].WriteLock();
  2571. // memcpy(workingPacket, internalPacketArray[ i ], sizeof(InternalPacket));
  2572. // sendPacketSet[internalPacket->priority].WriteUnlock();
  2573. }
  2574. // Do not delete, original is referenced by all split packets to avoid numerous allocations. See AllocInternalPacketData above
  2575. // FreeInternalPacketData(internalPacket, _FILE_AND_LINE_ );
  2576. ReleaseToInternalPacketPool( internalPacket );
  2577. if (usedAlloca==false)
  2578. rakFree_Ex(internalPacketArray, _FILE_AND_LINE_ );
  2579. }
  2580. //-------------------------------------------------------------------------------------------------------
  2581. // Insert a packet into the split packet list
  2582. //-------------------------------------------------------------------------------------------------------
  2583. void ReliabilityLayer::InsertIntoSplitPacketList( InternalPacket * internalPacket, CCTimeType time )
  2584. {
  2585. bool objectExists;
  2586. unsigned index;
  2587. // Find in splitPacketChannelList if a SplitPacketChannel with this splitPacketId was already allocated. If not, allocate and insert the channel into the list.
  2588. index=splitPacketChannelList.GetIndexFromKey(internalPacket->splitPacketId, &objectExists);
  2589. if (objectExists==false)
  2590. {
  2591. SplitPacketChannel *newChannel = RakNet::OP_NEW<SplitPacketChannel>( __FILE__, __LINE__ );
  2592. #if PREALLOCATE_LARGE_MESSAGES==1
  2593. index=splitPacketChannelList.Insert(internalPacket->splitPacketId, newChannel, true, __FILE__,__LINE__);
  2594. newChannel->returnedPacket=CreateInternalPacketCopy( internalPacket, 0, 0, time );
  2595. newChannel->gotFirstPacket=false;
  2596. newChannel->splitPacketsArrived=0;
  2597. AllocInternalPacketData(newChannel->returnedPacket, BITS_TO_BYTES( internalPacket->dataBitLength*internalPacket->splitPacketCount ), false, __FILE__, __LINE__ );
  2598. RakAssert(newChannel->returnedPacket->data);
  2599. #else
  2600. newChannel->firstPacket=0;
  2601. index=splitPacketChannelList.Insert(internalPacket->splitPacketId, newChannel, true, __FILE__,__LINE__);
  2602. // Preallocate to the final size, to avoid runtime copies
  2603. newChannel->splitPacketList.Preallocate(internalPacket->splitPacketCount, __FILE__,__LINE__);
  2604. #endif
  2605. }
  2606. #if PREALLOCATE_LARGE_MESSAGES==1
  2607. splitPacketChannelList[index]->lastUpdateTime=time;
  2608. splitPacketChannelList[index]->splitPacketsArrived++;
  2609. splitPacketChannelList[index]->returnedPacket->dataBitLength+=internalPacket->dataBitLength;
  2610. bool dealloc;
  2611. if (internalPacket->splitPacketIndex==0)
  2612. {
  2613. splitPacketChannelList[index]->gotFirstPacket=true;
  2614. splitPacketChannelList[index]->stride=BITS_TO_BYTES(internalPacket->dataBitLength);
  2615. for (unsigned int j=0; j < splitPacketChannelList[index]->splitPacketList.Size(); j++)
  2616. {
  2617. memcpy(splitPacketChannelList[index]->returnedPacket->data+internalPacket->splitPacketIndex*splitPacketChannelList[index]->stride, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength));
  2618. FreeInternalPacketData(splitPacketChannelList[index]->splitPacketList[j], __FILE__, __LINE__ );
  2619. ReleaseToInternalPacketPool(splitPacketChannelList[index]->splitPacketList[j]);
  2620. }
  2621. memcpy(splitPacketChannelList[index]->returnedPacket->data, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength));
  2622. splitPacketChannelList[index]->splitPacketList.Clear(true,__FILE__,__LINE__);
  2623. dealloc=true;
  2624. }
  2625. else
  2626. {
  2627. if (splitPacketChannelList[index]->gotFirstPacket==true)
  2628. {
  2629. memcpy(splitPacketChannelList[index]->returnedPacket->data+internalPacket->splitPacketIndex*splitPacketChannelList[index]->stride, internalPacket->data, (size_t) BITS_TO_BYTES(internalPacket->dataBitLength));
  2630. dealloc=true;
  2631. }
  2632. else
  2633. {
  2634. splitPacketChannelList[index]->splitPacketList.Push(internalPacket,__FILE__,__LINE__);
  2635. dealloc=false;
  2636. }
  2637. }
  2638. if (splitPacketChannelList[index]->gotFirstPacket==true &&
  2639. splitMessageProgressInterval &&
  2640. // splitPacketChannelList[index]->firstPacket &&
  2641. // splitPacketChannelList[index]->splitPacketList.Size()!=splitPacketChannelList[index]->firstPacket->splitPacketCount &&
  2642. // (splitPacketChannelList[index]->splitPacketList.Size()%splitMessageProgressInterval)==0
  2643. splitPacketChannelList[index]->gotFirstPacket &&
  2644. splitPacketChannelList[index]->splitPacketsArrived!=splitPacketChannelList[index]->returnedPacket->splitPacketCount &&
  2645. (splitPacketChannelList[index]->splitPacketsArrived%splitMessageProgressInterval)==0
  2646. )
  2647. {
  2648. // Return ID_DOWNLOAD_PROGRESS
  2649. // Write splitPacketIndex (SplitPacketIndexType)
  2650. // Write splitPacketCount (SplitPacketIndexType)
  2651. // Write byteLength (4)
  2652. // Write data, splitPacketChannelList[index]->splitPacketList[0]->data
  2653. InternalPacket *progressIndicator = AllocateFromInternalPacketPool();
  2654. // unsigned int len = sizeof(MessageID) + sizeof(unsigned int)*2 + sizeof(unsigned int) + (unsigned int) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength);
  2655. unsigned int l = (unsigned int) splitPacketChannelList[index]->stride;
  2656. const unsigned int len = sizeof(MessageID) + sizeof(unsigned int)*2 + sizeof(unsigned int) + l;
  2657. AllocInternalPacketData(progressIndicator, len, false, __FILE__, __LINE__ );
  2658. progressIndicator->dataBitLength=BYTES_TO_BITS(len);
  2659. progressIndicator->data[0]=(MessageID)ID_DOWNLOAD_PROGRESS;
  2660. unsigned int temp;
  2661. // temp=splitPacketChannelList[index]->splitPacketList.Size();
  2662. temp=splitPacketChannelList[index]->splitPacketsArrived;
  2663. memcpy(progressIndicator->data+sizeof(MessageID), &temp, sizeof(unsigned int));
  2664. temp=(unsigned int)internalPacket->splitPacketCount;
  2665. memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*1, &temp, sizeof(unsigned int));
  2666. // temp=(unsigned int) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength);
  2667. temp=(unsigned int) BITS_TO_BYTES(l);
  2668. memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*2, &temp, sizeof(unsigned int));
  2669. //memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*3, splitPacketChannelList[index]->firstPacket->data, (size_t) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength));
  2670. memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*3, splitPacketChannelList[index]->returnedPacket->data, (size_t) BITS_TO_BYTES(l));
  2671. }
  2672. if (dealloc)
  2673. {
  2674. FreeInternalPacketData(internalPacket, __FILE__, __LINE__ );
  2675. ReleaseToInternalPacketPool(internalPacket);
  2676. }
  2677. #else
  2678. // Insert the packet into the SplitPacketChannel
  2679. splitPacketChannelList[index]->splitPacketList.Insert(internalPacket, __FILE__, __LINE__ );
  2680. splitPacketChannelList[index]->lastUpdateTime=time;
  2681. // If the index is 0, then this is the first packet. Record this so it can be returned to the user with download progress
  2682. if (internalPacket->splitPacketIndex==0)
  2683. splitPacketChannelList[index]->firstPacket=internalPacket;
  2684. // Return download progress if we have the first packet, the list is not complete, and there are enough packets to justify it
  2685. if (splitMessageProgressInterval &&
  2686. splitPacketChannelList[index]->firstPacket &&
  2687. splitPacketChannelList[index]->splitPacketList.Size()!=splitPacketChannelList[index]->firstPacket->splitPacketCount &&
  2688. (splitPacketChannelList[index]->splitPacketList.Size()%splitMessageProgressInterval)==0)
  2689. {
  2690. // Return ID_DOWNLOAD_PROGRESS
  2691. // Write splitPacketIndex (SplitPacketIndexType)
  2692. // Write splitPacketCount (SplitPacketIndexType)
  2693. // Write byteLength (4)
  2694. // Write data, splitPacketChannelList[index]->splitPacketList[0]->data
  2695. InternalPacket *progressIndicator = AllocateFromInternalPacketPool();
  2696. unsigned int length = sizeof(MessageID) + sizeof(unsigned int)*2 + sizeof(unsigned int) + (unsigned int) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength);
  2697. AllocInternalPacketData(progressIndicator, length, false, __FILE__, __LINE__ );
  2698. progressIndicator->dataBitLength=BYTES_TO_BITS(length);
  2699. progressIndicator->data[0]=(MessageID)ID_DOWNLOAD_PROGRESS;
  2700. unsigned int temp;
  2701. temp=splitPacketChannelList[index]->splitPacketList.Size();
  2702. memcpy(progressIndicator->data+sizeof(MessageID), &temp, sizeof(unsigned int));
  2703. temp=(unsigned int)internalPacket->splitPacketCount;
  2704. memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*1, &temp, sizeof(unsigned int));
  2705. temp=(unsigned int) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength);
  2706. memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*2, &temp, sizeof(unsigned int));
  2707. memcpy(progressIndicator->data+sizeof(MessageID)+sizeof(unsigned int)*3, splitPacketChannelList[index]->firstPacket->data, (size_t) BITS_TO_BYTES(splitPacketChannelList[index]->firstPacket->dataBitLength));
  2708. outputQueue.Push(progressIndicator, __FILE__, __LINE__ );
  2709. }
  2710. #endif
  2711. }
  2712. //-------------------------------------------------------------------------------------------------------
  2713. // Take all split chunks with the specified splitPacketId and try to
  2714. //reconstruct a packet. If we can, allocate and return it. Otherwise return 0
  2715. // Optimized version
  2716. //-------------------------------------------------------------------------------------------------------
  2717. InternalPacket * ReliabilityLayer::BuildPacketFromSplitPacketList( SplitPacketChannel *splitPacketChannel, CCTimeType time )
  2718. {
  2719. #if PREALLOCATE_LARGE_MESSAGES==1
  2720. InternalPacket *returnedPacket=splitPacketChannel->returnedPacket;
  2721. RakNet::OP_DELETE(splitPacketChannel, __FILE__, __LINE__);
  2722. (void) time;
  2723. return returnedPacket;
  2724. #else
  2725. unsigned int j;
  2726. InternalPacket * internalPacket, *splitPacket;
  2727. // int splitPacketPartLength;
  2728. // Reconstruct
  2729. internalPacket = CreateInternalPacketCopy( splitPacketChannel->splitPacketList[0], 0, 0, time );
  2730. internalPacket->dataBitLength=0;
  2731. for (j=0; j < splitPacketChannel->splitPacketList.Size(); j++)
  2732. internalPacket->dataBitLength+=splitPacketChannel->splitPacketList[j]->dataBitLength;
  2733. // splitPacketPartLength=BITS_TO_BYTES(splitPacketChannel->firstPacket->dataBitLength);
  2734. internalPacket->data = (unsigned char*) rakMalloc_Ex( (size_t) BITS_TO_BYTES( internalPacket->dataBitLength ), _FILE_AND_LINE_ );
  2735. internalPacket->allocationScheme=InternalPacket::NORMAL;
  2736. BitSize_t offset = 0;
  2737. for (j=0; j < splitPacketChannel->splitPacketList.Size(); j++)
  2738. {
  2739. splitPacket=splitPacketChannel->splitPacketList[j];
  2740. memcpy(internalPacket->data + BITS_TO_BYTES(offset), splitPacket->data, (size_t)BITS_TO_BYTES(splitPacketChannel->splitPacketList[j]->dataBitLength));
  2741. offset += splitPacketChannel->splitPacketList[j]->dataBitLength;
  2742. }
  2743. for (j=0; j < splitPacketChannel->splitPacketList.Size(); j++)
  2744. {
  2745. FreeInternalPacketData(splitPacketChannel->splitPacketList[j], _FILE_AND_LINE_ );
  2746. ReleaseToInternalPacketPool(splitPacketChannel->splitPacketList[j]);
  2747. }
  2748. RakNet::OP_DELETE(splitPacketChannel, __FILE__, __LINE__);
  2749. return internalPacket;
  2750. #endif
  2751. }
  2752. //-------------------------------------------------------------------------------------------------------
  2753. InternalPacket * ReliabilityLayer::BuildPacketFromSplitPacketList( SplitPacketIdType splitPacketId, CCTimeType time,
  2754. RakNetSocket2 *s, SystemAddress &systemAddress, RakNetRandom *rnr,
  2755. BitStream &updateBitStream)
  2756. {
  2757. unsigned int i;
  2758. bool objectExists;
  2759. SplitPacketChannel *splitPacketChannel;
  2760. InternalPacket * internalPacket;
  2761. // Find in splitPacketChannelList the SplitPacketChannel with this splitPacketId
  2762. i=splitPacketChannelList.GetIndexFromKey(splitPacketId, &objectExists);
  2763. splitPacketChannel=splitPacketChannelList[i];
  2764. #if PREALLOCATE_LARGE_MESSAGES==1
  2765. if (splitPacketChannel->splitPacketsArrived==splitPacketChannel->returnedPacket->splitPacketCount)
  2766. #else
  2767. if (splitPacketChannel->splitPacketList.Size()==splitPacketChannel->splitPacketList[0]->splitPacketCount)
  2768. #endif
  2769. {
  2770. // Ack immediately, because for large files this can take a long time
  2771. SendACKs(s, systemAddress, time, rnr, updateBitStream);
  2772. internalPacket=BuildPacketFromSplitPacketList(splitPacketChannel,time);
  2773. splitPacketChannelList.RemoveAtIndex(i);
  2774. return internalPacket;
  2775. }
  2776. else
  2777. {
  2778. return 0;
  2779. }
  2780. }
  2781. /*
  2782. //-------------------------------------------------------------------------------------------------------
  2783. // Delete any unreliable split packets that have long since expired
  2784. void ReliabilityLayer::DeleteOldUnreliableSplitPackets( CCTimeType time )
  2785. {
  2786. unsigned i,j;
  2787. i=0;
  2788. while (i < splitPacketChannelList.Size())
  2789. {
  2790. #if CC_TIME_TYPE_BYTES==4
  2791. if (time > splitPacketChannelList[i]->lastUpdateTime + timeoutTime &&
  2792. #else
  2793. if (time > splitPacketChannelList[i]->lastUpdateTime + (CCTimeType)timeoutTime*(CCTimeType)1000 &&
  2794. #endif
  2795. (splitPacketChannelList[i]->splitPacketList[0]->reliability==UNRELIABLE || splitPacketChannelList[i]->splitPacketList[0]->reliability==UNRELIABLE_SEQUENCED))
  2796. {
  2797. for (j=0; j < splitPacketChannelList[i]->splitPacketList.Size(); j++)
  2798. {
  2799. RakNet::OP_DELETE_ARRAY(splitPacketChannelList[i]->splitPacketList[j]->data, _FILE_AND_LINE_);
  2800. ReleaseToInternalPacketPool(splitPacketChannelList[i]->splitPacketList[j]);
  2801. }
  2802. RakNet::OP_DELETE(splitPacketChannelList[i], _FILE_AND_LINE_);
  2803. splitPacketChannelList.RemoveAtIndex(i);
  2804. }
  2805. else
  2806. i++;
  2807. }
  2808. }
  2809. */
  2810. //-------------------------------------------------------------------------------------------------------
  2811. // Creates a copy of the specified internal packet with data copied from the original starting at dataByteOffset for dataByteLength bytes.
  2812. // Does not copy any split data parameters as that information is always generated does not have any reason to be copied
  2813. //-------------------------------------------------------------------------------------------------------
  2814. InternalPacket * ReliabilityLayer::CreateInternalPacketCopy( InternalPacket *original, int dataByteOffset, int dataByteLength, CCTimeType time )
  2815. {
  2816. InternalPacket * copy = AllocateFromInternalPacketPool();
  2817. #ifdef _DEBUG
  2818. // Remove accessing undefined memory error
  2819. memset( copy, 255, sizeof( InternalPacket ) );
  2820. #endif
  2821. // Copy over our chunk of data
  2822. if ( dataByteLength > 0 )
  2823. {
  2824. AllocInternalPacketData(copy, BITS_TO_BYTES(dataByteLength ), false, _FILE_AND_LINE_ );
  2825. memcpy( copy->data, original->data + dataByteOffset, dataByteLength );
  2826. }
  2827. else
  2828. copy->data = 0;
  2829. copy->dataBitLength = dataByteLength << 3;
  2830. copy->creationTime = time;
  2831. copy->nextActionTime = 0;
  2832. copy->orderingIndex = original->orderingIndex;
  2833. copy->sequencingIndex = original->sequencingIndex;
  2834. copy->orderingChannel = original->orderingChannel;
  2835. copy->reliableMessageNumber = original->reliableMessageNumber;
  2836. copy->priority = original->priority;
  2837. copy->reliability = original->reliability;
  2838. #if PREALLOCATE_LARGE_MESSAGES==1
  2839. copy->splitPacketCount = original->splitPacketCount;
  2840. copy->splitPacketId = original->splitPacketId;
  2841. copy->splitPacketIndex = original->splitPacketIndex;
  2842. #endif
  2843. return copy;
  2844. }
  2845. //-------------------------------------------------------------------------------------------------------
  2846. // Get the specified ordering list
  2847. //-------------------------------------------------------------------------------------------------------
  2848. /*
  2849. DataStructures::LinkedList<InternalPacket*> *ReliabilityLayer::GetOrderingListAtOrderingStream( unsigned char orderingChannel )
  2850. {
  2851. if ( orderingChannel >= orderingList.Size() )
  2852. return 0;
  2853. return orderingList[ orderingChannel ];
  2854. }
  2855. //-------------------------------------------------------------------------------------------------------
  2856. // Add the internal packet to the ordering list in order based on order index
  2857. //-------------------------------------------------------------------------------------------------------
  2858. void ReliabilityLayer::AddToOrderingList( InternalPacket * internalPacket )
  2859. {
  2860. }
  2861. */
  2862. //-------------------------------------------------------------------------------------------------------
  2863. // Inserts a packet into the resend list in order
  2864. //-------------------------------------------------------------------------------------------------------
  2865. void ReliabilityLayer::InsertPacketIntoResendList( InternalPacket *internalPacket, CCTimeType time, bool firstResend, bool modifyUnacknowledgedBytes )
  2866. {
  2867. (void) firstResend;
  2868. (void) time;
  2869. (void) internalPacket;
  2870. AddToListTail(internalPacket, modifyUnacknowledgedBytes);
  2871. RakAssert(internalPacket->nextActionTime!=0);
  2872. }
  2873. //-------------------------------------------------------------------------------------------------------
  2874. // Were you ever unable to deliver a packet despite retries?
  2875. //-------------------------------------------------------------------------------------------------------
  2876. bool ReliabilityLayer::IsDeadConnection( void ) const
  2877. {
  2878. return deadConnection;
  2879. }
  2880. //-------------------------------------------------------------------------------------------------------
  2881. // Causes IsDeadConnection to return true
  2882. //-------------------------------------------------------------------------------------------------------
  2883. void ReliabilityLayer::KillConnection( void )
  2884. {
  2885. deadConnection=true;
  2886. }
  2887. //-------------------------------------------------------------------------------------------------------
  2888. // Statistics
  2889. //-------------------------------------------------------------------------------------------------------
  2890. RakNetStatistics * ReliabilityLayer::GetStatistics( RakNetStatistics *rns )
  2891. {
  2892. unsigned i;
  2893. RakNet::TimeUS time = RakNet::GetTimeUS();
  2894. uint64_t uint64Denominator;
  2895. double doubleDenominator;
  2896. for (i=0; i < RNS_PER_SECOND_METRICS_COUNT; i++)
  2897. {
  2898. statistics.valueOverLastSecond[i]=bpsMetrics[i].GetBPS1Threadsafe(time);
  2899. statistics.runningTotal[i]=bpsMetrics[i].GetTotal1();
  2900. }
  2901. memcpy(rns, &statistics, sizeof(statistics));
  2902. if (rns->valueOverLastSecond[USER_MESSAGE_BYTES_SENT]+rns->valueOverLastSecond[USER_MESSAGE_BYTES_RESENT]>0)
  2903. rns->packetlossLastSecond=(float)((double) rns->valueOverLastSecond[USER_MESSAGE_BYTES_RESENT]/((double) rns->valueOverLastSecond[USER_MESSAGE_BYTES_SENT]+(double) rns->valueOverLastSecond[USER_MESSAGE_BYTES_RESENT]));
  2904. else
  2905. rns->packetlossLastSecond=0.0f;
  2906. rns->packetlossTotal=0.0f;
  2907. uint64Denominator=(rns->runningTotal[USER_MESSAGE_BYTES_SENT]+rns->runningTotal[USER_MESSAGE_BYTES_RESENT]);
  2908. if (uint64Denominator!=0&&rns->runningTotal[USER_MESSAGE_BYTES_SENT]/uint64Denominator>0)
  2909. {
  2910. doubleDenominator=((double) rns->runningTotal[USER_MESSAGE_BYTES_SENT]+(double) rns->runningTotal[USER_MESSAGE_BYTES_RESENT]);
  2911. if(doubleDenominator!=0)
  2912. {
  2913. rns->packetlossTotal=(float)((double) rns->runningTotal[USER_MESSAGE_BYTES_RESENT]/doubleDenominator);
  2914. }
  2915. }
  2916. rns->isLimitedByCongestionControl=statistics.isLimitedByCongestionControl;
  2917. rns->BPSLimitByCongestionControl=statistics.BPSLimitByCongestionControl;
  2918. rns->isLimitedByOutgoingBandwidthLimit=statistics.isLimitedByOutgoingBandwidthLimit;
  2919. rns->BPSLimitByOutgoingBandwidthLimit=statistics.BPSLimitByOutgoingBandwidthLimit;
  2920. return rns;
  2921. }
  2922. //-------------------------------------------------------------------------------------------------------
  2923. // Returns the number of packets in the resend queue, not counting holes
  2924. //-------------------------------------------------------------------------------------------------------
  2925. unsigned int ReliabilityLayer::GetResendListDataSize(void) const
  2926. {
  2927. // Not accurate but thread-safe. The commented version might crash if the queue is cleared while we loop through it
  2928. // return resendTree.Size();
  2929. return statistics.messagesInResendBuffer;
  2930. }
  2931. //-------------------------------------------------------------------------------------------------------
  2932. bool ReliabilityLayer::AckTimeout(RakNet::Time curTime)
  2933. {
  2934. // I check timeLastDatagramArrived-curTime because with threading it is possible that timeLastDatagramArrived is
  2935. // slightly greater than curTime, in which case this is NOT an ack timeout
  2936. return (timeLastDatagramArrived-curTime)>10000 && curTime-timeLastDatagramArrived>timeoutTime;
  2937. }
  2938. //-------------------------------------------------------------------------------------------------------
  2939. CCTimeType ReliabilityLayer::GetNextSendTime(void) const
  2940. {
  2941. return nextSendTime;
  2942. }
  2943. //-------------------------------------------------------------------------------------------------------
  2944. CCTimeType ReliabilityLayer::GetTimeBetweenPackets(void) const
  2945. {
  2946. return timeBetweenPackets;
  2947. }
  2948. //-------------------------------------------------------------------------------------------------------
  2949. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  2950. CCTimeType ReliabilityLayer::GetAckPing(void) const
  2951. {
  2952. return ackPing;
  2953. }
  2954. #endif
  2955. //-------------------------------------------------------------------------------------------------------
  2956. void ReliabilityLayer::ResetPacketsAndDatagrams(void)
  2957. {
  2958. packetsToSendThisUpdate.Clear(true, _FILE_AND_LINE_);
  2959. packetsToDeallocThisUpdate.Clear(true, _FILE_AND_LINE_);
  2960. packetsToSendThisUpdateDatagramBoundaries.Clear(true, _FILE_AND_LINE_);
  2961. datagramsToSendThisUpdateIsPair.Clear(true, _FILE_AND_LINE_);
  2962. datagramSizesInBytes.Clear(true, _FILE_AND_LINE_);
  2963. datagramSizeSoFar=0;
  2964. }
  2965. //-------------------------------------------------------------------------------------------------------
  2966. void ReliabilityLayer::PushPacket(CCTimeType time, InternalPacket *internalPacket, bool isReliable)
  2967. {
  2968. BitSize_t bitsForThisPacket=BYTES_TO_BITS(BITS_TO_BYTES(internalPacket->dataBitLength)+BITS_TO_BYTES(internalPacket->headerLength));
  2969. datagramSizeSoFar+=bitsForThisPacket;
  2970. RakAssert(BITS_TO_BYTES(datagramSizeSoFar)<MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE);
  2971. allDatagramSizesSoFar+=bitsForThisPacket;
  2972. packetsToSendThisUpdate.Push(internalPacket, _FILE_AND_LINE_ );
  2973. packetsToDeallocThisUpdate.Push(isReliable==false, _FILE_AND_LINE_ );
  2974. RakAssert(internalPacket->headerLength==GetMessageHeaderLengthBits(internalPacket));
  2975. // This code tells me how much time elapses between when you send, and when the message actually goes out
  2976. // if (internalPacket->data[0]==0)
  2977. // {
  2978. // RakNet::TimeMS t;
  2979. // RakNet::BitStream bs(internalPacket->data+1,sizeof(t),false);
  2980. // bs.Read(t);
  2981. // RakNet::TimeMS curTime=RakNet::GetTimeMS();
  2982. // RakNet::TimeMS diff = curTime-t;
  2983. // }
  2984. congestionManager.OnSendBytes(time, BITS_TO_BYTES(internalPacket->dataBitLength)+BITS_TO_BYTES(internalPacket->headerLength));
  2985. }
  2986. //-------------------------------------------------------------------------------------------------------
  2987. void ReliabilityLayer::PushDatagram(void)
  2988. {
  2989. if (datagramSizeSoFar>0)
  2990. {
  2991. packetsToSendThisUpdateDatagramBoundaries.Push(packetsToSendThisUpdate.Size(), _FILE_AND_LINE_ );
  2992. datagramsToSendThisUpdateIsPair.Push(false, _FILE_AND_LINE_ );
  2993. RakAssert(BITS_TO_BYTES(datagramSizeSoFar)<MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE);
  2994. datagramSizesInBytes.Push(BITS_TO_BYTES(datagramSizeSoFar), _FILE_AND_LINE_ );
  2995. datagramSizeSoFar=0;
  2996. // Disable packet pairs
  2997. /*
  2998. if (countdownToNextPacketPair==0)
  2999. {
  3000. if (TagMostRecentPushAsSecondOfPacketPair())
  3001. countdownToNextPacketPair=15;
  3002. }
  3003. else
  3004. countdownToNextPacketPair--;
  3005. */
  3006. }
  3007. }
  3008. //-------------------------------------------------------------------------------------------------------
  3009. bool ReliabilityLayer::TagMostRecentPushAsSecondOfPacketPair(void)
  3010. {
  3011. if (datagramsToSendThisUpdateIsPair.Size()>=2)
  3012. {
  3013. datagramsToSendThisUpdateIsPair[datagramsToSendThisUpdateIsPair.Size()-2]=true;
  3014. datagramsToSendThisUpdateIsPair[datagramsToSendThisUpdateIsPair.Size()-1]=true;
  3015. return true;
  3016. }
  3017. return false;
  3018. }
  3019. //-------------------------------------------------------------------------------------------------------
  3020. void ReliabilityLayer::ClearPacketsAndDatagrams(void)
  3021. {
  3022. unsigned int i;
  3023. for (i=0; i < packetsToDeallocThisUpdate.Size(); i++)
  3024. {
  3025. // packetsToDeallocThisUpdate holds a boolean indicating if packetsToSendThisUpdate at this index should be freed
  3026. if (packetsToDeallocThisUpdate[i])
  3027. {
  3028. RemoveFromUnreliableLinkedList(packetsToSendThisUpdate[i]);
  3029. FreeInternalPacketData(packetsToSendThisUpdate[i], _FILE_AND_LINE_ );
  3030. // if (keepInternalPacketIfNeedsAck==false || packetsToSendThisUpdate[i]->reliability<RELIABLE_WITH_ACK_RECEIPT)
  3031. ReleaseToInternalPacketPool( packetsToSendThisUpdate[i] );
  3032. }
  3033. }
  3034. packetsToDeallocThisUpdate.Clear(true, _FILE_AND_LINE_);
  3035. }
  3036. //-------------------------------------------------------------------------------------------------------
  3037. void ReliabilityLayer::MoveToListHead(InternalPacket *internalPacket)
  3038. {
  3039. if ( internalPacket == resendLinkedListHead )
  3040. return;
  3041. if (resendLinkedListHead==0)
  3042. {
  3043. internalPacket->resendNext=internalPacket;
  3044. internalPacket->resendPrev=internalPacket;
  3045. resendLinkedListHead=internalPacket;
  3046. return;
  3047. }
  3048. internalPacket->resendPrev->resendNext = internalPacket->resendNext;
  3049. internalPacket->resendNext->resendPrev = internalPacket->resendPrev;
  3050. internalPacket->resendNext=resendLinkedListHead;
  3051. internalPacket->resendPrev=resendLinkedListHead->resendPrev;
  3052. internalPacket->resendPrev->resendNext=internalPacket;
  3053. resendLinkedListHead->resendPrev=internalPacket;
  3054. resendLinkedListHead=internalPacket;
  3055. RakAssert(internalPacket->headerLength+internalPacket->dataBitLength>0);
  3056. //ValidateResendList();
  3057. }
  3058. //-------------------------------------------------------------------------------------------------------
  3059. void ReliabilityLayer::RemoveFromList(InternalPacket *internalPacket, bool modifyUnacknowledgedBytes)
  3060. {
  3061. InternalPacket *newPosition;
  3062. internalPacket->resendPrev->resendNext = internalPacket->resendNext;
  3063. internalPacket->resendNext->resendPrev = internalPacket->resendPrev;
  3064. newPosition = internalPacket->resendNext;
  3065. if ( internalPacket == resendLinkedListHead )
  3066. resendLinkedListHead = newPosition;
  3067. if (resendLinkedListHead==internalPacket)
  3068. resendLinkedListHead=0;
  3069. if (modifyUnacknowledgedBytes)
  3070. {
  3071. RakAssert(unacknowledgedBytes>=BITS_TO_BYTES(internalPacket->headerLength+internalPacket->dataBitLength));
  3072. unacknowledgedBytes-=BITS_TO_BYTES(internalPacket->headerLength+internalPacket->dataBitLength);
  3073. // printf("-unacknowledgedBytes:%i ", unacknowledgedBytes);
  3074. // ValidateResendList();
  3075. }
  3076. }
  3077. //-------------------------------------------------------------------------------------------------------
  3078. void ReliabilityLayer::AddToListTail(InternalPacket *internalPacket, bool modifyUnacknowledgedBytes)
  3079. {
  3080. if (modifyUnacknowledgedBytes)
  3081. {
  3082. unacknowledgedBytes+=BITS_TO_BYTES(internalPacket->headerLength+internalPacket->dataBitLength);
  3083. // printf("+unacknowledgedBytes:%i ", unacknowledgedBytes);
  3084. }
  3085. if (resendLinkedListHead==0)
  3086. {
  3087. internalPacket->resendNext=internalPacket;
  3088. internalPacket->resendPrev=internalPacket;
  3089. resendLinkedListHead=internalPacket;
  3090. return;
  3091. }
  3092. internalPacket->resendNext=resendLinkedListHead;
  3093. internalPacket->resendPrev=resendLinkedListHead->resendPrev;
  3094. internalPacket->resendPrev->resendNext=internalPacket;
  3095. resendLinkedListHead->resendPrev=internalPacket;
  3096. // ValidateResendList();
  3097. }
  3098. //-------------------------------------------------------------------------------------------------------
  3099. void ReliabilityLayer::PopListHead(bool modifyUnacknowledgedBytes)
  3100. {
  3101. RakAssert(resendLinkedListHead!=0);
  3102. RemoveFromList(resendLinkedListHead, modifyUnacknowledgedBytes);
  3103. }
  3104. //-------------------------------------------------------------------------------------------------------
  3105. bool ReliabilityLayer::IsResendQueueEmpty(void) const
  3106. {
  3107. return resendLinkedListHead==0;
  3108. }
  3109. //-------------------------------------------------------------------------------------------------------
  3110. void ReliabilityLayer::SendACKs(RakNetSocket2 *s, SystemAddress &systemAddress, CCTimeType time, RakNetRandom *rnr, BitStream &updateBitStream)
  3111. {
  3112. BitSize_t maxDatagramPayload = GetMaxDatagramSizeExcludingMessageHeaderBits();
  3113. while (acknowlegements.Size()>0)
  3114. {
  3115. // Send acks
  3116. updateBitStream.Reset();
  3117. DatagramHeaderFormat dhf;
  3118. dhf.isACK=true;
  3119. dhf.isNAK=false;
  3120. dhf.isPacketPair=false;
  3121. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  3122. dhf.sourceSystemTime=time;
  3123. #endif
  3124. double B;
  3125. double AS;
  3126. bool hasBAndAS;
  3127. if (remoteSystemNeedsBAndAS)
  3128. {
  3129. congestionManager.OnSendAckGetBAndAS(time, &hasBAndAS,&B,&AS);
  3130. dhf.AS=(float)AS;
  3131. dhf.hasBAndAS=hasBAndAS;
  3132. }
  3133. else
  3134. dhf.hasBAndAS=false;
  3135. #if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1
  3136. dhf.sourceSystemTime=nextAckTimeToSend;
  3137. #endif
  3138. // dhf.B=(float)B;
  3139. updateBitStream.Reset();
  3140. dhf.Serialize(&updateBitStream);
  3141. CC_DEBUG_PRINTF_1("AckSnd ");
  3142. acknowlegements.Serialize(&updateBitStream, maxDatagramPayload, true);
  3143. SendBitStream( s, systemAddress, &updateBitStream, rnr, time );
  3144. congestionManager.OnSendAck(time,updateBitStream.GetNumberOfBytesUsed());
  3145. // I think this is causing a bug where if the estimated bandwidth is very low for the recipient, only acks ever get sent
  3146. // congestionManager.OnSendBytes(time,UDP_HEADER_SIZE+updateBitStream.GetNumberOfBytesUsed());
  3147. }
  3148. }
  3149. /*
  3150. //-------------------------------------------------------------------------------------------------------
  3151. ReliabilityLayer::DatagramMessageIDList* ReliabilityLayer::AllocateFromDatagramMessageIDPool(void)
  3152. {
  3153. DatagramMessageIDList*s;
  3154. s=datagramMessageIDPool.Allocate( _FILE_AND_LINE_ );
  3155. // Call new operator, memoryPool doesn't do this
  3156. s = new ((void*)s) DatagramMessageIDList;
  3157. return s;
  3158. }
  3159. //-------------------------------------------------------------------------------------------------------
  3160. void ReliabilityLayer::ReleaseToDatagramMessageIDPool(DatagramMessageIDList* d)
  3161. {
  3162. d->~DatagramMessageIDList();
  3163. datagramMessageIDPool.Release(d);
  3164. }
  3165. */
  3166. //-------------------------------------------------------------------------------------------------------
  3167. InternalPacket* ReliabilityLayer::AllocateFromInternalPacketPool(void)
  3168. {
  3169. InternalPacket *ip = internalPacketPool.Allocate( _FILE_AND_LINE_ );
  3170. ip->reliableMessageNumber = (MessageNumberType) (const uint32_t)-1;
  3171. ip->messageNumberAssigned=false;
  3172. ip->nextActionTime = 0;
  3173. ip->splitPacketCount = 0;
  3174. ip->splitPacketIndex = 0;
  3175. ip->splitPacketId = 0;
  3176. ip->allocationScheme=InternalPacket::NORMAL;
  3177. ip->data=0;
  3178. ip->timesSent=0;
  3179. return ip;
  3180. }
  3181. //-------------------------------------------------------------------------------------------------------
  3182. void ReliabilityLayer::ReleaseToInternalPacketPool(InternalPacket *ip)
  3183. {
  3184. internalPacketPool.Release(ip, _FILE_AND_LINE_);
  3185. }
  3186. //-------------------------------------------------------------------------------------------------------
  3187. void ReliabilityLayer::RemoveFromUnreliableLinkedList(InternalPacket *internalPacket)
  3188. {
  3189. if (internalPacket->reliability==UNRELIABLE ||
  3190. internalPacket->reliability==UNRELIABLE_SEQUENCED ||
  3191. internalPacket->reliability==UNRELIABLE_WITH_ACK_RECEIPT
  3192. // ||
  3193. // internalPacket->reliability==UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT
  3194. )
  3195. {
  3196. InternalPacket *newPosition;
  3197. internalPacket->unreliablePrev->unreliableNext = internalPacket->unreliableNext;
  3198. internalPacket->unreliableNext->unreliablePrev = internalPacket->unreliablePrev;
  3199. newPosition = internalPacket->unreliableNext;
  3200. if ( internalPacket == unreliableLinkedListHead )
  3201. unreliableLinkedListHead = newPosition;
  3202. if (unreliableLinkedListHead==internalPacket)
  3203. unreliableLinkedListHead=0;
  3204. }
  3205. }
  3206. //-------------------------------------------------------------------------------------------------------
  3207. void ReliabilityLayer::AddToUnreliableLinkedList(InternalPacket *internalPacket)
  3208. {
  3209. if (internalPacket->reliability==UNRELIABLE ||
  3210. internalPacket->reliability==UNRELIABLE_SEQUENCED ||
  3211. internalPacket->reliability==UNRELIABLE_WITH_ACK_RECEIPT
  3212. // ||
  3213. // internalPacket->reliability==UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT
  3214. )
  3215. {
  3216. if (unreliableLinkedListHead==0)
  3217. {
  3218. internalPacket->unreliableNext=internalPacket;
  3219. internalPacket->unreliablePrev=internalPacket;
  3220. unreliableLinkedListHead=internalPacket;
  3221. return;
  3222. }
  3223. internalPacket->unreliableNext=unreliableLinkedListHead;
  3224. internalPacket->unreliablePrev=unreliableLinkedListHead->unreliablePrev;
  3225. internalPacket->unreliablePrev->unreliableNext=internalPacket;
  3226. unreliableLinkedListHead->unreliablePrev=internalPacket;
  3227. }
  3228. }
  3229. //-------------------------------------------------------------------------------------------------------
  3230. void ReliabilityLayer::ValidateResendList(void) const
  3231. {
  3232. // unsigned int count1=0, count2=0;
  3233. // for (unsigned int i=0; i < RESEND_BUFFER_ARRAY_LENGTH; i++)
  3234. // if (resendBuffer[i])
  3235. // count1++;
  3236. //
  3237. // if (resendLinkedListHead)
  3238. // {
  3239. // InternalPacket *internalPacket = resendLinkedListHead;
  3240. // do
  3241. // {
  3242. // count2++;
  3243. // internalPacket=internalPacket->resendNext;
  3244. // } while (internalPacket!=resendLinkedListHead);
  3245. // }
  3246. // RakAssert(count1==count2);
  3247. // RakAssert(count2<=RESEND_BUFFER_ARRAY_LENGTH);
  3248. }
  3249. //-------------------------------------------------------------------------------------------------------
  3250. bool ReliabilityLayer::ResendBufferOverflow(void) const
  3251. {
  3252. int index1 = sendReliableMessageNumberIndex & (uint32_t) RESEND_BUFFER_ARRAY_MASK;
  3253. // int index2 = (sendReliableMessageNumberIndex+(uint32_t)1) & (uint32_t) RESEND_BUFFER_ARRAY_MASK;
  3254. RakAssert(index1<RESEND_BUFFER_ARRAY_LENGTH);
  3255. return resendBuffer[index1]!=0; // || resendBuffer[index2]!=0;
  3256. }
  3257. //-------------------------------------------------------------------------------------------------------
  3258. ReliabilityLayer::MessageNumberNode* ReliabilityLayer::GetMessageNumberNodeByDatagramIndex(DatagramSequenceNumberType index, CCTimeType *timeSent)
  3259. {
  3260. if (datagramHistory.IsEmpty())
  3261. return 0;
  3262. if (congestionManager.LessThan(index, datagramHistoryPopCount))
  3263. return 0;
  3264. DatagramSequenceNumberType offsetIntoList = index - datagramHistoryPopCount;
  3265. if (offsetIntoList >= datagramHistory.Size())
  3266. return 0;
  3267. *timeSent=datagramHistory[offsetIntoList].timeSent;
  3268. return datagramHistory[offsetIntoList].head;
  3269. }
  3270. //-------------------------------------------------------------------------------------------------------
  3271. void ReliabilityLayer::RemoveFromDatagramHistory(DatagramSequenceNumberType index)
  3272. {
  3273. DatagramSequenceNumberType offsetIntoList = index - datagramHistoryPopCount;
  3274. MessageNumberNode *mnm = datagramHistory[offsetIntoList].head;
  3275. MessageNumberNode *next;
  3276. while (mnm)
  3277. {
  3278. next=mnm->next;
  3279. datagramHistoryMessagePool.Release(mnm, _FILE_AND_LINE_);
  3280. mnm=next;
  3281. }
  3282. datagramHistory[offsetIntoList].head=0;
  3283. }
  3284. //-------------------------------------------------------------------------------------------------------
  3285. void ReliabilityLayer::AddFirstToDatagramHistory(DatagramSequenceNumberType datagramNumber, CCTimeType timeSent)
  3286. {
  3287. (void) datagramNumber;
  3288. if (datagramHistory.Size()>DATAGRAM_MESSAGE_ID_ARRAY_LENGTH)
  3289. {
  3290. RemoveFromDatagramHistory(datagramHistoryPopCount);
  3291. datagramHistory.Pop();
  3292. datagramHistoryPopCount++;
  3293. }
  3294. datagramHistory.Push(DatagramHistoryNode(0, timeSent), _FILE_AND_LINE_);
  3295. // printf("%p Pushed empty DatagramHistoryNode to datagram history at index %i\n", this, datagramHistory.Size()-1);
  3296. }
  3297. //-------------------------------------------------------------------------------------------------------
  3298. ReliabilityLayer::MessageNumberNode* ReliabilityLayer::AddFirstToDatagramHistory(DatagramSequenceNumberType datagramNumber, DatagramSequenceNumberType messageNumber, CCTimeType timeSent)
  3299. {
  3300. (void) datagramNumber;
  3301. // RakAssert(datagramHistoryPopCount+(unsigned int) datagramHistory.Size()==datagramNumber);
  3302. if (datagramHistory.Size()>DATAGRAM_MESSAGE_ID_ARRAY_LENGTH)
  3303. {
  3304. RemoveFromDatagramHistory(datagramHistoryPopCount);
  3305. datagramHistory.Pop();
  3306. datagramHistoryPopCount++;
  3307. }
  3308. MessageNumberNode *mnm = datagramHistoryMessagePool.Allocate(_FILE_AND_LINE_);
  3309. mnm->next=0;
  3310. mnm->messageNumber=messageNumber;
  3311. datagramHistory.Push(DatagramHistoryNode(mnm, timeSent), _FILE_AND_LINE_);
  3312. // printf("%p Pushed message %i to DatagramHistoryNode to datagram history at index %i\n", this, messageNumber.val, datagramHistory.Size()-1);
  3313. return mnm;
  3314. }
  3315. //-------------------------------------------------------------------------------------------------------
  3316. ReliabilityLayer::MessageNumberNode* ReliabilityLayer::AddSubsequentToDatagramHistory(MessageNumberNode *messageNumberNode, DatagramSequenceNumberType messageNumber)
  3317. {
  3318. messageNumberNode->next=datagramHistoryMessagePool.Allocate(_FILE_AND_LINE_);
  3319. messageNumberNode->next->messageNumber=messageNumber;
  3320. messageNumberNode->next->next=0;
  3321. return messageNumberNode->next;
  3322. }
  3323. //-------------------------------------------------------------------------------------------------------
  3324. void ReliabilityLayer::AllocInternalPacketData(InternalPacket *internalPacket, InternalPacketRefCountedData **refCounter, unsigned char *externallyAllocatedPtr, unsigned char *ourOffset)
  3325. {
  3326. internalPacket->allocationScheme=InternalPacket::REF_COUNTED;
  3327. internalPacket->data=ourOffset;
  3328. if (*refCounter==0)
  3329. {
  3330. *refCounter = refCountedDataPool.Allocate(_FILE_AND_LINE_);
  3331. // *refCounter = RakNet::OP_NEW<InternalPacketRefCountedData>(_FILE_AND_LINE_);
  3332. (*refCounter)->refCount=1;
  3333. (*refCounter)->sharedDataBlock=externallyAllocatedPtr;
  3334. }
  3335. else
  3336. (*refCounter)->refCount++;
  3337. internalPacket->refCountedData=(*refCounter);
  3338. }
  3339. //-------------------------------------------------------------------------------------------------------
  3340. void ReliabilityLayer::AllocInternalPacketData(InternalPacket *internalPacket, unsigned char *externallyAllocatedPtr)
  3341. {
  3342. internalPacket->allocationScheme=InternalPacket::NORMAL;
  3343. internalPacket->data=externallyAllocatedPtr;
  3344. }
  3345. //-------------------------------------------------------------------------------------------------------
  3346. void ReliabilityLayer::AllocInternalPacketData(InternalPacket *internalPacket, unsigned int numBytes, bool allowStack, const char *file, unsigned int line)
  3347. {
  3348. if (allowStack && numBytes <= sizeof(internalPacket->stackData))
  3349. {
  3350. internalPacket->allocationScheme=InternalPacket::STACK;
  3351. internalPacket->data=internalPacket->stackData;
  3352. }
  3353. else
  3354. {
  3355. internalPacket->allocationScheme=InternalPacket::NORMAL;
  3356. internalPacket->data=(unsigned char*) rakMalloc_Ex(numBytes,file,line);
  3357. }
  3358. }
  3359. //-------------------------------------------------------------------------------------------------------
  3360. void ReliabilityLayer::FreeInternalPacketData(InternalPacket *internalPacket, const char *file, unsigned int line)
  3361. {
  3362. if (internalPacket==0)
  3363. return;
  3364. if (internalPacket->allocationScheme==InternalPacket::REF_COUNTED)
  3365. {
  3366. if (internalPacket->refCountedData==0)
  3367. return;
  3368. internalPacket->refCountedData->refCount--;
  3369. if (internalPacket->refCountedData->refCount==0)
  3370. {
  3371. rakFree_Ex(internalPacket->refCountedData->sharedDataBlock, file, line );
  3372. internalPacket->refCountedData->sharedDataBlock=0;
  3373. // RakNet::OP_DELETE(internalPacket->refCountedData,file, line);
  3374. refCountedDataPool.Release(internalPacket->refCountedData,file, line);
  3375. internalPacket->refCountedData=0;
  3376. }
  3377. }
  3378. else if (internalPacket->allocationScheme==InternalPacket::NORMAL)
  3379. {
  3380. if (internalPacket->data==0)
  3381. return;
  3382. rakFree_Ex(internalPacket->data, file, line );
  3383. internalPacket->data=0;
  3384. }
  3385. else
  3386. {
  3387. // Data was on stack
  3388. internalPacket->data=0;
  3389. }
  3390. }
  3391. //-------------------------------------------------------------------------------------------------------
  3392. unsigned int ReliabilityLayer::GetMaxDatagramSizeExcludingMessageHeaderBytes(void)
  3393. {
  3394. unsigned int val = congestionManager.GetMTU() - DatagramHeaderFormat::GetDataHeaderByteLength();
  3395. #if LIBCAT_SECURITY==1
  3396. if (useSecurity)
  3397. val -= cat::AuthenticatedEncryption::OVERHEAD_BYTES;
  3398. #endif
  3399. return val;
  3400. }
  3401. //-------------------------------------------------------------------------------------------------------
  3402. BitSize_t ReliabilityLayer::GetMaxDatagramSizeExcludingMessageHeaderBits(void)
  3403. {
  3404. return BYTES_TO_BITS(GetMaxDatagramSizeExcludingMessageHeaderBytes());
  3405. }
  3406. //-------------------------------------------------------------------------------------------------------
  3407. void ReliabilityLayer::InitHeapWeights(void)
  3408. {
  3409. for (int priorityLevel=0; priorityLevel < NUMBER_OF_PRIORITIES; priorityLevel++)
  3410. outgoingPacketBufferNextWeights[priorityLevel]=(1<<priorityLevel)*priorityLevel+priorityLevel;
  3411. }
  3412. //-------------------------------------------------------------------------------------------------------
  3413. reliabilityHeapWeightType ReliabilityLayer::GetNextWeight(int priorityLevel)
  3414. {
  3415. uint64_t next = outgoingPacketBufferNextWeights[priorityLevel];
  3416. if (outgoingPacketBuffer.Size()>0)
  3417. {
  3418. int peekPL = outgoingPacketBuffer.Peek()->priority;
  3419. reliabilityHeapWeightType weight = outgoingPacketBuffer.PeekWeight();
  3420. reliabilityHeapWeightType min = weight - (1<<peekPL)*peekPL+peekPL;
  3421. if (next<min)
  3422. next=min + (1<<priorityLevel)*priorityLevel+priorityLevel;
  3423. outgoingPacketBufferNextWeights[priorityLevel]=next+(1<<priorityLevel)*(priorityLevel+1)+priorityLevel;
  3424. }
  3425. else
  3426. {
  3427. InitHeapWeights();
  3428. }
  3429. return next;
  3430. }
  3431. //-------------------------------------------------------------------------------------------------------
  3432. // #if defined(RELIABILITY_LAYER_NEW_UNDEF_ALLOCATING_QUEUE)
  3433. // #pragma pop_macro("new")
  3434. // #undef RELIABILITY_LAYER_NEW_UNDEF_ALLOCATING_QUEUE
  3435. // #endif
  3436. #ifdef _MSC_VER
  3437. #pragma warning( pop )
  3438. #endif
粤ICP备19079148号