Loading...
Searching...
No Matches
CharInBuffer.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// CharInBuffer.cpp
//------------------------------------------------------------------------------
// Copyright (C) 2002 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#include <errno.h>
#include "assa/CharInBuffer.h"
#include "assa/MemDump.h"
#include "assa/Logger.h"
using namespace ASSA;
/*******************************************************************************
Member functions
*******************************************************************************/
CharInBuffer::
CharInBuffer (size_t size_, const string& delimiter_)
: 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);
}
const char*
CharInBuffer::
state_name (state_t state_)
{
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_];
}
void
CharInBuffer::
dump () const
{
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"));
}
namespace ASSA {
Socket&
operator>> (Socket& s_, CharInBuffer& b_)
{
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_;
}
} // end namespace ASSA
CharInBuffer.h
A bucket for collecting character-based stream records of certain length or terminated by designated ...
Logger.h
An abstraction to message logging facility.
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
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
MemDump.h
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::CharInBuffer
CharInBuffer is a bucket for the character-based streams/messages.
Definition CharInBuffer.h:45
ASSA::CharInBuffer::state_name
static const char * state_name(state_t state_)
Report the state name.
Definition CharInBuffer.cpp:38
ASSA::CharInBuffer::m_buffer
std::string m_buffer
Buffer to store the bytes received.
Definition CharInBuffer.h:110
ASSA::CharInBuffer::m_state
state_t m_state
Internal state of an object.
Definition CharInBuffer.h:107
ASSA::CharInBuffer::CharInBuffer
CharInBuffer(size_t size_, const string &delimiter_)
Constructor.
Definition CharInBuffer.cpp:25
ASSA::CharInBuffer::m_delimiter
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition CharInBuffer.h:116
ASSA::CharInBuffer::state_t
state_t
States: start, waiting, complete, error.
Definition CharInBuffer.h:85
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
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::CharInBuffer::dump
void dump() const
Write the state of an object to the log file.
Definition CharInBuffer.cpp:51
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::Socket
Definition Socket.h:71
ASSA
Definition Acceptor.h:40
ASSA::TRACE
@ TRACE
Function call trace
Definition LogMask.h:26
ASSA::CHARINBUF
@ CHARINBUF
Class CharInBuffer messages
Definition LogMask.h:50
ASSA::operator>>
Socket & operator>>(Socket &s_, CharInBuffer &b_)
Regardless of the delimeter size, which can be >1, add the character received to the buffer and compa...
Definition CharInBuffer.cpp:80