OpenClonk
C4NetIOTCP::Peer Class Reference

#include <C4NetIO.h>

Collaboration diagram for C4NetIOTCP::Peer:
[legend]

Public Member Functions

 Peer (const C4NetIO::addr_t &naddr, SOCKET nsock, C4NetIOTCP *pnParent)
 
 ~Peer ()
 
const C4NetIO::addr_tGetAddr () const
 
SOCKET GetSocket () const
 
int GetIRate () const
 
int GetORate () const
 
bool Send (const C4NetIOPacket &rPacket)
 
bool Send ()
 
void * GetRecvBuf (int iSize)
 
void OnRecv (int iSize)
 
void Close ()
 
bool Open () const
 
bool doBroadcast () const
 
bool hasWaitingData () const
 
void SetBroadcast (bool fSet)
 
void ClearStatistics ()
 

Public Attributes

PeerNext
 

Protected Attributes

C4NetIOTCP *const pParent
 
C4NetIO::addr_t addr
 
SOCKET sock
 
StdBuf IBuf
 
StdBuf OBuf
 
int iIBufUsage
 
int iIRate
 
int iORate
 
bool fOpen
 
bool fDoBroadcast
 
CStdCSec ICSec
 
CStdCSec OCSec
 

Static Protected Attributes

static const unsigned int iTCPHeaderSize = 28 + 24
 
static const unsigned int iMinIBufSize = 8192
 

Detailed Description

Definition at line 399 of file C4NetIO.h.

Constructor & Destructor Documentation

◆ Peer()

C4NetIOTCP::Peer::Peer ( const C4NetIO::addr_t naddr,
SOCKET  nsock,
C4NetIOTCP pnParent 
)

Definition at line 1735 of file C4NetIO.cpp.

1736  : pParent(pnParent),
1737  addr(naddr), sock(nsock),
1738  iIBufUsage(0), iIRate(0), iORate(0),
1739  fOpen(true), fDoBroadcast(false), Next(nullptr)
1740 {
1741 }
bool fDoBroadcast
Definition: C4NetIO.h:422
C4NetIOTCP *const pParent
Definition: C4NetIO.h:409
C4NetIO::addr_t addr
Definition: C4NetIO.h:411

◆ ~Peer()

C4NetIOTCP::Peer::~Peer ( )

Definition at line 1743 of file C4NetIO.cpp.

References Close().

1744 {
1745  // close socket
1746  Close();
1747 }
Here is the call graph for this function:

Member Function Documentation

◆ ClearStatistics()

void C4NetIOTCP::Peer::ClearStatistics ( )

Definition at line 1879 of file C4NetIO.cpp.

References ICSec, iIRate, iORate, and OCSec.

1880 {
1881  CStdLock ILock(&ICSec); CStdLock OLock(&OCSec);
1882  iIRate = iORate = 0;
1883 }
CStdCSec ICSec
Definition: C4NetIO.h:424
CStdCSec OCSec
Definition: C4NetIO.h:424

◆ Close()

void C4NetIOTCP::Peer::Close ( )

Definition at line 1864 of file C4NetIO.cpp.

References StdBuf::Clear(), closesocket, fOpen, IBuf, ICSec, iIBufUsage, iIRate, iORate, OBuf, OCSec, and sock.

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::Close(), and ~Peer().

1865 {
1866  CStdLock ILock(&ICSec); CStdLock OLock(&OCSec);
1867  if (!fOpen) return;
1868  // close socket
1869  closesocket(sock);
1870  // set flag
1871  fOpen = false;
1872  // clear buffers
1873  IBuf.Clear(); OBuf.Clear();
1874  iIBufUsage = 0;
1875  // reset statistics
1876  iIRate = iORate = 0;
1877 }
void Clear()
Definition: StdBuf.h:190
CStdCSec ICSec
Definition: C4NetIO.h:424
#define closesocket
Definition: C4NetIO.cpp:46
CStdCSec OCSec
Definition: C4NetIO.h:424
Here is the call graph for this function:
Here is the caller graph for this function:

◆ doBroadcast()

bool C4NetIOTCP::Peer::doBroadcast ( ) const
inline

Definition at line 444 of file C4NetIO.h.

444 { return fDoBroadcast; }
bool fDoBroadcast
Definition: C4NetIO.h:422

