Logo
~Sockets~
~Examples~
~Contact~


CircularBuffer Class Reference
[Internal utility]

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

#include <CircularBuffer.h>

Collaboration diagram for CircularBuffer:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 CircularBuffer (Socket &owner, 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

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

Private Attributes

Socketm_owner
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 47 of file CircularBuffer.h.


Constructor & Destructor Documentation

CircularBuffer::CircularBuffer ( Socket owner,
size_t  size 
)

Definition at line 44 of file CircularBuffer.cpp.

00045 :m_owner(owner)
00046 ,buf(new char[2 * size])
00047 ,m_max(size)
00048 ,m_q(0)
00049 ,m_b(0)
00050 ,m_t(0)
00051 ,m_count(0)
00052 {
00053 }

CircularBuffer::~CircularBuffer (  ) 

Definition at line 56 of file CircularBuffer.cpp.

References buf.

00057 {
00058         delete[] buf;
00059 }

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

Definition at line 76 of file CircularBuffer.h.

00076 : m_owner( s.GetOwner() ) {}


Member Function Documentation

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

append l bytes from p to buffer

Definition at line 62 of file CircularBuffer.cpp.

References buf, Socket::Handler(), ISocketHandler::LogError(), m_count, m_max, m_owner, m_q, and m_t.

Referenced by TcpSocket::OnRead(), TcpSocket::OnWrite(), and TcpSocket::SendBuf().

00063 {
00064         if (m_q + l > m_max)
00065         {
00066                 m_owner.Handler().LogError(&m_owner, "CircularBuffer::Write", -1, "write buffer overflow");
00067                 return false; // overflow
00068         }
00069         m_count += (unsigned long)l;
00070         if (m_t + l > m_max) // block crosses circular border
00071         {
00072                 size_t l1 = m_max - m_t; // size left until circular border crossing
00073                 // always copy full block to buffer(buf) + top pointer(m_t)
00074                 // because we have doubled the buffer size for performance reasons
00075                 memcpy(buf + m_t, s, l);
00076                 memcpy(buf, s + l1, l - l1);
00077                 m_t = l - l1;
00078                 m_q += l;
00079         }
00080         else
00081         {
00082                 memcpy(buf + m_t, s, l);
00083                 memcpy(buf + m_max + m_t, s, l);
00084                 m_t += l;
00085                 if (m_t >= m_max)
00086                         m_t -= m_max;
00087                 m_q += l;
00088         }
00089         return true;
00090 }

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

copy l bytes from buffer to dest

Definition at line 93 of file CircularBuffer.cpp.

References buf, Socket::Handler(), ISocketHandler::LogError(), m_b, m_max, m_owner, m_q, and m_t.

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

00094 {
00095         if (l > m_q)
00096         {
00097                 m_owner.Handler().LogError(&m_owner, s ? "CircularBuffer::Read" : "CircularBuffer::Write", -1, "attempt to read beyond buffer");
00098                 return false; // not enough chars
00099         }
00100         if (m_b + l > m_max) // block crosses circular border
00101         {
00102                 size_t l1 = m_max - m_b;
00103                 if (s)
00104                 {
00105                         memcpy(s, buf + m_b, l1);
00106                         memcpy(s + l1, buf, l - l1);
00107                 }
00108                 m_b = l - l1;
00109                 m_q -= l;
00110         }
00111         else
00112         {
00113                 if (s)
00114                 {
00115                         memcpy(s, buf + m_b, l);
00116                 }
00117                 m_b += l;
00118                 if (m_b >= m_max)
00119                         m_b -= m_max;
00120                 m_q -= l;
00121         }
00122         if (!m_q)
00123         {
00124                 m_b = m_t = 0;
00125         }
00126         return true;
00127 }

bool CircularBuffer::Remove ( size_t  l  ) 

skip l bytes from buffer

Definition at line 130 of file CircularBuffer.cpp.

References Read().

Referenced by TcpSocket::OnWrite().

00131 {
00132         return Read(NULL, l);
00133 }

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

read l bytes from buffer, returns as string.

Definition at line 178 of file CircularBuffer.cpp.

References Read().

00179 {
00180         char *sz = new char[l + 1];
00181         if (!Read(sz, l)) // failed, debug printout in Read() method
00182         {
00183                 delete[] sz;
00184                 return "";
00185         }
00186         sz[l] = 0;
00187         std::string tmp = sz;
00188         delete[] sz;
00189         return tmp;
00190 }

size_t CircularBuffer::GetLength (  ) 

total buffer length

Definition at line 136 of file CircularBuffer.cpp.

References m_q.

Referenced by TcpSocket::GetInputLength(), TcpSocket::GetOutputLength(), TcpSocket::OnWrite(), and TcpSocket::SendBuf().

00137 {
00138         return m_q;
00139 }

const char * CircularBuffer::GetStart (  ) 

pointer to circular buffer beginning

Definition at line 142 of file CircularBuffer.cpp.

References buf, and m_b.

Referenced by TcpSocket::OnWrite().

00143 {
00144         return buf + m_b;
00145 }

size_t CircularBuffer::GetL (  ) 

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

Definition at line 148 of file CircularBuffer.cpp.

References m_b, m_max, and m_q.

00149 {
00150         return (m_b + m_q > m_max) ? m_max - m_b : m_q;
00151 }

size_t CircularBuffer::Space (  ) 

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

Definition at line 154 of file CircularBuffer.cpp.

References m_max, and m_q.

Referenced by TcpSocket::OnWrite(), and TcpSocket::SendBuf().

00155 {
00156         return m_max - m_q;
00157 }

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

return total number of bytes written to this buffer, ever

Definition at line 160 of file CircularBuffer.cpp.

References m_count.

00161 {
00162         if (clear)
00163         {
00164                 unsigned long x = m_count;
00165                 m_count = 0;
00166                 return x;
00167         }
00168         return m_count;
00169 }

Socket & CircularBuffer::GetOwner (  )  const [private]

Definition at line 172 of file CircularBuffer.cpp.

References m_owner.

00173 { 
00174         return m_owner; 
00175 }

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

Definition at line 77 of file CircularBuffer.h.

00077 { return *this; }


Member Data Documentation

Definition at line 78 of file CircularBuffer.h.

Referenced by GetOwner(), Read(), and Write().

char* CircularBuffer::buf [private]

Definition at line 79 of file CircularBuffer.h.

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

size_t CircularBuffer::m_max [private]

Definition at line 80 of file CircularBuffer.h.

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

size_t CircularBuffer::m_q [private]

Definition at line 81 of file CircularBuffer.h.

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

size_t CircularBuffer::m_b [private]

Definition at line 82 of file CircularBuffer.h.

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

size_t CircularBuffer::m_t [private]

Definition at line 83 of file CircularBuffer.h.

Referenced by Read(), and Write().

unsigned long CircularBuffer::m_count [private]

Definition at line 84 of file CircularBuffer.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