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

#include <C4Network2Res.h>

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

Public Member Functions

 C4Network2ResChunk ()
 
 ~C4Network2ResChunk () override
 
int32_t getResID () const
 
uint32_t getChunkNr () const
 
bool Set (C4Network2Res *pRes, uint32_t iChunk)
 
bool AddTo (C4Network2Res *pRes, C4Network2IO *pIO) 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 Attributes

int32_t iResID
 
uint32_t iChunk
 
StdBuf Data
 

Detailed Description

Definition at line 305 of file C4Network2Res.h.

Constructor & Destructor Documentation

C4Network2ResChunk::C4Network2ResChunk ( )
default
C4Network2ResChunk::~C4Network2ResChunk ( )
overridedefault

Member Function Documentation

bool C4Network2ResChunk::AddTo ( C4Network2Res pRes,
C4Network2IO pIO 
) const

Definition at line 1205 of file C4Network2Res.cpp.

References C4Network2ResChunkData::AddChunk(), Application, C4Network2Res::Chunks, Data, C4Network2ResCore::getChunkSize(), C4Network2Res::getCore(), StdBuf::getData(), C4Network2ResCore::getFileName(), C4Network2ResCore::getFileSize(), C4Network2Res::getResID(), StdBuf::getSize(), iChunk, C4Application::InteractiveThread, iResID, C4Network2Res::OpenFileWrite(), and C4InteractiveThread::ThreadLogS().

Referenced by C4Network2Res::OnChunk().

1206 {
1207  assert(pRes); assert(pIO);
1208  const C4Network2ResCore &Core = pRes->getCore();
1209  // check
1210  if (iResID != pRes->getResID())
1211  {
1212 #ifdef C4NET2RES_DEBUG_LOG
1213  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): Resource ID mismatch!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID());
1214 #endif
1215  return false;
1216  }
1217  // calculate offset and size
1218  int32_t iOffset = iChunk * Core.getChunkSize();
1219  if (iOffset + Data.getSize() > Core.getFileSize())
1220  {
1221 #ifdef C4NET2RES_DEBUG_LOG
1222  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): Adding %d bytes at offset %d exceeds expected file size of %d!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID(), (int) Data.getSize(), (int) iOffset, (int) Core.getFileSize());
1223 #endif
1224  return false;
1225  }
1226  // open file
1227  int32_t f = pRes->OpenFileWrite();
1228  if (f == -1)
1229  {
1230 #ifdef C4NET2RES_DEBUG_LOG
1231  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): Open write file error: %s!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID(), strerror(errno));
1232 #endif
1233  return false;
1234  }
1235  // seek
1236  if (iOffset)
1237  if (lseek(f, iOffset, SEEK_SET) != iOffset)
1238  {
1239 #ifdef C4NET2RES_DEBUG_LOG
1240  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): lseek file error: %s!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID(), strerror(errno));
1241 #endif
1242  close(f);
1243  return false;
1244  }
1245  // write
1246  if (write(f, Data.getData(), Data.getSize()) != int32_t(Data.getSize()))
1247  {
1248 #ifdef C4NET2RES_DEBUG_LOG
1249  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): write error: %s!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID(), strerror(errno));
1250 #endif
1251  close(f);
1252  return false;
1253  }
1254  // ok, add chunks
1255  close(f);
1256  pRes->Chunks.AddChunk(iChunk);
1257  return true;
1258 }
uint32_t getChunkSize() const
Definition: C4Network2Res.h:95
const void * getData() const
Definition: StdBuf.h:99
uint32_t getFileSize() const
Definition: C4Network2Res.h:89
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
const char * getFileName() const
Definition: C4Network2Res.h:94
size_t getSize() const
Definition: StdBuf.h:101
const C4Network2ResCore & getCore() const
C4Network2ResChunkData Chunks
void AddChunk(int32_t iChunk)
int32_t OpenFileWrite()
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
int32_t getResID() const

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResChunk::CompileFunc ( StdCompiler pComp)
overridevirtual

Implements C4PacketBase.

