4.19. xdrIOBuffer CLASS

xdrIOBuffer helps to read XDR-encoded data from Socket stream asynchronously and then read from it as if from a stream of intermixed integers and floats. Data are XDR-decoded as they read from the stream.


class xdrIOBuffer
    enum state_t { waiting, xmitted, parsed, error };

    xdrIOBuffer (u_int len_);
    ~xdrIOBuffer ();

    friend Socket& operator>>(Socket&, xdrIOBuffer&);

    xdrIOBuffer&   operator>>(int&);
    xdrIOBuffer&   operator>>(float&);

    operator void*() const;
    string get_state () const;
    int size () const;
    int buffer_size () const;
    const char* str () const;

4.19.2. USAGE

xdrIOBuffer class can be used in the following way:

#include <assa/xdrIOBuffer.h>

void foo (Socket& s_)
    int i = 0;
    float f = 0;

    xdrIOBuffer buf (4*2);     // Create buffer for 2 quadruples (32-bit data).

    s_ >> buf;           // Read raw data into the buffer

    if (buf) {          // If reading is completed
        if (s_)         // and there were no errors on the socket
            MemDump md (buf.str (), buf.size ());   // Buffer dump
            std::cout << md;

            buf >> i    // read and XDR-decode an integer
                >> f;   // read and XDR-decode a float
    else {
        // Wait for more data to come ...

Testing xdrIOBuffer object in conditional statement will produce false if:

Thus, data accumulation and data decoding functions are separated.

Initially, buffer is in waiting state. In this state, xdrIOBuffer object waits for bytes to be read from the Socket stream. The bytes are transmitted via:

Socket s;
xdrIOBuffer b (4);
s >> b;

When buffer object becomes full, the state is switched to xmitted. Now, an application code can read intermixed integer or float data from the buffer. The data are XDR-decoded!

xdrIOBuffer b;

int i; 
float f;

if (b) {
    b >> i >> f;

xdrIOBuffer object will yield TRUE in conditional statements only in xmitted state. In other words, buffer is TRUE if it is full, and FALSE otherwise.


xdrIOBuffer can be used only once. You cannot "rewind" it.