#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. |
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.
Definition at line 85 of file CharInBuffer.h.
|
|
Constructor.
Definition at line 25 of file CharInBuffer.cpp. References ASSA::CHARINBUF, error, m_delimiter, m_max_size, state(), and trace_with_mask. 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 }
|
|
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 (); }
|
|
Remove the delimiter from the end of the buffer.
Definition at line 145 of file CharInBuffer.h. References m_buffer, and m_delimiter. 00146 { 00147 m_buffer.replace (m_buffer.find (m_delimiter), m_delimiter.length (), ""); 00148 }
|
|
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 }
|
|
Bytes in the buffer so far.
Definition at line 69 of file CharInBuffer.h. References m_buffer. 00069 { return m_buffer.length (); }
|
|
Test the state of an object.
Definition at line 128 of file CharInBuffer.h. References complete, and m_state. 00129 { 00130 return (m_state == complete 00131 ? (void *) (-1) // good state 00132 : (void *) 0); // bad state 00133 }
|
|
Discard all accumulated characters and be ready to receive a new message.
Definition at line 137 of file CharInBuffer.h. References m_buffer, state(), and waiting.
|
|
Bytes in the buffer so far.
Definition at line 72 of file CharInBuffer.h. References m_buffer. 00072 { return m_buffer.size (); }
|
|
Go to the new state.
Definition at line 100 of file CharInBuffer.h. References m_state. 00100 { m_state = new_state_; }
|
|
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; }
|
|
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 }
|
|
Read bytes from Socket stream until either record delimiter is detected, or EOF occured, or Socket stream is exhausted. Regardless of the delimeter size, which can be >1, add the character received to the buffer and compare the end of the buffer with the delimeter. If match, bite off delimiter and set the state to complete. If not, continue reading till either there is no more characters to read, or Socket error (fail || eof), or buffer overflow. If overflow occurs, set the state to 'error' and terminate. Definition at line 68 of file CharInBuffer.cpp. 00069 { 00070 trace_with_mask ("Socket >> CharInBuffer", CHARINBUF); 00071 register char c; 00072 00073 if (b_.state () != CharInBuffer::waiting) { 00074 DL((CHARINBUF,"Wrong state %s\n", b_.state_name (b_.state ()))); 00075 return s_; 00076 } 00088 while (s_.read (&c, 1) == 1) { 00089 b_.m_buffer += c; 00090 00091 if (b_.m_buffer.size() < b_.m_delimiter.size()) { // Bug # 1252926 00092 continue; 00093 } 00094 00095 if (b_.m_buffer.substr ( 00096 b_.m_buffer.size ()-b_.m_delimiter.size ()) == b_.m_delimiter) 00097 { 00098 b_.chop (); 00099 b_.m_state = CharInBuffer::complete; 00100 return s_; 00101 } 00102 00103 if (b_.m_buffer.length () >= b_.m_max_size) { 00104 b_.m_state = CharInBuffer::error; 00105 break; 00106 } 00107 } 00108 00109 if (!s_) { // eof or error 00110 b_.state (CharInBuffer::error); 00111 } 00112 00113 return s_; 00114 }
|
|
Buffer to store the bytes received.
Definition at line 110 of file CharInBuffer.h. Referenced by c_str(), chop(), dump(), length(), reset(), and size(). |
|
Delimiter. Multibyte delimiter is allowed.
Definition at line 116 of file CharInBuffer.h. Referenced by CharInBuffer(), chop(), and dump(). |
|
Maximum allowable size (delimiter included) before overflow occurs.
Definition at line 113 of file CharInBuffer.h. Referenced by CharInBuffer(), and dump(). |
|
Internal state of an object.
Definition at line 107 of file CharInBuffer.h. Referenced by dump(), operator void *(), and state(). |