libassa 3.5.1
Loading...
Searching...
No Matches
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.
 
 ~xdrIOBuffer ()
 Destructor.
 
 xdrIOBuffer (const xdrIOBuffer &rhs_)
 Copy constructor.
 
xdrIOBufferoperator= (const xdrIOBuffer &rhs_)
 Assign operator.
 
xdrIOBufferoperator>> (std::string &)
 Read and XDR-decode STL string from the buffer.
 
xdrIOBufferoperator>> (int &)
 Read and XDR-decode an integer from the buffer.
 
xdrIOBufferoperator>> (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 charstr () 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

charm_buf
 Buffer.
 
int m_sz
 Buffer size and maximum expected size.
 
charm_ptr
 Pointer for next I/O operation into the buffer

 
state_t m_state
 Object state.
 

Friends

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

Detailed Description

Definition at line 55 of file xdrIOBuffer.h.

Member Enumeration Documentation

◆ state_t

Enumerator
waiting 
xmitted 
parsed 
error 

Definition at line 60 of file xdrIOBuffer.h.

Constructor & Destructor Documentation

◆ xdrIOBuffer() [1/2]

xdrIOBuffer::xdrIOBuffer ( u_int  len_)

Constructor.

Definition at line 78 of file xdrIOBuffer.cpp.

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}
#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 wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
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.
char * m_buf
Buffer.
void dump() const
Dump object's internal state to the log file.
@ XDRBUF
Class xdrIOBuffer messages
Definition LogMask.h:43
@ XDRBUFTRACE
Extended xdrIOBuffer messages
Definition LogMask.h:44

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

◆ ~xdrIOBuffer()

xdrIOBuffer::~xdrIOBuffer ( )

Destructor.

Definition at line 92 of file xdrIOBuffer.cpp.

94{
95 trace_with_mask("xdrIOBuffer::~xdrIOBuffer", XDRBUFTRACE);
96
97 DL((XDRBUFTRACE,"xdrIOBuffer->this = 0x%x\n", long(this)));
98 delete [] m_buf;
99}

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

◆ xdrIOBuffer() [2/2]

ASSA::xdrIOBuffer::xdrIOBuffer ( const xdrIOBuffer rhs_)
inline

Copy constructor.

Definition at line 150 of file xdrIOBuffer.h.

152{
153 trace_with_mask("xdrIOBuffer::xdrIOBuffer(xdrIOBuffer&)", XDRBUFTRACE);
154
155 copy (rhs_);
156}
void copy(const xdrIOBuffer &)
Copy object from argument.

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

Member Function Documentation

◆ buffer_size()

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

Return buffer (maximum expected/allowable) size.

Definition at line 177 of file xdrIOBuffer.h.

179{
180 return (m_sz);
181}

References m_sz.

◆ copy()

void xdrIOBuffer::copy ( const xdrIOBuffer rhs_)
protected

Copy object from argument.

Definition at line 112 of file xdrIOBuffer.cpp.

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}

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

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

◆ dump()

void xdrIOBuffer::dump ( ) const

Dump object's internal state to the log file.

Definition at line 224 of file xdrIOBuffer.cpp.

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.
string get_state() const
Give verbal interpretation of object's state.

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

Referenced by xdrIOBuffer().

◆ get_state()

string xdrIOBuffer::get_state ( ) const

Give verbal interpretation of object's state.

Definition at line 209 of file xdrIOBuffer.cpp.

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}

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

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

◆ operator void *()

ASSA::xdrIOBuffer::operator void * ( ) const
inline

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

Definition at line 159 of file xdrIOBuffer.h.

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}

References trace_with_mask, and ASSA::XDRBUFTRACE.

◆ operator=()

xdrIOBuffer & xdrIOBuffer::operator= ( const xdrIOBuffer rhs_)

Assign operator.

Definition at line 102 of file xdrIOBuffer.cpp.

103{
104 trace_with_mask("xdrIOBuffer::operator=()", XDRBUFTRACE);
105
106 delete [] m_buf;
107 copy (rhs_);
108 return *this;
109}

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

◆ operator>>() [1/3]

xdrIOBuffer & xdrIOBuffer::operator>> ( float n_)

Read and XDR-decode a float from the buffer.

Definition at line 174 of file xdrIOBuffer.cpp.

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;
190 xdr_float (&xdrs, &n_);
191 xdr_destroy (&xdrs);
192
193 if (size () == m_sz)
194 m_state = parsed;
195 return *this;
196}
#define EL(X)
A macro for writing error message to the Logger.
Definition Logger.h:285
@ ASSAERR
ASSA and system errors
Definition LogMask.h:34

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

◆ operator>>() [2/3]

xdrIOBuffer & xdrIOBuffer::operator>> ( int n_)

Read and XDR-decode an integer from the buffer.

Definition at line 125 of file xdrIOBuffer.cpp.

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}

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

◆ operator>>() [3/3]

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 147 of file xdrIOBuffer.cpp.

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 }
166
167 if (size () == m_sz) {
168 m_state = parsed;
169 }
170 return *this;
171}
unsigned long u_long
Definition Logger_Impl.h:41
static size_t xdr_length(const std::string &s_)
Give the true length of the XDR-encoded STL string.
Definition Socket.h:343

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.

◆ reset()

void xdrIOBuffer::reset ( )

Clear up the internal buffer and reset state to waiting.

Definition at line 199 of file xdrIOBuffer.cpp.

200{
201 trace_with_mask("xdrIOBuffer::reset", XDRBUFTRACE);
202
203 m_ptr = m_buf;
204 memset (m_buf, 0, m_sz);
206}

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

◆ size()

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 170 of file xdrIOBuffer.h.

172{
173 return (m_ptr - m_buf);
174}

References m_buf, and m_ptr.

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

◆ str()

const char * ASSA::xdrIOBuffer::str ( ) const
inline

Return pointer to the first byte of xdrIOBuffer.

Definition at line 184 of file xdrIOBuffer.h.

186{
187 return ((const char*) m_buf);
188}

References m_buf.

Friends And Related Symbol Documentation

◆ operator>>

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)
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}

Member Data Documentation

◆ m_buf

char* ASSA::xdrIOBuffer::m_buf
private

Buffer.

Definition at line 137 of file xdrIOBuffer.h.

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

◆ m_ptr

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>>(), operator>>(), operator>>(), reset(), size(), and xdrIOBuffer().

◆ m_state

state_t ASSA::xdrIOBuffer::m_state
private

Object state.

Definition at line 146 of file xdrIOBuffer.h.

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

◆ m_sz

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>>(), operator>>(), operator>>(), reset(), and xdrIOBuffer().


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