CharInBuffer is a bucket for the character-based streams/messages. More...
#include <CharInBuffer.h>
Public Types | |
enum | state_t { start, waiting, complete, error } |
States: start, waiting, complete, error. More... | |
Public Member Functions | |
CharInBuffer (size_t size_, const string &delimiter_) | |
Constructor. | |
operator void * () const | |
Test the state of an object. | |
const char * | c_str () const |
Get the constant character pointer to the buffer. | |
size_t | length () const |
Bytes in the buffer so far. | |
size_t | size () const |
Bytes in the buffer so far. | |
void | reset () |
Discard all accumulated characters and be ready to receive a new message. | |
void | dump () const |
Write the state of an object to the log file. | |
state_t | state () const |
Report the current state of the object. | |
Private Member Functions | |
void | state (state_t new_state_) |
Go to the new state. | |
void | chop () |
Remove the delimiter from the end of the buffer. | |
Static Private Member Functions | |
static const char * | state_name (state_t state_) |
Report the state name. | |
Private Attributes | |
state_t | m_state |
Internal state of an object. | |
std::string | m_buffer |
Buffer to store the bytes received. | |
size_t | m_max_size |
Maximum allowable size (delimiter included) before overflow occurs. | |
std::string | m_delimiter |
Delimiter. Multibyte delimiter is allowed. | |
Friends | |
ASSA::Socket & | operator>> (ASSA::Socket &, ASSA::CharInBuffer &) |
Read bytes from Socket stream until either record delimiter is detected, or EOF occured, or Socket stream is exhausted. |
CharInBuffer is a bucket for the character-based streams/messages.
It helps in reading, parsing, and storing record-oriented character streams from Socket stream asynchronously. The record terminator can be multibyte. The terminator is detected and removed from the bucket. When terminator is detected, the block of characters collected in the bucket is ready to be processed further by the application according to its communication protocol. If either Socket read() error is encountered, or an overflow occurs (number of characters read exceeds the maximum limit), the object goes into the error state and won't accept further input, unless reset.
Definition at line 44 of file CharInBuffer.h.
States: start, waiting, complete, error.
start |
start state |
waiting |
incomplete record is in the buffer |
complete |
matched end-of-record - full record |
error |
overflow or Socket I/O error |
Definition at line 85 of file CharInBuffer.h.
CharInBuffer::CharInBuffer | ( | size_t | size_, | |
const string & | delimiter_ | |||
) |
Constructor.
size_ | Maximum expected size before buffer overflow | |
delimiter_ | End-of-record character(s). Can be multi-byte. |
Definition at line 25 of file CharInBuffer.cpp.
References ASSA::CHARINBUF, error, m_delimiter, m_max_size, state(), trace_with_mask, and waiting.
00026 : m_state (start), m_max_size (size_), m_delimiter (delimiter_) 00027 { 00028 trace_with_mask ("CharInBuffer::CharInBuffer", CHARINBUF); 00029 00030 if (m_max_size == 0 || m_delimiter.length () == 0) { 00031 state (error); 00032 } 00033 state (waiting); 00034 }
const char* ASSA::CharInBuffer::c_str | ( | ) | const [inline] |
Get the constant character pointer to the buffer.
Definition at line 66 of file CharInBuffer.h.
References m_buffer.
00066 { return m_buffer.c_str (); }
void CharInBuffer::chop | ( | ) | [inline, private] |
Remove the delimiter from the end of the buffer.
Definition at line 145 of file CharInBuffer.h.
References m_buffer, and m_delimiter.
Referenced by ASSA::operator>>().
00146 { 00147 m_buffer.replace (m_buffer.find (m_delimiter), m_delimiter.length (), ""); 00148 }
void CharInBuffer::dump | ( | ) | const |
Write the state of an object to the log file.
Definition at line 51 of file CharInBuffer.cpp.
References ASSA::CHARINBUF, DL, ASSA::MemDump::dump_to_log(), m_buffer, m_delimiter, m_max_size, m_state, state_name(), and ASSA::TRACE.
00052 { 00053 DL((CHARINBUF,"== CharInBuffer state ==\n")); 00054 DL((CHARINBUF,"m_state = %s\n", state_name (m_state))); 00055 DL((CHARINBUF,"m_max_size = %d\n", m_max_size)); 00056 00057 MemDump::dump_to_log (TRACE, "m_delimiter:\n", 00058 m_delimiter.c_str (), m_delimiter.length ()); 00059 00060 MemDump::dump_to_log (TRACE, "m_buffer:\n", 00061 m_buffer.c_str (), m_buffer.length ()); 00062 00063 DL((CHARINBUF,"========================\n")); 00064 }
size_t ASSA::CharInBuffer::length | ( | ) | const [inline] |
Bytes in the buffer so far.
Definition at line 69 of file CharInBuffer.h.
References m_buffer.
00069 { return m_buffer.length (); }
CharInBuffer::operator void * | ( | ) | const [inline] |
Test the state of an object.
Definition at line 128 of file CharInBuffer.h.
void CharInBuffer::reset | ( | ) | [inline] |
size_t ASSA::CharInBuffer::size | ( | ) | const [inline] |
Bytes in the buffer so far.
Definition at line 72 of file CharInBuffer.h.
References m_buffer.
00072 { return m_buffer.size (); }
void ASSA::CharInBuffer::state | ( | state_t | new_state_ | ) | [inline, private] |
Go to the new state.
Definition at line 100 of file CharInBuffer.h.
References m_state.
00100 { m_state = new_state_; }
state_t ASSA::CharInBuffer::state | ( | ) | const [inline] |
Report the current state of the object.
Definition at line 93 of file CharInBuffer.h.
References m_state.
Referenced by CharInBuffer(), ASSA::operator>>(), and reset().
00093 { return m_state; }
const char * CharInBuffer::state_name | ( | state_t | state_ | ) | [static, private] |
Report the state name.
Definition at line 38 of file CharInBuffer.cpp.
References error.
Referenced by dump(), and ASSA::operator>>().
00039 { 00040 static const char* vmsg[] = 00041 { "start", "waiting", "complete", "error", "unknown state" }; 00042 00043 if (state_ < CharInBuffer::start || state_ > CharInBuffer::error) { 00044 return vmsg [sizeof (vmsg)-1]; 00045 } 00046 return vmsg [state_]; 00047 }
ASSA::Socket& operator>> | ( | ASSA::Socket & | , | |
ASSA::CharInBuffer & | ||||
) | [friend] |
std::string ASSA::CharInBuffer::m_buffer [private] |
Buffer to store the bytes received.
Definition at line 110 of file CharInBuffer.h.
Referenced by c_str(), chop(), dump(), length(), ASSA::operator>>(), reset(), and size().
std::string ASSA::CharInBuffer::m_delimiter [private] |
Delimiter. Multibyte delimiter is allowed.
Definition at line 116 of file CharInBuffer.h.
Referenced by CharInBuffer(), chop(), dump(), and ASSA::operator>>().
size_t ASSA::CharInBuffer::m_max_size [private] |
Maximum allowable size (delimiter included) before overflow occurs.
Definition at line 113 of file CharInBuffer.h.
Referenced by CharInBuffer(), dump(), and ASSA::operator>>().
state_t ASSA::CharInBuffer::m_state [private] |
Internal state of an object.
Definition at line 107 of file CharInBuffer.h.
Referenced by dump(), operator void *(), ASSA::operator>>(), and state().