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

Constructor & Destructor Documentation

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

Definition at line 1673 of file C4NetIO.cpp.

1674  : pParent(pnParent),
1675  addr(naddr), sock(nsock),
1676  iIBufUsage(0), iIRate(0), iORate(0),
1677  fOpen(true), fDoBroadcast(false), Next(nullptr)
1678 {
1679 }
bool fDoBroadcast
Definition: C4NetIO.h:407
C4NetIOTCP *const pParent
Definition: C4NetIO.h:394
C4NetIO::addr_t addr
Definition: C4NetIO.h:396
C4NetIOTCP::Peer::~Peer ( )

Definition at line 1681 of file C4NetIO.cpp.

References C4NetIOTCP::Close().

1682 {
1683  // close socket
1684  Close();
1685 }

Here is the call graph for this function:

Member Function Documentation

void C4NetIOTCP::Peer::ClearStatistics ( )

Definition at line 1817 of file C4NetIO.cpp.

1818 {
1819  CStdLock ILock(&ICSec); CStdLock OLock(&OCSec);
1820  iIRate = iORate = 0;
1821 }
CStdCSec ICSec
Definition: C4NetIO.h:409
CStdCSec OCSec
Definition: C4NetIO.h:409
void C4NetIOTCP::Peer::Close ( )

Definition at line 1802 of file C4NetIO.cpp.

References closesocket.

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

1803 {
1804  CStdLock ILock(&ICSec); CStdLock OLock(&OCSec);
1805  if (!fOpen) return;
1806  // close socket
1807  closesocket(sock);
1808  // set flag
1809  fOpen = false;
1810  // clear buffers
1811  IBuf.Clear(); OBuf.Clear();
1812  iIBufUsage = 0;
1813  // reset statistics
1814  iIRate = iORate = 0;
1815 }
void Clear()
Definition: StdBuf.h:190
CStdCSec ICSec
Definition: C4NetIO.h:409
#define closesocket
Definition: C4NetIO.cpp:46
CStdCSec OCSec
Definition: C4NetIO.h:409

Here is the caller graph for this function:

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

Definition at line 429 of file C4NetIO.h.

References fDoBroadcast.

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

Definition at line 412 of file C4NetIO.h.

References addr.

Referenced by C4NetIOTCP::Close().

412 { return addr; }
C4NetIO::addr_t addr
Definition: C4NetIO.h:396

Here is the caller graph for this function:

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

Definition at line 414 of file C4NetIO.h.

References iIRate.

Referenced by C4NetIOTCP::GetConnStatistic().

414 { return iIRate; }

Here is the caller graph for this function:

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

Definition at line 415 of file C4NetIO.h.

References iORate.

Referenced by C4NetIOTCP::GetConnStatistic().

415 { return iORate; }

Here is the caller graph for this function:

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

Definition at line 1743 of file C4NetIO.cpp.

References iSize.

1744 {
1745  CStdLock ILock(&ICSec);
1746  // Enlarge input buffer?
1747  size_t iIBufSize = std::max<size_t>(iMinIBufSize, IBuf.getSize());
1748  while ((size_t)(iIBufUsage + iSize) > iIBufSize)
1749  iIBufSize *= 2;
1750  if (iIBufSize != IBuf.getSize())
1751  IBuf.SetSize(iIBufSize);
1752  // Return the appropriate part of the input buffer
1753  return IBuf.getMPtr(iIBufUsage);
1754 }
static const unsigned int iMinIBufSize
Definition: C4NetIO.h:392
size_t getSize() const
Definition: StdBuf.h:101
void SetSize(size_t inSize)
Definition: StdBuf.h:204
CStdCSec ICSec
Definition: C4NetIO.h:409
void * getMPtr(size_t i)
Definition: StdBuf.h:105
int iSize
Definition: TstC4NetIO.cpp:32
SOCKET C4NetIOTCP::Peer::GetSocket ( ) const
inline

Definition at line 413 of file C4NetIO.h.

References sock.

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

Definition at line 431 of file C4NetIO.h.

References StdBuf::isNull().

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

Here is the call graph for this function:

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

Definition at line 1756 of file C4NetIO.cpp.

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

1757 {
1758  CStdLock ILock(&ICSec);
1759  // increase input rate and input buffer usage
1761  iIBufUsage += iSize;
1762  // a prior call to GetRecvBuf should have ensured this
1763  assert(static_cast<size_t>(iIBufUsage) <= IBuf.getSize());
1764  // read packets
1765  size_t iPos = 0, iPacketPos;
1766  while ((iPacketPos = iPos) < (size_t)iIBufUsage)
1767  {
1768  // Try to unpack a packet
1769  StdBuf IBufPart = IBuf.getPart(iPos, iIBufUsage - iPos);
1770  int32_t iBytes = pParent->UnpackPacket(IBufPart, addr);
1771  // Could not unpack?
1772  if (!iBytes)
1773  break;
1774  // Advance
1775  iPos += iBytes;
1776  }
1777  // data left?
1778  if (iPacketPos < (size_t) iIBufUsage)
1779  {
1780  // no packet read?
1781  if (!iPacketPos) return;
1782  // move data
1783  IBuf.Move(iPacketPos, IBuf.getSize() - iPacketPos);
1784  iIBufUsage -= iPacketPos;
1785  // shrink buffer
1786  size_t iIBufSize = IBuf.getSize();
1787  while ((size_t) iIBufUsage <= iIBufSize / 2)
1788  iIBufSize /= 2;
1789  if (iIBufSize != IBuf.getSize())
1790  IBuf.Shrink(iPacketPos);
1791  }
1792  else
1793  {
1794  // the buffer is empty
1795  iIBufUsage = 0;
1796  // shrink buffer to minimum
1797  if (IBuf.getSize() > iMinIBufSize)
1799  }
1800 }
Definition: StdBuf.h:29
static const unsigned int iMinIBufSize
Definition: C4NetIO.h:392
size_t getSize() const
Definition: StdBuf.h:101
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:391
CStdCSec ICSec
Definition: C4NetIO.h:409
void Shrink(size_t iShrink)
Definition: StdBuf.h:180
virtual size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &Addr)
Definition: C4NetIO.cpp:1643
C4NetIOTCP *const pParent
Definition: C4NetIO.h:394
C4NetIO::addr_t addr
Definition: C4NetIO.h:396
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:

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

