MCS
0.3.3-alpha7
|
Manage TCP sockets. More...
#include <mcs.hh>
Public Member Functions | |
void | Close () |
Close the socket. More... | |
string | getline () |
Reads from a socket until a newline is encountered. More... | |
Socket & | operator= (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... | |
![]() | |
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... | |
HostInfo & | operator= (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... | |
![]() | |
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 | |
![]() | |
string | host |
string | ipaddr |
struct sockaddr_in | sin |
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:
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 | ( | ) |
|
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. |
|
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. |
|
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. |
|
private |
Same as recvChunk(void*, unsigned int) but stores data in the AccumBuffer object passed as parameter.
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.
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. |
unsigned int mcs::Socket::recvData | ( | string | filename | ) |
unsigned int mcs::Socket::recvData | ( | int | filedes | ) |
unsigned int mcs::Socket::recvData | ( | ofstream & | stream | ) |
|
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. |
|
static |
|
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 |
|
private |
|
private |
|
private |
|
private |
|
private |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha7
|