◆ GetAddr()

const C4NetIO::addr_t& C4NetIOTCP::Peer::GetAddr ( ) const
inline

Definition at line 427 of file C4NetIO.h.

Referenced by C4NetIOTCP::Close().

427 { return addr; }
C4NetIO::addr_t addr
Definition: C4NetIO.h:411
Here is the caller graph for this function:

◆ GetIRate()

int C4NetIOTCP::Peer::GetIRate ( ) const
inline

Definition at line 429 of file C4NetIO.h.

Referenced by C4NetIOTCP::GetConnStatistic().

429 { return iIRate; }
Here is the caller graph for this function:

◆ GetORate()

int C4NetIOTCP::Peer::GetORate ( ) const
inline

Definition at line 430 of file C4NetIO.h.

References C4NetIO::Close(), iSize, and C4NetIO::Send().

Referenced by C4NetIOTCP::GetConnStatistic().

430 { return iORate; }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetRecvBuf()

void * C4NetIOTCP::Peer::GetRecvBuf ( int  iSize)

Definition at line 1805 of file C4NetIO.cpp.

References StdBuf::getMPtr(), StdBuf::getSize(), IBuf, ICSec, iIBufUsage, iMinIBufSize, iSize, and StdBuf::SetSize().

1806 {
1807  CStdLock ILock(&ICSec);
1808  // Enlarge input buffer?
1809  size_t iIBufSize = std::max<size_t>(iMinIBufSize, IBuf.getSize());
1810  while ((size_t)(iIBufUsage + iSize) > iIBufSize)
1811  iIBufSize *= 2;
1812  if (iIBufSize != IBuf.getSize())
1813  IBuf.SetSize(iIBufSize);
1814  // Return the appropriate part of the input buffer
1815  return IBuf.getMPtr(iIBufUsage);
1816 }
static const unsigned int iMinIBufSize
Definition: C4NetIO.h:407
void SetSize(size_t inSize)
Definition: StdBuf.h:204
size_t getSize() const
Definition: StdBuf.h:101
CStdCSec ICSec
Definition: C4NetIO.h:424
void * getMPtr(size_t i)
Definition: StdBuf.h:105
int iSize
Definition: TstC4NetIO.cpp:32
Here is the call graph for this function:

◆ GetSocket()

SOCKET C4NetIOTCP::Peer::GetSocket ( ) const
inline

Definition at line 428 of file C4NetIO.h.

428 { return sock; }

◆ hasWaitingData()

bool C4NetIOTCP::Peer::hasWaitingData ( ) const
inline

Definition at line 446 of file C4NetIO.h.

References StdBuf::isNull().

446 { return !OBuf.isNull(); }
bool isNull() const
Definition: StdBuf.h:98
Here is the call graph for this function:

◆ OnRecv()

void C4NetIOTCP::Peer::OnRecv ( int  iSize)

Definition at line 1818 of file C4NetIO.cpp.

References addr, StdBuf::getPart(), StdBuf::getSize(), IBuf, ICSec, iIBufUsage, iIRate, iMinIBufSize, iSize, iTCPHeaderSize, StdBuf::Move(), pParent, StdBuf::SetSize(), StdBuf::Shrink(), and C4NetIOTCP::UnpackPacket().

