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

src/PooledSocketSet.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 #include <spl/net/PooledSocketSet.h>
00018 
00019 PooledSocketSet::PooledSocketSet(int poolSize)
00020 : m_sets(), m_socketCount(0), m_setsMtx()
00021 {
00022         for ( int x = 0; x < poolSize; x++ )
00023         {
00024                 m_sets.Add( SocketSetPtr(new SocketSet()) );
00025         }
00026 }
00027 
00028 PooledSocketSet::~PooledSocketSet()
00029 {
00030         int count = m_sets.Count();
00031         for ( int x = 0; x < count; x++ )
00032         {
00033                 SocketSetPtr ss = m_sets.ElementAt(x);
00034                 ss.ValidateMem();
00035                 ss->CloseAndDelete();
00036         }
00037         m_sets.Clear();
00038 }
00039 
00040 int PooledSocketSet::SocketCount() const
00041 { 
00042         return m_socketCount; 
00043 }
00044 
00045 void PooledSocketSet::AddSocket( IStreamReadListenerPtr listener, TcpSocketPtr sp )
00046 {
00047         m_setsMtx.Lock();
00048 
00049         for ( int x = 0; x < m_sets.Count(); x++ )
00050         {
00051                 if ( m_sets.ElementAt(x)->SocketCount() < FD_SETSIZE )
00052                 {
00053                         m_sets.ElementAt(x)->AddSocket( listener, sp );
00054                         m_setsMtx.Unlock();
00055                         return;
00056                 }
00057         }
00058         listener->IStreamRead_OnClose();
00059         sp->Close();
00060 
00061         m_setsMtx.Unlock();
00062 }
00063 /*
00064 void PooledSocketSet::RemoveSocket( TcpSocket *sp )
00065 {
00066         m_setsMtx.Lock();
00067         for ( int x = 0; x < m_sets.Count(); x++ )
00068         {
00069                 if ( m_sets.ElementAt(x)->RemoveSocket(sp) )
00070                 {
00071                         m_setsMtx.Unlock();
00072                         return;
00073                 }
00074         }
00075         m_setsMtx.Unlock();
00076 }
00077 */
00078 void PooledSocketSet::Close()
00079 {
00080         m_setsMtx.Lock();
00081         for ( int x = 0; x < m_sets.Count(); x++ )
00082         {
00083                 m_sets.ElementAt(x)->Close();
00084         }
00085         m_setsMtx.Unlock();
00086 }
00087 
00088 void PooledSocketSet::CloseAndDelete()
00089 {
00090         m_setsMtx.Lock();
00091         for ( int x = 0; x < m_sets.Count(); x++ )
00092         {
00093                 m_sets.ElementAt(x)->CloseAndDelete();
00094         }
00095         m_setsMtx.Unlock();     
00096 }
00097 
00098 void PooledSocketSet::Broadcast( const Array<byte>& buf, const int len )
00099 {
00100         m_setsMtx.Lock();
00101         for ( int x = 0; x < m_sets.Count(); x++ )
00102         {
00103                 m_sets.ElementAt(x)->Broadcast(buf, len);
00104         }
00105         m_setsMtx.Unlock();
00106 }
00107 
00108 void PooledSocketSet::Join(int timeoutms)
00109 {
00110         for ( int x = 0; x < m_sets.Count(); x++ )
00111         {
00112                 m_sets.ElementAt(x)->Join(timeoutms);
00113         }
00114 }
00115 
00116 void PooledSocketSet::Join()
00117 {
00118         for ( int x = 0; x < m_sets.Count(); x++ )
00119         {
00120                 m_sets.ElementAt(x)->Join();
00121         }
00122 }
00123 
00124 #if defined(DEBUG) || defined(_DEBUG)
00125 void PooledSocketSet::CheckMem() const
00126 {
00127         //m_setsMtx.Lock();
00128         m_sets.CheckMem();
00129         for ( int x = 0; x < m_sets.Count(); x++ )
00130         {
00131                 m_sets.ElementAt(x).CheckMem();
00132         }
00133         //m_setsMtx.Unlock();
00134 }
00135 
00136 void PooledSocketSet::ValidateMem() const
00137 {
00138         //m_setsMtx.Lock();
00139         m_sets.ValidateMem();
00140         for ( int x = 0; x < m_sets.Count(); x++ )
00141         {
00142                 m_sets.ElementAt(x).ValidateMem();
00143         }
00144         //m_setsMtx.Unlock();
00145 }
00146 #endif
00147 
00148 PooledSocketServer::PooledSocketServer
00149 (
00150         IServerConnectionFactory *conFactory, 
00151         int serverPort, 
00152         int poolSize
00153 )
00154 : m_ss(poolSize), m_listener(serverPort), m_conFactory(conFactory)
00155 {
00156         m_listener.Delegates().Add(this);
00157 }
00158 
00159 PooledSocketServer::~PooledSocketServer()
00160 {
00161         m_listener.Stop();
00162 }
00163 
00164 void PooledSocketServer::IPortListener_OnConnect( TcpSocketPtr sock )
00165 {
00166         m_ss.AddSocket(m_conFactory->Create(sock), sock);
00167 }
00168 
00169 void PooledSocketServer::IPortListener_OnStop()
00170 {
00171         // Not needed, since OnStop is called by m_listener.Stop() below.
00172         //m_ss.CloseAndDelete();
00173 }
00174 
00175 void PooledSocketServer::AddSocket( IStreamReadListenerPtr listener, TcpSocketPtr sp ) 
00176 { 
00177         m_ss.AddSocket(listener, sp); 
00178 }
00179 
00180 void PooledSocketServer::Close() 
00181 { 
00182         m_listener.Stop(); 
00183         m_ss.Close(); 
00184 }
00185 
00186 void PooledSocketServer::CloseAndDelete() 
00187 { 
00188         m_listener.Stop(); 
00189         m_ss.CloseAndDelete(); 
00190 }
00191 
00192 int PooledSocketServer::SocketCount() const
00193 { 
00194         return m_ss.SocketCount(); 
00195 }
00196 
00197 void PooledSocketServer::Broadcast( const Array<byte>& buf, const int len ) 
00198 { 
00199         m_ss.Broadcast(buf, len); 
00200 }
00201 
00202 void PooledSocketServer::Join(int timeoutms) 
00203 { 
00204         m_ss.Join(timeoutms); 
00205 }
00206 
00207 void PooledSocketServer::Join() 
00208 { 
00209         m_ss.Join(); 
00210 }
00211 
00212 #if defined(DEBUG) || defined(_DEBUG)
00213 void PooledSocketServer::CheckMem() const
00214 {
00215         m_ss.CheckMem();
00216         m_listener.CheckMem();
00217 }
00218 
00219 void PooledSocketServer::ValidateMem() const
00220 {
00221         m_ss.ValidateMem();
00222         m_listener.ValidateMem();
00223 }
00224 #endif