Public Member Functions | Private Member Functions | Private Attributes

SocketReader Struct Reference

#include <sockets.h>

Collaboration diagram for SocketReader:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SocketReader (Socket &socket)
int get_state ()
int peek ()
void next ()
int read_line (char *line, size_t len)
int read (char *buffer, size_t len)
string receive_all ()

Private Member Functions

void fetch ()

Private Attributes

Socket_socket
int _state
char _buffer [BUFFER_LEN]
char * _g_ptr
char * _e_ptr

Detailed Description

Definition at line 325 of file sockets.h.


Constructor & Destructor Documentation

SocketReader::SocketReader ( Socket socket ) [inline]

Definition at line 327 of file sockets.h.

References _buffer.

     :  _socket(socket),
        _state(0),
        _g_ptr(_buffer),
        _e_ptr(_buffer)
    {
        *_buffer = '\0';
    }

Member Function Documentation

void SocketReader::fetch (  ) [inline, private]

Definition at line 465 of file sockets.h.

References _buffer, _e_ptr, _g_ptr, _socket, BUFFER_LEN, read(), and Socket::receive_fragmented().

Referenced by peek().

    {
        int read = _socket.receive_fragmented(_buffer, BUFFER_LEN);

        if (read >= 0) {
            _g_ptr = _buffer;
            _e_ptr = _buffer + read;
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

int SocketReader::get_state (  ) [inline]

Definition at line 336 of file sockets.h.

References _state.

    {
        return _state;
    }
void SocketReader::next (  ) [inline]

Definition at line 352 of file sockets.h.

References _e_ptr, and _g_ptr.

Referenced by read_line().

    {
        if (_g_ptr != _e_ptr)
            ++_g_ptr;
    }

Here is the caller graph for this function:

int SocketReader::peek (  ) [inline]

Definition at line 341 of file sockets.h.

References _e_ptr, _g_ptr, and fetch().

Referenced by read_line().

    {
        if (_g_ptr == _e_ptr)
            fetch();

        if (_g_ptr != _e_ptr)
            return *_g_ptr;
        else
            return EOF;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

int SocketReader::read ( char *  buffer,
size_t  len 
) [inline]

Definition at line 407 of file sockets.h.

References _e_ptr, _g_ptr, _socket, and Socket::receive().

Referenced by fetch(), HttpServer::handle_request(), and receive_all().

    {
        int sum = 0;

         // first read the available data from the buffer
        size_t avail = _e_ptr - _g_ptr;

        if (avail > 0) {
            size_t read = min(avail, len);
            memcpy(buffer, _g_ptr, read);
            _g_ptr += read;

            buffer += read;
            len -= read;
        }

         // Do we need more data?
        if (len > 0)
            sum += _socket.receive(buffer, len);

        return sum;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

int SocketReader::read_line ( char *  line,
size_t  len 
) [inline]

Definition at line 359 of file sockets.h.

References _state, next(), and peek().

Referenced by HttpServer::handle_request().

    {
        char* p = NULL;
        char* end = line + (len-1);

        for(;;) {
            if (p && p>=end)
                break;

            int c = peek();

            if (c == EOF) {
                _state |= ios_base::eofbit;

                if (p == line)
                    _state |= ios_base::failbit;
                break;
            }

            if (!p)
                p = line;

            if (c == '\n') {
                next();

                break;
            } else if (c == '\r') {
                next();

                if (peek() == '\n')
                    next();

                break;
            } else
                *p++ = (char)c;

            next();
        }

        if (!p)
            p = line;

        *p = '\0';

        return p - line;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

string SocketReader::receive_all (  ) [inline]

Definition at line 431 of file sockets.h.

References _e_ptr, _g_ptr, _socket, BUFFER_LEN, read(), and Socket::receive_fragmented().

    {
        ostringstream ret;
        char buffer[BUFFER_LEN];

         // first read all data from the buffer
        int avail = _e_ptr - _g_ptr;

        if (avail > 0) {
            ret.write(_g_ptr, avail);
            _g_ptr = _e_ptr;
        }

         // now loop until no more data can be read from the socket
        for(;;) {
            int read = _socket.receive_fragmented(buffer, BUFFER_LEN);

            if (read <= 0)
                break;

            ret.write(buffer, read);
        }

        return ret.str();
    }

Here is the call graph for this function:


Member Data Documentation

char SocketReader::_buffer[BUFFER_LEN] [private]

Definition at line 461 of file sockets.h.

Referenced by fetch(), and SocketReader().

char* SocketReader::_e_ptr [private]

Definition at line 463 of file sockets.h.

Referenced by fetch(), next(), peek(), read(), and receive_all().

char* SocketReader::_g_ptr [private]

Definition at line 462 of file sockets.h.

Referenced by fetch(), next(), peek(), read(), and receive_all().

Definition at line 458 of file sockets.h.

Referenced by fetch(), read(), and receive_all().

int SocketReader::_state [private]

Definition at line 459 of file sockets.h.

Referenced by get_state(), and read_line().


The documentation for this struct was generated from the following file: