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 /* DcSocketServer.h: interface for the CdcSocketServer class. */ 00021 00022 #ifndef __CDC_SOCKETSERVER 00023 #define __CDC_SOCKETSERVER 00024 00025 #include "../DcGlobals.h" 00026 #include "../DcParameters.h" 00027 00028 #include "DcSocket.h" 00029 00030 #define DEFAULT_PORT 80 00031 00032 /** 00033 * This class extends CdcSocket, it implements a server socket. This socket can 00034 * listen to an incoming connection on a specific port, and creates a connection 00035 * to the client with a CdcSocket object. 00036 * The only way to create an object from this class is to call 00037 * <b>CdcSocketserver::GetSocketServer</b>. You can create just one object from 00038 * this class (singleton), <b> and the user has to delete the object when she is 00039 * done </b>. 00040 * 00041 * <p> 00042 * @author Erez Bibi 00043 * @version 1.0 00044 */ 00045 class CdcSocketServer : public CdcSocket 00046 { 00047 private: 00048 /** Static objects counter. */ 00049 static int nObjCounter; 00050 00051 protected: 00052 /** 00053 * Empty constructor. The only way to create object from this class, is 00054 * calling the static function GetSocketServer. 00055 */ 00056 CdcSocketServer () {}; 00057 00058 public: 00059 /** 00060 * The Destructor just decrease the object counter by one. 00061 */ 00062 virtual ~CdcSocketServer () 00063 { nObjCounter--; } 00064 00065 /** 00066 * This is a static function to create (just one) dynamically allocated 00067 * object from this class, and get a pointer to the object. The object is 00068 * initialized to listen to a port. 00069 * <p> 00070 * <b> The user has to delete this object when he is done. </b> 00071 * <p> 00072 * @param port The port to listen to. 00073 * @return (CdcSocketServer*) Pointer to a new SocketServer object. 00074 * @throw (CdcException) Error number from the OS socket API, 00075 * or 'More than one Object' exception. 00076 */ 00077 static CdcSocketServer* GetSocketServer (int port) throw (CdcException); 00078 static CdcSocketServer* GetSocketServer () throw (CdcException); 00079 00080 /** 00081 * This function listens to the port, until a client connects to the server. 00082 * Then it returns a pointer to a CdcSocket object that is connected to this 00083 * client. The function blocks infinitely. 00084 * <p> 00085 * <b> The user has to delete the returned object when he is done. </b> 00086 * <p> 00087 * @return (CdcSocket*) A pointer to a new CdcSocket object. 00088 * @throw (CdcException) Error number from the OS socket API. 00089 */ 00090 CdcSocket* Listen () throw (CdcException); 00091 00092 /** 00093 * This function is the same as <b>Listen</b> but it returns after 00094 * <b>wait</b> mSec. If there was no connection, it returns <b>NULL</b>. 00095 * Internally it uses the <b>select</b> command. 00096 * <p> 00097 * @param wait The time to wait for a connection [mSec]. If the 00098 * time is 0, the function just poll the socket for a connection. 00099 * 0 is the default. 00100 * @return (CdcSocket*) A pointer to a new CdcSocket object. or NULL if 00101 * there was no connection. 00102 * @throw (CdcException) Error number from the OS socket API. 00103 */ 00104 CdcSocket* ListenNonBlock (int wait = 0) throw (CdcException); 00105 }; 00106 00107 #endif /* __CDC_SOCKETSERVER */