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

#include <C4Network2Res.h>

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

Public Member Functions

 C4Network2ResList ()
 
virtual ~C4Network2ResList ()
 
bool Init (int32_t iClientID, C4Network2IO *pIOClass)
 
void SetLocalID (int32_t iClientID)
 
C4Network2Res::Ref getRefRes (int32_t iResID)
 
C4Network2Res::Ref getRefRes (const char *szFile, bool fLocalOnly=false)
 
C4Network2Res::Ref getRefNextRes (int32_t iResID)
 
void Add (C4Network2Res *pRes)
 
C4Network2Res::Ref AddByFile (const char *strFilePath, bool fTemp, C4Network2ResType eType, int32_t iResID=-1, const char *szResName=nullptr, bool fAllowUnloadable=false)
 
C4Network2Res::Ref AddByGroup (C4Group *pGrp, bool fTemp, C4Network2ResType eType, int32_t iResID=-1, const char *szResName=nullptr, bool fAllowUnloadable=false)
 
C4Network2Res::Ref AddByCore (const C4Network2ResCore &Core, bool fLoad=true)
 
C4Network2Res::Ref AddLoad (const C4Network2ResCore &Core)
 
void RemoveAtClient (int32_t iClientID)
 
void Clear ()
 
bool SendDiscover (C4Network2IOConnection *pTo=nullptr)
 
void OnClientConnect (C4Network2IOConnection *pConn)
 
void HandlePacket (char cStatus, const C4PacketBase *pPacket, C4Network2IOConnection *pConn)
 
void OnTimer ()
 
void OnShareFree (CStdCSecEx *pCSec)
 
C4Network2IOgetIOClass ()
 

Protected Member Functions

int32_t nextResID ()
 
C4Network2ResgetRes (int32_t iResID)
 
C4Network2ResgetRes (const char *szFile, bool fLocalOnly)
 
void OnResComplete (C4Network2Res *pRes)
 
bool CreateNetworkFolder ()
 
bool FindTempResFileName (const char *szFilename, char *pTarget)
 

Protected Attributes

C4Network2RespFirst
 
CStdCSecEx ResListCSec
 
CStdCSec ResListAddCSec
 
int32_t iClientID
 
int32_t iNextResID
 
CStdCSec ResIDCSec
 
int32_t iLastDiscover
 
int32_t iLastStatus
 
C4Network2IOpIO
 

Friends

class C4Network2Res
 
class C4Network2
 

Detailed Description

Definition at line 326 of file C4Network2Res.h.

Constructor & Destructor Documentation

C4Network2ResList::C4Network2ResList ( )

Definition at line 1295 of file C4Network2Res.cpp.

1296  : pFirst(nullptr),
1297  ResListCSec(this),
1298  iClientID(-1),
1299  iNextResID((-1) << 16),
1300  iLastDiscover(0), iLastStatus(0),
1301  pIO(nullptr)
1302 {
1303 
1304 }
C4Network2Res * pFirst
C4Network2IO * pIO
CStdCSecEx ResListCSec
C4Network2ResList::~C4Network2ResList ( )
virtual

Definition at line 1306 of file C4Network2Res.cpp.

References Clear().

1307 {
1308  Clear();
1309 }

Here is the call graph for this function:

Member Function Documentation

void C4Network2ResList::Add ( C4Network2Res pRes)

Definition at line 1396 of file C4Network2Res.cpp.

References C4Network2Res::AddRef(), pFirst, C4Network2Res::pNext, ResListAddCSec, and ResListCSec.

Referenced by AddByCore(), AddByFile(), AddByGroup(), AddLoad(), and C4Network2Res::Derive().

1397 {
1398  // get locks
1399  CStdShareLock ResListLock(&ResListCSec);
1400  CStdLock ResListAddLock(&ResListAddCSec);
1401  // reference
1402  pRes->AddRef();
1403  // add
1404  pRes->pNext = pFirst;
1405  pFirst = pRes;
1406 }
C4Network2Res * pFirst
CStdCSec ResListAddCSec
CStdCSecEx ResListCSec
C4Network2Res * pNext

Here is the call graph for this function:

Here is the caller graph for this function:

C4Network2Res::Ref C4Network2ResList::AddByCore ( const C4Network2ResCore Core,
bool  fLoad = true 
)

Definition at line 1459 of file C4Network2Res.cpp.

References Add(), AddLoad(), Application, C4Network2Res, C4Network2Res::Ref::Clear(), C4Network2Res::getCore(), C4Network2ResCore::getFileName(), C4Network2ResCore::getID(), getRefRes(), C4Application::InteractiveThread, C4Network2ResCore::isLoadable(), C4Network2Res::SetByCore(), and C4InteractiveThread::ThreadLogS().

