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

spl/net/PortListener.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 _PortListener_h
00018 #define _PortListener_h
00019 
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022 
00023 #ifdef _WINDOWS
00024 #include <spl/configwin32.h>
00025 #else
00026 #include <spl/autoconf/config.h>
00027 #endif
00028 
00029 #include <spl/Memory.h>
00030 #include <spl/net/ServerSocket.h>
00031 #include <spl/threading/Thread.h>
00032 
00040 class IPortListenerListener
00041 {
00042 public:
00043         virtual ~IPortListenerListener();
00044         virtual void IPortListener_OnConnect( TcpSocketPtr sock ) = 0;
00045         virtual void IPortListener_OnStop() = 0;
00046 };
00047 
00048 REGISTER_TYPEOF(38, IPortListenerListener);
00049 
00051 class PortListenerDelegateDispatch : public IMemoryValidate
00052 {
00053 private:
00054         DelegateDispatch m_onStop;
00055         DelegateDispatchOneParameter<TcpSocketPtr> m_onConnect;
00056 
00057 public:
00058         inline PortListenerDelegateDispatch()
00059         : m_onConnect(), m_onStop()
00060         {
00061         }
00062 
00063         virtual ~PortListenerDelegateDispatch();
00064 
00065         inline DelegateDispatch& OnStop() { return m_onStop; }
00066         inline DelegateDispatchOneParameter<TcpSocketPtr>& OnConnect() { return m_onConnect; }
00067 
00068         inline void DispatchOnStop() { m_onStop.Call(); }
00069         inline void DispatchOnConnect(TcpSocketPtr sock) { m_onConnect.Call(sock); }
00070 
00072         void Add(IPortListenerListener *l);
00073 
00074 #ifdef DEBUG
00075         virtual void ValidateMem() const;
00076         virtual void CheckMem() const;
00077 #endif
00078 };
00079 
00080 REGISTER_TYPEOF(40, PortListenerDelegateDispatch);
00081 
00084 class PortListener : public Thread
00085 {
00086 private:
00087         // Copy constructor doesn't make sense for this class
00088         inline PortListener(const PortListener& sock) {}
00089         inline void operator =(const PortListener& sock) {}
00090 
00091 protected:
00092         int m_port;
00093         volatile bool m_running;
00094         ServerSocketPtr m_socket;
00095         PortListenerDelegateDispatch m_listeners;
00096 
00097 public:
00098         PortListener(int port);
00099         virtual ~PortListener();
00100 
00101         virtual void Run();
00102         void Stop();
00103 
00104         inline PortListenerDelegateDispatch& Delegates() { return m_listeners; }
00105 
00106 #if defined(DEBUG) || defined(_DEBUG)
00107         void CheckMem() const;
00108         void ValidateMem() const;
00109 #endif
00110 };
00111 
00112 REGISTER_TYPEOF(42, PortListener);
00113 
00116 #endif