• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Windows/DcSocket.h

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 Beesnest is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 /* DcSocket.h: interface for the CdcSocket class. */
00021 
00022 /**
00023  * This class is a basic socket for the HTTP server, its a wrapper of the OS
00024  * socket. the user can get a pointer to CdcSocket object (that was dynamically
00025  * allocated) just from the <b>CdcSocketServer::Listen</b>. The socket then is
00026  * already connected to the client. The user has to delete this object when he
00027  * is done. This class can be used just from a <b>CdcRequest</b> or
00028  * <b>CdcResponse</b> objects (the HTTP abstraction).
00029  *
00030  * <p>
00031  * There is a timeout limit for the reading operation (blocking operation).
00032  *
00033  * <p>
00034  * This class is  trying to protect from a Buffer Overflow Attack. Before the
00035  * user starts to read a new message, one has to call <b>StartNewRequest</b>
00036  * (even though buffer overflow is impossible here).
00037  *
00038  * <p>
00039  * @author Erez Bibi
00040  * @version 1.0
00041  */
00042 
00043 #ifndef __CDC_SOCKET
00044 #define __CDC_SOCKET
00045 
00046 #include "../DcGlobals.h"
00047 #include "../DcParameters.h"
00048 #include "../DcExceptionSocket.h"
00049 
00050 
00051 class CdcSocket
00052 {
00053 private:
00054     /** Buffer for the incoming message (dynamically allocated). */
00055     char* aBuffer;
00056 
00057     /** The length of the current request (for buffer overflow protection). */
00058     int nReqLength;
00059 
00060     /** The client address, as a string. */
00061     CdcString sClientIP;
00062 
00063     /** The maximum length of a request. */
00064     static int nReqMaxLength;
00065 
00066     /** The time [mSec] to wait for a request. */
00067     static int nWaitReqTime;
00068 
00069 protected:
00070     /**
00071      * Socket address of the client.
00072      */
00073     SOCKADDR_IN oClient;
00074 
00075     /**
00076      * The socket itself.
00077      */
00078     SOCKET oSocket;
00079 
00080     /**
00081      * Some static communication layer data Struct.
00082      */
00083     static WSADATA oData;
00084 
00085 protected:
00086     /**
00087      * The only constructor of this object is protected. the user cannot create
00088      * objects directly. The constructor just resets the data fields.
00089      * <p>
00090      * @throw (CdcException) When cannot dynamically allocate the incoming
00091      * buffer.
00092      */
00093     CdcSocket () throw (CdcException);
00094     CdcSocket (const CdcSocket& soc) throw (CdcException); /* Copy cator */
00095     void operator= (const CdcSocket& soc);
00096 
00097     /**
00098      * This static function allocates a new CdcSocket object by calling the
00099      * private constructor, then it initializes the socket, and returns a
00100      * pointer to it. This is the only way to get a new CdcSocket object.
00101      * <p>
00102      * <b> The user has to delete the object when he is done. </b>
00103      * <p>
00104      * @param soc The socket to wrap.
00105      * @param from (Optional) Struct that holds the info of the client.
00106      * @return (CdcSocket*) Pointer to the new CdcSocket object.
00107      * @throw (CdcException) Error code and message from the OS socket API.
00108      */
00109     static CdcSocket* GetNewSocket (SOCKET soc, SOCKADDR_IN from) throw (CdcException);
00110 
00111 
00112     /**
00113      * This functions initializes the socket (connects it with the OS socket
00114      * Struct, and set the receive timeout).
00115      * <p>
00116      * @param soc The socket to wrap.
00117      * @param from (Optional) Struct that holds the info of the client.
00118      * @throws (CdcException) Error code and message from the OS socket API.
00119      */
00120     virtual void Init (SOCKET soc, SOCKADDR_IN from) throw (CdcException);
00121     virtual void Init (SOCKET soc) throw (CdcException);
00122 
00123 
00124 public:
00125     /**
00126      * The Destructor frees all the system resources used be the socket.
00127      */
00128     virtual ~CdcSocket ();
00129 
00130     /**
00131      * The reading function. The socket reads into a pre-allocated buffer
00132      * (char*). It is the user responsibility to transfer the maximum buffer
00133      * length. this is a blocking function.
00134      * <p>
00135      * @param buff Pointer to the buffer to read to.
00136      * @param lng The maximum buffer length.
00137      * @return (int) The length of data received.
00138      * @throw (CdcException) Error code and message from the OS socket API.
00139      */
00140      virtual int Read (char* buff, int lng) throw (CdcException);
00141 
00142      /**
00143       * This Read function appends to the end of <b>msg</b> the data arrives
00144       * from the socket, as a string. This function uses the dynamically
00145       * allocated buffer, the maximum message length is from the CdcParameters
00146       * class.
00147       * <p>
00148       * @param msg Referance to a string object.
00149       * @return (int) The length of data received.
00150       * @throw (CdcException) Error code and message from the OS socket API.
00151       */
00152      virtual int Read (CdcString& msg) throw (CdcException);
00153 
00154     /**
00155      * The writing function. The socket writes the content of a buffer (char*).
00156      * <p>
00157      * @param buff Pointer to the buffer to send.
00158      * @param lng The length of the buffer.
00159      * @return (int) The length of data that was actually been sent.
00160      * @throw (CdcException) Error code and message from the OS socket API.
00161      */
00162     virtual int Write (const char* buff, int lng) throw (CdcException);
00163 
00164 
00165     /**
00166      * Get the client address. if no client data exists, the function does
00167      * nothing to the address Struct.
00168      * <p>
00169      * @return (const CdcString&) Reference to the IP address of the client
00170      *  (or empty string if client data does not exist).
00171      *  Note: This is a reference return value, also the empty string.
00172      *  DO NOT CHANGE IT!!!
00173      */
00174     const CdcString& GetClientAddress ();
00175 
00176 
00177     /**
00178      * Gets the client port number.
00179      * <p>
00180      * @return (int) The port number of the client, or 0 if there is no client
00181      * data.
00182      */
00183     int GetClientPort () const;
00184 
00185 
00186     /**
00187      * Resets the Request Length. Call this function before starting to read a
00188      * new request.
00189      */
00190     void StartNewRequest () { nReqLength = 0; }
00191 
00192     /* Static functions. */
00193 
00194     /**
00195      * Static function to initialize the OS socket library.
00196      * <p>
00197      * @param max_req_len The longest request allowed.
00198      * @param wait_rec_time The time [mSec] to wait for request.
00199      * @throw (CdcException) Error code and message from the OS socket API.
00200      */
00201     static void Startup (int max_req_len = 1024,
00202         int wait_req_time = 10000) throw (CdcException);
00203 
00204     /**
00205      * Static function to release the OS socket library.
00206      * <p>
00207      * @throw (CdcException) Error code and message from the OS socket API.
00208      */
00209     static void Cleanup () throw (CdcException);
00210 };
00211 
00212 
00213 #endif /* __CDC_SOCKET */
00214 

Generated on Mon Oct 11 2010 16:23:25 for Beesnest by  doxygen 1.7.2