Referenced by C4Network2::HandleJoinData(), C4GameRes::Load(), C4PlayerInfo::LoadResource(), C4ControlJoinPlayer::PreExecute(), and C4Network2::RetrieveRes().

1460 {
1461  // already in list?
1462  C4Network2Res::Ref pRes = getRefRes(Core.getID());
1463  if (pRes) return pRes;
1464 #ifdef C4NET2RES_LOAD_ALL
1465  // load without check (if possible)
1466  if (Core.isLoadable()) return AddLoad(Core);
1467 #endif
1468  // create new
1469  pRes = new C4Network2Res(this);
1470  // try set by core
1471  if (!pRes->SetByCore(Core, true))
1472  {
1473  pRes.Clear();
1474  // try load (if specified)
1475  return fLoad ? AddLoad(Core) : nullptr;
1476  }
1477  // log
1478  Application.InteractiveThread.ThreadLogS("Network: Found identical %s. Not loading.", pRes->getCore().getFileName());
1479  // add to list
1480  Add(pRes);
1481  // ok
1482  return pRes;
1483 }
friend class C4Network2Res
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
bool isLoadable() const
Definition: C4Network2Res.h:88
void Add(C4Network2Res *pRes)
C4Network2Res::Ref AddLoad(const C4Network2ResCore &Core)
bool SetByCore(const C4Network2ResCore &nCore, bool fSilent=false, const char *szAsFilename=nullptr, int32_t iRecursion=0)
const char * getFileName() const
Definition: C4Network2Res.h:94
int32_t getID() const
Definition: C4Network2Res.h:86
const C4Network2ResCore & getCore() const
C4Network2Res::Ref getRefRes(int32_t iResID)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45

Here is the call graph for this function:

Here is the caller graph for this function:

C4Network2Res::Ref C4Network2ResList::AddByFile ( const char *  strFilePath,
bool  fTemp,
C4Network2ResType  eType,
int32_t  iResID = -1,
const char *  szResName = nullptr,
bool  fAllowUnloadable = false 
)

Definition at line 1408 of file C4Network2Res.cpp.

References Add(), C4Network2Res, getRefRes(), C4Network2Res::GetStandalone(), Log(), nextResID(), NRT_System, and C4Network2Res::SetByFile().

Referenced by C4Network2::CreateDynamic(), and C4GameRes::Publish().

1409 {
1410  // already in list?
1411  C4Network2Res::Ref pRes = getRefRes(strFilePath);
1412  if (pRes) return pRes;
1413  // get resource ID
1414  if (iResID < 0) iResID = nextResID();
1415  if (iResID < 0) { Log("AddByFile: no more resource IDs available!"); return nullptr; }
1416  // create new
1417  pRes = new C4Network2Res(this);
1418  // initialize
1419  if (!pRes->SetByFile(strFilePath, fTemp, eType, iResID, szResName)) { return nullptr; }
1420  // create standalone for non-system files
1421  // system files shouldn't create a standalone; they should never be marked loadable!
1422  if (eType != NRT_System)
1423  if (!pRes->GetStandalone(nullptr, 0, true, fAllowUnloadable))
1424  if (!fAllowUnloadable)
1425  {
1426  delete pRes;
1427  return nullptr;
1428  }
1429  // add to list
1430  Add(pRes);
1431  return pRes;
1432 }
friend class C4Network2Res
bool SetByFile(const char *strFilePath, bool fTemp, C4Network2ResType eType, int32_t iResID, const char *szResName=nullptr, bool fSilent=false)
void Add(C4Network2Res *pRes)
C4Network2Res::Ref getRefRes(int32_t iResID)
bool GetStandalone(char *pTo, int32_t iMaxL, bool fSetOfficial, bool fAllowUnloadable=false, bool fSilent=false)
bool Log(const char *szMessage)
Definition: C4Log.cpp:195

Here is the call graph for this function:

Here is the caller graph for this function:

C4Network2Res::Ref C4Network2ResList::AddByGroup ( C4Group pGrp,
bool  fTemp,
C4Network2ResType  eType,
int32_t  iResID = -1,
const char *  szResName = nullptr,
bool  fAllowUnloadable = false 
)

Definition at line 1434 of file C4Network2Res.cpp.

References Add(), C4Network2Res, C4Network2Res::GetStandalone(), Log(), nextResID(), and C4Network2Res::SetByGroup().

Referenced by C4PlayerInfo::LoadFromLocalFile().

1435 {
1436  // get resource ID
1437  if (iResID < 0) iResID = nextResID();
1438  if (iResID < 0) { Log("AddByGroup: no more resource IDs available!"); return nullptr; }
1439  // create new
1440  C4Network2Res::Ref pRes = new C4Network2Res(this);
1441  // initialize
1442  if (!pRes->SetByGroup(pGrp, fTemp, eType, iResID, szResName))
1443  {
1444  delete pRes;
1445  return nullptr;
1446  }
1447  // create standalone
1448  if (!pRes->GetStandalone(nullptr, 0, true, fAllowUnloadable))
1449  if (!fAllowUnloadable)
1450  {
1451  delete pRes;
1452  return nullptr;
1453  }
1454  // add to list
1455  Add(pRes);
1456  return pRes;
1457 }
friend class C4Network2Res
void Add(C4Network2Res *pRes)
bool SetByGroup(C4Group *pGrp, bool fTemp, C4Network2ResType eType, int32_t iResID, const char *szResName=nullptr, bool fSilent=false)
bool GetStandalone(char *pTo, int32_t iMaxL, bool fSetOfficial, bool fAllowUnloadable=false, bool fSilent=false)
bool Log(const char *szMessage)
Definition: C4Log.cpp:195

Here is the call graph for this function:

Here is the caller graph for this function:

C4Network2Res::Ref C4Network2ResList::AddLoad ( const C4Network2ResCore Core)

Definition at line 1485 of file C4Network2Res.cpp.

References Add(), Application, C4Network2Res, C4Network2ResCore::getFileName(), C4Application::InteractiveThread, C4Network2ResCore::isLoadable(), C4Network2Res::SetLoad(), C4InteractiveThread::ThreadLog(), and C4InteractiveThread::ThreadLogS().

Referenced by AddByCore().

