TcpSocket::CircularBuffer Class ReferenceBuffer class containing one read/write circular buffer.
More...
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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) | |
| CircularBuffer & | operator= (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 |
Definition at line 68 of file TcpSocket.h.
| TcpSocket::CircularBuffer::CircularBuffer | ( | size_t | size | ) |
| TcpSocket::CircularBuffer::~CircularBuffer | ( | ) |
| TcpSocket::CircularBuffer::CircularBuffer | ( | const CircularBuffer & | s | ) | [inline, private] |
| bool TcpSocket::CircularBuffer::Write | ( | const char * | p, | |
| size_t | l | |||
| ) |
append l bytes from p to buffer
Definition at line 1586 of file TcpSocket.cpp.
References buf, m_count, m_max, m_q, and m_t.
Referenced by TcpSocket::OnRead().
01587 { 01588 if (m_q + l > m_max) 01589 { 01590 return false; // overflow 01591 } 01592 m_count += (unsigned long)l; 01593 if (m_t + l > m_max) // block crosses circular border 01594 { 01595 size_t l1 = m_max - m_t; // size left until circular border crossing 01596 // always copy full block to buffer(buf) + top pointer(m_t) 01597 // because we have doubled the buffer size for performance reasons 01598 memcpy(buf + m_t, s, l); 01599 memcpy(buf, s + l1, l - l1); 01600 m_t = l - l1; 01601 m_q += l; 01602 } 01603 else 01604 { 01605 memcpy(buf + m_t, s, l); 01606 memcpy(buf + m_max + m_t, s, l); 01607 m_t += l; 01608 if (m_t >= m_max) 01609 m_t -= m_max; 01610 m_q += l; 01611 } 01612 return true; 01613 }
| bool TcpSocket::CircularBuffer::Read | ( | char * | dest, | |
| size_t | l | |||
| ) |
copy l bytes from buffer to dest
Definition at line 1616 of file TcpSocket.cpp.
References buf, m_b, m_max, m_q, and m_t.
Referenced by TcpSocket::OnSocks4Read(), TcpSocket::ReadInput(), ReadString(), and Remove().
01617 { 01618 if (l > m_q) 01619 { 01620 return false; // not enough chars 01621 } 01622 if (m_b + l > m_max) // block crosses circular border 01623 { 01624 size_t l1 = m_max - m_b; 01625 if (s) 01626 { 01627 memcpy(s, buf + m_b, l1); 01628 memcpy(s + l1, buf, l - l1); 01629 } 01630 m_b = l - l1; 01631 m_q -= l; 01632 } 01633 else 01634 { 01635 if (s) 01636 { 01637 memcpy(s, buf + m_b, l); 01638 } 01639 m_b += l; 01640 if (m_b >= m_max) 01641 m_b -= m_max; 01642 m_q -= l; 01643 } 01644 if (!m_q) 01645 { 01646 m_b = m_t = 0; 01647 } 01648 return true; 01649 }
| bool TcpSocket::CircularBuffer::Remove | ( | size_t | l | ) |
skip l bytes from buffer
Definition at line 1652 of file TcpSocket.cpp.
References Read().
01653 { 01654 return Read(NULL, l); 01655 }
| std::string TcpSocket::CircularBuffer::ReadString | ( | size_t | l | ) |
read l bytes from buffer, returns as string.
Definition at line 1694 of file TcpSocket.cpp.
References Read().
01695 { 01696 char *sz = new char[l + 1]; 01697 if (!Read(sz, l)) // failed, debug printout in Read() method 01698 { 01699 delete[] sz; 01700 return ""; 01701 } 01702 sz[l] = 0; 01703 std::string tmp = sz; 01704 delete[] sz; 01705 return tmp; 01706 }
| size_t TcpSocket::CircularBuffer::GetLength | ( | ) |
total buffer length
Definition at line 1658 of file TcpSocket.cpp.
References m_q.
Referenced by TcpSocket::GetInputLength().
01659 { 01660 return m_q; 01661 }
| const char * TcpSocket::CircularBuffer::GetStart | ( | ) |
| size_t TcpSocket::CircularBuffer::GetL | ( | ) |
| size_t TcpSocket::CircularBuffer::Space | ( | ) |
| unsigned long TcpSocket::CircularBuffer::ByteCounter | ( | bool | clear = false |
) |
| CircularBuffer& TcpSocket::CircularBuffer::operator= | ( | const CircularBuffer & | ) | [inline, private] |
char* TcpSocket::CircularBuffer::buf [private] |
Definition at line 98 of file TcpSocket.h.
Referenced by GetStart(), Read(), Write(), and ~CircularBuffer().
size_t TcpSocket::CircularBuffer::m_max [private] |
size_t TcpSocket::CircularBuffer::m_q [private] |
Definition at line 100 of file TcpSocket.h.
Referenced by GetL(), GetLength(), Read(), Space(), and Write().
size_t TcpSocket::CircularBuffer::m_b [private] |
size_t TcpSocket::CircularBuffer::m_t [private] |
unsigned long TcpSocket::CircularBuffer::m_count [private] |
1.4.4