Old fashion design for PC-104
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.
|
|
//----------------------------------------------------------------------------
// 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 BASE_SOCKET>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<BASE_SOCKET> *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<BASE_SOCKET>::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<BASE_SOCKET>* pConnection ) { delete pConnection; }
virtual CLgSocketServerConnection<BASE_SOCKET>* OnCreateConnection() { return new CLgSocketServerConnection<BASE_SOCKET>( this ); }
private: friend class CLgSocketServerConnection<BASE_SOCKET>; BOOL OnCloseConnection( CLgSocketServerConnection<BASE_SOCKET>* pConnection ) { if( !pConnection ) return FALSE;
// do not call CLgSocketServerConnection<BASE_SOCKET>::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<CLgSocketServerConnection<BASE_SOCKET>*> 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 )
|