OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 385 of file C4NetIO.h.

Constructor & Destructor Documentation

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

Definition at line 1691 of file C4NetIO.cpp.

1692  : pParent(pnParent),
1693  addr(naddr), sock(nsock),
1694  iIBufUsage(0), iIRate(0), iORate(0),
1695  fOpen(true), fDoBroadcast(false), Next(nullptr)
1696 {
1697 }
bool fDoBroadcast
Definition: C4NetIO.h:408
C4NetIOTCP *const pParent
Definition: C4NetIO.h:395
C4NetIO::addr_t addr
Definition: C4NetIO.h:397
C4NetIOTCP::Peer::~Peer ( )

Definition at line 1699 of file C4NetIO.cpp.

References C4NetIOTCP::Close().

1700 {
1701  // close socket
1702  Close();
1703 }

Here is the call graph for this function:

Member Function Documentation

void C4NetIOTCP::Peer::ClearStatistics ( )

Definition at line 1835 of file C4NetIO.cpp.

1836 {
1837  CStdLock ILock(&ICSec); CStdLock OLock(&OCSec);
1838  iIRate = iORate = 0;
1839 }
CStdCSec ICSec
Definition: C4NetIO.h:410
CStdCSec OCSec
Definition: C4NetIO.h:410
void C4NetIOTCP::Peer::Close ( )

Definition at line 1820 of file C4NetIO.cpp.

References closesocket.

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

1821 {
1822  CStdLock ILock(&ICSec); CStdLock OLock(&OCSec);
1823  if (!fOpen) return;
1824  // close socket
1825  closesocket(sock);
1826  // set flag
1827  fOpen = false;
1828  // clear buffers
1829  IBuf.Clear(); OBuf.Clear();
1830  iIBufUsage = 0;
1831  // reset statistics
1832  iIRate = iORate = 0;
1833 }
void Clear()
Definition: StdBuf.h:198
CStdCSec ICSec
Definition: C4NetIO.h:410
#define closesocket
Definition: C4NetIO.cpp:52
CStdCSec OCSec
Definition: C4NetIO.h:410

Here is the caller graph for this function:

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

Definition at line 430 of file C4NetIO.h.

References fDoBroadcast.

430 { return fDoBroadcast; }
bool fDoBroadcast
Definition: C4NetIO.h:408
const C4NetIO::addr_t& C4NetIOTCP::Peer::GetAddr ( ) const
inline

Definition at line 413 of file C4NetIO.h.

References addr.

Referenced by C4NetIOTCP::Close().

413 { return addr; }
C4NetIO::addr_t addr
Definition: C4NetIO.h:397

Here is the caller graph for this function:

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

Definition at line 415 of file C4NetIO.h.

References iIRate.

Referenced by C4NetIOTCP::GetConnStatistic().

415 { return iIRate; }

Here is the caller graph for this function:

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

Definition at line 416 of file C4NetIO.h.

References iORate.

Referenced by C4NetIOTCP::GetConnStatistic().

416 { return iORate; }

Here is the caller graph for this function:

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

Definition at line 1761 of file C4NetIO.cpp.

References iSize.

1762 {
1763  CStdLock ILock(&ICSec);
1764  // Enlarge input buffer?
1765  size_t iIBufSize = std::max<size_t>(iMinIBufSize, IBuf.getSize());
1766  while ((size_t)(iIBufUsage + iSize) > iIBufSize)
1767  iIBufSize *= 2;
1768  if (iIBufSize != IBuf.getSize())
1769  IBuf.SetSize(iIBufSize);
1770  // Return the appropriate part of the input buffer
1771  return IBuf.getMPtr(iIBufUsage);
1772 }
static const unsigned int iMinIBufSize
Definition: C4NetIO.h:393
size_t getSize() const
Definition: StdBuf.h:109
void SetSize(size_t inSize)
Definition: StdBuf.h:212
CStdCSec ICSec
Definition: C4NetIO.h:410
void * getMPtr(size_t i)
Definition: StdBuf.h:113
int iSize
Definition: TstC4NetIO.cpp:35
SOCKET C4NetIOTCP::Peer::GetSocket ( ) const
inline

