//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 10/11/2003 // PROJECT: // SUBSYSTEM: // FILE: LgSocketServer.h // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Header file of LgSocketServer.h //---------------------------------------------------------------------------- #pragma warning( disable : 4786 ) #ifndef __LG__SOCKET__SERVER__H__INCLUDED__ #define __LG__SOCKET__SERVER__H__INCLUDED__ #if _MSC_VER > 1000 #pragma once #endif #include "..\..\LgLib\Thread.h" #include "..\..\LgLib\Templ.h" #include "..\..\LgLib\LgDebug.h" #include "LgSocket.h" #include "LgSocketServerConnection.h" template class CLgSocketServer : public BASE_SOCKET { public: CLgSocketServer() { m_hConnectionEvent = NULL; } virtual ~CLgSocketServer() { Destroy(); } public: BOOL Create( UINT nHostPort ) { // create event m_hConnectionEvent = ::CreateEvent( NULL, FALSE, FALSE, NULL ); if( !m_hConnectionEvent ) return FALSE; // create socket if( !BASE_SOCKET::Create( nHostPort ) ) return FALSE; // run sockets listening if( !BASE_SOCKET::Listen() ) return FALSE; return TRUE; } virtual void Destroy() { // close socket Close(); // destroy all incoming sockets CLgLocker locker( &m_SocketArray ); for( INT i = 0; i < m_SocketArray.GetCount(); i++ ) delete m_SocketArray[i]; m_SocketArray.RemoveAll(); // close notification event if( m_hConnectionEvent ) { ::CloseHandle( m_hConnectionEvent ); m_hConnectionEvent = NULL; } } public: //{{AFX_VIRTUAL(CLgSocketServer) public: virtual void OnAccept(int nErrorCode) { CLgSocketServerConnection *pSocket = OnCreateConnection(); if( !pSocket ) return; SOCKADDR_IN SockAddr; INT SockAddrLen = sizeof(SockAddr); if( !Accept( *pSocket, (SOCKADDR*)&SockAddr, &SockAddrLen ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgSocketServer::OnAccept"), _T("Accept failed") ); delete pSocket; return; } CLgLocker locker( &m_SocketArray ); m_SocketArray.Add( pSocket ); BASE_SOCKET::OnAccept(nErrorCode); pSocket->OnConnect( &SockAddr ); } //}}AFX_VIRTUAL //{{AFX_MSG(CLgSocketServer) //}}AFX_MSG protected: virtual void OnDeleteConnection( CLgSocketServerConnection* pConnection ) { delete pConnection; } virtual CLgSocketServerConnection* OnCreateConnection() { return new CLgSocketServerConnection( this ); } private: friend class CLgSocketServerConnection; BOOL OnCloseConnection( CLgSocketServerConnection* pConnection ) { if( !pConnection ) return FALSE; // do not call CLgSocketServerConnection::Close() here! it will cause deadlock pConnection->DeletePending(); return ::SetEvent( m_hConnectionEvent ); } private: virtual DWORD ThreadProc() { HANDLE hEvents[] = { CLgThread::GetThreadStopEvent(), BASE_SOCKET::GetSocketEventHandle(), m_hConnectionEvent }; while( TRUE ) { // wait for events switch( ::WaitForMultipleObjects( 3, hEvents, FALSE, INFINITE ) ) { default: case WAIT_OBJECT_0: goto EXIT_THREAD; case WAIT_OBJECT_0+1: // handle events DoHandleEvents(); continue; case WAIT_OBJECT_0+2: // handle connection closure DoDeleteConnection(); continue; } } EXIT_THREAD: return 0UL; } void DoDeleteConnection() { CLgLocker locker( &m_SocketArray ); for( INT i = 0; i < m_SocketArray.GetCount(); ) { if( m_SocketArray[i]->GetSocketHandle() == INVALID_SOCKET || m_SocketArray[i]->IsDeletePending() ) { OnDeleteConnection( m_SocketArray[i] ); m_SocketArray.Remove( i ); } else i++; } } private: HANDLE m_hConnectionEvent; protected: typedef CLgArrayP*> CSSConnectionArray; CSSConnectionArray& GetConnectionArray() { return m_SocketArray; } private: CSSConnectionArray m_SocketArray; }; // Do not edit the following lines, which are needed by ClassWizard. #if 0 BEGIN_MESSAGE_MAP(CLgSocketServer, CSocket) //{{AFX_MSG_MAP(CLgSocketServer) //}}AFX_MSG_MAP END_MESSAGE_MAP() #endif // 0 //{{AFX_INSERT_LOCATION}} #endif #pragma warning( default : 4786 )