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 ()
 
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
 
virtual void CompileFunc (StdCompiler *pComp)
 
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
 
int32_t iPresentChunkCnt
 
ChunkRangepChunkRanges
 
int32_t iChunkRangeCnt
 

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 ( )

Definition at line 158 of file C4Network2Res.cpp.

159  : iChunkCnt(0), iPresentChunkCnt(0),
160  pChunkRanges(nullptr), iChunkRangeCnt(0)
161 {
162 
163 }
ChunkRange * pChunkRanges
C4Network2ResChunkData::C4Network2ResChunkData ( const C4Network2ResChunkData Data2)

Definition at line 165 of file C4Network2Res.cpp.

References Merge().

166  : C4PacketBase(Data2),
168  pChunkRanges(nullptr), iChunkRangeCnt(0)
169 {
170  // add ranges
171  Merge(Data2);
172 }
ChunkRange * pChunkRanges
int32_t getChunkCnt() const
void Merge(const C4Network2ResChunkData &Data2)

Here is the call graph for this function:

C4Network2ResChunkData::~C4Network2ResChunkData ( )

Definition at line 174 of file C4Network2Res.cpp.

References Clear().

175 {
176  Clear();
177 }

Here is the call graph for this function:

Member Function Documentation

void C4Network2ResChunkData::AddChunk ( int32_t  iChunk)

Definition at line 206 of file C4Network2Res.cpp.

References AddChunkRange().

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

207 {
208  AddChunkRange(iChunk, 1);
209 }
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 211 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().

212 {
213  // security
214  if (iStart < 0 || iStart + iLength > iChunkCnt || iLength <= 0) return;
215  // find position
216  ChunkRange *pRange, *pPrev;
217  for (pRange = pChunkRanges, pPrev = nullptr; pRange; pPrev = pRange, pRange = pRange->Next)
218  if (pRange->Start >= iStart)
219  break;
220  // create new
221  ChunkRange *pNew = new ChunkRange;
222  pNew->Start = iStart; pNew->Length = iLength;
223  // add to list
224  pNew->Next = pRange;
225  (pPrev ? pPrev->Next : pChunkRanges) = pNew;
226  // counts
227  iPresentChunkCnt += iLength; iChunkRangeCnt++;
228  // check merges
229  if (pPrev && MergeRanges(pPrev))
230  while (MergeRanges(pPrev)) {}
231  else
232  while (MergeRanges(pNew)) {}
233 }
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 244 of file C4Network2Res.cpp.

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

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

245 {
247  // remove all ranges
248  while (pChunkRanges)
249  {
250  ChunkRange *pDelete = pChunkRanges;
251  pChunkRanges = pDelete->Next;
252  delete pDelete;
253  }
254 }
ChunkRange * pChunkRanges

Here is the caller graph for this function:

void C4Network2ResChunkData::CompileFunc ( StdCompiler pComp)
virtual

Implements C4PacketBase.

Definition at line 323 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().

324 {
325  bool deserializing = pComp->isDeserializer();
326  if (deserializing) Clear();
327  // Data
328  pComp->Value(mkNamingAdapt(mkIntPackAdapt(iChunkCnt), "ChunkCnt", 0));
329  pComp->Value(mkNamingAdapt(mkIntPackAdapt(iChunkRangeCnt), "ChunkRangeCnt", 0));
330  // Ranges
331  if (!pComp->Name("Ranges"))
332  pComp->excCorrupt("ResChunk ranges expected!");
333  ChunkRange *pRange = nullptr;
334  for (int32_t i = 0; i < iChunkRangeCnt; i++)
335  {
336  // Create new range / go to next range
337  if (deserializing)
338  pRange = (pRange ? pRange->Next : pChunkRanges) = new ChunkRange;
339  else
340  pRange = pRange ? pRange->Next : pChunkRanges;
341  // Separate
342  if (i) pComp->Separator();
343  // Compile range
344  pComp->Value(mkIntPackAdapt(pRange->Start));
346  pComp->Value(mkIntPackAdapt(pRange->Length));
347  }
348  // Terminate list
349  if (deserializing)
350  (pRange ? pRange->Next : pChunkRanges) = nullptr;
351  pComp->NameEnd();
352 }
virtual bool Separator(Sep eSep=SEP_SEP)
Definition: StdCompiler.h:129
ChunkRange * pChunkRanges
void excCorrupt(const char *szMessage,...)
Definition: StdCompiler.h:259
virtual bool Name(const char *szName)
Definition: StdCompiler.h:87
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:93
void Value(const T &rStruct)
Definition: StdCompiler.h:171
virtual bool isDeserializer()
Definition: StdCompiler.h:63
StdIntPackAdapt< T > mkIntPackAdapt(T &rVal)
Definition: StdAdaptors.h:757
virtual void NameEnd(bool fBreak=false)
Definition: StdCompiler.h:88

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 256 of file C4Network2Res.cpp.

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

Referenced by C4Network2Res::StartLoad().

257 {
258  // (this version is highly calculation-intensitive, yet the most satisfactory
259  // solution I could find)
260 
261  // find everything that should not be retrieved
262  C4Network2ResChunkData ChData; Available.GetNegative(ChData);
263  ChData.Merge(*this);
264  for (int32_t i = 0; i < iLoadingCnt; i++)
265  ChData.AddChunk(pLoading[i]);
266  // nothing to retrieve?
267  if (ChData.isComplete()) return -1;
268  // invert to get everything that should be retrieved
269  C4Network2ResChunkData ChData2; ChData.GetNegative(ChData2);
270  // select chunk (random)
271  int32_t iRetrieveChunk = UnsyncedRandom(ChData2.getPresentChunkCnt());
272  // return
273  return ChData2.getPresentChunk(iRetrieveChunk);
274 }
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 296 of file C4Network2Res.cpp.

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

