mcs::Socket Class Reference

#include <mcs.hh>

Inheritance diagram for mcs::Socket:

Inheritance graph
[legend]

List of all members.


Detailed Description

Manage TCP sockets.

This class implements a TCP socket. It has a timeout facility, as well as a mechanism to check that a connection is still open and ready to send/receive data. It is strongly recommended that the program ignores all SIGPIPE signals for this to work correctly, this can be done in the main program with:

  #include <signal.h>
  ...
  signal(SIGPIPE, SIG_IGN);

Definition at line 1696 of file mcs.hh.


Public Member Functions

void Close ()
 Close the socket.
string getline ()
 Reads from a socket until a newline is encountered.
Socketoperator= (const Socket &)
 Declared to avoid using of default assignment operator.
void print (string s)
 Writes a string in the socket adding a newline.
unsigned int read (void *buf, unsigned int count)
 Reads data from the socket and write in a buffer.
unsigned int recvData (ofstream &stream)
 Receive data and write to a ofstream object.
unsigned int recvData (int filedes)
 Receive data and write to a file descriptor.
unsigned int recvData (string filename)
 Receive data and write to a file.
unsigned int recvData (char **buffer, unsigned int maxsize)
 Receive data and store in a buffer.
void sendData (Serializable *from)
 Send a block of data through the socket.
 Socket (int sockfd, unsigned int readTimeout=1000, unsigned int writeTimeout=1000, void *ssl_ctx=NULL)
 Constructor based on an already existent socket.
 Socket (string host, unsigned short int port=0, unsigned int readTimeout=1000, unsigned int writeTimeout=1000, bool ssl=false)
 Constructor, to create a new connection.
 Socket (const Socket &)
 Declared to avoid using of default copy constructor.
unsigned int write (void *buf, unsigned int count)
 Reads data from a buffer and write in the socket.
 ~Socket ()
 Destructor.

Static Public Member Functions

static void set_struct_timeval (unsigned int millisec, struct timeval *time)
 Fill a "struct timeval" with the given interval.

Protected Member Functions

bool chkRecv (bool chkDataAvailable=false, enum ThrowExceptions throwexc=THROW)
 Check if you can read data from this socket.
bool chkSend (enum ThrowExceptions throwexc=THROW)
 Check if you can send data through this socket.

Private Member Functions

void initialize_ssl_bio (string keyfile)
unsigned int recvChunk (Buffer *buf)
 Same as recvChunk(void*, unsigned int) but stores data in the AccumBuffer object passed as parameter.
unsigned int recvChunk (void *buf, unsigned int size)
 Receive a chunk of binary data from the socket.
void sendChunk (void *buf, unsigned int size)
 Send a chunk of binary data through the socket.
int socketToHost (unsigned short port)
 Connect to the host.

Private Attributes

fd_set fds
 Set of socket file descriptors, used with select().
 MCS_DEBUG_ALLOC
unsigned short int port
 Port on which this socket will connect.
struct timeval readto
 Timeout interval for read operations.
BIO * sbio
 Pointer to a SSL BIO object.
int sockfd
 C socket file descriptor.
SSL * ssl
 Pointer to a SSL object.
SSL_CTX * ssl_ctx
 Pointer to a SSL context object, provided in the constructor.
bool use_ssl
 True if the connection should be encrypted.
struct timeval writeto
 Timeout interval fro write operations.

Constructor & Destructor Documentation

mcs::Socket::Socket ( const Socket  ) 

Declared to avoid using of default copy constructor.

This constructor is declared but not implemented. If you try to use it you will get a compilation error.

mcs::Socket::Socket ( string  host,
unsigned short int  port = 0,
unsigned int  readTimeout = 1000,
unsigned int  writeTimeout = 1000,
bool  ssl = false 
)

Constructor, to create a new connection.

This creates a new socket connecting to a specified host and port. It is possible to specify read and write timeout for the newly created socket. This can throw Event exceptions if the connection cannot be made.

Parameters:
host Host name to connect to;
port Port to connect to;
readTimeout Timeout for reading operations, in millisecond;
writeTimeout Timeout for writing operations, in millisecond;
ssl If the socket should use a secure connection.

Definition at line 242 of file Socket.cc.

mcs::Socket::Socket ( int  sockfd,
unsigned int  readTimeout = 1000,
unsigned int  writeTimeout = 1000,
void *  ssl_ctx = NULL 
)

Constructor based on an already existent socket.

This creates a new Socket object based on an already existent socket, provided, for example, from a ServerSocket object. It is possible to specify read and write timeout for the newly created socket.

