forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
forest::NetBuffer Class Reference

This class implements a simple user-space buffer for use when reading from a stream socket. More...

#include <NetBuffer.h>

Public Member Functions

 NetBuffer (int, int)
 Constructor for NetBuffer with no initialization. More...
 
 NetBuffer (string &)
 
 NetBuffer (char *, int)
 
void reset (string &)
 Replace contents of the buffer.
 
void reset (char *, int)
 
bool full () const
 
bool empty () const
 
bool isWordChar (char) const
 
void clear ()
 Clear the buffer, discarding contents.
 
void flushBuf (string &)
 Flush the remaining contents from the buffer. More...
 
bool readWord (string &)
 Read next word. More...
 
bool readName (string &)
 Read a name from the buffer. More...
 
bool readString (string &)
 Read next string (enclosed in double quotes). More...
 
bool readAlphas (string &)
 Read next non-blank of alphabetic characters. More...
 
bool readLine (string &)
 Read a line of input. More...
 
bool readBit (bool &)
 Read a bit from the buffer. More...
 
bool readInt (int &)
 Read an integer from the buffer. More...
 
bool readInt (uint64_t &)
 
bool readForestAddress (string &)
 Read a Forest unicast address and return it as a string. More...
 
bool readPktType (Forest::ptyp_t &)
 
bool readCpType (CtlPkt::CpType &)
 
bool readIpAddress (string &)
 Read an Ip address in dotted decimal form and return it as a string. More...
 
int readBlock (char *, int)
 Read a block of bytes from the buffer. More...
 
bool readRspec (RateSpec &)
 
bool nextLine ()
 Advance to next line of input. More...
 
bool skipSpace ()
 Skip white space in the buffer. More...
 
bool skipSpaceInLine ()
 Skip white space in the current line. More...
 
bool verify (char)
 Verify next character. More...
 
string & toString (string &)
 

Private Member Functions

void advance (char *&, int=1)
 
bool refill ()
 Add more data to the buffer from socket. More...
 
void extract (int, string &)
 Extract and return a portion of buffer contents. More...
 

Private Attributes

int sock
 socket to use when reading
 
char * rp
 read pointer (next character to read)
 
char * wp
 write pointer (next empty position)
 
char * buf
 socket used to store input characters
 
int size
 number of bytes in buffer
 
bool noRefill
 disables automatic reads from socket
 

Detailed Description

This class implements a simple user-space buffer for use when reading from a stream socket.

It provides methods for reading higher level chunks of data from a socket, while hiding the system calls required to refill the buffer when necessary. Note that using a NetBuffer while also reading from socket directly is asking for trouble.

Definition at line 26 of file NetBuffer.h.

Constructor & Destructor Documentation

forest::NetBuffer::NetBuffer ( int  socket,
int  size1 
)

Constructor for NetBuffer with no initialization.

Parameters
socketis the number of the socket to use when reading; should be an open, blocking stream socket
sizeis the number of bytes the buffer can hold at once

Definition at line 18 of file NetBuffer.cpp.

Member Function Documentation

void forest::NetBuffer::extract ( int  len,
string &  s 
)
private

Extract and return a portion of buffer contents.

Parameters
lenis the number of characters to extract
sis a string in which the extracted characters are returned

Definition at line 87 of file NetBuffer.cpp.

Here is the caller graph for this function:

void forest::NetBuffer::flushBuf ( string &  leftOver)

Flush the remaining contents from the buffer.

Parameters
leftOveris a reference to a string in which unread buffer contents is returned.

Definition at line 407 of file NetBuffer.cpp.

Here is the call graph for this function:

bool forest::NetBuffer::nextLine ( )

Advance to next line of input.

Returns
true if a newline is found in the input, else false

Definition at line 393 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readAlphas ( string &  s)

Read next non-blank of alphabetic characters.

Parameters
sis a reference to a string in which result is returned
Returns
true on success, false on failure

Definition at line 179 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readBit ( bool &  b)

Read a bit from the buffer.

Looks for a 0 or 1 as next non-whitespace character.

Parameters
bis a reference to a bool in which result is returned
Returns
true on success, false on failure

Definition at line 245 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::NetBuffer::readBlock ( char *  xbuf,
int  siz 
)

Read a block of bytes from the buffer.

Parameters
xbufis a pointer to a character buffer
sizis the number of bytes that xbuf can hold
Returns
the number of bytes that were transferred from this NetBuffer object into xbuf.

Definition at line 359 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readForestAddress ( string &  s)

Read a Forest unicast address and return it as a string.

Initial white space is skipped and input terminates on first non-digit following the second part of the address.

Parameters
sis a reference to a string in which result is returned
Returns
true on success, false on failure

Definition at line 298 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readInt ( int &  i)

Read an integer from the buffer.

Initial white space is skipped and input terminates on first non-digit following a digit string.

Parameters
iis a reference to an integer in which result is returned
Returns
true on success, false on failure

Definition at line 258 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readIpAddress ( string &  s)

Read an Ip address in dotted decimal form and return it as a string.

Initial white space is skipped and input terminates on first non-digit following the last part of the address.

Parameters
sis a reference to a string in which result is returned
Returns
true on success, false on failure

Definition at line 334 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readLine ( string &  line)

Read a line of input.

We scan the buffer for a newline and return all characters up to the newline (but not including it). Lines are assumed to be no longer than the size of the buffer. If no newline is found even when the buffer is full, the operation fails and the contents of the buffer is not changed.

Parameters
lineis a reference to a string in which result is returned
Returns
true on success; operation fails if there is an error on the socket, if eof is encountered before a newline, or if the buffer fills before a newline is received.

Definition at line 139 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readName ( string &  s)

Read a name from the buffer.

Here a name starts with a letter and may also contain digits and underscores.

Parameters
sis a reference to a string in which result is returned
Returns
true on success, false on failure on the current line and return it in s. Return true on success, false on failure.

Definition at line 205 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readString ( string &  s)

Read next string (enclosed in double quotes).

The string may not contain double quotes. The quotes are not included in the string that is returned.

Parameters
sis a reference to a string in which result is returned
Returns
true on success, false on failure

Definition at line 226 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::readWord ( string &  s)

Read next word.

A word contains letters, numbers, underscores, @-signs, periods, slashes, and hyphens.

Parameters
sis a reference to a string in which result is returned
Returns
true on success, false on failure on the current line and return it in s. Return true on success, false on failure.

Definition at line 160 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::refill ( )
private

Add more data to the buffer from socket.

Returns
false if no space available in buffer or connection was closed by peer

Definition at line 68 of file NetBuffer.cpp.

Here is the caller graph for this function:

bool forest::NetBuffer::skipSpace ( )

Skip white space in the buffer.

Advances rp to first non-space character.

Returns
true if successful, false if connection terminates before delivering a non-space character.

Definition at line 103 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::skipSpaceInLine ( )

Skip white space in the current line.

Advances rp to first non-space character or newline.

Returns
true if successful, false if connection terminates before delivering a non-space character or newline.

Definition at line 118 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::NetBuffer::verify ( char  c)

Verify next character.

Parameters
cis a non-space character
Returns
true if the next non-space character on the current line is equal to c, else false

Definition at line 384 of file NetBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:


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