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 400 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 }
C4NetIOTCP *const pParent
Definition: C4NetIO.h:410
bool fDoBroadcast
Definition: C4NetIO.h:423
C4NetIO::addr_t addr
Definition: C4NetIO.h:412

◆ ~Peer()

C4NetIOTCP::Peer::~Peer ( )

Definition at line 1743 of file C4NetIO.cpp.

1744 {
1745  // close socket
1746  Close();
1747 }

References C4NetIOTCP::Close().

Here is the call graph for this function:

Member Function Documentation

◆ ClearStatistics()

void C4NetIOTCP::Peer::ClearStatistics ( )

Definition at line 1879 of file C4NetIO.cpp.

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

◆ Close()

void C4NetIOTCP::Peer::Close ( )

Definition at line 1864 of file C4NetIO.cpp.

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 }
#define closesocket
Definition: C4NetIO.cpp:46
void Clear()
Definition: StdBuf.h:190

References closesocket.

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

Here is the caller graph for this function:

◆ doBroadcast()

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

Definition at line 445 of file C4NetIO.h.

445 { return fDoBroadcast; }

References fDoBroadcast.

◆ GetAddr()

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

Definition at line 428 of file C4NetIO.h.

428 { return addr; }

References addr.

Referenced by C4NetIOTCP::Close().

Here is the caller graph for this function:

◆ GetIRate()

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

Definition at line 430 of file C4NetIO.h.

430 { return iIRate; }

References iIRate.

Referenced by C4NetIOTCP::GetConnStatistic().

Here is the caller graph for this function:

◆ GetORate()

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

Definition at line 431 of file C4NetIO.h.

431 { return iORate; }

References iORate.

Referenced by C4NetIOTCP::GetConnStatistic().

Here is the caller graph for this function:

◆ GetRecvBuf()

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

Definition at line 1805 of file C4NetIO.cpp.

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 }
int iSize
Definition: TstC4NetIO.cpp:32
static const unsigned int iMinIBufSize
Definition: C4NetIO.h:408
size_t getSize() const
Definition: StdBuf.h:101
void * getMPtr(size_t i)
Definition: StdBuf.h:105
void SetSize(size_t inSize)
Definition: StdBuf.h:204

References iSize.

◆ GetSocket()

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

Definition at line 429 of file C4NetIO.h.

429 { return sock; }

References sock.

◆ hasWaitingData()

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

Definition at line 447 of file C4NetIO.h.

447 { return !OBuf.isNull(); }
bool isNull() const
Definition: StdBuf.h:98

References StdBuf::isNull(), and OBuf.

Here is the call graph for this function:

◆ OnRecv()

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

Definition at line 1818 of file C4NetIO.cpp.

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 }
static const unsigned int iTCPHeaderSize
Definition: C4NetIO.h:407
virtual size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &Addr)
Definition: C4NetIO.cpp:1705
Definition: StdBuf.h:30
void Shrink(size_t iShrink)
Definition: StdBuf.h:180
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:159
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107

References StdBuf::getPart(), iSize, and StdBuf::Move().

Here is the call graph for this function:

◆ Open()

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

Definition at line 443 of file C4NetIO.h.

443 { return fOpen; }

References fOpen.

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

Here is the caller graph for this function:

◆ Send() [1/2]

bool C4NetIOTCP::Peer::Send ( )

Definition at line 1766 of file C4NetIO.cpp.

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 }
bool HaveWouldBlockError()
Definition: C4NetIO.cpp:203
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
virtual void UnBlock()
Definition: C4NetIO.cpp:1343

References HaveWouldBlockError(), and SOCKET_ERROR.

Here is the call graph for this function:

◆ Send() [2/2]

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

Definition at line 1751 of file C4NetIO.cpp.

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

References C4NetIOTCP::Send().

Referenced by C4NetIOTCP::Send().

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 449 of file C4NetIO.h.

449 { fDoBroadcast = fSet; }

References fDoBroadcast.

Referenced by C4NetIOTCP::SetBroadcast().

Here is the caller graph for this function:

Member Data Documentation

◆ addr

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

Definition at line 412 of file C4NetIO.h.

Referenced by GetAddr().

◆ fDoBroadcast

bool C4NetIOTCP::Peer::fDoBroadcast
protected

Definition at line 423 of file C4NetIO.h.

Referenced by doBroadcast(), and SetBroadcast().

◆ fOpen

bool C4NetIOTCP::Peer::fOpen
protected

Definition at line 421 of file C4NetIO.h.

Referenced by Open().

◆ IBuf

StdBuf C4NetIOTCP::Peer::IBuf
protected

Definition at line 416 of file C4NetIO.h.

◆ ICSec

CStdCSec C4NetIOTCP::Peer::ICSec
protected

Definition at line 425 of file C4NetIO.h.

◆ iIBufUsage

int C4NetIOTCP::Peer::iIBufUsage
protected

Definition at line 417 of file C4NetIO.h.

◆ iIRate

int C4NetIOTCP::Peer::iIRate
protected

Definition at line 419 of file C4NetIO.h.

Referenced by GetIRate().

◆ iMinIBufSize

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

Definition at line 408 of file C4NetIO.h.

◆ iORate

int C4NetIOTCP::Peer::iORate
protected

Definition at line 419 of file C4NetIO.h.

Referenced by GetORate().

◆ iTCPHeaderSize

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

Definition at line 407 of file C4NetIO.h.

◆ Next

◆ OBuf

StdBuf C4NetIOTCP::Peer::OBuf
protected

Definition at line 416 of file C4NetIO.h.

Referenced by hasWaitingData().

◆ OCSec

CStdCSec C4NetIOTCP::Peer::OCSec
protected

Definition at line 425 of file C4NetIO.h.

◆ pParent

C4NetIOTCP* const C4NetIOTCP::Peer::pParent
protected

Definition at line 410 of file C4NetIO.h.

◆ sock

SOCKET C4NetIOTCP::Peer::sock
protected

Definition at line 414 of file C4NetIO.h.

Referenced by GetSocket().


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