Parameters:
sockfd The C socket file descriptor;
readTimeout Timeout for reading operations, in millisecond;
writeTimeout Timeout for writing operations, in millisecond.
ssl_ctx The SSL context object, if the socket should use a secure connection.

Definition at line 297 of file Socket.cc.

mcs::Socket::~Socket (  ) 

Destructor.

This flushes the socket with a write timeout, then close the socket.

Definition at line 362 of file Socket.cc.


Member Function Documentation

bool mcs::Socket::chkRecv ( bool  chkDataAvailable = false,
enum ThrowExceptions  throwexc = THROW 
) [protected]

Check if you can read data from this socket.

This is just a check that should be made just before any read from the socket. Depending on the throwexc it will throw a FATAL exception or return false if the remote socket has been closed or timed out.

Parameters:
chkDataAvailable Will set timeout to zero, so that it will only check for data availability without waiting.
throwexc If DONT_THROW will return false instead of throwing an exception.
Returns:
True if its possible to read from the socket, false otherwise.
Exceptions:
FATAL MSG_CALLING_SELECT;
FATAL MSG_TIME_OUT;
FATAL MSG_CALLING_RECV;
FATAL MSG_CLOSED_BY_PEER;
FATAL MSG_UNEXPECTED.

Definition at line 418 of file Socket.cc.

bool mcs::Socket::chkSend ( enum ThrowExceptions  throwexc = THROW  )  [protected]

Check if you can send data through this socket.

This is just a check that should be made just before any write to the socket.

Parameters:
throwexc If DONT_THROW will return false instead of throwing an exception.
Returns:
True if its possible to write in the socket, false otherwise.
Exceptions:
FATAL MSG_CALLING_SELECT;
FATAL MSG_TIME_OUT;
FATAL MSG_UNEXPECTED.

Definition at line 387 of file Socket.cc.

void mcs::Socket::Close (  ) 

Close the socket.

Definition at line 368 of file Socket.cc.

string mcs::Socket::getline (  ) 

Reads from a socket until a newline is encountered.

Internally calls chkRecv, so this can throw exceptions if the socket has been closed or the operation had timed out.

Returns:
a A string containing the data read without the newline.
Warning:
Don't use this if you want to receive binary data.

Definition at line 614 of file Socket.cc.

Socket& mcs::Socket::operator= ( const Socket  ) 

Declared to avoid using of default assignment operator.

Warning:
This operator is declared but not implemented. If you try to use

void mcs::Socket::print ( string  s  ) 

Writes a string in the socket adding a newline.

Internally calls chkSend, so this can throw exceptions if the socket has been closed.

Parameters:
s The string containing the data to be send.
Warning:
Don't use this if you want to send binary data.

Definition at line 629 of file Socket.cc.

unsigned int mcs::Socket::read ( void *  buf,
unsigned int  count 
)

Reads data from the socket and write in a buffer.

This will read count bytes from the socket and write in the memory buffer pointed by buf. It checks socket availability with chkRecv().

Parameters:
buf Buffer to store data;
count Number of bytes to read from the socket.
Returns:
Number of bytes actually read.
Exceptions:
FATAL MSG_CLOSED_BY_PEER.

Definition at line 545 of file Socket.cc.

unsigned int mcs::Socket::recvChunk ( Buffer buf  )  [private]

Same as recvChunk(void*, unsigned int) but stores data in the AccumBuffer object passed as parameter.

Definition at line 519 of file Socket.cc.

unsigned int mcs::Socket::recvChunk ( void *  buf,
unsigned int  size 
) [private]

Receive a chunk of binary data from the socket.

This method receives a chunk of binary data from another mcs::Socket object attached to the other side of the socket, using the MCS protocol. On the other side the Socket object should send data with the Socket.sendChunk() method.

Parameters:
buf Pointer to a buffer where store data, this buffer must be big at least "size" bytes;
size Size of data received.
Exceptions:
MCS_FATAL MSG_PROTOCOL;
MCS_FATAL MSG_NOT_ENOUGH_SPACE.

Definition at line 495 of file Socket.cc.

unsigned int mcs::Socket::recvData ( ofstream &  stream  ) 

Receive data and write to a ofstream object.

Parameters:
stream C++ ofstream object.
Returns:
Size of the data written in the file.
Exceptions:
MCS_FATAL MSG_PROTOCOL;

Definition at line 732 of file Socket.cc.

unsigned int mcs::Socket::recvData ( int  filedes  ) 

Receive data and write to a file descriptor.