Definition at line 414 of file C4NetIO.h.

References sock.

414 { return sock; }
bool C4NetIOTCP::Peer::hasWaitingData ( ) const
inline

Definition at line 432 of file C4NetIO.h.

References StdBuf::isNull().

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

Here is the call graph for this function:

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

Definition at line 1774 of file C4NetIO.cpp.

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

1775 {
1776  CStdLock ILock(&ICSec);
1777  // increase input rate and input buffer usage
1779  iIBufUsage += iSize;
1780  // a prior call to GetRecvBuf should have ensured this
1781  assert(static_cast<size_t>(iIBufUsage) <= IBuf.getSize());
1782  // read packets
1783  size_t iPos = 0, iPacketPos;
1784  while ((iPacketPos = iPos) < (size_t)iIBufUsage)
1785  {
1786  // Try to unpack a packet
1787  StdBuf IBufPart = IBuf.getPart(iPos, iIBufUsage - iPos);
1788  int32_t iBytes = pParent->UnpackPacket(IBufPart, addr);
1789  // Could not unpack?
1790  if (!iBytes)
1791  break;
1792  // Advance
1793  iPos += iBytes;
1794  }
1795  // data left?
1796  if (iPacketPos < (size_t) iIBufUsage)
1797  {
1798  // no packet read?
1799  if (!iPacketPos) return;
1800  // move data
1801  IBuf.Move(iPacketPos, IBuf.getSize() - iPacketPos);
1802  iIBufUsage -= iPacketPos;
1803  // shrink buffer
1804  size_t iIBufSize = IBuf.getSize();
1805  while ((size_t) iIBufUsage <= iIBufSize / 2)
1806  iIBufSize /= 2;
1807  if (iIBufSize != IBuf.getSize())
1808  IBuf.Shrink(iPacketPos);
1809  }
1810  else
1811  {
1812  // the buffer is empty
1813  iIBufUsage = 0;
1814  // shrink buffer to minimum
1815  if (IBuf.getSize() > iMinIBufSize)
1817  }
1818 }
Definition: StdBuf.h:37
static const unsigned int iMinIBufSize
Definition: C4NetIO.h:393
size_t getSize() const
Definition: StdBuf.h:109
void SetSize(size_t inSize)
Definition: StdBuf.h:212
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:115
static const unsigned int iTCPHeaderSize
Definition: C4NetIO.h:392
CStdCSec ICSec
Definition: C4NetIO.h:410
void Shrink(size_t iShrink)
Definition: StdBuf.h:188
virtual size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &Addr)
Definition: C4NetIO.cpp:1661
C4NetIOTCP *const pParent
Definition: C4NetIO.h:395
C4NetIO::addr_t addr
Definition: C4NetIO.h:397
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:167
int iSize
Definition: TstC4NetIO.cpp:35

Here is the call graph for this function:

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

Definition at line 428 of file C4NetIO.h.

References fOpen.

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

428 { return fOpen; }

Here is the caller graph for this function:

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

Definition at line 1707 of file C4NetIO.cpp.

References C4NetIOTCP::Send().

Referenced by C4NetIOTCP::Send().

1708 {
1709  CStdLock OLock(&OCSec);
1710 
1711  // already data pending to be sent? try to sent them first (empty buffer)
1712  if (!OBuf.isNull()) Send();
1713  bool fSend = OBuf.isNull();
1714 
1715  // pack packet
1716  pParent->PackPacket(rPacket, OBuf);
1717 
1718  // (try to) send
1719  return fSend ? Send() : true;
1720 }
virtual void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf)
Definition: C4NetIO.cpp:1644
bool isNull() const
Definition: StdBuf.h:106
C4NetIOTCP *const pParent
Definition: C4NetIO.h:395
CStdCSec OCSec
Definition: C4NetIO.h:410

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOTCP::Peer::Send ( )

Definition at line 1722 of file C4NetIO.cpp.

