libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | Protected Member Functions | Private Attributes | Friends | List of all members
ASSA::xdrIOBuffer Class Reference

#include <xdrIOBuffer.h>

Public Types

enum  state_t { waiting, xmitted, parsed, error }
 

Public Member Functions

 xdrIOBuffer (u_int len_)
 Constructor. More...
 
 ~xdrIOBuffer ()
 Destructor. More...
 
 xdrIOBuffer (const xdrIOBuffer &rhs_)
 Copy constructor. More...
 
xdrIOBufferoperator= (const xdrIOBuffer &rhs_)
 Assign operator. More...
 
xdrIOBufferoperator>> (std::string &)
 Read and XDR-decode STL string from the buffer. More...
 
xdrIOBufferoperator>> (int &)
 Read and XDR-decode an integer from the buffer. More...
 
xdrIOBufferoperator>> (float &)
 Read and XDR-decode a float from the buffer. More...
 
 operator void * () const
 Convertion to void* (for testing where bool is required). More...
 
string get_state () const
 Give verbal interpretation of object's state. More...
 
int size () const
 Return number of bytes in xdrIOBuffer. More...
 
int buffer_size () const
 Return buffer (maximum expected/allowable) size. More...
 
const char * str () const
 Return pointer to the first byte of xdrIOBuffer. More...
 
void reset ()
 Clear up the internal buffer and reset state to waiting. More...
 
void dump () const
 Dump object's internal state to the log file. More...
 

Protected Member Functions

void copy (const xdrIOBuffer &)
 Copy object from argument. More...
 

Private Attributes

char * m_buf
 Buffer. More...
 
int m_sz
 Buffer size and maximum expected size. More...
 
char * m_ptr
 Pointer for next I/O operation into the buffer. More...
 
state_t m_state
 Object state. More...
 

Friends

Socketoperator>> (Socket &src_, xdrIOBuffer &dest_)
 Read raw data from Socket nonblocking and store into internal buffer. More...
 

Detailed Description

Definition at line 55 of file xdrIOBuffer.h.

Member Enumeration Documentation

Enumerator
waiting 
xmitted 
parsed 
error 

Definition at line 60 of file xdrIOBuffer.h.

Constructor & Destructor Documentation

xdrIOBuffer::xdrIOBuffer ( u_int  len_)

Constructor.

Definition at line 79 of file xdrIOBuffer.cpp.

References DL, dump(), m_buf, m_ptr, m_sz, trace_with_mask, ASSA::XDRBUF, and ASSA::XDRBUFTRACE.

80  : m_sz (sz_),
82 {
83  trace_with_mask("xdrIOBuffer::xdrIOBuffer", XDRBUFTRACE);
84 
85  m_buf = new char[sz_];
86  m_ptr = m_buf;
87  memset (m_buf, 0, m_sz);
88  DL((XDRBUF,"Allocated xdrIOBuffer [%d]\n",m_sz));
89  dump ();
90 }
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
char * m_ptr
Pointer for next I/O operation into the buffer.
Definition: xdrIOBuffer.h:143
int m_sz
Buffer size and maximum expected size.
Definition: xdrIOBuffer.h:140
#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
void dump() const
Dump object&#39;s internal state to the log file.
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
char * m_buf
Buffer.
Definition: xdrIOBuffer.h:137
Class xdrIOBuffer messages.
Definition: LogMask.h:43
xdrIOBuffer::~xdrIOBuffer ( )

Destructor.

Definition at line 93 of file xdrIOBuffer.cpp.

References DL, m_buf, trace_with_mask, and ASSA::XDRBUFTRACE.

94 {
95  trace_with_mask("xdrIOBuffer::~xdrIOBuffer", XDRBUFTRACE);
96 
97  DL((XDRBUFTRACE,"xdrIOBuffer->this = 0x%x\n", long(this)));
98  delete [] m_buf;
99 }
#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
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
char * m_buf
Buffer.
Definition: xdrIOBuffer.h:137
ASSA::xdrIOBuffer::xdrIOBuffer ( const xdrIOBuffer rhs_)
inline

