ccuty  1.0
 All Classes Namespaces Files Functions Enumerations Macros Pages
Public Member Functions | List of all members
ccuty::SocketBuffer Class Reference

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...
 
Socketsocket ()
 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...
 

Detailed Description

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:

int main() {
Socket sock;
SocketBuffer sockbuf(sock);
int status = sock.connect("localhost", 3331);
if (status < 0) {
cerr << "Could not connect" << endl;
return 1;
}
while (cin) {
string request, response;
cout << "Request: ";
getline(cin, request);
if (sockbuf.writeLine(request) < 0) {
cerr << "Could not send message" << endl;
return 2;
}
if (sockbuf.readLine(response) < 0) {
cerr << "Couldn't receive message" << endl;
return 3;
}
}
return 0;
}

Constructor & Destructor Documentation

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.

Member Function Documentation

ssize_t ccuty::SocketBuffer::readLine ( std::string &  message)
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.

Returns
The number of bytes that were received or one of the following values:
  • 0: shutdownOutput() was called on the other side
  • Socket::Failed (-1): a connection error occured
  • Socket::InvalidSocket (-2): the socket is invalid.

Note that the message separator is counted in the value returned by readLine().

ssize_t ccuty::SocketBuffer::writeLine ( const std::string &  message)
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.

Returns
The number of bytes that were received or one of the following values:
  • 0: shutdownInput() was called on the other side
  • Socket::Failed (-1): a connection error occured
  • Socket::InvalidSocket (-2): the socket is invalid.

Note that the message separator is counted in the value returned by writeLine().

Note
if message constains one or several occurences of the message separator, readLine() will be called as many times on the other side.
void ccuty::SocketBuffer::setReadSeparator ( int  separ)
virtual

Change the message separator used by readLine().

This function changes the character(s) used by readLine() to separate successive messages:

  • if separ >= 0, readLine() searches for this character to separate messages,
  • if separ < 0 (the default) readLine() searches for \n, \r or \n\r.

Obviously, writeLine() must use the same message separator on the other side of the socket (see setWriteSeparator())

void ccuty::SocketBuffer::setWriteSeparator ( int  separ)
virtual

Change the message separator used by writeLine().

This function changes the character(s) used by writeLine() to separate successive messages:

  • if separ >= 0, writeLine() inserts separ between successive lines,
  • if separ < 0 (the default) writeLine() inserts \n\r between successive lines.

Obviously, readLine() must use the same message separator on the other side of the socket (see setReadSeparator())


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