Definition at line 427 of file C4NetIO.h.

References fOpen.

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

427 { return fOpen; }

Here is the caller graph for this function:

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

Definition at line 1689 of file C4NetIO.cpp.

References C4NetIOTCP::Send().

Referenced by C4NetIOTCP::Send().

1690 {
1691  CStdLock OLock(&OCSec);
1692 
1693  // already data pending to be sent? try to sent them first (empty buffer)
1694  if (!OBuf.isNull()) Send();
1695  bool fSend = OBuf.isNull();
1696 
1697  // pack packet
1698  pParent->PackPacket(rPacket, OBuf);
1699 
1700  // (try to) send
1701  return fSend ? Send() : true;
1702 }
virtual void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf)
Definition: C4NetIO.cpp:1626
bool isNull() const
Definition: StdBuf.h:98
C4NetIOTCP *const pParent
Definition: C4NetIO.h:394
CStdCSec OCSec
Definition: C4NetIO.h:409

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOTCP::Peer::Send ( )

Definition at line 1704 of file C4NetIO.cpp.

References HaveWouldBlockError(), and SOCKET_ERROR.

1705 {
1706  CStdLock OLock(&OCSec);
1707  if (OBuf.isNull()) return true;
1708 
1709  // send as much as possibile
1710  int iBytesSent;
1711  if ((iBytesSent = ::send(sock, getBufPtr<char>(OBuf), OBuf.getSize(), 0)) == SOCKET_ERROR)
1712  if (!HaveWouldBlockError())
1713  {
1714  pParent->SetError("send failed", true);
1715  return false;
1716  }
1717 
1718  // nothin sent?
1719  if (iBytesSent == SOCKET_ERROR || !iBytesSent) return true;
1720 
1721  // increase output rate
1722  iORate += iBytesSent + iTCPHeaderSize;
1723 
1724  // data remaining?
1725  if (unsigned(iBytesSent) < OBuf.getSize())
1726  {
1727  // Shrink buffer
1728  OBuf.Move(iBytesSent, OBuf.getSize() - iBytesSent);
1729  OBuf.Shrink(iBytesSent);
1730 #ifndef STDSCHEDULER_USE_EVENTS
1731  // Unblock parent so the FD-list can be refreshed
1732  pParent->UnBlock();
1733 #endif
1734  }
1735  else
1736  // just delete buffer
1737  OBuf.Clear();
1738 
1739  // ok
1740  return true;
1741 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
void Clear()
Definition: StdBuf.h:190
bool isNull() const
Definition: StdBuf.h:98
size_t getSize() const
Definition: StdBuf.h:101
virtual void UnBlock()
Definition: C4NetIO.cpp:1281
static const unsigned int iTCPHeaderSize
Definition: C4NetIO.h:391
void Shrink(size_t iShrink)
Definition: StdBuf.h:180
bool HaveWouldBlockError()
Definition: C4NetIO.cpp:203
C4NetIOTCP *const pParent
Definition: C4NetIO.h:394
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:159
CStdCSec OCSec
Definition: C4NetIO.h:409
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the call graph for this function:

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

Definition at line 433 of file C4NetIO.h.

Referenced by C4NetIOTCP::SetBroadcast().

433 { fDoBroadcast = fSet; }
bool fDoBroadcast
Definition: C4NetIO.h:407

Here is the caller graph for this function:

Member Data Documentation

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

Definition at line 396 of file C4NetIO.h.

Referenced by GetAddr().

bool C4NetIOTCP::Peer::fDoBroadcast
protected

Definition at line 407 of file C4NetIO.h.

Referenced by doBroadcast().

bool C4NetIOTCP::Peer::fOpen
protected

Definition at line 405 of file C4NetIO.h.

Referenced by Open().

StdBuf C4NetIOTCP::Peer::IBuf
protected

Definition at line 400 of file C4NetIO.h.

CStdCSec C4NetIOTCP::Peer::ICSec
protected

Definition at line 409 of file C4NetIO.h.

int C4NetIOTCP::Peer::iIBufUsage
protected

Definition at line 401 of file C4NetIO.h.

int C4NetIOTCP::Peer::iIRate
protected

Definition at line 403 of file C4NetIO.h.

Referenced by GetIRate().

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

Definition at line 392 of file C4NetIO.h.

int C4NetIOTCP::Peer::iORate
protected

Definition at line 403 of file C4NetIO.h.

Referenced by GetORate().

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

Definition at line 391 of file C4NetIO.h.

StdBuf C4NetIOTCP::Peer::OBuf
protected

Definition at line 400 of file C4NetIO.h.

CStdCSec C4NetIOTCP::Peer::OCSec
protected

Definition at line 409 of file C4NetIO.h.

C4NetIOTCP* const C4NetIOTCP::Peer::pParent
protected

Definition at line 394 of file C4NetIO.h.

SOCKET C4NetIOTCP::Peer::sock
protected

Definition at line 398 of file C4NetIO.h.

Referenced by GetSocket().


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