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

src/PortListener.cpp

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 
00018 #include <spl/Log.h>
00019 #include <spl/net/PortListener.h>
00020 
00021 IPortListenerListener::~IPortListenerListener()
00022 {
00023 }
00024 
00025 PortListenerDelegateDispatch::~PortListenerDelegateDispatch()
00026 {
00027 }
00028 
00029 void PortListenerDelegateDispatch::Add(IPortListenerListener *l)
00030 {
00031         m_onStop += Delegate<IPortListenerListener>::Create(l, &IPortListenerListener::IPortListener_OnStop);
00032         m_onConnect += DelegateOneParameter<IPortListenerListener, TcpSocketPtr>::Create(l, &IPortListenerListener::IPortListener_OnConnect);
00033 }
00034 
00035 #ifdef DEBUG
00036 void PortListenerDelegateDispatch::ValidateMem() const
00037 {
00038         m_onStop.ValidateMem();
00039         m_onConnect.ValidateMem();
00040 }
00041 
00042 void PortListenerDelegateDispatch::CheckMem() const
00043 {
00044         m_onStop.CheckMem();
00045         m_onConnect.CheckMem();
00046 }
00047 #endif
00048 
00049 PortListener::PortListener(int port)
00050 : m_port(port), m_listeners(), m_socket(ServerSocketPtr(new ServerSocket(port, 100)))
00051 {
00052         Start();
00053 }
00054 
00055 PortListener::~PortListener()
00056 {
00057         m_running = false; 
00058         m_socket->Close(); 
00059         Thread::YYield();
00060 }
00061 
00062 void PortListener::Stop() 
00063 { 
00064         m_running =false; 
00065         m_socket->Close(); 
00066         m_listeners.DispatchOnStop(); 
00067 }
00068 
00069 void PortListener::Run()
00070 {
00071         m_running = true;
00072         while ( m_running )
00073         {
00074                 try
00075                 {
00076                         TcpSocketPtr sock = m_socket->Accept();
00077                         if ( sock.IsNull() )
00078                         {
00079                                 // out of memory or timeout?
00080                                 continue;
00081                         }
00082                         m_listeners.DispatchOnConnect( sock );
00083                 }
00084                 catch ( SocketException *se )
00085                 {
00086                         // Log the error
00087                         Log::SWriteError("SocketException: " + String(se->Message()));
00088                         delete se;
00089                 }
00090                 ValidateMem();
00091         }
00092 }
00093 
00094 #if defined(DEBUG) || defined(_DEBUG)
00095 void PortListener::CheckMem() const
00096 {
00097         m_socket.CheckMem();
00098         m_listeners.CheckMem();
00099 }
00100 
00101 void PortListener::ValidateMem() const
00102 {
00103         m_socket.ValidateMem();
00104         m_listeners.ValidateMem();
00105 }
00106 #endif