Definition at line 1260 of file C4Network2Res.cpp.

References Data, iChunk, iResID, mkNamingAdapt(), and StdCompiler::Value().

1261 {
1262  // pack header
1263  pComp->Value(mkNamingAdapt(iResID, "ResID", -1));
1264  pComp->Value(mkNamingAdapt(iChunk, "Chunk", ~0U));
1265  // Data
1266  pComp->Value(mkNamingAdapt(Data, "Data"));
1267 }
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
void Value(const T &rStruct)
Definition: StdCompiler.h:161

Here is the call graph for this function:

uint32_t C4Network2ResChunk::getChunkNr ( ) const
inline

Definition at line 318 of file C4Network2Res.h.

References iChunk.

Referenced by C4Network2Res::OnChunk().

318 { return iChunk; }

Here is the caller graph for this function:

int32_t C4Network2ResChunk::getResID ( ) const
inline

Definition at line 317 of file C4Network2Res.h.

References iResID.

Referenced by C4Network2Res::OnChunk().

317 { return iResID; }

Here is the caller 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:

bool C4Network2ResChunk::Set ( C4Network2Res pRes,
uint32_t  iChunk 
)

Definition at line 1177 of file C4Network2Res.cpp.

References C4NetResChunkSize, Data, C4Network2ResCore::getChunkSize(), C4Network2Res::getCore(), C4Network2Res::getFile(), C4Network2ResCore::getFileSize(), C4Network2Res::getResID(), iChunk, iResID, iSize, LogF(), C4Network2Res::OpenFileRead(), and StdBuf::Take().

Referenced by C4Network2Res::SendChunk().

1178 {
1179  const C4Network2ResCore &Core = pRes->getCore();
1180  iResID = pRes->getResID();
1181  iChunk = inChunk;
1182  // calculate offset and size
1183  int32_t iOffset = iChunk * Core.getChunkSize(),
1184  iSize = std::min<int32_t>(Core.getFileSize() - iOffset, C4NetResChunkSize);
1185  if (iSize < 0) { LogF("Network: could not get chunk from offset %d from resource file %s: File size is only %d!", iOffset, pRes->getFile(), Core.getFileSize()); return false; }
1186  // open file
1187  int32_t f = pRes->OpenFileRead();
1188  if (f == -1) { LogF("Network: could not open resource file %s!", pRes->getFile()); return false; }
1189  // seek
1190  if (iOffset)
1191  if (lseek(f, iOffset, SEEK_SET) != iOffset)
1192  { close(f); LogF("Network: could not read resource file %s!", pRes->getFile()); return false; }
1193  // read chunk of data
1194  char *pBuf = (char *) malloc(iSize);
1195  if (read(f, pBuf, iSize) != iSize)
1196  { free(pBuf); close(f); LogF("Network: could not read resource file %s!", pRes->getFile()); return false; }
1197  // set
1198  Data.Take(pBuf, iSize);
1199  // close
1200  close(f);
1201  // ok
1202  return true;
1203 }
uint32_t getChunkSize() const
Definition: C4Network2Res.h:95
void Take(void *pnData, size_t inSize)
Definition: StdBuf.h:124
int32_t OpenFileRead()
uint32_t getFileSize() const
Definition: C4Network2Res.h:89
const char * getFile() const
const C4Network2ResCore & getCore() const
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:250
int32_t getResID() const
int iSize
Definition: TstC4NetIO.cpp:32
const uint32_t C4NetResChunkSize
Definition: C4Network2Res.h:28

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

StdBuf C4Network2ResChunk::Data
protected

Definition at line 314 of file C4Network2Res.h.

Referenced by AddTo(), CompileFunc(), and Set().

uint32_t C4Network2ResChunk::iChunk
protected

Definition at line 313 of file C4Network2Res.h.

Referenced by AddTo(), CompileFunc(), getChunkNr(), and Set().

int32_t C4Network2ResChunk::iResID
protected

Definition at line 312 of file C4Network2Res.h.

Referenced by AddTo(), CompileFunc(), getResID(), and Set().


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