This class is a basic socket for the HTTP server, its a wrapper of the OS socket. More...
#include <DcSocket.h>
Public Member Functions | |
virtual | ~CdcSocket () |
The Destructor frees all the system resources used be the socket. | |
virtual int | Read (char *buff, int lng) throw (CdcException) |
The reading function. | |
virtual int | Read (CdcString &msg) throw (CdcException) |
This Read function appends to the end of msg the data arrives from the socket, as a string. | |
virtual int | Write (const char *buff, int lng) throw (CdcException) |
The writing function. | |
const CdcString & | GetClientAddress () |
Get the client address. | |
int | GetClientPort () const |
Gets the client port number. | |
void | StartNewRequest () |
Resets the Request Length. | |
Static Public Member Functions | |
static void | Startup (int max_req_len=1024, int wait_req_time=10000) throw (CdcException) |
Static function to initialize the OS socket library. | |
static void | Cleanup () throw (CdcException) |
Static function to release the OS socket library. | |
Protected Member Functions | |
CdcSocket () throw (CdcException) | |
The only constructor of this object is protected. | |
CdcSocket (const CdcSocket &soc) throw (CdcException) | |
void | operator= (const CdcSocket &soc) |
virtual void | Init (SOCKET soc, SOCKADDR_IN from) throw (CdcException) |
This functions initializes the socket (connects it with the OS socket Struct, and set the receive timeout). | |
virtual void | Init (SOCKET soc) throw (CdcException) |
Static Protected Member Functions | |
static CdcSocket * | GetNewSocket (SOCKET soc, SOCKADDR_IN from) throw (CdcException) |
This static function allocates a new CdcSocket object by calling the private constructor, then it initializes the socket, and returns a pointer to it. | |
Protected Attributes | |
SOCKADDR_IN | oClient |
Socket address of the client. | |
SOCKET | oSocket |
The socket itself. | |
Static Protected Attributes | |
static WSADATA | oData |
Some static communication layer data Struct. |
This class is a basic socket for the HTTP server, its a wrapper of the OS socket.
the user can get a pointer to CdcSocket object (that was dynamically allocated) just from the CdcSocketServer::Listen. The socket then is already connected to the client. The user has to delete this object when he is done. This class can be used just from a CdcRequest or CdcResponse objects (the HTTP abstraction).
There is a timeout limit for the reading operation (blocking operation).
This class is trying to protect from a Buffer Overflow Attack. Before the user starts to read a new message, one has to call StartNewRequest (even though buffer overflow is impossible here).
Definition at line 51 of file DcSocket.h.
CdcSocket::CdcSocket | ( | ) | throw (CdcException) [protected] |
The only constructor of this object is protected.
the user cannot create objects directly. The constructor just resets the data fields.
(CdcException) | When cannot dynamically allocate the incoming buffer. |
Definition at line 33 of file DcSocket.cpp.
CdcSocket::CdcSocket | ( | const CdcSocket & | soc ) | throw (CdcException) [protected] |
CdcSocket::~CdcSocket | ( | ) | [virtual] |
The Destructor frees all the system resources used be the socket.
Definition at line 47 of file DcSocket.cpp.
void CdcSocket::Cleanup | ( | ) | throw (CdcException) [static] |
Static function to release the OS socket library.
(CdcException) | Error code and message from the OS socket API. |
Definition at line 171 of file DcSocket.cpp.
const CdcString & CdcSocket::GetClientAddress | ( | ) |
Get the client address.
if no client data exists, the function does nothing to the address Struct.
Definition at line 134 of file DcSocket.cpp.
int CdcSocket::GetClientPort | ( | ) | const |
Gets the client port number.
Definition at line 150 of file DcSocket.cpp.
CdcSocket * CdcSocket::GetNewSocket | ( | SOCKET | soc, |
SOCKADDR_IN | from | ||
) | throw (CdcException) [static, protected] |
This static function allocates a new CdcSocket object by calling the private constructor, then it initializes the socket, and returns a pointer to it.
This is the only way to get a new CdcSocket object.
The user has to delete the object when he is done.
soc | The socket to wrap. |
from | (Optional) Struct that holds the info of the client. |
(CdcException) | Error code and message from the OS socket API. |
Definition at line 58 of file DcSocket.cpp.
void CdcSocket::Init | ( | SOCKET | soc ) | throw (CdcException) [protected, virtual] |
Definition at line 74 of file DcSocket.cpp.
void CdcSocket::Init | ( | SOCKET | soc, |
SOCKADDR_IN | from | ||
) | throw (CdcException) [protected, virtual] |
This functions initializes the socket (connects it with the OS socket Struct, and set the receive timeout).
soc | The socket to wrap. |
from | (Optional) Struct that holds the info of the client. |
(CdcException) | Error code and message from the OS socket API. |
Definition at line 66 of file DcSocket.cpp.
void CdcSocket::operator= | ( | const CdcSocket & | soc ) | [protected] |
int CdcSocket::Read | ( | char * | buff, |
int | lng | ||
) | throw (CdcException) [virtual] |
The reading function.
The socket reads into a pre-allocated buffer (char*). It is the user responsibility to transfer the maximum buffer length. this is a blocking function.
buff | Pointer to the buffer to read to. |
lng | The maximum buffer length. |
(CdcException) | Error code and message from the OS socket API. |
Definition at line 93 of file DcSocket.cpp.
int CdcSocket::Read | ( | CdcString & | msg ) | throw (CdcException) [virtual] |
This Read function appends to the end of msg the data arrives from the socket, as a string.
This function uses the dynamically allocated buffer, the maximum message length is from the CdcParameters class.
msg | Referance to a string object. |
(CdcException) | Error code and message from the OS socket API. |
Definition at line 112 of file DcSocket.cpp.
void CdcSocket::StartNewRequest | ( | ) | [inline] |
Resets the Request Length.
Call this function before starting to read a new request.
Definition at line 190 of file DcSocket.h.
void CdcSocket::Startup | ( | int | max_req_len = 1024 , |
int | wait_req_time = 10000 |
||
) | throw (CdcException) [static] |
Static function to initialize the OS socket library.
max_req_len | The longest request allowed. |
wait_rec_time | The time [mSec] to wait for request. |
(CdcException) | Error code and message from the OS socket API. |
Definition at line 156 of file DcSocket.cpp.
int CdcSocket::Write | ( | const char * | buff, |
int | lng | ||
) | throw (CdcException) [virtual] |
The writing function.
The socket writes the content of a buffer (char*).
buff | Pointer to the buffer to send. |
lng | The length of the buffer. |
(CdcException) | Error code and message from the OS socket API. |
Definition at line 121 of file DcSocket.cpp.
SOCKADDR_IN CdcSocket::oClient [protected] |
Socket address of the client.
Definition at line 73 of file DcSocket.h.
WSADATA CdcSocket::oData [static, protected] |
Some static communication layer data Struct.
Definition at line 83 of file DcSocket.h.
SOCKET CdcSocket::oSocket [protected] |
The socket itself.
Definition at line 78 of file DcSocket.h.