Logo
~Sockets~
~Examples~
~Contact~


TcpSocket::CircularBuffer Class Reference
[Internal utility]

Buffer class containing one read/write circular buffer. More...

#include <TcpSocket.h>

List of all members.


Public Member Functions

 CircularBuffer (size_t size)
 ~CircularBuffer ()
bool Write (const char *p, size_t l)
 append l bytes from p to buffer
bool Read (char *dest, size_t l)
 copy l bytes from buffer to dest
bool Remove (size_t l)
 skip l bytes from buffer
std::string ReadString (size_t l)
 read l bytes from buffer, returns as string.
size_t GetLength ()
 total buffer length
const char * GetStart ()
 pointer to circular buffer beginning
size_t GetL ()
 return number of bytes from circular buffer beginning to buffer physical end
size_t Space ()
 return free space in buffer, number of bytes until buffer overrun
unsigned long ByteCounter (bool clear=false)
 return total number of bytes written to this buffer, ever

Private Member Functions

 CircularBuffer (const CircularBuffer &s)
CircularBufferoperator= (const CircularBuffer &)

Private Attributes

char * buf
size_t m_max
size_t m_q
size_t m_b
size_t m_t
unsigned long m_count

Detailed Description

Buffer class containing one read/write circular buffer.

Definition at line 66 of file TcpSocket.h.


Constructor & Destructor Documentation

TcpSocket::CircularBuffer::CircularBuffer ( size_t  size  ) 

Definition at line 1524 of file TcpSocket.cpp.

01525 :buf(new char[2 * size])
01526 ,m_max(size)
01527 ,m_q(0)
01528 ,m_b(0)
01529 ,m_t(0)
01530 ,m_count(0)
01531 {
01532 }

TcpSocket::CircularBuffer::~CircularBuffer (  ) 

Definition at line 1535 of file TcpSocket.cpp.

01536 {
01537         delete[] buf;
01538 }

TcpSocket::CircularBuffer::CircularBuffer ( const CircularBuffer s  )  [inline, private]

Definition at line 94 of file TcpSocket.h.

00094 {}


Member Function Documentation

bool TcpSocket::CircularBuffer::Write ( const char *  p,
size_t  l 
)

append l bytes from p to buffer

Definition at line 1541 of file TcpSocket.cpp.

References m_count, m_max, m_q, and m_t.

Referenced by TcpSocket::OnRead().

01542 {
01543         if (m_q + l > m_max)
01544         {
01545                 return false; // overflow
01546         }
01547         m_count += (unsigned long)l;
01548         if (m_t + l > m_max) // block crosses circular border
01549         {
01550                 size_t l1 = m_max - m_t; // size left until circular border crossing
01551                 // always copy full block to buffer(buf) + top pointer(m_t)
01552                 // because we have doubled the buffer size for performance reasons
01553                 memcpy(buf + m_t, s, l);
01554                 memcpy(buf, s + l1, l - l1);
01555                 m_t = l - l1;
01556                 m_q += l;
01557         }
01558         else
01559         {
01560                 memcpy(buf + m_t, s, l);
01561                 memcpy(buf + m_max + m_t, s, l);
01562                 m_t += l;
01563                 if (m_t >= m_max)
01564                         m_t -= m_max;
01565                 m_q += l;
01566         }
01567         return true;
01568 }

bool TcpSocket::CircularBuffer::Read ( char *  dest,
size_t  l 
)

copy l bytes from buffer to dest

Definition at line 1571 of file TcpSocket.cpp.

References m_b, m_max, m_q, and m_t.

Referenced by TcpSocket::OnSocks4Read(), TcpSocket::ReadInput(), ReadString(), and Remove().

01572 {
01573         if (l > m_q)
01574         {
01575                 return false; // not enough chars
01576         }
01577         if (m_b + l > m_max) // block crosses circular border
01578         {
01579                 size_t l1 = m_max - m_b;
01580                 if (s)
01581                 {
01582                         memcpy(s, buf + m_b, l1);
01583                         memcpy(s + l1, buf, l - l1);
01584                 }
01585                 m_b = l - l1;
01586                 m_q -= l;
01587         }
01588         else
01589         {
01590                 if (s)
01591                 {
01592                         memcpy(s, buf + m_b, l);
01593                 }
01594                 m_b += l;
01595                 if (m_b >= m_max)
01596                         m_b -= m_max;
01597                 m_q -= l;
01598         }
01599         if (!m_q)
01600         {
01601                 m_b = m_t = 0;
01602         }
01603         return true;
01604 }

bool TcpSocket::CircularBuffer::Remove ( size_t  l  ) 

skip l bytes from buffer

Definition at line 1607 of file TcpSocket.cpp.

References Read().

01608 {
01609         return Read(NULL, l);
01610 }

std::string TcpSocket::CircularBuffer::ReadString ( size_t  l  ) 

read l bytes from buffer, returns as string.

Definition at line 1649 of file TcpSocket.cpp.

References Read().

01650 {
01651         char *sz = new char[l + 1];
01652         if (!Read(sz, l)) // failed, debug printout in Read() method
01653         {
01654                 delete[] sz;
01655                 return "";
01656         }
01657         sz[l] = 0;
01658         std::string tmp = sz;
01659         delete[] sz;
01660         return tmp;
01661 }

size_t TcpSocket::CircularBuffer::GetLength (  ) 

total buffer length

Definition at line 1613 of file TcpSocket.cpp.

References m_q.

Referenced by TcpSocket::GetInputLength().

01614 {
01615         return m_q;
01616 }

const char * TcpSocket::CircularBuffer::GetStart (  ) 

pointer to circular buffer beginning

Definition at line 1619 of file TcpSocket.cpp.

References m_b.

01620 {
01621         return buf + m_b;
01622 }

size_t TcpSocket::CircularBuffer::GetL (  ) 

return number of bytes from circular buffer beginning to buffer physical end

Definition at line 1625 of file TcpSocket.cpp.

References m_b, m_max, and m_q.

01626 {
01627         return (m_b + m_q > m_max) ? m_max - m_b : m_q;
01628 }

size_t TcpSocket::CircularBuffer::Space (  ) 

return free space in buffer, number of bytes until buffer overrun

Definition at line 1631 of file TcpSocket.cpp.

References m_max, and m_q.

01632 {
01633         return m_max - m_q;
01634 }

unsigned long TcpSocket::CircularBuffer::ByteCounter ( bool  clear = false  ) 

return total number of bytes written to this buffer, ever

Definition at line 1637 of file TcpSocket.cpp.

References m_count.

01638 {
01639         if (clear)
01640         {
01641                 unsigned long x = m_count;
01642                 m_count = 0;
01643                 return x;
01644         }
01645         return m_count;
01646 }

CircularBuffer& TcpSocket::CircularBuffer::operator= ( const CircularBuffer  )  [inline, private]

Definition at line 95 of file TcpSocket.h.

00095 { return *this; }


Member Data Documentation

Definition at line 96 of file TcpSocket.h.

Definition at line 97 of file TcpSocket.h.

Referenced by GetL(), Read(), Space(), and Write().

Definition at line 98 of file TcpSocket.h.

Referenced by GetL(), GetLength(), Read(), Space(), and Write().

Definition at line 99 of file TcpSocket.h.

Referenced by GetL(), GetStart(), and Read().

Definition at line 100 of file TcpSocket.h.

Referenced by Read(), and Write().

unsigned long TcpSocket::CircularBuffer::m_count [private]

Definition at line 101 of file TcpSocket.h.

Referenced by ByteCounter(), and Write().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4