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 59 of file TcpSocket.h.


Constructor & Destructor Documentation

TcpSocket::CircularBuffer::CircularBuffer ( size_t  size  ) 

Definition at line 1447 of file TcpSocket.cpp.

01448 :buf(new char[2 * size])
01449 ,m_max(size)
01450 ,m_q(0)
01451 ,m_b(0)
01452 ,m_t(0)
01453 ,m_count(0)
01454 {
01455 }

TcpSocket::CircularBuffer::~CircularBuffer (  ) 

Definition at line 1458 of file TcpSocket.cpp.

01459 {
01460         delete[] buf;
01461 }

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

Definition at line 87 of file TcpSocket.h.

00087 {}


Member Function Documentation

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

append l bytes from p to buffer

Definition at line 1464 of file TcpSocket.cpp.

References m_count, m_max, m_q, and m_t.

Referenced by TcpSocket::OnRead().

01465 {
01466         if (m_q + l > m_max)
01467         {
01468                 return false; // overflow
01469         }
01470         m_count += (unsigned long)l;
01471         if (m_t + l > m_max) // block crosses circular border
01472         {
01473                 size_t l1 = m_max - m_t; // size left until circular border crossing
01474                 // always copy full block to buffer(buf) + top pointer(m_t)
01475                 // because we have doubled the buffer size for performance reasons
01476                 memcpy(buf + m_t, s, l);
01477                 memcpy(buf, s + l1, l - l1);
01478                 m_t = l - l1;
01479                 m_q += l;
01480         }
01481         else
01482         {
01483                 memcpy(buf + m_t, s, l);
01484                 memcpy(buf + m_max + m_t, s, l);
01485                 m_t += l;
01486                 if (m_t >= m_max)
01487                         m_t -= m_max;
01488                 m_q += l;
01489         }
01490         return true;
01491 }

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

copy l bytes from buffer to dest

Definition at line 1494 of file TcpSocket.cpp.

References m_b, m_max, m_q, and m_t.

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

01495 {
01496         if (l > m_q)
01497         {
01498                 return false; // not enough chars
01499         }
01500         if (m_b + l > m_max) // block crosses circular border
01501         {
01502                 size_t l1 = m_max - m_b;
01503                 if (s)
01504                 {
01505                         memcpy(s, buf + m_b, l1);
01506                         memcpy(s + l1, buf, l - l1);
01507                 }
01508                 m_b = l - l1;
01509                 m_q -= l;
01510         }
01511         else
01512         {
01513                 if (s)
01514                 {
01515                         memcpy(s, buf + m_b, l);
01516                 }
01517                 m_b += l;
01518                 if (m_b >= m_max)
01519                         m_b -= m_max;
01520                 m_q -= l;
01521         }
01522         if (!m_q)
01523         {
01524                 m_b = m_t = 0;
01525         }
01526         return true;
01527 }

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

skip l bytes from buffer

Definition at line 1530 of file TcpSocket.cpp.

References Read().

01531 {
01532         return Read(NULL, l);
01533 }

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

read l bytes from buffer, returns as string.

Definition at line 1572 of file TcpSocket.cpp.

References Read().

01573 {
01574         char *sz = new char[l + 1];
01575         if (!Read(sz, l)) // failed, debug printout in Read() method
01576         {
01577                 delete[] sz;
01578                 return "";
01579         }
01580         sz[l] = 0;
01581         std::string tmp = sz;
01582         delete[] sz;
01583         return tmp;
01584 }

size_t TcpSocket::CircularBuffer::GetLength (  ) 

total buffer length

Definition at line 1536 of file TcpSocket.cpp.

References m_q.

Referenced by TcpSocket::GetInputLength().

01537 {
01538         return m_q;
01539 }

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

pointer to circular buffer beginning

Definition at line 1542 of file TcpSocket.cpp.

References m_b.

01543 {
01544         return buf + m_b;
01545 }

size_t TcpSocket::CircularBuffer::GetL (  ) 

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

Definition at line 1548 of file TcpSocket.cpp.

References m_b, m_max, and m_q.

01549 {
01550         return (m_b + m_q > m_max) ? m_max - m_b : m_q;
01551 }

size_t TcpSocket::CircularBuffer::Space (  ) 

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

Definition at line 1554 of file TcpSocket.cpp.

References m_max, and m_q.

01555 {
01556         return m_max - m_q;
01557 }

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

return total number of bytes written to this buffer, ever

Definition at line 1560 of file TcpSocket.cpp.

References m_count.

01561 {
01562         if (clear)
01563         {
01564                 unsigned long x = m_count;
01565                 m_count = 0;
01566                 return x;
01567         }
01568         return m_count;
01569 }

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

Definition at line 88 of file TcpSocket.h.

00088 { return *this; }


Member Data Documentation

Definition at line 89 of file TcpSocket.h.

Definition at line 90 of file TcpSocket.h.

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

Definition at line 91 of file TcpSocket.h.

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

Definition at line 92 of file TcpSocket.h.

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

Definition at line 93 of file TcpSocket.h.

Referenced by Read(), and Write().

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

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