libassa  3.5.1
CharInBuffer.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // CharInBuffer.cpp
4 //------------------------------------------------------------------------------
5 // Copyright (C) 2002 Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 
13 #include <errno.h>
14 
15 #include "assa/CharInBuffer.h"
16 #include "assa/MemDump.h"
17 #include "assa/Logger.h"
18 
19 using namespace ASSA;
20 
21 /*******************************************************************************
22  Member functions
23 *******************************************************************************/
25 CharInBuffer (size_t size_, const string& delimiter_)
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 }
35 
36 const char*
38 state_name (state_t 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 }
48 
49 void
51 dump () const
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 }
65 
66 namespace ASSA {
67 
79 Socket&
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 }
118 } // end namespace ASSA
A bucket for collecting character-based stream records of certain length or terminated by designated ...
An abstraction to message logging facility.
#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
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
CharInBuffer is a bucket for the character-based streams/messages.
Definition: CharInBuffer.h:45
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
state_t m_state
Internal state of an object.
Definition: CharInBuffer.h:107
void chop()
Remove the delimiter from the end of the buffer.
Definition: CharInBuffer.h:145
CharInBuffer(size_t size_, const string &delimiter_)
Constructor.
std::string m_delimiter
Delimiter. Multibyte delimiter is allowed.
Definition: CharInBuffer.h:116
state_t
States: start, waiting, complete, error.
Definition: CharInBuffer.h:85
@ complete
matched end-of-record - full record
Definition: CharInBuffer.h:88
@ error
overflow or Socket I/O error
Definition: CharInBuffer.h:89
@ waiting
incomplete record is in the buffer
Definition: CharInBuffer.h:87
size_t m_max_size
Maximum allowable size (delimiter included) before overflow occurs.
Definition: CharInBuffer.h:113
state_t state() const
Report the current state of the object.
Definition: CharInBuffer.h:93
void dump() const
Write the state of an object to the log file.
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
virtual int read(char *buf_, const u_int size_)
Read expected number of bytes from the socket.
Definition: Socket.h:551
Definition: Acceptor.h:40
@ TRACE
Function call trace
Definition: LogMask.h:26
@ CHARINBUF
Class CharInBuffer messages
Definition: LogMask.h:50
Socket & operator>>(Socket &s_, CharInBuffer &b_)
Regardless of the delimeter size, which can be >1, add the character received to the buffer and compa...