Loading...
Searching...
No Matches
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.
| 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 &s_, ASSA::CharInBuffer &b_)
| Read bytes from Socket stream until either record delimiter is detected, or EOF occured, or Socket stream is exhausted.

## 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

enum ASSA::CharInBuffer::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.

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

## 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 24 of file CharInBuffer.cpp.

: m_state (start), m_max_size (size_), m_delimiter (delimiter_)
{
trace_with_mask ("CharInBuffer::CharInBuffer", CHARINBUF);
if (m_max_size == 0 || m_delimiter.length () == 0) {
state (error);
}
state (waiting);
}
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
ASSA::CharInBuffer::m_state
state_t m_state
Internal state of an object.
Definition CharInBuffer.h:107
ASSA::CharInBuffer::m_delimiter
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition CharInBuffer.h:116
ASSA::CharInBuffer::m_max_size
size_t m_max_size
Maximum allowable size (delimiter included) before overflow occurs.
Definition CharInBuffer.h:113
ASSA::CharInBuffer::state
state_t state() const
Report the current state of the object.
Definition CharInBuffer.h:93
ASSA::CHARINBUF
@ CHARINBUF
Class CharInBuffer messages.
Definition LogMask.h:50

References ASSA::CHARINBUF, error, m_delimiter, m_max_size, m_state, start, state(), trace_with_mask, and waiting.

Referenced by operator>>.

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

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

References m_buffer.

## chop()

void CharInBuffer::chop | ( |  | ) |

inline private

Remove the delimiter from the end of the buffer.

Definition at line 144 of file CharInBuffer.h.

{
m_buffer.replace (m_buffer.find (m_delimiter), m_delimiter.length (), "");
}

References m_buffer, and m_delimiter.

Referenced by operator>>.

## dump()

void CharInBuffer::dump | ( |  | ) | const

Write the state of an object to the log file.

Definition at line 50 of file CharInBuffer.cpp.

{
DL((CHARINBUF,"== CharInBuffer state ==\n"));
DL((CHARINBUF,"m_state = %s\n", state_name (m_state)));
DL((CHARINBUF,"m_max_size = %d\n", m_max_size));
MemDump::dump_to_log (TRACE, "m_delimiter:\n",
m_delimiter.c_str (), m_delimiter.length ());
MemDump::dump_to_log (TRACE, "m_buffer:\n",
m_buffer.c_str (), m_buffer.length ());
DL((CHARINBUF,"========================\n"));
}
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
ASSA::CharInBuffer::state_name
static const char * state_name(state_t state_)
Report the state name.
Definition CharInBuffer.cpp:38
ASSA::MemDump::dump_to_log
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
ASSA::TRACE
@ TRACE
Function call trace.
Definition LogMask.h:26

References ASSA::CHARINBUF, DL, ASSA::MemDump::dump_to_log(), m_buffer, m_delimiter, m_max_size, m_state, state_name(), and ASSA::TRACE.

## length()

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

inline

Bytes in the buffer so far.

Definition at line 69 of file CharInBuffer.h.

{ return m_buffer.length (); }

References m_buffer.

## 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 127 of file CharInBuffer.h.

{
return (m_state == complete
? (void *) (-1) // good state
: (void *) 0); // bad state
}

References complete, and m_state.

## reset()

void CharInBuffer::reset | ( |  | ) |

inline

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

Definition at line 136 of file CharInBuffer.h.

{
m_buffer = "";
state (waiting);
}

References m_buffer, state(), and waiting.

## size()

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

inline

Bytes in the buffer so far.

Definition at line 72 of file CharInBuffer.h.

{ return m_buffer.size (); }

References m_buffer.

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

{ return m_state; }

References m_state.

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

## state() [2/2]

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

inline private

Go to the new state.

Definition at line 100 of file CharInBuffer.h.

{ m_state = new_state_; }

References m_state.

## state_name()

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

static private

Report the state name.

Definition at line 37 of file CharInBuffer.cpp.

{
static const char* vmsg[] =
{ "start", "waiting", "complete", "error", "unknown state" };
if (state_ < CharInBuffer::start || state_ > CharInBuffer::error) {
return vmsg [sizeof (vmsg)-1];
}
return vmsg [state_];
}

References error.

Referenced by dump(), and operator>>.

## Friends And Related Symbol 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.

{
trace_with_mask ("Socket >> CharInBuffer", CHARINBUF);
register char c;
if (b_.state () != CharInBuffer::waiting) {
DL((CHARINBUF,"Wrong state %s\n", b_.state_name (b_.state ())));
return s_;
}
while (s_.read (&c, 1) == 1)
{
b_.m_buffer += c;
if (b_.m_buffer.size() < b_.m_delimiter.size()) { // Bug # 1252926
continue;
}
if (b_.m_buffer.substr (
b_.m_buffer.size ()-b_.m_delimiter.size ()) == b_.m_delimiter)
{
b_.chop ();
b_.m_state = CharInBuffer::complete;
return s_;
}
if (b_.m_buffer.length () >= b_.m_max_size) {
b_.m_state = CharInBuffer::error;
break;
}
}
if (!s_) { // EOF or error
b_.state (CharInBuffer::error);
}
return s_;
}
ASSA::CharInBuffer::chop
void chop()
Remove the delimiter from the end of the buffer.
Definition CharInBuffer.h:145
ASSA::Socket::read
virtual int read(char *buf_, const u_int size_)
Read expected number of bytes from the socket.
Definition Socket.h:551

References ASSA::CHARINBUF, CharInBuffer(), chop(), complete, DL, error, m_buffer, m_delimiter, m_max_size, m_state, ASSA::Socket::read(), state(), state_name(), trace_with_mask, and waiting.

## 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(), 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 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 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 CharInBuffer(), dump(), operator void *(), operator>>, state(), and state().

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

- CharInBuffer.h
- CharInBuffer.cpp