1486 {
1487  // marked unloadable by creator?
1488  if (!Core.isLoadable())
1489  {
1490  // show error msg
1491  Application.InteractiveThread.ThreadLog("Network: Cannot load %s (marked unloadable)", Core.getFileName());
1492  return nullptr;
1493  }
1494  // create new
1495  C4Network2Res::Ref pRes = new C4Network2Res(this);
1496  // initialize
1497  pRes->SetLoad(Core);
1498  // log
1499  Application.InteractiveThread.ThreadLogS("Network: loading %s...", Core.getFileName());
1500  // add to list
1501  Add(pRes);
1502  return pRes;
1503 }
friend class C4Network2Res
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
bool isLoadable() const
Definition: C4Network2Res.h:88
void Add(C4Network2Res *pRes)
bool SetLoad(const C4Network2ResCore &nCore)
const char * getFileName() const
Definition: C4Network2Res.h:94
bool ThreadLog(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResList::Clear ( )

Definition at line 1513 of file C4Network2Res.cpp.

References C4ClientIDUnknown, iClientID, iLastDiscover, iLastStatus, pFirst, C4Network2Res::pNext, and ResListCSec.

Referenced by C4Network2::Clear(), Init(), and ~C4Network2ResList().

1514 {
1515  CStdShareLock ResListLock(&ResListCSec);
1516  for (C4Network2Res *pRes = pFirst; pRes; pRes = pRes->pNext)
1517  {
1518  pRes->Remove();
1519  pRes->iLastReqTime = 0;
1520  }
1522  iLastDiscover = iLastStatus = 0;
1523 }
C4Network2Res * pFirst
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
CStdCSecEx ResListCSec
C4Network2Res * pNext

Here is the caller graph for this function:

bool C4Network2ResList::CreateNetworkFolder ( )
protected

Definition at line 1696 of file C4Network2Res.cpp.

References _MAX_PATH, AppendBackslash(), C4Config::AtNetworkPath(), Config, CreatePath(), DirectoryExists(), LogFatal(), C4Config::Network, SCopy(), TruncateBackslash(), and C4ConfigNetwork::WorkPath.

Referenced by Init().

1697 {
1698  // get network path without trailing backslash
1699  char szNetworkPath[_MAX_PATH+1];
1700  SCopy(Config.AtNetworkPath(""), szNetworkPath, _MAX_PATH);
1701  TruncateBackslash(szNetworkPath);
1702  // but make sure that the configured path has one
1704  // does not exist?
1705  if (!DirectoryExists(szNetworkPath))
1706  {
1707  if (!CreatePath(szNetworkPath))
1708  { LogFatal("Network: could not create network path!"); return false; }
1709  return true;
1710  }
1711  return true;
1712 }
C4Config Config
Definition: C4Config.cpp:831
void TruncateBackslash(char *szFilename)
Definition: StdFile.cpp:276
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
bool CreatePath(const std::string &path)
Definition: StdFile.cpp:642
#define _MAX_PATH
void AppendBackslash(char *szFilename)
Definition: StdFile.cpp:267
const char * AtNetworkPath(const char *szFilename)
Definition: C4Config.cpp:545
C4ConfigNetwork Network
Definition: C4Config.h:256
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:230
char WorkPath[CFG_MaxString+1]
Definition: C4Config.h:146
bool DirectoryExists(const char *szFilename)
Definition: StdFile.cpp:694

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2ResList::FindTempResFileName ( const char *  szFilename,
char *  pTarget 
)
protected

Definition at line 1714 of file C4Network2Res.cpp.

References _MAX_PATH, C4Config::AtNetworkPath(), Config, GetExtension(), GetFilename(), ItemExists(), SAppend(), and SCopy().

Referenced by C4Network2::CreateDynamic(), C4Network2Res::Derive(), C4Network2Res::GetStandalone(), C4Network2Res::OptimizeStandalone(), C4Network2::RetrieveScenario(), and C4Network2Res::SetLoad().

1715 {
1716  char safeFilename[_MAX_PATH];
1717  char* safePos = safeFilename;
1718  while (*szFilename)
1719  {
1720  if ((*szFilename >= 'a' && *szFilename <= 'z') ||
1721  (*szFilename >= 'A' && *szFilename <= 'Z') ||
1722  (*szFilename >= '0' && *szFilename <= '9') ||
1723  (*szFilename == '.') || (*szFilename == '/'))
1724  *safePos = *szFilename;
1725  else
1726  *safePos = '_';
1727 
1728  ++safePos;
1729  ++szFilename;
1730  }
1731  *safePos = 0;
1732  szFilename = safeFilename;
1733 
1734  // create temporary file
1735  SCopy(Config.AtNetworkPath(GetFilename(szFilename)), pTarget, _MAX_PATH);
1736  // file name is free?
1737  if (!ItemExists(pTarget)) return true;
1738  // find another file name
1739  char szFileMask[_MAX_PATH+1];
1740  SCopy(pTarget, szFileMask, GetExtension(pTarget)-1-pTarget);
1741  SAppend("_%d", szFileMask, _MAX_PATH);
1742  SAppend(GetExtension(pTarget)-1, szFileMask, _MAX_PATH);
1743  for (int32_t i = 2; i < 1000; i++)
1744  {
1745  snprintf(pTarget, _MAX_PATH, szFileMask, i);
1746  // doesn't exist?
1747  if (!ItemExists(pTarget))
1748  return true;
1749  }
1750  // not found
1751  return false;
1752 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
C4Config Config
Definition: C4Config.cpp:831
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
#define _MAX_PATH
const char * AtNetworkPath(const char *szFilename)
Definition: C4Config.cpp:545
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131
bool ItemExists(const char *szItemName)
Definition: StdFile.h:75

Here is the call graph for this function:

Here is the caller graph for this function:

C4Network2IO* C4Network2ResList::getIOClass ( )
inline

Definition at line 387 of file C4Network2Res.h.

References pIO.

Referenced by C4Network2Res::FinishDerive(), C4Network2Res::OnChunk(), C4Network2Res::SendChunk(), C4Network2Res::SendStatus(), and C4Network2Res::StartLoad().

387 { return pIO; }
C4Network2IO * pIO

Here is the caller graph for this function:

C4Network2Res::Ref C4Network2ResList::getRefNextRes ( int32_t  iResID)

Definition at line 1385 of file C4Network2Res.cpp.

References C4Network2Res::getResID(), pFirst, C4Network2Res::pNext, and ResListCSec.

Referenced by C4Network2ResDlg::Update().

1386 {
1387  CStdShareLock ResListLock(&ResListCSec);
1388  C4Network2Res *pRes = nullptr;
1389  for (C4Network2Res *pCur = pFirst; pCur; pCur = pCur->pNext)
1390  if (!pCur->isRemoved() && pCur->getResID() >= iResID)
1391  if (!pRes || pRes->getResID() > pCur->getResID())
1392  pRes = pCur;
1393  return pRes;
1394 }
C4Network2Res * pFirst
CStdCSecEx ResListCSec
C4Network2Res * pNext
int32_t getResID() const

Here is the call graph for this function:

Here is the caller graph for this function:

C4Network2Res::Ref C4Network2ResList::getRefRes ( int32_t  iResID)

Definition at line 1373 of file C4Network2Res.cpp.

References getRes(), and ResListCSec.

Referenced by AddByCore(), AddByFile(), C4PlayerInfo::LoadFromLocalFile(), C4ControlJoinPlayer::PreExecute(), C4ControlJoinPlayer::PreRec(), C4MessageInput::ProcessCommand(), C4Network2::RemoveDynamic(), C4Network2::RetrieveRes(), and C4Player::Save().

1374 {
1375  CStdShareLock ResListLock(&ResListCSec);
1376  return getRes(iResID);
1377 }
C4Network2Res * getRes(int32_t iResID)
CStdCSecEx ResListCSec

Here is the call graph for this function:

Here is the caller graph for this function:

C4Network2Res::Ref C4Network2ResList::getRefRes ( const char *  szFile,
bool  fLocalOnly = false 
)

Definition at line 1379 of file C4Network2Res.cpp.

References getRes(), and ResListCSec.

1380 {
1381  CStdShareLock ResListLock(&ResListCSec);
1382  return getRes(szFile, fLocalOnly);
1383 }
C4Network2Res * getRes(int32_t iResID)
CStdCSecEx ResListCSec

Here is the call graph for this function:

C4Network2Res * C4Network2ResList::getRes ( int32_t  iResID)
protected

Definition at line 1353 of file C4Network2Res.cpp.

References pFirst, C4Network2Res::pNext, and ResListCSec.

Referenced by getRefRes(), HandlePacket(), and nextResID().

1354 {
1355  CStdShareLock ResListLock(&ResListCSec);
1356  for (C4Network2Res *pCur = pFirst; pCur; pCur = pCur->pNext)
1357  if (pCur->getResID() == iResID)
1358  return pCur;
1359  return nullptr;
1360 }
C4Network2Res * pFirst
CStdCSecEx ResListCSec
C4Network2Res * pNext

Here is the caller graph for this function:

C4Network2Res * C4Network2ResList::getRes ( const char *  szFile,
bool  fLocalOnly 
)
protected

Definition at line 1362 of file C4Network2Res.cpp.

References iClientID, pFirst, C4Network2Res::pNext, ResListCSec, and SEqual().

1363 {
1364  CStdShareLock ResListLock(&ResListCSec);
1365  for (C4Network2Res *pCur = pFirst; pCur; pCur = pCur->pNext)
1366  if (!pCur->isAnonymous())
1367  if (SEqual(pCur->getFile(), szFile))
1368  if (!fLocalOnly || pCur->getResClient()==iClientID)
1369  return pCur;
1370  return nullptr;
1371 }
C4Network2Res * pFirst
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:97
CStdCSecEx ResListCSec
C4Network2Res * pNext

Here is the call graph for this function:

void C4Network2ResList::HandlePacket ( char  cStatus,
const C4PacketBase pPacket,
C4Network2IOConnection pConn 
)

Definition at line 1531 of file C4Network2Res.cpp.

References C4Network2IOConnection::getClientID(), GETPKT, getRes(), C4Network2Res::IsBinaryCompatible(), C4Network2IOConnection::isOpen(), C4Network2Res::OnChunk(), C4Network2Res::OnStatus(), pFirst, PID_NetResData, PID_NetResDerive, PID_NetResDis, PID_NetResReq, PID_NetResStat, C4Network2Res::pNext, ResListCSec, and C4Network2Res::SendChunk().

Referenced by C4Network2IO::CallHandlers().

1532 {
1533  // security
1534  if (!pConn) return;
1535 
1536 #define GETPKT(type, name) \
1537  assert(pPacket); const type &name = \
1538  static_cast<const type &>(*pPacket);
1539 
1540  switch (cStatus)
1541  {
1542 
1543  case PID_NetResDis: // resource discover
1544  {
1545  if (!pConn->isOpen()) break;
1547  // search matching resources
1548  CStdShareLock ResListLock(&ResListCSec);
1549  for (C4Network2Res *pRes = pFirst; pRes; pRes = pRes->pNext)
1550  if (Pkt.isIDPresent(pRes->getResID()))
1551  // must be binary compatible
1552  if (pRes->IsBinaryCompatible())
1553  pRes->OnDiscover(pConn);
1554  }
1555  break;
1556 
1557  case PID_NetResStat: // resource status
1558  {
1559  if (!pConn->isOpen()) break;
1560  GETPKT(C4PacketResStatus, Pkt);
1561  // matching resource?
1562  CStdShareLock ResListLock(&ResListCSec);
1563  C4Network2Res *pRes = getRes(Pkt.getResID());
1564  // present / being loaded? call handler
1565  if (pRes)
1566  pRes->OnStatus(Pkt.getChunks(), pConn);
1567  }
1568  break;
1569 
1570  case PID_NetResDerive: // resource derive
1571  {
1572  GETPKT(C4Network2ResCore, Core);
1573  if (Core.getDerID() < 0) break;
1574  // Check if there is a anonymous derived resource with matching parent.
1575  CStdShareLock ResListLock(&ResListCSec);
1576  for (C4Network2Res *pRes = pFirst; pRes; pRes = pRes->pNext)
1577  if (pRes->isAnonymous() && pRes->getCore().getDerID() == Core.getDerID())
1578  pRes->FinishDerive(Core);
1579  }
1580  break;
1581 
1582  case PID_NetResReq: // resource request
1583  {
1584  GETPKT(C4PacketResRequest, Pkt);
1585  // find resource
1586  CStdShareLock ResListLock(&ResListCSec);
1587  C4Network2Res *pRes = getRes(Pkt.getReqID());
1588  // send requested chunk
1589  if (pRes && pRes->IsBinaryCompatible()) pRes->SendChunk(Pkt.getReqChunk(), pConn->getClientID());
1590  }
1591  break;
1592 
1593  case PID_NetResData: // a chunk of data is coming in
1594  {
1595  GETPKT(C4Network2ResChunk, Chunk);
1596  // find resource
1597  CStdShareLock ResListLock(&ResListCSec);
1598  C4Network2Res *pRes = getRes(Chunk.getResID());
1599  // send requested chunk
1600  if (pRes) pRes->OnChunk(Chunk);
1601  }
1602  break;
1603  }
1604 #undef GETPKT
1605 }
C4Network2Res * pFirst
bool isAnonymous() const
bool SendChunk(uint32_t iChunk, int32_t iToClient)
#define GETPKT(type, name)
bool IsBinaryCompatible()
const C4Network2ResCore & getCore() const
int32_t getDerID() const
Definition: C4Network2Res.h:87
int getClientID() const
Definition: C4Network2IO.h:271
C4Network2Res * getRes(int32_t iResID)
CStdCSecEx ResListCSec
void OnStatus(const C4Network2ResChunkData &rChunkData, C4Network2IOConnection *pBy)
C4Network2Res * pNext
bool isOpen() const
Definition: C4Network2IO.h:285
void OnChunk(const C4Network2ResChunk &rChunk)

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2ResList::Init ( int32_t  iClientID,
C4Network2IO pIOClass 
)

Definition at line 1311 of file C4Network2Res.cpp.

References Clear(), CreateNetworkFolder(), iClientID, iNextResID, pIO, and SetLocalID().

Referenced by C4Network2::InitHost().

1312 {
1313  // clear old list
1314  Clear();
1315  // safe IO class
1316  pIO = pIOClass;
1317  // set client id
1318  iNextResID = iClientID = 0;
1319  SetLocalID(inClientID);
1320  // create network path
1321  if (!CreateNetworkFolder()) return false;
1322  // ok
1323  return true;
1324 }
C4Network2IO * pIO
void SetLocalID(int32_t iClientID)

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t C4Network2ResList::nextResID ( )
protected

Definition at line 1341 of file C4Network2Res.cpp.

References getRes(), iClientID, iNextResID, and ResIDCSec.

Referenced by AddByFile(), AddByGroup(), and C4Network2Res::FinishDerive().

1342 {
1343  CStdLock ResIDLock(&ResIDCSec);
1344  assert(iNextResID >= (iClientID << 16));
1345  if (iNextResID >= ((iClientID+1) << 16) - 1)
1346  iNextResID = std::max<int32_t>(0, iClientID) << 16;
1347  // find free
1348  while (getRes(iNextResID))
1349  iNextResID++;
1350  return iNextResID++;
1351 }
C4Network2Res * getRes(int32_t iResID)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResList::OnClientConnect ( C4Network2IOConnection pConn)

Definition at line 1525 of file C4Network2Res.cpp.

References SendDiscover().

Referenced by C4Network2::OnClientConnect().

1526 {
1527  // discover resources
1528  SendDiscover(pConn);
1529 }
bool SendDiscover(C4Network2IOConnection *pTo=nullptr)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResList::OnResComplete ( C4Network2Res pRes)
protected

Definition at line 1688 of file C4Network2Res.cpp.

References Application, Control, C4Network2Res::getCore(), C4Network2ResCore::getFileName(), C4Application::InteractiveThread, C4GameControl::Network, C4GameControlNetwork::OnResComplete(), and C4InteractiveThread::ThreadLogS().

Referenced by C4Network2Res::EndLoad().

1689 {
1690  // log (network thread -> ThreadLog)
1691  Application.InteractiveThread.ThreadLogS("Network: %s received.", pRes->getCore().getFileName());
1692  // call handler (ctrl might wait for this resource)
1694 }
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
const char * getFileName() const
Definition: C4Network2Res.h:94
const C4Network2ResCore & getCore() const
C4GameControl Control
C4GameControlNetwork Network
Definition: C4GameControl.h:67
void OnResComplete(C4Network2Res *pRes)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResList::OnShareFree ( CStdCSecEx pCSec)
virtual

Implements CStdCSecExCallback.

Definition at line 1641 of file C4Network2Res.cpp.

References C4NetResDeleteTime, pFirst, and ResListCSec.

1642 {
1643  if (pCSec == &ResListCSec)
1644  {
1645  // remove entries
1646  for (C4Network2Res *pRes = pFirst, *pNext, *pPrev = nullptr; pRes; pRes = pNext)
1647  {
1648  pNext = pRes->pNext;
1649  if (pRes->isRemoved() && (!pRes->getLastReqTime() || difftime(time(nullptr), pRes->getLastReqTime()) > C4NetResDeleteTime))
1650  {
1651  // unlink
1652  (pPrev ? pPrev->pNext : pFirst) = pNext;
1653  // remove
1654  pRes->pNext = nullptr;
1655  pRes->DelRef();
1656  }
1657  else
1658  pPrev = pRes;
1659  }
1660  }
1661 }
C4Network2Res * pFirst
const int32_t C4NetResDeleteTime
Definition: C4Network2Res.h:35
CStdCSecEx ResListCSec
void C4Network2ResList::OnTimer ( )

Definition at line 1607 of file C4Network2Res.cpp.

References C4NetResDiscoverInterval, C4NetResStatusInterval, C4Network2Res::DoLoad(), iLastDiscover, iLastStatus, C4Network2Res::isDirty(), C4Network2Res::isLoading(), C4Network2Res::isRemoved(), C4Network2Res::NeedsDiscover(), pFirst, C4Network2Res::pNext, C4Network2Res::Remove(), ResListCSec, SendDiscover(), and C4Network2Res::SendStatus().

Referenced by C4Network2IO::Execute().

1608 {
1609  CStdShareLock ResListLock(&ResListCSec);
1610  C4Network2Res *pRes;
1611  // do loads, check timeouts
1612  for (pRes = pFirst; pRes; pRes = pRes->pNext)
1613  if (pRes->isLoading() && !pRes->isRemoved())
1614  if (!pRes->DoLoad())
1615  pRes->Remove();
1616  // discovery time?
1617  if (!iLastDiscover || difftime(time(nullptr), iLastDiscover) >= C4NetResDiscoverInterval)
1618  {
1619  // needed?
1620  bool fSendDiscover = false;
1621  for (C4Network2Res *pRes = pFirst; pRes; pRes = pRes->pNext)
1622  if (pRes->isLoading() && !pRes->isRemoved())
1623  fSendDiscover |= pRes->NeedsDiscover();
1624  // send
1625  if (fSendDiscover)
1626  SendDiscover();
1627  }
1628  // status update?
1629  if (!iLastStatus || difftime(time(nullptr), iLastStatus) >= C4NetResStatusInterval)
1630  {
1631  // any?
1632  bool fStatusUpdates = false;
1633  for (pRes = pFirst; pRes; pRes = pRes->pNext)
1634  if (pRes->isDirty() && !pRes->isRemoved())
1635  fStatusUpdates |= pRes->SendStatus();
1636  // set time accordingly
1637  iLastStatus = fStatusUpdates ? time(nullptr) : 0;
1638  }
1639 }
C4Network2Res * pFirst
const int32_t C4NetResStatusInterval
Definition: C4Network2Res.h:32
bool isLoading() const
bool SendStatus(C4Network2IOConnection *pTo=nullptr)
bool SendDiscover(C4Network2IOConnection *pTo=nullptr)
bool isRemoved() const
const int32_t C4NetResDiscoverInterval
Definition: C4Network2Res.h:31
bool isDirty() const
CStdCSecEx ResListCSec
C4Network2Res * pNext

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResList::RemoveAtClient ( int32_t  iClientID)

Definition at line 1505 of file C4Network2Res.cpp.

References iClientID, pFirst, C4Network2Res::pNext, and ResListCSec.

1506 {
1507  CStdShareLock ResListLock(&ResListCSec);
1508  for (C4Network2Res *pRes = pFirst; pRes; pRes = pRes->pNext)
1509  if (pRes->getResClient() == iClientID)
1510  pRes->Remove();
1511 }
C4Network2Res * pFirst
CStdCSecEx ResListCSec
C4Network2Res * pNext
bool C4Network2ResList::SendDiscover ( C4Network2IOConnection pTo = nullptr)

Definition at line 1663 of file C4Network2Res.cpp.

References C4PacketResDiscover::AddDisID(), C4Network2IO::BroadcastMsg(), CStdShareLock::Clear(), C4PacketResDiscover::getDisIDCnt(), iLastDiscover, MkC4NetIOPacket(), pFirst, PID_NetResDis, pIO, C4Network2Res::pNext, ResListCSec, and C4Network2IOConnection::Send().

Referenced by OnClientConnect(), and OnTimer().

1664 {
1665  // make packet
1666  C4PacketResDiscover Pkt;
1667  // add special retrieves
1668  CStdShareLock ResListLock(&ResListCSec);
1669  for (C4Network2Res *pRes = pFirst; pRes; pRes = pRes->pNext)
1670  if (!pRes->isRemoved())
1671  if (pRes->isLoading())
1672  Pkt.AddDisID(pRes->getResID());
1673  ResListLock.Clear();
1674  // empty?
1675  if (!Pkt.getDisIDCnt()) return false;
1676  // broadcast?
1677  if (!pTo)
1678  {
1679  // save time
1680  iLastDiscover = time(nullptr);
1681  // send
1683  }
1684  else
1685  return pTo->Send(MkC4NetIOPacket(PID_NetResDis, Pkt));
1686 }
C4Network2Res * pFirst
bool AddDisID(int32_t iID)
Definition: C4Packet2.cpp:568
bool Send(const C4NetIOPacket &rPkt)
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
bool BroadcastMsg(const C4NetIOPacket &rPkt)
C4Network2IO * pIO
CStdCSecEx ResListCSec
int32_t getDisIDCnt() const
C4Network2Res * pNext

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2ResList::SetLocalID ( int32_t  iClientID)

Definition at line 1326 of file C4Network2Res.cpp.

References iClientID, iNextResID, pFirst, C4Network2Res::pNext, ResIDCSec, and ResListCSec.

Referenced by C4Network2::HandleJoinData(), and Init().

1327 {
1328  CStdLock ResIDLock(&ResIDCSec);
1329  int32_t iOldClientID = iClientID;
1330  int32_t iIDDiff = (inClientID - iClientID) << 16;
1331  // set new counter
1332  iClientID = inClientID;
1333  iNextResID += iIDDiff;
1334  // change resource ids
1335  CStdLock ResListLock(&ResListCSec);
1336  for (C4Network2Res *pRes = pFirst; pRes; pRes = pRes->pNext)
1337  if (pRes->getResClient() == iOldClientID)
1338  pRes->ChangeID(pRes->getResID() + iIDDiff);
1339 }
C4Network2Res * pFirst
CStdCSecEx ResListCSec
C4Network2Res * pNext

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class C4Network2
friend

Definition at line 329 of file C4Network2Res.h.

friend class C4Network2Res
friend

Definition at line 328 of file C4Network2Res.h.

Referenced by AddByCore(), AddByFile(), AddByGroup(), and AddLoad().

Member Data Documentation

int32_t C4Network2ResList::iClientID
protected

Definition at line 340 of file C4Network2Res.h.

Referenced by Clear(), getRes(), Init(), nextResID(), RemoveAtClient(), and SetLocalID().

int32_t C4Network2ResList::iLastDiscover
protected

Definition at line 344 of file C4Network2Res.h.

Referenced by Clear(), OnTimer(), and SendDiscover().

int32_t C4Network2ResList::iLastStatus
protected

Definition at line 344 of file C4Network2Res.h.

Referenced by Clear(), and OnTimer().

int32_t C4Network2ResList::iNextResID
protected

Definition at line 340 of file C4Network2Res.h.

Referenced by Init(), nextResID(), and SetLocalID().

C4Network2Res* C4Network2ResList::pFirst
protected
C4Network2IO* C4Network2ResList::pIO
protected

Definition at line 347 of file C4Network2Res.h.

Referenced by getIOClass(), Init(), and SendDiscover().

CStdCSec C4Network2ResList::ResIDCSec
protected

Definition at line 341 of file C4Network2Res.h.

Referenced by nextResID(), and SetLocalID().

CStdCSec C4Network2ResList::ResListAddCSec
protected

Definition at line 338 of file C4Network2Res.h.

Referenced by Add().

CStdCSecEx C4Network2ResList::ResListCSec
protected

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