Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes

CdcSocket Class Reference

This class is a basic socket for the HTTP server, its a wrapper of the OS socket. More...

#include <DcSocket.h>

Inheritance diagram for CdcSocket:
CdcSocketServer

List of all members.

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 CdcStringGetClientAddress ()
 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 CdcSocketGetNewSocket (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.

Detailed Description

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).

Author:
Erez Bibi
Version:
1.0

Definition at line 51 of file DcSocket.h.


Constructor & Destructor Documentation

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.

Exceptions:
(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.


Member Function Documentation

void CdcSocket::Cleanup (  ) throw (CdcException) [static]

Static function to release the OS socket library.

Exceptions:
(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.

Returns:
(const CdcString&) Reference to the IP address of the client (or empty string if client data does not exist). Note: This is a reference return value, also the empty string. DO NOT CHANGE IT!!!

Definition at line 134 of file DcSocket.cpp.

int CdcSocket::GetClientPort (  ) const

Gets the client port number.

Returns:
(int) The port number of the client, or 0 if there is no client data.

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.

Parameters:
socThe socket to wrap.
from(Optional) Struct that holds the info of the client.
Returns:
(CdcSocket*) Pointer to the new CdcSocket object.
Exceptions:
(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).

Parameters:
socThe socket to wrap.
from(Optional) Struct that holds the info of the client.
Exceptions:
(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.

Parameters:
buffPointer to the buffer to read to.
lngThe maximum buffer length.
Returns:
(int) The length of data received.
Exceptions:
(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.

Parameters:
msgReferance to a string object.
Returns:
(int) The length of data received.
Exceptions:
(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.

Parameters:
max_req_lenThe longest request allowed.
wait_rec_timeThe time [mSec] to wait for request.
Exceptions:
(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*).

Parameters:
buffPointer to the buffer to send.
lngThe length of the buffer.
Returns:
(int) The length of data that was actually been sent.
Exceptions:
(CdcException)Error code and message from the OS socket API.

Definition at line 121 of file DcSocket.cpp.


Member Data Documentation

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.


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