Referenced by GetChunkToRetrieve().

297 {
298  // clear target
299  Target.SetIncomplete(iChunkCnt);
300  // add all ranges that are missing
301  int32_t iFreeStart = 0;
302  for (ChunkRange *pRange = pChunkRanges; pRange; pRange = pRange->Next)
303  {
304  // add range
305  Target.AddChunkRange(iFreeStart, pRange->Start - iFreeStart);
306  // safe new start
307  iFreeStart = pRange->Start + pRange->Length;
308  }
309  // add last range
310  Target.AddChunkRange(iFreeStart, iChunkCnt - iFreeStart);
311 }
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 313 of file C4Network2Res.cpp.

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

Referenced by GetChunkToRetrieve().

314 {
315  for (ChunkRange *pRange = pChunkRanges; pRange; pRange = pRange->Next)
316  if (iNr < pRange->Length)
317  return iNr + pRange->Start;
318  else
319  iNr -= pRange->Length;
320  return -1;
321 }
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.

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.

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

Here is the caller graph for this function:

void C4Network2ResChunkData::Merge ( const C4Network2ResChunkData Data2)

Definition at line 235 of file C4Network2Res.cpp.

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

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

236 {
237  // must have same basis chunk count
238  assert(iChunkCnt == Data2.getChunkCnt());
239  // add ranges
240  for (ChunkRange *pRange = Data2.pChunkRanges; pRange; pRange = pRange->Next)
241  AddChunkRange(pRange->Start, pRange->Length);
242 }
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 276 of file C4Network2Res.cpp.

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

Referenced by AddChunkRange().

277 {
278  // no next entry?
279  if (!pRange || !pRange->Next) return false;
280  // do merge?
281  ChunkRange *pNext = pRange->Next;
282  if (pRange->Start + pRange->Length < pNext->Start) return false;
283  // get overlap
284  int32_t iOverlap = std::min((pRange->Start + pRange->Length) - pNext->Start, pNext->Length);
285  // set new chunk range
286  pRange->Length += pNext->Length - iOverlap;
287  // remove range
288  pRange->Next = pNext->Next;
289  delete pNext;
290  // counts
291  iChunkRangeCnt--; iPresentChunkCnt -= iOverlap;
292  // ok
293  return true;
294 }

Here is the caller graph for this function:

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

Definition at line 179 of file C4Network2Res.cpp.

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

180 {
181  // clear, merge
182  SetIncomplete(Data2.getChunkCnt());
183  Merge(Data2);
184  return *this;
185 }
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 156 of file C4Packet2.cpp.

Referenced by MkC4NetIOPacket().

157 {
158  return C4NetIOPacket(DecompileToBuf<StdCompilerBinWrite>(*this), addr);
159 }

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 161 of file C4Packet2.cpp.

References mkDecompileAdapt(), and mkInsertAdapt().

162 {
163  return C4NetIOPacket(DecompileToBuf<StdCompilerBinWrite>(mkInsertAdapt(mkDecompileAdapt(*this), cStatus)), addr);
164 }
StdDecompileAdapt< T > mkDecompileAdapt(const T &rValue)
Definition: StdAdaptors.h:154
StdInsertAdapt< T, I > mkInsertAdapt(T &&rObj, I &&rIns, bool fBefore=true)
Definition: StdAdaptors.h:435

Here is the call graph for this function:

void C4Network2ResChunkData::SetComplete ( int32_t  iChunkCnt)

Definition at line 194 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().

195 {
196  Clear();
197  // set total chunk count
198  iPresentChunkCnt = iChunkCnt = inChunkCnt;
199  // create one range
200  ChunkRange *pRange = new ChunkRange;
201  pRange->Start = 0; pRange->Length = iChunkCnt;
202  pRange->Next = nullptr;
203  pChunkRanges = pRange;
204 }
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 187 of file C4Network2Res.cpp.

References Clear(), and iChunkCnt.

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

188 {
189  Clear();
190  // just set total chunk count
191  iChunkCnt = inChunkCnt;
192 }

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 166 of file C4Packet2.cpp.

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

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

167 {
168  if (pStatus) *pStatus = Pkt.getStatus();
169  CompileFromBuf<StdCompilerBinRead>(*this, pStatus ? Pkt.getPBuf() : Pkt.getRef());
170 }
StdBuf getPBuf() const
Definition: C4NetIO.h:321
C4NetIOPacket getRef() const
Definition: C4NetIO.h:324
uint8_t getStatus() const
Definition: C4NetIO.h:318

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

int32_t C4Network2ResChunkData::iChunkCnt
protected
int32_t C4Network2ResChunkData::iChunkRangeCnt
protected

Definition at line 149 of file C4Network2Res.h.

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

int32_t C4Network2ResChunkData::iPresentChunkCnt
protected

Definition at line 144 of file C4Network2Res.h.

Referenced by AddChunkRange(), Clear(), getPresentChunkCnt(), MergeRanges(), and SetComplete().

ChunkRange* C4Network2ResChunkData::pChunkRanges
protected

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