1819 {
1820  CStdLock ILock(&ICSec);
1821  // increase input rate and input buffer usage
1823  iIBufUsage += iSize;
1824  // a prior call to GetRecvBuf should have ensured this
1825  assert(static_cast<size_t>(iIBufUsage) <= IBuf.getSize());
1826  // read packets
1827  size_t iPos = 0, iPacketPos;
1828  while ((iPacketPos = iPos) < (size_t)iIBufUsage)
1829  {
1830  // Try to unpack a packet
1831  StdBuf IBufPart = IBuf.getPart(iPos, iIBufUsage - iPos);
1832  int32_t iBytes = pParent->UnpackPacket(IBufPart, addr);
1833  // Could not unpack?
1834  if (!iBytes)
1835  break;
1836  // Advance
1837  iPos += iBytes;
1838  }
1839  // data left?
1840  if (iPacketPos < (size_t) iIBufUsage)
1841  {
1842  // no packet read?
1843  if (!iPacketPos) return;
1844  // move data
1845  IBuf.Move(iPacketPos, IBuf.getSize() - iPacketPos);
1846  iIBufUsage -= iPacketPos;
1847  // shrink buffer
1848  size_t iIBufSize = IBuf.getSize();
1849  while ((size_t) iIBufUsage <= iIBufSize / 2)
1850  iIBufSize /= 2;
1851  if (iIBufSize != IBuf.getSize())
1852  IBuf.Shrink(iPacketPos);
1853  }
1854  else
1855  {
1856  // the buffer is empty
1857  iIBufUsage = 0;
1858  // shrink buffer to minimum
1859  if (IBuf.getSize() > iMinIBufSize)
1861  }
1862 }
Definition: StdBuf.h:29
static const unsigned int iMinIBufSize
Definition: C4NetIO.h:407
void SetSize(size_t inSize)
Definition: StdBuf.h:204
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107
static const unsigned int iTCPHeaderSize
Definition: C4NetIO.h:406
size_t getSize() const
Definition: StdBuf.h:101
CStdCSec ICSec
Definition: C4NetIO.h:424
void Shrink(size_t iShrink)
Definition: StdBuf.h:180
virtual size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &Addr)
Definition: C4NetIO.cpp:1705
C4NetIOTCP *const pParent
Definition: C4NetIO.h:409
C4NetIO::addr_t addr
Definition: C4NetIO.h:411
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:159
int iSize
Definition: TstC4NetIO.cpp:32
Here is the call graph for this function:

◆ Open()

bool C4NetIOTCP::Peer::Open ( ) const
inline

Definition at line 442 of file C4NetIO.h.

Referenced by C4NetIOTCP::GetConnStatistic(), and C4NetIOTCP::OnShareFree().

442 { return fOpen; }
Here is the caller graph for this function:

◆ Send() [1/2]

bool C4NetIOTCP::Peer::Send ( const C4NetIOPacket rPacket)

Definition at line 1751 of file C4NetIO.cpp.

References StdBuf::isNull(), OBuf, OCSec, C4NetIOTCP::PackPacket(), pParent, and Send().

Referenced by C4NetIOTCP::Send().

1752 {
1753  CStdLock OLock(&OCSec);
1754 
1755  // already data pending to be sent? try to sent them first (empty buffer)
1756  if (!OBuf.isNull()) Send();
1757  bool fSend = OBuf.isNull();
1758 
1759  // pack packet
1760  pParent->PackPacket(rPacket, OBuf);
1761 
1762  // (try to) send
1763  return fSend ? Send() : true;
1764 }
virtual void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf)
Definition: C4NetIO.cpp:1688
bool isNull() const
Definition: StdBuf.h:98
C4NetIOTCP *const pParent
Definition: C4NetIO.h:409
CStdCSec OCSec
Definition: C4NetIO.h:424
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Send() [2/2]

bool C4NetIOTCP::Peer::Send ( )

Definition at line 1766 of file C4NetIO.cpp.

References StdBuf::Clear(), StdBuf::getSize(), HaveWouldBlockError(), iORate, StdBuf::isNull(), iTCPHeaderSize, StdBuf::Move(), OBuf, OCSec, pParent, C4NetIO::SetError(), StdBuf::Shrink(), sock, SOCKET_ERROR, and C4NetIOTCP::UnBlock().

Referenced by Send().