Parameters:
filedes libc's file descriptor.
Returns:
Size of the data written in the file.
Exceptions:
MCS_FATAL MSG_PROTOCOL;

Definition at line 756 of file Socket.cc.

unsigned int mcs::Socket::recvData ( string  filename  ) 

Receive data and write to a file.

Parameters:
filename File to be written.
Returns:
Size of the data written in the file.
Exceptions:
MCS_FATAL MSG_PROTOCOL;
MCS_FATAL MSG_CANT_OPEN_FILE.

Definition at line 722 of file Socket.cc.

unsigned int mcs::Socket::recvData ( char **  buffer,
unsigned int  maxsize 
)

Receive data and store in a buffer.

If the buffer is not yet created it will be automatically allocated. In this case it is the user responsibility to free the buffer after use. Note that this buffer will contin all data, not just a chunk. The method will consider the buffer allocated if buffer is not NULL, and will write in that buffer. The second parameter tells the size of the buffer, and must be large enough to store all data otherwise an exception will be thrown.

Parameters:
buffer Address of a pointer to the buffer;
maxsize Size of the buffer, if already allocated.
Returns:
Size of the data written in the buffer.
Exceptions:
MCS_FATAL MSG_PROTOCOL;
MCS_FATAL MSG_NOT_ENOUGH_SPACE.

Definition at line 661 of file Socket.cc.

void mcs::Socket::sendChunk ( void *  buf,
unsigned int  size 
) [private]

Send a chunk of binary data through the socket.

This method send a chunk of binary data to another mcs::Socket object attached to the other side of the socket, using the MCS protocol. On the other side the Socket object should be waiting for data in the Socket.recvChunk() method.

Parameters:
buf Pointer to data to send;
size Size of the buffer.
Exceptions:
WARN MSG_SEND_ABORT_BY_RECEIVER.

Definition at line 477 of file Socket.cc.

void mcs::Socket::sendData ( Serializable from  ) 

Send a block of data through the socket.

The data must be handled through a Serializable object or a derived class.

Parameters:
from Addrees of a Serializable object used to send data.

Definition at line 636 of file Socket.cc.

void mcs::Socket::set_struct_timeval ( unsigned int  millisec,
struct timeval *  time 
) [static]

Fill a "struct timeval" with the given interval.

Parameters:
millisec Interval in milliseconds.
time Address of a "struct timeval".

Definition at line 781 of file Socket.cc.

int mcs::Socket::socketToHost ( unsigned short  port  )  [private]

Connect to the host.

This method tries to connect to the host specified in the constructor, to the port specified as parameter.

Note:
The timeout interval to connect to a remote host is not the one you specified in the constructor, but is set by the operating system.
Parameters:
port Port to connect to.
Returns:
C socket file descriptor.
Exceptions:
FATAL MSG_CANT_CONNECT_TO_HOST.

Definition at line 332 of file Socket.cc.

unsigned int mcs::Socket::write ( void *  buf,
unsigned int  count 
)

Reads data from a buffer and write in the socket.

This will read count bytes from the buffer pointed by buf and write in the socket. It checks socket availability with chkSend().

Parameters:
buf Buffer from which read data to send;
count Number of bytes to read from the buffer.
Returns:
Number of bytes actually written in the socket.
Exceptions:
FATAL MSG_CALLING_SEND

Definition at line 587 of file Socket.cc.


Member Data Documentation

fd_set mcs::Socket::fds [private]

Set of socket file descriptors, used with select().

Definition at line 1705 of file mcs.hh.

unsigned short int mcs::Socket::port [private]

Port on which this socket will connect.

Definition at line 1721 of file mcs.hh.

struct timeval mcs::Socket::readto [read, private]

Timeout interval for read operations.

Definition at line 1727 of file mcs.hh.

BIO* mcs::Socket::sbio [private]

Pointer to a SSL BIO object.

Definition at line 1715 of file mcs.hh.

int mcs::Socket::sockfd [private]

C socket file descriptor.

Definition at line 1702 of file mcs.hh.

SSL* mcs::Socket::ssl [private]

Pointer to a SSL object.

Definition at line 1712 of file mcs.hh.

SSL_CTX* mcs::Socket::ssl_ctx [private]

Pointer to a SSL context object, provided in the constructor.

Definition at line 1709 of file mcs.hh.

bool mcs::Socket::use_ssl [private]

True if the connection should be encrypted.

Definition at line 1724 of file mcs.hh.

struct timeval mcs::Socket::writeto [read, private]

Timeout interval fro write operations.

Definition at line 1730 of file mcs.hh.


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

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012