Copy constructor.

Definition at line 151 of file xdrIOBuffer.h.

References copy(), trace_with_mask, and ASSA::XDRBUFTRACE.

152 {
153  trace_with_mask("xdrIOBuffer::xdrIOBuffer(xdrIOBuffer&)", XDRBUFTRACE);
154 
155  copy (rhs_);
156 }
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
void copy(const xdrIOBuffer &)
Copy object from argument.

Member Function Documentation

int ASSA::xdrIOBuffer::buffer_size ( ) const
inline

Return buffer (maximum expected/allowable) size.

Definition at line 178 of file xdrIOBuffer.h.

References m_sz.

179 {
180  return (m_sz);
181 }
int m_sz
Buffer size and maximum expected size.
Definition: xdrIOBuffer.h:140
void xdrIOBuffer::copy ( const xdrIOBuffer rhs_)
protected

Copy object from argument.

Definition at line 113 of file xdrIOBuffer.cpp.

References m_buf, m_ptr, m_state, m_sz, size(), trace_with_mask, and ASSA::XDRBUFTRACE.

Referenced by operator=(), and xdrIOBuffer().

114 {
115  trace_with_mask("xdrIOBuffer::copy", XDRBUFTRACE);
116 
117  m_sz = rhs_.m_sz;
118  m_buf = new char[m_sz];
119  memcpy (m_buf, rhs_.m_buf, m_sz);
120  m_ptr = m_buf + (rhs_.size ());
121  m_state = rhs_.m_state;
122 }
int size() const
Return number of bytes in xdrIOBuffer.
Definition: xdrIOBuffer.h:171
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
char * m_ptr
Pointer for next I/O operation into the buffer.
Definition: xdrIOBuffer.h:143
int m_sz
Buffer size and maximum expected size.
Definition: xdrIOBuffer.h:140
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
char * m_buf
Buffer.
Definition: xdrIOBuffer.h:137
void xdrIOBuffer::dump ( void  ) const

Dump object's internal state to the log file.

Definition at line 225 of file xdrIOBuffer.cpp.

References DL, get_state(), ASSA::MemDump::getMemDump(), m_buf, m_ptr, m_state, m_sz, size(), trace_with_mask, ASSA::XDRBUFTRACE, and xmitted.

Referenced by ASSA::operator>>(), and xdrIOBuffer().

226 {
227  trace_with_mask("xdrIOBuffer::dump", XDRBUFTRACE);
228 
229  DL((XDRBUFTRACE,"xdrIOBuffer->this = 0x%x\n", long(this) ));
230  DL((XDRBUFTRACE,"\n\n" \
231  "\tm_buf ........: 0x%x \n" \
232  "\tm_sz .........: %d \n" \
233  "\tm_ptr ........: 0x%x \n" \
234  "\tbytes left ...: %d \n" \
235  "\tm_state ......: %s \n\n",
236  long (m_buf), m_sz, long (m_ptr),(m_sz - size ()),
237  get_state ().c_str ()));
238 
239  if (m_ptr != m_buf) {
240  MemDump image (m_buf, size ());
241  DL((XDRBUFTRACE,"Bytes in buffer so far:\n\n%s\n\n",
242  image.getMemDump () ));
243  }
244  else if (m_ptr == m_buf && m_state == xmitted) {
245  MemDump image (m_buf, (m_sz));
246  DL((XDRBUFTRACE,"Complete buffer:\n\n%s\n\n",
247  image.getMemDump () ));
248  }
249  else {
250  DL((XDRBUFTRACE,"Empty buffer\n" ));
251  }
252 }
int size() const
Return number of bytes in xdrIOBuffer.
Definition: xdrIOBuffer.h:171
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
char * m_ptr
Pointer for next I/O operation into the buffer.
Definition: xdrIOBuffer.h:143
int m_sz
Buffer size and maximum expected size.
Definition: xdrIOBuffer.h:140
#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
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
char * m_buf
Buffer.
Definition: xdrIOBuffer.h:137
string get_state() const
Give verbal interpretation of object&#39;s state.
string xdrIOBuffer::get_state ( ) const

