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 &)
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 68 of file TcpSocket.h.


Constructor & Destructor Documentation

TcpSocket::CircularBuffer::CircularBuffer ( size_t  size  ) 

Definition at line 1584 of file TcpSocket.cpp.

01585 :buf(new char[2 * size])
01586 ,m_max(size)
01587 ,m_q(0)
01588 ,m_b(0)
01589 ,m_t(0)
01590 ,m_count(0)
01591 {
01592 }

TcpSocket::CircularBuffer::~CircularBuffer (  ) 

Definition at line 1595 of file TcpSocket.cpp.

References buf.

01596 {
01597         delete[] buf;
01598 }

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

Definition at line 96 of file TcpSocket.h.

00096 {}


Member Function Documentation

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

append l bytes from p to buffer

Definition at line 1601 of file TcpSocket.cpp.

References buf, m_count, m_max, m_q, and m_t.

Referenced by TcpSocket::OnRead().

01602 {
01603         if (m_q + l > m_max)
01604         {
01605                 return false; // overflow
01606         }
01607         m_count += (unsigned long)l;
01608         if (m_t + l > m_max) // block crosses circular border
01609         {
01610                 size_t l1 = m_max - m_t; // size left until circular border crossing
01611                 // always copy full block to buffer(buf) + top pointer(m_t)
01612                 // because we have doubled the buffer size for performance reasons
01613                 memcpy(buf + m_t, s, l);
01614                 memcpy(buf, s + l1, l - l1);
01615                 m_t = l - l1;
01616                 m_q += l;
01617         }
01618         else
01619         {
01620                 memcpy(buf + m_t, s, l);
01621                 memcpy(buf + m_max + m_t, s, l);
01622                 m_t += l;
01623                 if (m_t >= m_max)
01624                         m_t -= m_max;
01625                 m_q += l;
01626         }
01627         return true;
01628 }

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

copy l bytes from buffer to dest

Definition at line 1631 of file TcpSocket.cpp.

References buf, m_b, m_max, m_q, and m_t.

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

01632 {
01633         if (l > m_q)
01634         {
01635                 return false; // not enough chars
01636         }
01637         if (m_b + l > m_max) // block crosses circular border
01638         {
01639                 size_t l1 = m_max - m_b;
01640                 if (s)
01641                 {
01642                         memcpy(s, buf + m_b, l1);
01643                         memcpy(s + l1, buf, l - l1);
01644                 }
01645                 m_b = l - l1;
01646                 m_q -= l;
01647         }
01648         else
01649         {
01650                 if (s)
01651                 {
01652                         memcpy(s, buf + m_b, l);
01653                 }
01654                 m_b += l;
01655                 if (m_b >= m_max)
01656                         m_b -= m_max;
01657                 m_q -= l;
01658         }
01659         if (!m_q)
01660         {
01661                 m_b = m_t = 0;
01662         }
01663         return true;
01664 }

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

skip l bytes from buffer

Definition at line 1667 of file TcpSocket.cpp.

References Read().

01668 {
01669         return Read(NULL, l);
01670 }

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

read l bytes from buffer, returns as string.

Definition at line 1709 of file TcpSocket.cpp.

References Read().

01710 {
01711         char *sz = new char[l + 1];
01712         if (!Read(sz, l)) // failed, debug printout in Read() method
01713         {
01714                 delete[] sz;
01715                 return "";
01716         }
01717         sz[l] = 0;
01718         std::string tmp = sz;
01719         delete[] sz;
01720         return tmp;
01721 }

size_t TcpSocket::CircularBuffer::GetLength (  ) 

total buffer length

Definition at line 1673 of file TcpSocket.cpp.

References m_q.

Referenced by TcpSocket::GetInputLength().

01674 {
01675         return m_q;
01676 }

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

pointer to circular buffer beginning

Definition at line 1679 of file TcpSocket.cpp.

References buf, and m_b.

01680 {
01681         return buf + m_b;
01682 }

size_t TcpSocket::CircularBuffer::GetL (  ) 

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

Definition at line 1685 of file TcpSocket.cpp.

References m_b, m_max, and m_q.

01686 {
01687         return (m_b + m_q > m_max) ? m_max - m_b : m_q;
01688 }

size_t TcpSocket::CircularBuffer::Space (  ) 

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

Definition at line 1691 of file TcpSocket.cpp.

References m_max, and m_q.

01692 {
01693         return m_max - m_q;
01694 }

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

return total number of bytes written to this buffer, ever

Definition at line 1697 of file TcpSocket.cpp.

References m_count.

01698 {
01699         if (clear)
01700         {
01701                 unsigned long x = m_count;
01702                 m_count = 0;
01703                 return x;
01704         }
01705         return m_count;
01706 }

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

Definition at line 97 of file TcpSocket.h.

00097 { return *this; }


Member Data Documentation

Definition at line 98 of file TcpSocket.h.

Referenced by GetStart(), Read(), Write(), and ~CircularBuffer().

Definition at line 99 of file TcpSocket.h.

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

Definition at line 100 of file TcpSocket.h.

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

Definition at line 101 of file TcpSocket.h.

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

Definition at line 102 of file TcpSocket.h.

Referenced by Read(), and Write().

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

Definition at line 103 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