Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

simpleserver.h

Go to the documentation of this file.
00001 /********************************************************************
00002        Copyright (c) 2001, Lee Patterson & Ant Works Software
00003        http://ssobjects.sourceforge.net
00004 
00005        created  :  December <19, 2000
00006        filename :  simpleserver.h
00007        author   :  Lee Patterson (lee@antws.com)
00008        purpose  :  Simple server is a class that you dirive from to
00009                    create a fully functional server. The simple server
00010                    class handles creating a listen socket, reading a full
00011                    packet from a client connection, idling the server.
00012 
00013                    You only need to override processSingleMsg() in order 
00014                    to process packets that come from a client socket.
00015                    See the section "user overrideables" below for what 
00016                    methods you would tipically call.
00017 
00018                    Override idle() method if you want to do things on a 
00019                    regular basis without waiting for a packet from a client.
00020 
00021                    The two ways of constructing a simple server are for a 
00022                    single threaded server, and a multi threaded server.
00023 
00024                    NOTE: Any shared data in your dirived classes should be 
00025                    in the SimpleManager and passed into the simple server. 
00026                    If you idle in the manager, you should idle the same way
00027                    in the server. The simple server however should check if 
00028                    it is running multi threaded, and only idle shared data
00029                    if it is running single threaded.
00030 
00031 *********************************************************************/
00032 
00033 #ifndef SIMPLESERVER_H
00034 #define SIMPLESERVER_H
00035 
00036 #ifdef _WIN32
00037 # include <time.h>
00038 #else
00039 # include <sys/time.h>
00040 # include <unistd.h>
00041 #endif
00042 
00043 #include "packetbuffer.h"
00044 #include "flags.h"
00045 #include "linkedlist.h"
00046 #include "bufferedsocket.h"
00047 #include "threadutils.h"
00048 #include "packetmessageque.h"
00049 #include "serversocket.h"
00050 #include "simplemanager.h"
00051 
00052 namespace ssobjects
00053 {
00054 
00055 #define throwSimpleServerException(m) (throw SimpleServerException(m,__FILE__,__LINE__))
00056 
00060 class SimpleServerException : public GeneralException
00061 {
00062   public: // Constructor/destructor
00063     SimpleServerException(char* pchMessage,const char* pFname,const int iLine) 
00064       : GeneralException(pchMessage,pFname,iLine){};
00065 };
00066 
00067 class ServerSocket;
00068 class ServerHandler;
00069 class SimpleManager;
00070 
00150 class SimpleServer : public ThreadHandler
00151 {
00152     friend SimpleManager;
00153 
00154   public:
00155     enum 
00156       {
00157       MIN_FREQ=10   
00158       };
00159 
00160   public:     //construction
00161     SimpleServer(const SockAddr& saBind,
00162                  const unsigned32 nFreq=1000,
00163                  const unsigned32 nMaxConnections=100);   
00164     SimpleServer(ServerSocket* const psocket,
00165                  const unsigned32 nFreq);                 
00166     virtual ~SimpleServer();
00167         
00168   protected:  //user overridables
00220     virtual void    processSingleMsg(PacketMessage* pmsg) = 0;
00221 
00223     virtual void    idle(unsigned32 nTimer=0) {UNUSED_ALWAYS(nTimer);}
00224       
00225 
00226   public:     //server controls
00227     virtual void    startServer();                                              
00228     virtual void    killServer();                                               
00229     virtual void    pauseIncomingConnections();                                 
00230     virtual void    sendPacket(ServerSocket* psocket,const PacketBuffer& packet);   
00231     virtual void    sendPacket(ServerSocket* psocket,const PacketBuffer* ppacket);  
00232     virtual void    send2All(const PacketBuffer& packet);                           
00233     virtual void    send2All(const PacketBuffer* ppacket);                          
00234     virtual void    removeSocket(ServerSocket* psocket);                        
00235 
00236   public:
00237     static bool     canBind(SockAddr& saBind);                                
00238     unsigned32      getTicks();                                                 
00239     bool            isThreaded() { return m_bUsingThread; }                     
00240 
00241     //nothing servicable beyond this point
00242   protected:
00243     threadReturn    ThreadHandlerProc(void);                //main handler routine
00244     void            processMessages();
00245     bool            processSockets(int iReady);
00246     void            processSelectError();
00247     void            acceptConnection();
00248     int             getMaxFD();                             //return highest connected sock fd
00249     long            getSleepTime();                         //how many usecs to sleep according to idle frequency
00250     void            addMsg(PacketMessage* pmsg) { m_que.add(pmsg); }
00251     void            queClosedMessage(ServerSocket* psocketRemoving);
00252 
00253   private:    //unused overrides
00254     SimpleServer(const SimpleServer& server);
00255     SimpleServer& operator=(const SimpleServer& server);
00256         
00257   protected:
00258     SocketInstance          m_sListen;
00259     SockAddr                m_saServer;
00260     LinkedList<ServerSocket>    m_listClients;
00261     PacketMessageQue        m_que;
00262     unsigned32              m_nIdleFrequency;       //how often idle will be called
00263     unsigned32              m_nMaxCon;
00264     bool                    m_bUsingThread;
00265     bool                    m_bPause;      
00266     unsigned32              m_nSleepTime;
00267     struct timeval          m_tvServerStarted;
00268     fd_set                  m_rset,m_wset;
00269 };
00270 
00271 };
00272 
00273 #endif //SIMPLESERVER_H

Generated on Thu Nov 8 12:39:26 2001 for SimpleServerObjects by doxygen1.2.11 written by Dimitri van Heesch, © 1997-2001