1767 {
1768  CStdLock OLock(&OCSec);
1769  if (OBuf.isNull()) return true;
1770 
1771  // send as much as possibile
1772  int iBytesSent;
1773  if ((iBytesSent = ::send(sock, getBufPtr<char>(OBuf), OBuf.getSize(), 0)) == SOCKET_ERROR)
1774  if (!HaveWouldBlockError())
1775  {
1776  pParent->SetError("send failed", true);
1777  return false;
1778  }
1779 
1780  // nothin sent?
1781  if (iBytesSent == SOCKET_ERROR || !iBytesSent) return true;
1782 
1783  // increase output rate
1784  iORate += iBytesSent + iTCPHeaderSize;
1785 
1786  // data remaining?
1787  if (unsigned(iBytesSent) < OBuf.getSize())
1788  {
1789  // Shrink buffer
1790  OBuf.Move(iBytesSent, OBuf.getSize() - iBytesSent);
1791  OBuf.Shrink(iBytesSent);
1792 #ifndef STDSCHEDULER_USE_EVENTS
1793  // Unblock parent so the FD-list can be refreshed
1794  pParent->UnBlock();
1795 #endif
1796  }
1797  else
1798  // just delete buffer
1799  OBuf.Clear();
1800 
1801  // ok
1802  return true;
1803 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
void Clear()
Definition: StdBuf.h:190
virtual void UnBlock()
Definition: C4NetIO.cpp:1343
static const unsigned int iTCPHeaderSize
Definition: C4NetIO.h:406
size_t getSize() const
Definition: StdBuf.h:101
bool isNull() const
Definition: StdBuf.h:98
void Shrink(size_t iShrink)
Definition: StdBuf.h:180
bool HaveWouldBlockError()
Definition: C4NetIO.cpp:203
C4NetIOTCP *const pParent
Definition: C4NetIO.h:409
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:159
CStdCSec OCSec
Definition: C4NetIO.h:424
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetBroadcast()

void C4NetIOTCP::Peer::SetBroadcast ( bool  fSet)
inline

Definition at line 448 of file C4NetIO.h.

Referenced by C4NetIOTCP::SetBroadcast().

448 { fDoBroadcast = fSet; }
bool fDoBroadcast
Definition: C4NetIO.h:422
Here is the caller graph for this function:

Member Data Documentation

◆ addr

C4NetIO::addr_t C4NetIOTCP::Peer::addr
protected

Definition at line 411 of file C4NetIO.h.

Referenced by OnRecv().

◆ fDoBroadcast

bool C4NetIOTCP::Peer::fDoBroadcast
protected

Definition at line 422 of file C4NetIO.h.

◆ fOpen

bool C4NetIOTCP::Peer::fOpen
protected

Definition at line 420 of file C4NetIO.h.

Referenced by Close().

◆ IBuf

StdBuf C4NetIOTCP::Peer::IBuf
protected

Definition at line 415 of file C4NetIO.h.

Referenced by Close(), GetRecvBuf(), and OnRecv().

◆ ICSec

CStdCSec C4NetIOTCP::Peer::ICSec
protected

Definition at line 424 of file C4NetIO.h.

Referenced by ClearStatistics(), Close(), GetRecvBuf(), and OnRecv().

◆ iIBufUsage

int C4NetIOTCP::Peer::iIBufUsage
protected

Definition at line 416 of file C4NetIO.h.

Referenced by Close(), GetRecvBuf(), and OnRecv().

◆ iIRate

int C4NetIOTCP::Peer::iIRate
protected

Definition at line 418 of file C4NetIO.h.

Referenced by ClearStatistics(), Close(), and OnRecv().

◆ iMinIBufSize

const unsigned int C4NetIOTCP::Peer::iMinIBufSize = 8192
staticprotected

Definition at line 407 of file C4NetIO.h.

Referenced by GetRecvBuf(), OnRecv(), and C4NetIOTCP::UnpackPacket().

◆ iORate

int C4NetIOTCP::Peer::iORate
protected

Definition at line 418 of file C4NetIO.h.

Referenced by ClearStatistics(), Close(), and Send().

◆ iTCPHeaderSize

const unsigned int C4NetIOTCP::Peer::iTCPHeaderSize = 28 + 24
staticprotected

Definition at line 406 of file C4NetIO.h.

Referenced by OnRecv(), Send(), and C4NetIOTCP::UnpackPacket().

◆ Next

◆ OBuf

StdBuf C4NetIOTCP::Peer::OBuf
protected

Definition at line 415 of file C4NetIO.h.

Referenced by Close(), and Send().

◆ OCSec

CStdCSec C4NetIOTCP::Peer::OCSec
protected

Definition at line 424 of file C4NetIO.h.

Referenced by ClearStatistics(), Close(), and Send().

◆ pParent

C4NetIOTCP* const C4NetIOTCP::Peer::pParent
protected

Definition at line 409 of file C4NetIO.h.

Referenced by OnRecv(), and Send().

◆ sock

SOCKET C4NetIOTCP::Peer::sock
protected

Definition at line 413 of file C4NetIO.h.

Referenced by Close(), and Send().


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