OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Network2ResChunkData Class Reference

#include <C4Network2Res.h>

Inheritance diagram for C4Network2ResChunkData:
[legend]
Collaboration diagram for C4Network2ResChunkData:
[legend]

Classes

struct  ChunkRange
 

Public Member Functions

 C4Network2ResChunkData ()
 
 C4Network2ResChunkData (const C4Network2ResChunkData &Data2)
 
 ~C4Network2ResChunkData () override
 
C4Network2ResChunkDataoperator= (const C4Network2ResChunkData &Data2)
 
int32_t getChunkCnt () const
 
int32_t getPresentChunkCnt () const
 
int32_t getPresentPercent () const
 
bool isComplete () const
 
void SetIncomplete (int32_t iChunkCnt)
 
void SetComplete (int32_t iChunkCnt)
 
void AddChunk (int32_t iChunk)
 
void AddChunkRange (int32_t iStart, int32_t iLength)
 
void Merge (const C4Network2ResChunkData &Data2)
 
void Clear ()
 
int32_t GetChunkToRetrieve (const C4Network2ResChunkData &Available, int32_t iLoadingCnt, int32_t *pLoading) const
 
void CompileFunc (StdCompiler *pComp) override
 
C4NetIOPacket pack (const C4NetIO::addr_t &addr=C4NetIO::addr_t()) const
 
C4NetIOPacket pack (uint8_t cStatus, const C4NetIO::addr_t &addr=C4NetIO::addr_t()) const
 
void unpack (const C4NetIOPacket &Pkt, char *pStatus=nullptr)
 

Protected Member Functions

bool MergeRanges (ChunkRange *pRange)
 
void GetNegative (C4Network2ResChunkData &Target) const
 
int32_t getPresentChunk (int32_t iNr) const
 

Protected Attributes

int32_t iChunkCnt {0}
 
int32_t iPresentChunkCnt {0}
 
ChunkRangepChunkRanges {nullptr}
 
int32_t iChunkRangeCnt {0}
 

Detailed Description

Definition at line 135 of file C4Network2Res.h.


Class Documentation

struct C4Network2ResChunkData::ChunkRange

Definition at line 147 of file C4Network2Res.h.

Collaboration diagram for C4Network2ResChunkData::ChunkRange:
[legend]
Class Members
int32_t Length
ChunkRange * Next
int32_t Start

Constructor & Destructor Documentation

C4Network2ResChunkData::C4Network2ResChunkData ( )
default
C4Network2ResChunkData::C4Network2ResChunkData ( const C4Network2ResChunkData Data2)

Definition at line 148 of file C4Network2Res.cpp.

References Merge().

149  : C4PacketBase(Data2),
150  iChunkCnt(Data2.getChunkCnt())
151 {
152  // add ranges
153  Merge(Data2);
154 }
int32_t getChunkCnt() const
void Merge(const C4Network2ResChunkData &Data2)

Here is the call graph for this function:

C4Network2ResChunkData::~C4Network2ResChunkData ( )
override

Definition at line 156 of file C4Network2Res.cpp.

References Clear().

157 {
158  Clear();
159 }

Here is the call graph for this function:

Member Function Documentation

void C4Network2ResChunkData::AddChunk ( int32_t  iChunk)

Definition at line 188 of file C4Network2Res.cpp.

References AddChunkRange().

Referenced by C4Network2ResChunk::AddTo(), and GetChunkToRetrieve().

189 {
190  AddChunkRange(iChunk, 1);
191 }
void AddChunkRange(int32_t iStart, int32_t iLength)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResChunkData::AddChunkRange ( int32_t  iStart,
int32_t  iLength 
)

Definition at line 193 of file C4Network2Res.cpp.

References iChunkCnt, iChunkRangeCnt, iPresentChunkCnt, C4Network2ResChunkData::ChunkRange::Length, MergeRanges(), C4Network2ResChunkData::ChunkRange::Next, pChunkRanges, and C4Network2ResChunkData::ChunkRange::Start.

Referenced by AddChunk(), GetNegative(), and Merge().

194 {
195  // security
196  if (iStart < 0 || iStart + iLength > iChunkCnt || iLength <= 0) return;
197  // find position
198  ChunkRange *pRange, *pPrev;
199  for (pRange = pChunkRanges, pPrev = nullptr; pRange; pPrev = pRange, pRange = pRange->Next)
200  if (pRange->Start >= iStart)
201  break;
202  // create new
203  ChunkRange *pNew = new ChunkRange;
204  pNew->Start = iStart; pNew->Length = iLength;
205  // add to list
206  pNew->Next = pRange;
207  (pPrev ? pPrev->Next : pChunkRanges) = pNew;
208  // counts
209  iPresentChunkCnt += iLength; iChunkRangeCnt++;
210  // check merges
211  if (pPrev && MergeRanges(pPrev))
212  while (MergeRanges(pPrev)) {}
213  else
214  while (MergeRanges(pNew)) {}
215 }
ChunkRange * pChunkRanges
bool MergeRanges(ChunkRange *pRange)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResChunkData::Clear ( )

