Preserves record boundaries when exchanging messages between connected TCP/IP sockets. More...
Public Member Functions | |
SocketBuffer (Socket *socket, size_t inputBufferSize=8192, size_t ouputBufferSize=8192) | |
Constructor. More... | |
virtual ssize_t | readLine (std::string &message) |
Read a message from a connected socket. More... | |
virtual ssize_t | writeLine (const std::string &message) |
Send a message to a connected socket. More... | |
Socket * | socket () |
Return the associated socket. | |
int | readSeparator () const |
Return the message separator used by readLine(). | |
int | writeSeparator () const |
Return the message separator used by writeLine(). | |
virtual void | setReadSeparator (int separ) |
Change the message separator used by readLine(). More... | |
virtual void | setWriteSeparator (int separ) |
Change the message separator used by writeLine(). More... | |
Preserves record boundaries when exchanging messages between connected TCP/IP sockets.
This class ensures that one call to readLine() corresponds to one and exactly one call to writeLine() on the other side. This differs from the behavior of Socket::send() and Socket::receive() because TCP/IP connected sockets do not preserve record boundaries. writeLine() and readLine() solve this problem by automatically adding/searching for a message separator between successive messages.
By default, writeLine() automatically adds a \n separator at the end of each message and readLine() searches for \n, \r or \n\r for separating messages. Message separators can be changed by calling setReadSeparator() and setWriteSeparator() accordingly on both sides. Obviously, messages should not contain the message separator. If this is the case, readLine() will be called as many times as the number of occurences of the message separator in the message given to writeLine().
Exemple:
ccuty::SocketBuffer::SocketBuffer | ( | Socket * | socket, |
size_t | inputBufferSize = 8192 , |
||
size_t | ouputBufferSize = 8192 |
||
) |
Constructor.
socket must be a connected TCP/IP Socket (i.e. of SOCK_STREAM type). This socket must not be deleted while the SocketBuffer is used.
inputBufferSize and ouputBufferSize are the sizes of the buffers that are used internally for exchanging data.
|
virtual |
Read a message from a connected socket.
readLine() receives one (and only one) message sent by writeLine() on the other side. The message is stored in message. This method blocks until the message is fully received.
A call to readLine() corresponds to one and exactly one call to writeLine() on the other side. For this purpose, readLine() searches for a message separator (see setReadSeparator() and setWriteSeparator()). By default, readLine() searches for \n, \r or \n\r.
Note that the message separator is counted in the value returned by readLine().
|
virtual |
Send a message to a connected socket.
writeLine() sends a message that will be received by a single call to readLine() on the other side.
A call to writeLine() corresponds to one and exactly one call to readLine() on the other side. For this purpose, writeLine() automatically adds a message separator (see setReadSeparator() and setWriteSeparator()). By default, writeLine() adds the \n character.
Note that the message separator is counted in the value returned by writeLine().
|
virtual |
Change the message separator used by readLine().
This function changes the character(s) used by readLine() to separate successive messages:
Obviously, writeLine() must use the same message separator on the other side of the socket (see setWriteSeparator())
|
virtual |
Change the message separator used by writeLine().
This function changes the character(s) used by writeLine() to separate successive messages:
Obviously, readLine() must use the same message separator on the other side of the socket (see setReadSeparator())