• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/net/PooledSocketSet.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _poolsocketset_h
00018 #define _poolsocketset_h
00019 
00020 #define FD_SETSIZE 64
00021 
00022 #include <spl/Debug.h>
00023 #include <spl/Memory.h>
00024 #include <spl/threading/Mutex.h>
00025 #include <spl/net/PortListener.h>
00026 #include <spl/net/SocketSet.h>
00027 #include <spl/collection/Vector.h>
00028 
00035 
00036 
00037 class PooledSocketSet : public ISocketService
00038 {
00039 private:
00040         // Copy constructor doesn't make sense for this class
00041         inline PooledSocketSet(const PooledSocketSet& pss) {}
00042         inline void operator =(const PooledSocketSet& pss) {}
00043 
00044 protected:
00045         Vector<SocketSetPtr> m_sets;
00046         int m_socketCount;
00047         Mutex m_setsMtx;
00048 
00049         void RemoveSocket( TcpSocket& sp );
00050 
00051 public:
00052         PooledSocketSet(int poolSize = 1024/FD_SETSIZE);
00053         virtual ~PooledSocketSet();
00054 
00055         virtual void AddSocket( IStreamReadListenerPtr listener, TcpSocketPtr sp );
00056         virtual void Close();
00057         virtual void CloseAndDelete();
00058         virtual int SocketCount() const;
00059         virtual void Broadcast( const Array<byte>& buf, const int len );
00060 
00061         virtual void Join(int timeoutms);
00062         virtual void Join();
00063 
00064 #if defined(DEBUG) || defined(_DEBUG)
00065         void CheckMem() const;
00066         void ValidateMem() const;
00067 #endif
00068 };
00069 
00070 REGISTER_TYPEOF(34, PooledSocketSet);
00071 
00074 class PooledSocketServer : public ISocketService, public IPortListenerListener
00075 {
00076 private:
00077         // Copy constructor doesn't make sense for this class
00078         inline PooledSocketServer(const PooledSocketServer& pss) 
00079         : m_listener(0)
00080         {
00081         }
00082 
00083         inline void operator =(const PooledSocketServer& pss) {}
00084 
00085 protected:
00086         PooledSocketSet m_ss;
00087         PortListener m_listener;
00088         IServerConnectionFactory *m_conFactory;
00089 
00090         virtual void IPortListener_OnConnect( TcpSocketPtr sock );
00091         virtual void IPortListener_OnStop();
00092 
00093 public:
00094         PooledSocketServer(IServerConnectionFactory *conFactory, int serverPort, int poolSize = 1024/FD_SETSIZE);
00095         virtual ~PooledSocketServer();
00096 
00097         virtual void AddSocket( IStreamReadListenerPtr listener, TcpSocketPtr sp );
00098         virtual void Close();
00099         virtual void CloseAndDelete();
00100         virtual int SocketCount() const;
00101         virtual void Broadcast( const Array<byte>& buf, const int len );
00102 
00103         virtual void Join(int timeoutms);
00104         virtual void Join();
00105 
00106 #if defined(DEBUG) || defined(_DEBUG)
00107         virtual void CheckMem() const;
00108         virtual void ValidateMem() const;
00109 #endif
00110 };
00111 
00112 REGISTER_TYPEOF(36, PooledSocketServer);
00113 
00116 #endif