Definition at line 226 of file C4Network2Res.cpp.

References iChunkCnt, iChunkRangeCnt, iPresentChunkCnt, C4Network2ResChunkData::ChunkRange::Next, and pChunkRanges.

Referenced by C4Network2Res::Clear(), CompileFunc(), SetComplete(), SetIncomplete(), and ~C4Network2ResChunkData().

227 {
229  // remove all ranges
230  while (pChunkRanges)
231  {
232  ChunkRange *pDelete = pChunkRanges;
233  pChunkRanges = pDelete->Next;
234  delete pDelete;
235  }
236 }
ChunkRange * pChunkRanges

Here is the caller graph for this function:

void C4Network2ResChunkData::CompileFunc ( StdCompiler pComp)
overridevirtual

Implements C4PacketBase.

Definition at line 305 of file C4Network2Res.cpp.

References Clear(), StdCompiler::excCorrupt(), iChunkCnt, iChunkRangeCnt, StdCompiler::isDeserializer(), C4Network2ResChunkData::ChunkRange::Length, mkIntPackAdapt(), mkNamingAdapt(), StdCompiler::Name(), StdCompiler::NameEnd(), C4Network2ResChunkData::ChunkRange::Next, pChunkRanges, StdCompiler::SEP_PART2, StdCompiler::Separator(), C4Network2ResChunkData::ChunkRange::Start, and StdCompiler::Value().

306 {
307  bool deserializing = pComp->isDeserializer();
308  if (deserializing) Clear();
309  // Data
310  pComp->Value(mkNamingAdapt(mkIntPackAdapt(iChunkCnt), "ChunkCnt", 0));
311  pComp->Value(mkNamingAdapt(mkIntPackAdapt(iChunkRangeCnt), "ChunkRangeCnt", 0));
312  // Ranges
313  if (!pComp->Name("Ranges"))
314  pComp->excCorrupt("ResChunk ranges expected!");
315  ChunkRange *pRange = nullptr;
316  for (int32_t i = 0; i < iChunkRangeCnt; i++)
317  {
318  // Create new range / go to next range
319  if (deserializing)
320  pRange = (pRange ? pRange->Next : pChunkRanges) = new ChunkRange;
321  else
322  pRange = pRange ? pRange->Next : pChunkRanges;
323  // Separate
324  if (i) pComp->Separator();
325  // Compile range
326  pComp->Value(mkIntPackAdapt(pRange->Start));
328  pComp->Value(mkIntPackAdapt(pRange->Length));
329  }
330  // Terminate list
331  if (deserializing)
332  (pRange ? pRange->Next : pChunkRanges) = nullptr;
333  pComp->NameEnd();
334 }
virtual bool Separator(Sep eSep=SEP_SEP)
Definition: StdCompiler.h:119
ChunkRange * pChunkRanges
void excCorrupt(const char *szMessage,...)
Definition: StdCompiler.h:249
virtual bool Name(const char *szName)
Definition: StdCompiler.h:77
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53
StdIntPackAdapt< T > mkIntPackAdapt(T &rVal)
Definition: StdAdaptors.h:759
virtual void NameEnd(bool fBreak=false)
Definition: StdCompiler.h:78

Here is the call graph for this function:

int32_t C4Network2ResChunkData::getChunkCnt ( ) const
inline

Definition at line 152 of file C4Network2Res.h.

References iChunkCnt.

Referenced by Merge(), C4Network2Res::OnStatus(), and operator=().

152 { return iChunkCnt; }

Here is the caller graph for this function:

int32_t C4Network2ResChunkData::GetChunkToRetrieve ( const C4Network2ResChunkData Available,
int32_t  iLoadingCnt,
int32_t *  pLoading 
) const

Definition at line 238 of file C4Network2Res.cpp.

References AddChunk(), GetNegative(), getPresentChunk(), getPresentChunkCnt(), isComplete(), Merge(), and UnsyncedRandom().

Referenced by C4Network2Res::StartLoad().

239 {
240  // (this version is highly calculation-intensitive, yet the most satisfactory
241  // solution I could find)
242 
243  // find everything that should not be retrieved
244  C4Network2ResChunkData ChData; Available.GetNegative(ChData);
245  ChData.Merge(*this);
246  for (int32_t i = 0; i < iLoadingCnt; i++)
247  ChData.AddChunk(pLoading[i]);
248  // nothing to retrieve?
249  if (ChData.isComplete()) return -1;
250  // invert to get everything that should be retrieved
251  C4Network2ResChunkData ChData2; ChData.GetNegative(ChData2);
252  // select chunk (random)
253  int32_t iRetrieveChunk = UnsyncedRandom(ChData2.getPresentChunkCnt());
254  // return
255  return ChData2.getPresentChunk(iRetrieveChunk);
256 }
void GetNegative(C4Network2ResChunkData &Target) const
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
int32_t getPresentChunkCnt() const
void Merge(const C4Network2ResChunkData &Data2)
bool isComplete() const
int32_t getPresentChunk(int32_t iNr) const
void AddChunk(int32_t iChunk)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResChunkData::GetNegative ( C4Network2ResChunkData Target) const
protected

Definition at line 278 of file C4Network2Res.cpp.

References AddChunkRange(), iChunkCnt, C4Network2ResChunkData::ChunkRange::Next, pChunkRanges, and SetIncomplete().

Referenced by GetChunkToRetrieve().

279 {
280  // clear target
281  Target.SetIncomplete(iChunkCnt);
282  // add all ranges that are missing
283  int32_t iFreeStart = 0;
284  for (ChunkRange *pRange = pChunkRanges; pRange; pRange = pRange->Next)
285  {
286  // add range
287  Target.AddChunkRange(iFreeStart, pRange->Start - iFreeStart);
288  // safe new start
289  iFreeStart = pRange->Start + pRange->Length;
290  }
291  // add last range
292  Target.AddChunkRange(iFreeStart, iChunkCnt - iFreeStart);
293 }
ChunkRange * pChunkRanges
void AddChunkRange(int32_t iStart, int32_t iLength)
void SetIncomplete(int32_t iChunkCnt)

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t C4Network2ResChunkData::getPresentChunk ( int32_t  iNr) const
protected

Definition at line 295 of file C4Network2Res.cpp.

References C4Network2ResChunkData::ChunkRange::Next, and pChunkRanges.

Referenced by GetChunkToRetrieve().

296 {
297  for (ChunkRange *pRange = pChunkRanges; pRange; pRange = pRange->Next)
298  if (iNr < pRange->Length)
299  return iNr + pRange->Start;
300  else
301  iNr -= pRange->Length;
302  return -1;
303 }
ChunkRange * pChunkRanges

Here is the caller graph for this function:

int32_t C4Network2ResChunkData::getPresentChunkCnt ( ) const
inline

Definition at line 153 of file C4Network2Res.h.

References iPresentChunkCnt.

Referenced by GetChunkToRetrieve().

153 { return iPresentChunkCnt; }

Here is the caller graph for this function:

int32_t C4Network2ResChunkData::getPresentPercent ( ) const
inline

Definition at line 154 of file C4Network2Res.h.

References iChunkCnt, and iPresentChunkCnt.

Referenced by C4Network2Res::getPresentPercent().

154 { return iPresentChunkCnt * 100 / iChunkCnt; }

Here is the caller graph for this function:

bool C4Network2ResChunkData::isComplete ( ) const
inline

Definition at line 155 of file C4Network2Res.h.

References iChunkCnt, and iPresentChunkCnt.

Referenced by GetChunkToRetrieve(), and C4Network2Res::OnChunk().

Here is the caller graph for this function:

void C4Network2ResChunkData::Merge ( const C4Network2ResChunkData Data2)

Definition at line 217 of file C4Network2Res.cpp.

References AddChunkRange(), getChunkCnt(), iChunkCnt, C4Network2ResChunkData::ChunkRange::Next, and pChunkRanges.

Referenced by C4Network2ResChunkData(), GetChunkToRetrieve(), and operator=().

218 {
219  // must have same basis chunk count
220  assert(iChunkCnt == Data2.getChunkCnt());
221  // add ranges
222  for (ChunkRange *pRange = Data2.pChunkRanges; pRange; pRange = pRange->Next)
223  AddChunkRange(pRange->Start, pRange->Length);
224 }
ChunkRange * pChunkRanges
void AddChunkRange(int32_t iStart, int32_t iLength)
int32_t getChunkCnt() const

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2ResChunkData::MergeRanges ( ChunkRange pRange)
protected

Definition at line 258 of file C4Network2Res.cpp.

References iChunkRangeCnt, iPresentChunkCnt, C4Network2ResChunkData::ChunkRange::Length, C4Network2ResChunkData::ChunkRange::Next, and C4Network2ResChunkData::ChunkRange::Start.

Referenced by AddChunkRange().

259 {
260  // no next entry?
261  if (!pRange || !pRange->Next) return false;
262  // do merge?
263  ChunkRange *pNext = pRange->Next;
264  if (pRange->Start + pRange->Length < pNext->Start) return false;
265  // get overlap
266  int32_t iOverlap = std::min((pRange->Start + pRange->Length) - pNext->Start, pNext->Length);
267  // set new chunk range
268  pRange->Length += pNext->Length - iOverlap;
269  // remove range
270  pRange->Next = pNext->Next;
271  delete pNext;
272  // counts
273  iChunkRangeCnt--; iPresentChunkCnt -= iOverlap;
274  // ok
275  return true;
276 }

