libassa  3.5.1
Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
ASSA::CharInBuffer Class Reference

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. More...
 
 operator void * () const
 Test the state of an object. More...
 
const char * c_str () const
 Get the constant character pointer to the buffer. More...
 
size_t length () const
 Bytes in the buffer so far. More...
 
size_t size () const
 Bytes in the buffer so far. More...
 
void reset ()
 Discard all accumulated characters and be ready to receive a new message. More...
 
void dump () const
 Write the state of an object to the log file. More...
 
state_t state () const
 Report the current state of the object. More...
 

Private Member Functions

void state (state_t new_state_)
 Go to the new state. More...
 
void chop ()
 Remove the delimiter from the end of the buffer. More...
 

Static Private Member Functions

static const char * state_name (state_t state_)
 Report the state name. More...
 

Private Attributes

state_t m_state
 Internal state of an object. More...
 
std::string m_buffer
 Buffer to store the bytes received. More...
 
size_t m_max_size
 Maximum allowable size (delimiter included) before overflow occurs. More...
 
std::string m_delimiter
 Delimiter. Multibyte delimiter is allowed. More...
 

Friends

ASSA::Socketoperator>> (ASSA::Socket &, ASSA::CharInBuffer &)
 Read bytes from Socket stream until either record delimiter is detected, or EOF occured, or Socket stream is exhausted. More...
 

Detailed Description

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.

Member Enumeration Documentation

◆ state_t

States: start, waiting, complete, error.

Enumerator
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.

85  {
86  start,
87  waiting,
88  complete,
89  error
90  };
matched end-of-record - full record
Definition: CharInBuffer.h:88
incomplete record is in the buffer
Definition: CharInBuffer.h:87
overflow or Socket I/O error
Definition: CharInBuffer.h:89

Constructor & Destructor Documentation

◆ CharInBuffer()

CharInBuffer::CharInBuffer ( size_t  size_,
const string &  delimiter_ 
)

Constructor.

Parameters
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(), state_name(), trace_with_mask, and waiting.

26  : m_state (start), m_max_size (size_), m_delimiter (delimiter_)
27 {
28  trace_with_mask ("CharInBuffer::CharInBuffer", CHARINBUF);
29 
30  if (m_max_size == 0 || m_delimiter.length () == 0) {
31  state (error);
32  }
33  state (waiting);
34 }
size_t m_max_size
Maximum allowable size (delimiter included) before overflow occurs.
Definition: CharInBuffer.h:113
incomplete record is in the buffer
Definition: CharInBuffer.h:87
state_t state() const
Report the current state of the object.
Definition: CharInBuffer.h:93
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition: CharInBuffer.h:116
Class CharInBuffer messages.
Definition: LogMask.h:50
overflow or Socket I/O error
Definition: CharInBuffer.h:89
state_t m_state
Internal state of an object.
Definition: CharInBuffer.h:107

Member Function Documentation

◆ c_str()

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.

66 { return m_buffer.c_str (); }
std::string m_buffer
Buffer to store the bytes received.
Definition: CharInBuffer.h:110

◆ chop()

void CharInBuffer::chop ( )
inlineprivate

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>>(), reset(), and state().

146 {
147  m_buffer.replace (m_buffer.find (m_delimiter), m_delimiter.length (), "");
148 }
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition: CharInBuffer.h:116
std::string m_buffer
Buffer to store the bytes received.
Definition: CharInBuffer.h:110

◆ dump()

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.

Referenced by size(), and state_name().

52 {
53  DL((CHARINBUF,"== CharInBuffer state ==\n"));
54  DL((CHARINBUF,"m_state = %s\n", state_name (m_state)));
55  DL((CHARINBUF,"m_max_size = %d\n", m_max_size));
56 
57  MemDump::dump_to_log (TRACE, "m_delimiter:\n",
58  m_delimiter.c_str (), m_delimiter.length ());
59 
60  MemDump::dump_to_log (TRACE, "m_buffer:\n",
61  m_buffer.c_str (), m_buffer.length ());
62 
63  DL((CHARINBUF,"========================\n"));
64 }
size_t m_max_size
Maximum allowable size (delimiter included) before overflow occurs.
Definition: CharInBuffer.h:113
Function call trace.
Definition: LogMask.h:26
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition: CharInBuffer.h:116
static void dump_to_log(unsigned long mask_, const char *info_, const char *msg_, int len_)
Write hex/ascii dump of a memory region to log file.
Definition: MemDump.cpp:111
static const char * state_name(state_t state_)
Report the state name.
std::string m_buffer
Buffer to store the bytes received.
Definition: CharInBuffer.h:110
Class CharInBuffer messages.
Definition: LogMask.h:50
state_t m_state
Internal state of an object.
Definition: CharInBuffer.h:107

◆ length()

size_t ASSA::CharInBuffer::length ( ) const
inline

Bytes in the buffer so far.

Definition at line 69 of file CharInBuffer.h.

References m_buffer.

69 { return m_buffer.length (); }
std::string m_buffer
Buffer to store the bytes received.
Definition: CharInBuffer.h:110

◆ operator void *()

CharInBuffer::operator void * ( ) const
inline