Give verbal interpretation of object's state.

Definition at line 210 of file xdrIOBuffer.cpp.

References error, m_state, parsed, waiting, and xmitted.

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

211 {
212  string msg;
213  switch (m_state)
214  {
215  case xdrIOBuffer::waiting: msg = "waiting"; break;
216  case xdrIOBuffer::xmitted: msg = "xmitted"; break;
217  case xdrIOBuffer::parsed: msg = "parsed"; break;
218  case xdrIOBuffer::error: msg = "error"; break;
219  }
220  return msg;
221 }
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
ASSA::xdrIOBuffer::operator void * ( ) const
inline

Convertion to void* (for testing where bool is required).

Definition at line 160 of file xdrIOBuffer.h.

References trace_with_mask, and ASSA::XDRBUFTRACE.

161 {
162  trace_with_mask("xdrIOBuffer::opt void*()", XDRBUFTRACE);
163 
164  return (m_state == waiting || m_state == parsed)
165  ? (void *)0 // bad state
166  : (void *)(-1); // good state
167 }
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
xdrIOBuffer & xdrIOBuffer::operator= ( const xdrIOBuffer rhs_)

Assign operator.

Definition at line 102 of file xdrIOBuffer.cpp.

References copy(), m_buf, trace_with_mask, and ASSA::XDRBUFTRACE.

103 {
104  trace_with_mask("xdrIOBuffer::operator=()", XDRBUFTRACE);
105 
106  delete [] m_buf;
107  copy (rhs_);
108  return *this;
109 }
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
char * m_buf
Buffer.
Definition: xdrIOBuffer.h:137
void copy(const xdrIOBuffer &)
Copy object from argument.
xdrIOBuffer & xdrIOBuffer::operator>> ( std::string &  s_)

Read and XDR-decode STL string from the buffer.

XDR format for STL string is described in Socket::operator<< comments.

Definition at line 148 of file xdrIOBuffer.cpp.

References ASSA::ASSAERR, EL, get_state(), m_ptr, m_state, m_sz, parsed, size(), trace_with_mask, ASSA::Socket::xdr_length(), ASSA::XDRBUFTRACE, and xmitted.

149 {
150  trace_with_mask("xdrIOBuffer::operator>>(string)", XDRBUFTRACE);
151 
152  if (m_state != xmitted) {
153  EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () ));
154  return *this;
155  }
158  s_ = "";
159  u_long len = (u_long) *m_ptr;
160  char* cptr = m_ptr + 4;
161 
162  while (len--) {
163  s_ += *cptr++;
164  }
165  m_ptr += Socket::xdr_length (s_);
166 
167  if (size () == m_sz) {
168  m_state = parsed;
169  }
170  return *this;
171 }
ASSA and system errors.
Definition: LogMask.h:34
int size() const
Return number of bytes in xdrIOBuffer.
Definition: xdrIOBuffer.h:171
static size_t xdr_length(const std::string &s_)
Give the true length of the XDR-encoded STL string.
Definition: Socket.h:343
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
char * m_ptr
Pointer for next I/O operation into the buffer.
Definition: xdrIOBuffer.h:143
int m_sz
Buffer size and maximum expected size.
Definition: xdrIOBuffer.h:140
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
unsigned long u_long
Definition: Logger_Impl.h:41
string get_state() const
Give verbal interpretation of object&#39;s state.
xdrIOBuffer & xdrIOBuffer::operator>> ( int &  n_)

Read and XDR-decode an integer from the buffer.

Definition at line 126 of file xdrIOBuffer.cpp.

