//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgSocketServer.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgSocketServer.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include #include "LgSocketServer.h" #include "..\Version.h" /* template CLgSocketServer::CLgSocketServer() { m_hConnectionEvent = NULL; } template CLgSocketServer::~CLgSocketServer() { Destroy(); } // 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 template BOOL CLgSocketServer::Create( UINT nHostPort ) { // create socket if( !CLgSocket::Create( nHostPort ) ) return FALSE; // run sockets listening if( !CLgSocket::Listen() ) return FALSE; // create event m_hConnectionEvent = ::CreateEvent( NULL, FALSE, FALSE, NULL ); if( !m_hConnectionEvent ) return FALSE; return TRUE; } template void CLgSocketServer::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; } } template void CLgSocketServer::OnAccept( int nErrorCode ) { CLgSocketServerConnection *pSocket = OnCreateConnection(); if( !pSocket ) return; if( !Accept( *pSocket ) ) { delete pSocket; return; } CLgLocker locker( &m_SocketArray ); m_SocketArray.Add( pSocket ); BASE_SOCKET::OnAccept(nErrorCode); } template BOOL CLgSocketServer::DeleteConnection( CLgSocketServerConnection* pConnection ) { if( !pConnection ) return FALSE; pConnection->Close(); return ::SetEvent( m_hConnectionEvent ); } template void CLgSocketServer::DoDeleteConnection() { CLgLocker locker( &m_SocketArray ); for( INT i = 0; i < m_SocketArray.GetCount(); ) { if( m_SocketArray[i]->GetSocketHandle() == INVALID_SOCKET ) { OnDeleteConnection( m_SocketArray[i] ); m_SocketArray.Remove( i ); } else i++; } } template DWORD CLgSocketServer::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; } template CLgSocketServerConnection* CLgSocketServer::OnCreateConnection() { return new CLgSocketServerConnection( this ); } template void CLgSocketServer::OnDeleteConnection( CLgSocketServerConnection* pConnection ) { delete pConnection; } */