Here is the caller graph for this function:

C4Network2ResChunkData & C4Network2ResChunkData::operator= ( const C4Network2ResChunkData Data2)

Definition at line 161 of file C4Network2Res.cpp.

References getChunkCnt(), Merge(), and SetIncomplete().

162 {
163  // clear, merge
164  SetIncomplete(Data2.getChunkCnt());
165  Merge(Data2);
166  return *this;
167 }
int32_t getChunkCnt() const
void Merge(const C4Network2ResChunkData &Data2)
void SetIncomplete(int32_t iChunkCnt)

Here is the call graph for this function:

C4NetIOPacket C4PacketBase::pack ( const C4NetIO::addr_t addr = C4NetIO::addr_t()) const
inherited

Definition at line 149 of file C4Packet2.cpp.

Referenced by MkC4NetIOPacket().

150 {
151  return C4NetIOPacket(DecompileToBuf<StdCompilerBinWrite>(*this), addr);
152 }

Here is the caller graph for this function:

C4NetIOPacket C4PacketBase::pack ( uint8_t  cStatus,
const C4NetIO::addr_t addr = C4NetIO::addr_t() 
) const
inherited

Definition at line 154 of file C4Packet2.cpp.

References mkDecompileAdapt(), and mkInsertAdapt().

155 {
156  return C4NetIOPacket(DecompileToBuf<StdCompilerBinWrite>(mkInsertAdapt(mkDecompileAdapt(*this), cStatus)), addr);
157 }
StdDecompileAdapt< T > mkDecompileAdapt(const T &rValue)
Definition: StdAdaptors.h:153
StdInsertAdapt< T, I > mkInsertAdapt(T &&rObj, I &&rIns, bool fBefore=true)
Definition: StdAdaptors.h:437

Here is the call graph for this function:

void C4Network2ResChunkData::SetComplete ( int32_t  iChunkCnt)

Definition at line 176 of file C4Network2Res.cpp.

References Clear(), iChunkCnt, iPresentChunkCnt, C4Network2ResChunkData::ChunkRange::Length, C4Network2ResChunkData::ChunkRange::Next, pChunkRanges, and C4Network2ResChunkData::ChunkRange::Start.

Referenced by C4Network2Res::FinishDerive(), and C4Network2Res::GetStandalone().

177 {
178  Clear();
179  // set total chunk count
180  iPresentChunkCnt = iChunkCnt = inChunkCnt;
181  // create one range
182  ChunkRange *pRange = new ChunkRange;
183  pRange->Start = 0; pRange->Length = iChunkCnt;
184  pRange->Next = nullptr;
185  pChunkRanges = pRange;
186 }
ChunkRange * pChunkRanges

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResChunkData::SetIncomplete ( int32_t  iChunkCnt)

Definition at line 169 of file C4Network2Res.cpp.

References Clear(), and iChunkCnt.

Referenced by GetNegative(), operator=(), and C4Network2Res::SetLoad().

170 {
171  Clear();
172  // just set total chunk count
173  iChunkCnt = inChunkCnt;
174 }

Here is the call graph for this function:

Here is the caller graph for this function:

void C4PacketBase::unpack ( const C4NetIOPacket Pkt,
char *  pStatus = nullptr 
)
inherited

Definition at line 159 of file C4Packet2.cpp.

References C4NetIOPacket::getPBuf(), C4NetIOPacket::getRef(), and C4NetIOPacket::getStatus().

Referenced by C4IDPacket::C4IDPacket(), and C4Network2IO::HandlePacket().

160 {
161  if (pStatus) *pStatus = Pkt.getStatus();
162  CompileFromBuf<StdCompilerBinRead>(*this, pStatus ? Pkt.getPBuf() : Pkt.getRef());
163 }
StdBuf getPBuf() const
Definition: C4NetIO.h:320
C4NetIOPacket getRef() const
Definition: C4NetIO.h:323
uint8_t getStatus() const
Definition: C4NetIO.h:317

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

int32_t C4Network2ResChunkData::iChunkCnt {0}
protected
int32_t C4Network2ResChunkData::iChunkRangeCnt {0}
protected

Definition at line 149 of file C4Network2Res.h.

Referenced by AddChunkRange(), Clear(), CompileFunc(), and MergeRanges().

int32_t C4Network2ResChunkData::iPresentChunkCnt {0}
protected
ChunkRange* C4Network2ResChunkData::pChunkRanges {nullptr}
protected

The documentation for this class was generated from the following files: