MCS  0.3.3-alpha7
mcs::Socket Class Reference

Manage TCP sockets. More...

#include <mcs.hh>

+ Inheritance diagram for mcs::Socket:

Public Member Functions

void Close ()
 Close the socket. More...
 
string getline ()
 Reads from a socket until a newline is encountered. More...
 
Socketoperator= (const Socket &)
 Declared to avoid using of default assignment operator. More...
 
void print (string s)
 Writes a string in the socket adding a newline. More...
 
unsigned int read (void *buf, unsigned int count)
 Reads data from the socket and write in a buffer. More...
 
unsigned int recvData (char **buffer, unsigned int maxsize)
 Receive data and store in a buffer. More...
 
unsigned int recvData (string filename)
 Receive data and write to a file. More...
 
unsigned int recvData (int filedes)
 Receive data and write to a file descriptor. More...
 
unsigned int recvData (ofstream &stream)
 Receive data and write to a ofstream object. More...
 
void sendData (Serializable *from)
 Send a block of data through the socket. More...
 
 Socket (const Socket &)
 Declared to avoid using of default copy constructor. More...
 
 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. More...
 
 Socket (int sockfd, unsigned int readTimeout=1000, unsigned int writeTimeout=1000, void *ssl_ctx=NULL)
 Constructor based on an already existent socket. More...
 
unsigned int write (void *buf, unsigned int count)
 Reads data from a buffer and write in the socket. More...
 
 ~Socket ()
 Destructor. More...
 
- Public Member Functions inherited from mcs::HostInfo
 HostInfo (const HostInfo &)
 Declared to avoid using of default copy constructor. More...
 
 HostInfo (string host)
 Constructor. More...
 
 HostInfo (int sockfd)
 Constructor, to obtain information from an already connected socket. More...
 
string hostname ()
 Return the host name. More...
 
string ipaddress ()
 Return the host IP address. More...
 
HostInfooperator= (const HostInfo &)
 Declared to avoid using of default assignment operator. More...
 
 ~HostInfo ()
 Destructor. More...
 

Static Public Member Functions

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

Protected Member Functions

bool chkRecv (bool chkDataAvailable=false, enum ThrowExceptions throwexc=THROW)
 Check if you can read data from this socket. More...
 
bool chkSend (enum ThrowExceptions throwexc=THROW)
 Check if you can send data through this socket. More...
 
- Protected Member Functions inherited from mcs::HostInfo
void populate_sockaddr_in ()
 

Private Member Functions

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

Private Attributes

fd_set fds
 Set of socket file descriptors, used with select(). More...
 
 MCS_DEBUG_ALLOC
 
unsigned short int port
 Port on which this socket will connect. More...
 
struct timeval readto
 Timeout interval for read operations. More...
 
int sockfd
 C socket file descriptor. More...
 
bool use_ssl
 True if the connection should be encrypted. More...
 
struct timeval writeto
 Timeout interval fro write operations. More...
 

Additional Inherited Members

- Protected Attributes inherited from mcs::HostInfo
string host
 
string ipaddr
 
struct sockaddr_in sin
 

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 1662 of file mcs.hh.

Constructor & Destructor Documentation

◆ Socket() [1/3]

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.

◆ Socket() [2/3]

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
hostHost name to connect to;
portPort to connect to;
readTimeoutTimeout for reading operations, in millisecond;
writeTimeoutTimeout for writing operations, in millisecond;
sslIf the socket should use a secure connection.

Definition at line 242 of file Socket.cc.

◆ Socket() [3/3]

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
sockfdThe C socket file descriptor;
readTimeoutTimeout for reading operations, in millisecond;
writeTimeoutTimeout for writing operations, in millisecond.
ssl_ctxThe SSL context object, if the socket should use a secure connection.

Definition at line 297 of file Socket.cc.

◆ ~Socket()

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

◆ chkRecv()

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
chkDataAvailableWill set timeout to zero, so that it will only check for data availability without waiting.
throwexcIf DONT_THROW will return false instead of throwing an exception.
Returns
True if its possible to read from the socket, false otherwise.
Exceptions
FATALMSG_CALLING_SELECT;
FATALMSG_TIME_OUT;
FATALMSG_CALLING_RECV;
FATALMSG_CLOSED_BY_PEER;
FATALMSG_UNEXPECTED.

Definition at line 419 of file Socket.cc.