References HaveWouldBlockError(), and SOCKET_ERROR.

1723 {
1724  CStdLock OLock(&OCSec);
1725  if (OBuf.isNull()) return true;
1726 
1727  // send as much as possibile
1728  int iBytesSent;
1729  if ((iBytesSent = ::send(sock, getBufPtr<char>(OBuf), OBuf.getSize(), 0)) == SOCKET_ERROR)
1730  if (!HaveWouldBlockError())
1731  {
1732  pParent->SetError("send failed", true);
1733  return false;
1734  }
1735 
1736  // nothin sent?
1737  if (iBytesSent == SOCKET_ERROR || !iBytesSent) return true;
1738 
1739  // increase output rate
1740  iORate += iBytesSent + iTCPHeaderSize;
1741 
1742  // data remaining?
1743  if (unsigned(iBytesSent) < OBuf.getSize())
1744  {
1745  // Shrink buffer
1746  OBuf.Move(iBytesSent, OBuf.getSize() - iBytesSent);
1747  OBuf.Shrink(iBytesSent);
1748 #ifndef STDSCHEDULER_USE_EVENTS
1749  // Unblock parent so the FD-list can be refreshed
1750  pParent->UnBlock();
1751 #endif
1752  }
1753  else
1754  // just delete buffer
1755  OBuf.Clear();
1756 
1757  // ok
1758  return true;
1759 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:754
void Clear()
Definition: StdBuf.h:198
bool isNull() const
Definition: StdBuf.h:106
size_t getSize() const
Definition: StdBuf.h:109
virtual void UnBlock()
Definition: C4NetIO.cpp:1299
static const unsigned int iTCPHeaderSize
Definition: C4NetIO.h:392
void Shrink(size_t iShrink)
Definition: StdBuf.h:188
bool HaveWouldBlockError()
Definition: C4NetIO.cpp:209
C4NetIOTCP *const pParent
Definition: C4NetIO.h:395
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:167
CStdCSec OCSec
Definition: C4NetIO.h:410
#define SOCKET_ERROR
Definition: C4NetIO.cpp:53

Here is the call graph for this function:

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

Definition at line 434 of file C4NetIO.h.

Referenced by C4NetIOTCP::SetBroadcast().

434 { fDoBroadcast = fSet; }
bool fDoBroadcast
Definition: C4NetIO.h:408

Here is the caller graph for this function:

Member Data Documentation

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

Definition at line 397 of file C4NetIO.h.

Referenced by GetAddr().

bool C4NetIOTCP::Peer::fDoBroadcast
protected

Definition at line 408 of file C4NetIO.h.

Referenced by doBroadcast().

bool C4NetIOTCP::Peer::fOpen
protected

Definition at line 406 of file C4NetIO.h.

Referenced by Open().

StdBuf C4NetIOTCP::Peer::IBuf
protected

Definition at line 401 of file C4NetIO.h.

CStdCSec C4NetIOTCP::Peer::ICSec
protected

Definition at line 410 of file C4NetIO.h.

int C4NetIOTCP::Peer::iIBufUsage
protected

Definition at line 402 of file C4NetIO.h.

int C4NetIOTCP::Peer::iIRate
protected

Definition at line 404 of file C4NetIO.h.

Referenced by GetIRate().

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

Definition at line 393 of file C4NetIO.h.

int C4NetIOTCP::Peer::iORate
protected

Definition at line 404 of file C4NetIO.h.

Referenced by GetORate().

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

Definition at line 392 of file C4NetIO.h.

StdBuf C4NetIOTCP::Peer::OBuf
protected

Definition at line 401 of file C4NetIO.h.

CStdCSec C4NetIOTCP::Peer::OCSec
protected

Definition at line 410 of file C4NetIO.h.

C4NetIOTCP* const C4NetIOTCP::Peer::pParent
protected

Definition at line 395 of file C4NetIO.h.

SOCKET C4NetIOTCP::Peer::sock
protected

Definition at line 399 of file C4NetIO.h.

Referenced by GetSocket().


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