You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
3.7 KiB
168 lines
3.7 KiB
//----------------------------------------------------------------------------
|
|
// 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 <Winsock2.h>
|
|
#include "LgSocketServer.h"
|
|
#include "..\Version.h"
|
|
/*
|
|
template<class BASE_SOCKET>
|
|
CLgSocketServer<BASE_SOCKET>::CLgSocketServer()
|
|
{
|
|
m_hConnectionEvent = NULL;
|
|
}
|
|
|
|
template<class BASE_SOCKET>
|
|
CLgSocketServer<BASE_SOCKET>::~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<class BASE_SOCKET>
|
|
BOOL CLgSocketServer<BASE_SOCKET>::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<class BASE_SOCKET>
|
|
void CLgSocketServer<BASE_SOCKET>::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<class BASE_SOCKET>
|
|
void CLgSocketServer<BASE_SOCKET>::OnAccept( int nErrorCode )
|
|
{
|
|
CLgSocketServerConnection<BASE_SOCKET> *pSocket = OnCreateConnection();
|
|
if( !pSocket )
|
|
return;
|
|
|
|
if( !Accept( *pSocket ) )
|
|
{
|
|
delete pSocket;
|
|
return;
|
|
}
|
|
|
|
CLgLocker locker( &m_SocketArray );
|
|
|
|
m_SocketArray.Add( pSocket );
|
|
|
|
BASE_SOCKET::OnAccept(nErrorCode);
|
|
}
|
|
|
|
template<class BASE_SOCKET>
|
|
BOOL CLgSocketServer<BASE_SOCKET>::DeleteConnection( CLgSocketServerConnection<BASE_SOCKET>* pConnection )
|
|
{
|
|
if( !pConnection )
|
|
return FALSE;
|
|
|
|
pConnection->Close();
|
|
|
|
return ::SetEvent( m_hConnectionEvent );
|
|
}
|
|
|
|
template<class BASE_SOCKET>
|
|
void CLgSocketServer<BASE_SOCKET>::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<class BASE_SOCKET>
|
|
DWORD CLgSocketServer<BASE_SOCKET>::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<class BASE_SOCKET>
|
|
CLgSocketServerConnection<BASE_SOCKET>* CLgSocketServer<BASE_SOCKET>::OnCreateConnection()
|
|
{
|
|
return new CLgSocketServerConnection<BASE_SOCKET>( this );
|
|
}
|
|
|
|
template<class BASE_SOCKET>
|
|
void CLgSocketServer<BASE_SOCKET>::OnDeleteConnection( CLgSocketServerConnection<BASE_SOCKET>* pConnection )
|
|
{
|
|
delete pConnection;
|
|
}
|
|
|
|
*/
|