libassa 3.5.0
|
#include <xdrIOBuffer.h>
Public Types | |
enum | state_t { waiting, xmitted, parsed, error } |
Public Member Functions | |
xdrIOBuffer (u_int len_) | |
Constructor. | |
~xdrIOBuffer () | |
Destructor. | |
xdrIOBuffer (const xdrIOBuffer &rhs_) | |
Copy constructor. | |
xdrIOBuffer & | operator= (const xdrIOBuffer &rhs_) |
Assign operator. | |
xdrIOBuffer & | operator>> (std::string &) |
Read and XDR-decode STL string from the buffer. | |
xdrIOBuffer & | operator>> (int &) |
Read and XDR-decode an integer from the buffer. | |
xdrIOBuffer & | operator>> (float &) |
Read and XDR-decode a float from the buffer. | |
operator void * () const | |
Convertion to void* (for testing where bool is required). | |
string | get_state () const |
Give verbal interpretation of object's state. | |
int | size () const |
Return number of bytes in xdrIOBuffer. | |
int | buffer_size () const |
Return buffer (maximum expected/allowable) size. | |
const char * | str () const |
Return pointer to the first byte of xdrIOBuffer. | |
void | reset () |
Clear up the internal buffer and reset state to waiting. | |
void | dump () const |
Dump object's internal state to the log file. | |
Protected Member Functions | |
void | copy (const xdrIOBuffer &) |
Copy object from argument. | |
Private Attributes | |
char * | m_buf |
Buffer. | |
int | m_sz |
Buffer size and maximum expected size. | |
char * | m_ptr |
Pointer for next I/O operation into the buffer. | |
state_t | m_state |
Object state. | |
Friends | |
Socket & | operator>> (Socket &src_, xdrIOBuffer &dest_) |
Read raw data from Socket nonblocking and store into internal buffer. |
Definition at line 55 of file xdrIOBuffer.h.
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.
xdrIOBuffer::~xdrIOBuffer | ( | ) |
Destructor.
Definition at line 93 of file xdrIOBuffer.cpp.
References DL, m_buf, trace_with_mask, and ASSA::XDRBUFTRACE.
{ trace_with_mask("xdrIOBuffer::~xdrIOBuffer", XDRBUFTRACE); DL((XDRBUFTRACE,"xdrIOBuffer->this = 0x%x\n", long(this))); delete [] m_buf; }
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.
{ trace_with_mask("xdrIOBuffer::xdrIOBuffer(xdrIOBuffer&)", XDRBUFTRACE); copy (rhs_); }
int ASSA::xdrIOBuffer::buffer_size | ( | ) | const [inline] |
Return buffer (maximum expected/allowable) size.
Definition at line 178 of file xdrIOBuffer.h.
References m_sz.
{ return (m_sz); }
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().
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().
{ trace_with_mask("xdrIOBuffer::dump", XDRBUFTRACE); DL((XDRBUFTRACE,"xdrIOBuffer->this = 0x%x\n", long(this) )); DL((XDRBUFTRACE,"\n\n" \ "\tm_buf ........: 0x%x \n" \ "\tm_sz .........: %d \n" \ "\tm_ptr ........: 0x%x \n" \ "\tbytes left ...: %d \n" \ "\tm_state ......: %s \n\n", long (m_buf), m_sz, long (m_ptr),(m_sz - size ()), get_state ().c_str ())); if (m_ptr != m_buf) { MemDump image (m_buf, size ()); DL((XDRBUFTRACE,"Bytes in buffer so far:\n\n%s\n\n", image.getMemDump () )); } else if (m_ptr == m_buf && m_state == xmitted) { MemDump image (m_buf, (m_sz)); DL((XDRBUFTRACE,"Complete buffer:\n\n%s\n\n", image.getMemDump () )); } else { DL((XDRBUFTRACE,"Empty buffer\n" )); } }
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(), operator>>(), and ASSA::operator>>().
{ string msg; switch (m_state) { case xdrIOBuffer::waiting: msg = "waiting"; break; case xdrIOBuffer::xmitted: msg = "xmitted"; break; case xdrIOBuffer::parsed: msg = "parsed"; break; case xdrIOBuffer::error: msg = "error"; break; } return msg; }
ASSA::xdrIOBuffer::operator void * | ( | ) | const [inline] |
Convertion to void* (for testing where bool is required).
Definition at line 160 of file xdrIOBuffer.h.
References m_state, parsed, trace_with_mask, waiting, and ASSA::XDRBUFTRACE.
{ trace_with_mask("xdrIOBuffer::opt void*()", XDRBUFTRACE); return (m_state == waiting || m_state == parsed) ? (void *)0 // bad state : (void *)(-1); // good state }
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.
{ trace_with_mask("xdrIOBuffer::operator=()", XDRBUFTRACE); delete [] m_buf; copy (rhs_); return *this; }
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.
{ trace_with_mask("xdrIOBuffer::operator>>(float)", XDRBUFTRACE); if (m_state != xmitted) { EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () )); return *this; } float val; int unit_sz = sizeof (float); memcpy ((char*) &val, m_ptr, unit_sz); m_ptr += unit_sz; XDR xdrs; xdrmem_create (&xdrs, (caddr_t) &val, unit_sz, XDR_DECODE); xdr_float (&xdrs, &n_); xdr_destroy (&xdrs); if (size () == m_sz) m_state = parsed; return *this; }
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.
{ trace_with_mask("xdrIOBuffer::operator>>(string)", XDRBUFTRACE); if (m_state != xmitted) { EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () )); return *this; } s_ = ""; u_long len = (u_long) *m_ptr; char* cptr = m_ptr + 4; while (len--) { s_ += *cptr++; } m_ptr += Socket::xdr_length (s_); if (size () == m_sz) { m_state = parsed; } return *this; }
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.
{ trace_with_mask("xdrIOBuffer::operator>>(int)", XDRBUFTRACE); if (m_state != xmitted) { EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () )); return *this; } int val; int unit_sz = sizeof (int); memcpy ((char*) &val, m_ptr, unit_sz); m_ptr += unit_sz; n_ = (int) ntohl (val); if (size () == m_sz) m_state = parsed; return *this; }
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.
{ trace_with_mask("xdrIOBuffer::reset", XDRBUFTRACE); m_ptr = m_buf; memset (m_buf, 0, m_sz); m_state = waiting; }
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.
Referenced by copy(), dump(), operator>>(), and ASSA::operator>>().
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.
{ return ((const char*) m_buf); }
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.
{ trace_with_mask("Socket >> xdrIOBuffer", XDRBUFTRACE); DL((XDRBUFTRACE,"Buffer Initially:\n")); b_.dump (); if (b_.m_state != xdrIOBuffer::waiting) { EL((ASSAERR,"Wrong state: %s\n", b_.get_state ().c_str ())); return s_; } int expected = b_.m_sz - b_.size (); DL((XDRBUFTRACE,"Bytes expected: %d\n",expected)); DL((XDRBUFTRACE,"Bytes in Socket buffer(s): %d\n", s_.getBytesAvail ())); int ret; if ((ret = s_.read (b_.m_ptr, expected)) <= 0) { #if defined(WIN32) if (WSAGetLastError () != WSAEWOULDBLOCK) { WSASetLastError (0); EL((ASSAERR,"Socket::read() error!\n")); b_.m_state = xdrIOBuffer::error; } #else if (errno != EWOULDBLOCK) { EL((ASSAERR,"Socket::read() error!\n")); b_.m_state = xdrIOBuffer::error; } #endif else { EL((ASSAERR,"Socket::read() error! \n")); } return s_; } b_.m_ptr += ret; DL((XDRBUFTRACE,"Received %d bytes\n", ret)); b_.dump (); if (b_.m_sz == b_.size ()) { // at the end DL((XDRBUFTRACE,"Complete message is in the buffer!\n")); b_.m_state = xdrIOBuffer::xmitted; b_.m_ptr = b_.m_buf; // rewind m_ptr for parsing stage b_.dump (); } return s_; }
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(), operator>>(), ASSA::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(), operator void *(), operator>>(), ASSA::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(), operator>>(), ASSA::operator>>(), reset(), and xdrIOBuffer().