◆ chkSend()

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
throwexcIf DONT_THROW will return false instead of throwing an exception.
Returns
True if its possible to write in the socket, false otherwise.
Exceptions
FATALMSG_CALLING_SELECT;
FATALMSG_TIME_OUT;
FATALMSG_UNEXPECTED.

Definition at line 387 of file Socket.cc.

◆ Close()

void mcs::Socket::Close ( )

Close the socket.

Definition at line 368 of file Socket.cc.

◆ getline()

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 615 of file Socket.cc.

◆ operator=()

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

◆ print()

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
sThe string containing the data to be send.
Warning
Don't use this if you want to send binary data.

Definition at line 630 of file Socket.cc.

◆ read()

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
bufBuffer to store data;
countNumber of bytes to read from the socket.
Returns
Number of bytes actually read.
Exceptions
FATALMSG_CLOSED_BY_PEER.

Definition at line 546 of file Socket.cc.

◆ recvChunk() [1/2]

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
bufPointer to a buffer where store data, this buffer must be big at least "size" bytes;
sizeSize of data received.
Exceptions
MCS_FATALMSG_PROTOCOL;
MCS_FATALMSG_NOT_ENOUGH_SPACE.

Definition at line 496 of file Socket.cc.

◆ recvChunk() [2/2]

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 520 of file Socket.cc.

◆ recvData() [1/4]

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 contain 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
bufferAddress of a pointer to the buffer;
maxsizeSize of the buffer, if already allocated.
Returns
Size of the data written in the buffer.
Exceptions
MCS_FATALMSG_PROTOCOL;
MCS_FATALMSG_NOT_ENOUGH_SPACE.

Definition at line 662 of file Socket.cc.

◆ recvData() [2/4]

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

Receive data and write to a file.

Parameters
filenameFile to be written.
Returns
Size of the data written in the file.
Exceptions
MCS_FATALMSG_PROTOCOL;
MCS_FATALMSG_CANT_OPEN_FILE.

Definition at line 723 of file Socket.cc.

◆ recvData() [3/4]

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

Receive data and write to a file descriptor.

Parameters
filedeslibc's file descriptor.
Returns
Size of the data written in the file.
Exceptions
MCS_FATALMSG_PROTOCOL;

Definition at line 757 of file Socket.cc.

◆ recvData() [4/4]

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

Receive data and write to a ofstream object.

Parameters
streamC++ ofstream object.
Returns
Size of the data written in the file.
Exceptions
MCS_FATALMSG_PROTOCOL;

Definition at line 733 of file Socket.cc.

◆ sendChunk()

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
bufPointer to data to send;
sizeSize of the buffer.
Exceptions
WARNMSG_SEND_ABORT_BY_RECEIVER.

Definition at line 478 of file Socket.cc.

◆ sendData()

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
fromAddrees of a Serializable object used to send data.

Definition at line 637 of file Socket.cc.

◆ set_struct_timeval()

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

Fill a "struct timeval" with the given interval.

Parameters
millisecInterval in milliseconds.
timeAddress of a "struct timeval".

Definition at line 782 of file Socket.cc.

◆ socketToHost()

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
portPort to connect to.
Returns
C socket file descriptor.
Exceptions
FATALMSG_CANT_CONNECT_TO_HOST.

Definition at line 332 of file Socket.cc.

◆ write()

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
bufBuffer from which read data to send;
countNumber of bytes to read from the buffer.
Returns
Number of bytes actually written in the socket.
Exceptions
FATALMSG_CALLING_SEND

Definition at line 588 of file Socket.cc.

Member Data Documentation

◆ fds

fd_set mcs::Socket::fds
private

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

Definition at line 1671 of file mcs.hh.

◆ port

unsigned short int mcs::Socket::port
private

Port on which this socket will connect.

Definition at line 1687 of file mcs.hh.

◆ readto

struct timeval mcs::Socket::readto
private

Timeout interval for read operations.

Definition at line 1693 of file mcs.hh.

◆ sockfd

int mcs::Socket::sockfd
private

C socket file descriptor.

Definition at line 1668 of file mcs.hh.

◆ use_ssl

bool mcs::Socket::use_ssl
private

True if the connection should be encrypted.

Definition at line 1690 of file mcs.hh.

◆ writeto

struct timeval mcs::Socket::writeto
private

Timeout interval fro write operations.

Definition at line 1696 of file mcs.hh.


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

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018