Test the state of an object.

Returns
true if the object holds a complete message; false otherwise (eof, buffer overflow, or incomplete)

Definition at line 128 of file CharInBuffer.h.

References complete, m_state, and reset().

129 {
130  return (m_state == complete
131  ? (void *) (-1) // good state
132  : (void *) 0); // bad state
133 }
matched end-of-record - full record
Definition: CharInBuffer.h:88
state_t m_state
Internal state of an object.
Definition: CharInBuffer.h:107

◆ reset()

void CharInBuffer::reset ( )
inline

Discard all accumulated characters and be ready to receive a new message.

Definition at line 137 of file CharInBuffer.h.

References chop(), m_buffer, state(), and waiting.

Referenced by operator void *(), and size().

138 {
139  m_buffer = "";
140  state (waiting);
141 }
incomplete record is in the buffer
Definition: CharInBuffer.h:87
state_t state() const
Report the current state of the object.
Definition: CharInBuffer.h:93
std::string m_buffer
Buffer to store the bytes received.
Definition: CharInBuffer.h:110

◆ size()

size_t ASSA::CharInBuffer::size ( ) const
inline

Bytes in the buffer so far.

Definition at line 72 of file CharInBuffer.h.

References dump(), m_buffer, and reset().

72 { return m_buffer.size (); }
std::string m_buffer
Buffer to store the bytes received.
Definition: CharInBuffer.h:110

◆ state() [1/2]

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, and state_name().

Referenced by CharInBuffer(), ASSA::operator>>(), and reset().

93 { return m_state; }
state_t m_state
Internal state of an object.
Definition: CharInBuffer.h:107

◆ state() [2/2]

void ASSA::CharInBuffer::state ( state_t  new_state_)
inlineprivate

Go to the new state.

Definition at line 100 of file CharInBuffer.h.

References chop(), and m_state.

100 { m_state = new_state_; }
state_t m_state
Internal state of an object.
Definition: CharInBuffer.h:107

◆ state_name()

const char * CharInBuffer::state_name ( state_t  state_)
staticprivate

Report the state name.

Definition at line 38 of file CharInBuffer.cpp.

References dump(), and error.

Referenced by CharInBuffer(), dump(), ASSA::operator>>(), and state().

39 {
40  static const char* vmsg[] =
41  { "start", "waiting", "complete", "error", "unknown state" };
42 
43  if (state_ < CharInBuffer::start || state_ > CharInBuffer::error) {
44  return vmsg [sizeof (vmsg)-1];
45  }
46  return vmsg [state_];
47 }
overflow or Socket I/O error
Definition: CharInBuffer.h:89

Friends And Related Function Documentation

◆ operator>>

ASSA::Socket& operator>> ( ASSA::Socket s_,
ASSA::CharInBuffer b_ 
)
friend

Read bytes from Socket stream until either record delimiter is detected, or EOF occured, or Socket stream is exhausted.

Returns
Socket reference

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 or EOF), or buffer overflow. If overflow occurs, set the state to 'error' and terminate.

Definition at line 80 of file CharInBuffer.cpp.

81 {
82  trace_with_mask ("Socket >> CharInBuffer", CHARINBUF);
83  register char c;
84 
85  if (b_.state () != CharInBuffer::waiting) {
86  DL((CHARINBUF,"Wrong state %s\n", b_.state_name (b_.state ())));
87  return s_;
88  }
89 
90  while (s_.read (&c, 1) == 1)
91  {
92  b_.m_buffer += c;
93 
94  if (b_.m_buffer.size() < b_.m_delimiter.size()) { // Bug # 1252926
95  continue;
96  }
97 
98  if (b_.m_buffer.substr (
99  b_.m_buffer.size ()-b_.m_delimiter.size ()) == b_.m_delimiter)
100  {
101  b_.chop ();
103  return s_;
104  }
105 
106  if (b_.m_buffer.length () >= b_.m_max_size) {
108  break;
109  }
110  }
111 
112  if (!s_) { // EOF or error
114  }
115 
116  return s_;
117 }
size_t m_max_size
Maximum allowable size (delimiter included) before overflow occurs.
Definition: CharInBuffer.h:113
matched end-of-record - full record
Definition: CharInBuffer.h:88
incomplete record is in the buffer
Definition: CharInBuffer.h:87
void chop()
Remove the delimiter from the end of the buffer.
Definition: CharInBuffer.h:145
state_t state() const
Report the current state of the object.
Definition: CharInBuffer.h:93
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition: CharInBuffer.h:116
virtual int read(char *buf_, const u_int size_)
Read expected number of bytes from the socket.
Definition: Socket.h:551
static const char * state_name(state_t state_)
Report the state name.
std::string m_buffer
Buffer to store the bytes received.
Definition: CharInBuffer.h:110
Class CharInBuffer messages.
Definition: LogMask.h:50
overflow or Socket I/O error
Definition: CharInBuffer.h:89
state_t m_state
Internal state of an object.
Definition: CharInBuffer.h:107

Member Data Documentation

◆ m_buffer

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().

◆ m_delimiter

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>>().

◆ m_max_size

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>>().

◆ m_state

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().


The documentation for this class was generated from the following files: