#include <mcs.hh>
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. | |
Socket & | operator= (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. |
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.
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. |
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.
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. |
mcs::Socket::~Socket | ( | ) |
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.
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. |
FATAL | MSG_CALLING_SELECT; | |
FATAL | MSG_TIME_OUT; | |
FATAL | MSG_CALLING_RECV; | |
FATAL | MSG_CLOSED_BY_PEER; | |
FATAL | MSG_UNEXPECTED. |
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.
throwexc | If DONT_THROW will return false instead of throwing an exception. |
FATAL | MSG_CALLING_SELECT; | |
FATAL | MSG_TIME_OUT; | |
FATAL | MSG_UNEXPECTED. |
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.
Declared to avoid using of default assignment operator.
void mcs::Socket::print | ( | string | s | ) |
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().
buf | Buffer to store data; | |
count | Number of bytes to read from the socket. |
FATAL | MSG_CLOSED_BY_PEER. |
unsigned int mcs::Socket::recvChunk | ( | Buffer * | buf | ) | [private] |
Same as recvChunk(void*, unsigned int) but stores data in the AccumBuffer object passed as parameter.
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.
buf | Pointer to a buffer where store data, this buffer must be big at least "size" bytes; | |
size | Size of data received. |
MCS_FATAL | MSG_PROTOCOL; | |
MCS_FATAL | MSG_NOT_ENOUGH_SPACE. |
unsigned int mcs::Socket::recvData | ( | ofstream & | stream | ) |
unsigned int mcs::Socket::recvData | ( | int | filedes | ) |
unsigned int mcs::Socket::recvData | ( | string | filename | ) |
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.
buffer | Address of a pointer to the buffer; | |
maxsize | Size of the buffer, if already allocated. |
MCS_FATAL | MSG_PROTOCOL; | |
MCS_FATAL | MSG_NOT_ENOUGH_SPACE. |
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.
buf | Pointer to data to send; | |
size | Size of the buffer. |
WARN | MSG_SEND_ABORT_BY_RECEIVER. |
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.
from | Addrees of a Serializable object used to send data. |
void mcs::Socket::set_struct_timeval | ( | unsigned int | millisec, | |
struct timeval * | time | |||
) | [static] |
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.
port | Port to connect to. |
FATAL | MSG_CANT_CONNECT_TO_HOST. |
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().
buf | Buffer from which read data to send; | |
count | Number of bytes to read from the buffer. |
FATAL | MSG_CALLING_SEND |
fd_set mcs::Socket::fds [private] |
unsigned short int mcs::Socket::port [private] |
struct timeval mcs::Socket::readto [read, private] |
BIO* mcs::Socket::sbio [private] |
int mcs::Socket::sockfd [private] |
SSL* mcs::Socket::ssl [private] |
SSL_CTX* mcs::Socket::ssl_ctx [private] |
bool mcs::Socket::use_ssl [private] |
struct timeval mcs::Socket::writeto [read, private] |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha3
|