forest-net
an overlay networks for large-scale virtual worlds
|
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 | |
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.
forest::NetBuffer::NetBuffer | ( | int | socket, |
int | size1 | ||
) |
Constructor for NetBuffer with no initialization.
socket | is the number of the socket to use when reading; should be an open, blocking stream socket |
size | is the number of bytes the buffer can hold at once |
Definition at line 18 of file NetBuffer.cpp.
|
private |
Extract and return a portion of buffer contents.
len | is the number of characters to extract |
s | is a string in which the extracted characters are returned |
Definition at line 87 of file NetBuffer.cpp.
void forest::NetBuffer::flushBuf | ( | string & | leftOver) |
Flush the remaining contents from the buffer.
leftOver | is a reference to a string in which unread buffer contents is returned. |
Definition at line 407 of file NetBuffer.cpp.
bool forest::NetBuffer::nextLine | ( | ) |
Advance to next line of input.
Definition at line 393 of file NetBuffer.cpp.
bool forest::NetBuffer::readAlphas | ( | string & | s) |
Read next non-blank of alphabetic characters.
s | is a reference to a string in which result is returned |
Definition at line 179 of file NetBuffer.cpp.
bool forest::NetBuffer::readBit | ( | bool & | b) |
Read a bit from the buffer.
Looks for a 0 or 1 as next non-whitespace character.
b | is a reference to a bool in which result is returned |
Definition at line 245 of file NetBuffer.cpp.
int forest::NetBuffer::readBlock | ( | char * | xbuf, |
int | siz | ||
) |
Read a block of bytes from the buffer.
xbuf | is a pointer to a character buffer |
siz | is the number of bytes that xbuf can hold |
Definition at line 359 of file NetBuffer.cpp.
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.
s | is a reference to a string in which result is returned |
Definition at line 298 of file NetBuffer.cpp.
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.
i | is a reference to an integer in which result is returned |
Definition at line 258 of file NetBuffer.cpp.
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.
s | is a reference to a string in which result is returned |
Definition at line 334 of file NetBuffer.cpp.
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.
line | is a reference to a string in which result is returned |
Definition at line 139 of file NetBuffer.cpp.
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.
s | is a reference to a string in which result is returned |
Definition at line 205 of file NetBuffer.cpp.
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.
s | is a reference to a string in which result is returned |
Definition at line 226 of file NetBuffer.cpp.
bool forest::NetBuffer::readWord | ( | string & | s) |
Read next word.
A word contains letters, numbers, underscores, @-signs, periods, slashes, and hyphens.
s | is a reference to a string in which result is returned |
Definition at line 160 of file NetBuffer.cpp.
|
private |
Add more data to the buffer from socket.
Definition at line 68 of file NetBuffer.cpp.
bool forest::NetBuffer::skipSpace | ( | ) |
Skip white space in the buffer.
Advances rp to first non-space character.
Definition at line 103 of file NetBuffer.cpp.
bool forest::NetBuffer::skipSpaceInLine | ( | ) |
Skip white space in the current line.
Advances rp to first non-space character or newline.
Definition at line 118 of file NetBuffer.cpp.
bool forest::NetBuffer::verify | ( | char | c) |
Verify next character.
c | is a non-space character |
Definition at line 384 of file NetBuffer.cpp.