References ASSA::ASSAERR, EL, get_state(), m_ptr, m_state, m_sz, parsed, size(), trace_with_mask, ASSA::XDRBUFTRACE, and xmitted.

127 {
128  trace_with_mask("xdrIOBuffer::operator>>(int)", XDRBUFTRACE);
129 
130  if (m_state != xmitted) {
131  EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () ));
132  return *this;
133  }
134  int val;
135  int unit_sz = sizeof (int);
136  memcpy ((char*) &val, m_ptr, unit_sz);
137  m_ptr += unit_sz;
138 
139  n_ = (int) ntohl (val);
140 
141  if (size () == m_sz)
142  m_state = parsed;
143  return *this;
144 }
ASSA and system errors.
Definition: LogMask.h:34
int size() const
Return number of bytes in xdrIOBuffer.
Definition: xdrIOBuffer.h:171
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
char * m_ptr
Pointer for next I/O operation into the buffer.
Definition: xdrIOBuffer.h:143
int m_sz
Buffer size and maximum expected size.
Definition: xdrIOBuffer.h:140
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
string get_state() const
Give verbal interpretation of object&#39;s state.
xdrIOBuffer & xdrIOBuffer::operator>> ( float &  n_)

Read and XDR-decode a float from the buffer.

Definition at line 175 of file xdrIOBuffer.cpp.

References ASSA::ASSAERR, EL, get_state(), m_ptr, m_state, m_sz, parsed, size(), trace_with_mask, ASSA::XDRBUFTRACE, and xmitted.

176 {
177  trace_with_mask("xdrIOBuffer::operator>>(float)", XDRBUFTRACE);
178 
179  if (m_state != xmitted) {
180  EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () ));
181  return *this;
182  }
183  float val;
184  int unit_sz = sizeof (float);
185  memcpy ((char*) &val, m_ptr, unit_sz);
186  m_ptr += unit_sz;
187 
188  XDR xdrs;
189  xdrmem_create (&xdrs, (caddr_t) &val, unit_sz, XDR_DECODE);
190  xdr_float (&xdrs, &n_);
191  xdr_destroy (&xdrs);
192 
193  if (size () == m_sz)
194  m_state = parsed;
195  return *this;
196 }
ASSA and system errors.
Definition: LogMask.h:34
int size() const
Return number of bytes in xdrIOBuffer.
Definition: xdrIOBuffer.h:171
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
char * m_ptr
Pointer for next I/O operation into the buffer.
Definition: xdrIOBuffer.h:143
int m_sz
Buffer size and maximum expected size.
Definition: xdrIOBuffer.h:140
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
string get_state() const
Give verbal interpretation of object&#39;s state.
void xdrIOBuffer::reset ( )

Clear up the internal buffer and reset state to waiting.

Definition at line 199 of file xdrIOBuffer.cpp.

References m_buf, m_ptr, m_state, m_sz, trace_with_mask, waiting, and ASSA::XDRBUFTRACE.

200 {
201  trace_with_mask("xdrIOBuffer::reset", XDRBUFTRACE);
202 
203  m_ptr = m_buf;
204  memset (m_buf, 0, m_sz);
205  m_state = waiting;
206 }
state_t m_state
Object state.
Definition: xdrIOBuffer.h:146
char * m_ptr
Pointer for next I/O operation into the buffer.
Definition: xdrIOBuffer.h:143
int m_sz
Buffer size and maximum expected size.
Definition: xdrIOBuffer.h:140
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Extended xdrIOBuffer messages.
Definition: LogMask.h:44
char * m_buf
Buffer.
Definition: xdrIOBuffer.h:137
int ASSA::xdrIOBuffer::size ( ) const
inline

Return number of bytes in xdrIOBuffer.

In waiting state it's bytes transmitted so far. In xmitted state, number of bytes left to decode.

Definition at line 171 of file xdrIOBuffer.h.

References m_buf, and m_ptr.

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

172 {
173  return (m_ptr - m_buf);
174 }
char * m_ptr
Pointer for next I/O operation into the buffer.
Definition: xdrIOBuffer.h:143
char * m_buf
Buffer.
Definition: xdrIOBuffer.h:137
const char * ASSA::xdrIOBuffer::str ( ) const
inline

Return pointer to the first byte of xdrIOBuffer.

Definition at line 185 of file xdrIOBuffer.h.

References m_buf.

186 {
187  return ((const char*) m_buf);
188 }
char * m_buf
Buffer.
Definition: xdrIOBuffer.h:137

Friends And Related Function Documentation

Socket& operator>> ( Socket src_,
xdrIOBuffer dest_ 
)
friend

Read raw data from Socket nonblocking and store into internal buffer.

Definition at line 25 of file xdrIOBuffer.cpp.

26 {
27  trace_with_mask("Socket >> xdrIOBuffer", XDRBUFTRACE);
28 
29  DL((XDRBUFTRACE,"Buffer Initially:\n"));
30  b_.dump ();
31 
32  if (b_.m_state != xdrIOBuffer::waiting) {
33  EL((ASSAERR,"Wrong state: %s\n", b_.get_state ().c_str ()));
34  return s_;
35  }
36  int expected = b_.m_sz - b_.size ();
37 
38  DL((XDRBUFTRACE,"Bytes expected: %d\n",expected));
39  DL((XDRBUFTRACE,"Bytes in Socket buffer(s): %d\n", s_.getBytesAvail ()));
40  int ret;
41 
42  if ((ret = s_.read (b_.m_ptr, expected)) <= 0)
43  {
44 #if defined(WIN32)
45  if (WSAGetLastError () != WSAEWOULDBLOCK) {
46  WSASetLastError (0);
47  EL((ASSAERR,"Socket::read() error!\n"));
48  b_.m_state = xdrIOBuffer::error;
49  }
50 #else
51  if (errno != EWOULDBLOCK) {
52  EL((ASSAERR,"Socket::read() error!\n"));
53  b_.m_state = xdrIOBuffer::error;
54  }
55 #endif
56  else {
57  EL((ASSAERR,"Socket::read() error! \n"));
58  }
59  return s_;
60  }
61  b_.m_ptr += ret;
62 
63  DL((XDRBUFTRACE,"Received %d bytes\n", ret));
64  b_.dump ();
65 
66  if (b_.m_sz == b_.size ()) { // at the end
67  DL((XDRBUFTRACE,"Complete message is in the buffer!\n"));
68  b_.m_state = xdrIOBuffer::xmitted;
69  b_.m_ptr = b_.m_buf; // rewind m_ptr for parsing stage
70  b_.dump ();
71  }
72  return s_;
73 }
ASSA and system errors.
Definition: LogMask.h:34
#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
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
Extended xdrIOBuffer messages.
Definition: LogMask.h:44

Member Data Documentation

char* ASSA::xdrIOBuffer::m_buf
private

Buffer.

Definition at line 137 of file xdrIOBuffer.h.

Referenced by copy(), dump(), operator=(), ASSA::operator>>(), reset(), size(), str(), xdrIOBuffer(), and ~xdrIOBuffer().

char* ASSA::xdrIOBuffer::m_ptr
private

Pointer for next I/O operation into the buffer.

Definition at line 143 of file xdrIOBuffer.h.

Referenced by copy(), dump(), ASSA::operator>>(), operator>>(), reset(), size(), and xdrIOBuffer().

state_t ASSA::xdrIOBuffer::m_state
private

Object state.

Definition at line 146 of file xdrIOBuffer.h.

Referenced by copy(), dump(), get_state(), ASSA::operator>>(), operator>>(), and reset().

int ASSA::xdrIOBuffer::m_sz
private

Buffer size and maximum expected size.

Definition at line 140 of file xdrIOBuffer.h.

Referenced by buffer_size(), copy(), dump(), ASSA::operator>>(), operator>>(), reset(), and xdrIOBuffer().


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