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.
295 lines
6.6 KiB
295 lines
6.6 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 05/11/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LgSocket.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of LgSocket.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
#include "..\Version.h"
|
|
#include "LgSocket.h"
|
|
CLgSocket::CLgSocket()
|
|
{
|
|
m_hSocket = INVALID_SOCKET;
|
|
m_hSocketEvent = NULL;
|
|
m_lEventMask = 0;
|
|
|
|
// initiate windows sockets
|
|
// s_SocketInitialize.Initialize();
|
|
|
|
// initialize WinSock
|
|
WSADATA wsaData;
|
|
::WSAStartup( 0x202, &wsaData );
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgSocket::CLgSocket" );
|
|
}
|
|
|
|
CLgSocket::~CLgSocket()
|
|
{
|
|
Close();
|
|
|
|
// deinitiate windows sockets
|
|
// s_SocketInitialize.Deitialize();
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgSocket::~CLgSocket" );
|
|
}
|
|
|
|
BOOL CLgSocket::AsyncSelect( long lEvent )
|
|
{
|
|
// store thread running state
|
|
BOOL bThreadRunning = (m_lEventMask != 0);
|
|
|
|
// if needs to monitor any event
|
|
if( lEvent )
|
|
{
|
|
// create event (if needs)
|
|
if( !m_hSocketEvent )
|
|
{
|
|
m_hSocketEvent = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
if( !m_hSocketEvent )
|
|
return FALSE;
|
|
}
|
|
|
|
// establish event monitoring
|
|
if( ::WSAEventSelect( m_hSocket, m_hSocketEvent, lEvent ) )
|
|
return FALSE;
|
|
|
|
// store event mask
|
|
m_lEventMask = lEvent;
|
|
|
|
// run event wait thread
|
|
return bThreadRunning ? TRUE : CLgThread::Start( FALSE );
|
|
}
|
|
else
|
|
{
|
|
// establish event monitoring
|
|
if( ::WSAEventSelect( m_hSocket, m_hSocketEvent, lEvent ) )
|
|
return FALSE;
|
|
|
|
// store event mask
|
|
m_lEventMask = lEvent;
|
|
|
|
// run event wait thread
|
|
return bThreadRunning ? CLgThread::Stop( TRUE ) : TRUE;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CLgSocket::Create( UINT nSocketPort, int nSocketType, long lEvent, LPCTSTR lpszSocketAddress )
|
|
{
|
|
// create socket
|
|
m_hSocket = ::socket( AF_INET, nSocketType, 0 ); // TCP socket
|
|
if( m_hSocket == INVALID_SOCKET )
|
|
return FALSE;
|
|
|
|
// bind socket address with port
|
|
struct sockaddr_in local;
|
|
local.sin_family = AF_INET;
|
|
local.sin_addr.s_addr = lpszSocketAddress ? inet_addr( lpszSocketAddress ) : INADDR_ANY;
|
|
local.sin_port = htons( nSocketPort );
|
|
if( ::bind( m_hSocket, (struct sockaddr*)&local, sizeof(local) ) == SOCKET_ERROR )
|
|
return FALSE;
|
|
|
|
// establish event wait
|
|
return AsyncSelect( lEvent );
|
|
}
|
|
|
|
void CLgSocket::Close()
|
|
{
|
|
// stop threads
|
|
CLgThread::Stop( TRUE );
|
|
|
|
// close socket
|
|
if( m_hSocket != INVALID_SOCKET )
|
|
{
|
|
closesocket( m_hSocket );
|
|
m_hSocket = INVALID_SOCKET;
|
|
}
|
|
|
|
// close event
|
|
if( m_hSocketEvent )
|
|
{
|
|
::CloseHandle( m_hSocketEvent );
|
|
m_hSocketEvent = NULL;
|
|
}
|
|
|
|
// clear event mask
|
|
m_lEventMask = 0;
|
|
}
|
|
|
|
BOOL CLgSocket::Listen( int nConnectionBacklog )
|
|
{
|
|
return listen( m_hSocket, nConnectionBacklog ) == 0;
|
|
}
|
|
|
|
BOOL CLgSocket::Accept( CLgSocket& rConnectedSocket, SOCKADDR* lpSockAddr, int* lpSockAddrLen )
|
|
{
|
|
// get connected socket
|
|
SOCKET hSocket = accept( m_hSocket, lpSockAddr, lpSockAddrLen );
|
|
if( hSocket == INVALID_SOCKET )
|
|
return FALSE;
|
|
|
|
// if reference socket is not empty
|
|
if( rConnectedSocket.m_hSocket != INVALID_SOCKET )
|
|
{
|
|
closesocket( hSocket );
|
|
return FALSE;
|
|
}
|
|
|
|
// attach
|
|
rConnectedSocket.m_hSocket = hSocket;
|
|
|
|
// establish events
|
|
return rConnectedSocket.AsyncSelect( m_lEventMask );
|
|
}
|
|
|
|
BOOL CLgSocket::Connect( const SOCKADDR* lpSockAddr, int nSockAddrLen )
|
|
{
|
|
return connect( m_hSocket, lpSockAddr, nSockAddrLen ) != SOCKET_ERROR;
|
|
}
|
|
|
|
BOOL CLgSocket::Connect( LPCTSTR lpszHostAddress, UINT nHostPort )
|
|
{
|
|
SOCKADDR_IN sockAddr;
|
|
memset(&sockAddr,0,sizeof(sockAddr));
|
|
|
|
LPSTR lpszAscii = (LPTSTR)lpszHostAddress;
|
|
sockAddr.sin_family = AF_INET;
|
|
sockAddr.sin_addr.s_addr = inet_addr(lpszAscii);
|
|
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
|
|
{
|
|
LPHOSTENT lphost;
|
|
lphost = gethostbyname(lpszAscii);
|
|
if (lphost != NULL)
|
|
sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
|
|
else
|
|
{
|
|
WSASetLastError(WSAEINVAL);
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
sockAddr.sin_port = htons((u_short)nHostPort);
|
|
return Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
|
|
}
|
|
|
|
int CLgSocket::Send( const void* lpBuf, int nBufLen, int nFlags )
|
|
{
|
|
return send( m_hSocket, (const char*)lpBuf, nBufLen, nFlags );
|
|
}
|
|
|
|
int CLgSocket::Receive( void* lpBuf, int nBufLen, int nFlags )
|
|
{
|
|
return recv( m_hSocket, (char*)lpBuf, nBufLen, nFlags );
|
|
}
|
|
|
|
BOOL CLgSocket::GetSockName( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
|
|
{
|
|
return getsockname( m_hSocket, lpSockAddr, lpSockAddrLen );
|
|
}
|
|
|
|
void CLgSocket::OnAccept( int nErrorCode )
|
|
{
|
|
|
|
}
|
|
|
|
void CLgSocket::OnConnect( int nErrorCode )
|
|
{
|
|
|
|
}
|
|
|
|
void CLgSocket::OnClose( int nErrorCode )
|
|
{
|
|
|
|
}
|
|
|
|
void CLgSocket::OnOutOfBandData( int nErrorCode )
|
|
{
|
|
|
|
}
|
|
|
|
void CLgSocket::OnReceive( int nErrorCode )
|
|
{
|
|
|
|
}
|
|
|
|
void CLgSocket::OnSend( int nErrorCode )
|
|
{
|
|
|
|
}
|
|
|
|
SOCKET CLgSocket::GetSocketHandle()
|
|
{
|
|
return m_hSocket;
|
|
}
|
|
|
|
HANDLE CLgSocket::GetSocketEventHandle()
|
|
{
|
|
return m_hSocketEvent;
|
|
}
|
|
|
|
void CLgSocket::DoHandleEvents()
|
|
{
|
|
// check what the events were occured
|
|
WSANETWORKEVENTS NetworkEvents;
|
|
if( ::WSAEnumNetworkEvents( m_hSocket, m_hSocketEvent, &NetworkEvents ) )
|
|
return;
|
|
|
|
if( NetworkEvents.lNetworkEvents & FD_ACCEPT )
|
|
OnAccept( NetworkEvents.iErrorCode[FD_ACCEPT_BIT] );
|
|
|
|
if( NetworkEvents.lNetworkEvents & FD_CONNECT )
|
|
OnConnect( NetworkEvents.iErrorCode[FD_CONNECT_BIT] );
|
|
|
|
if( NetworkEvents.lNetworkEvents & FD_CLOSE )
|
|
OnClose( NetworkEvents.iErrorCode[FD_CLOSE_BIT] );
|
|
|
|
if( NetworkEvents.lNetworkEvents & FD_READ )
|
|
OnReceive( NetworkEvents.iErrorCode[FD_READ_BIT] );
|
|
|
|
if( NetworkEvents.lNetworkEvents & FD_WRITE )
|
|
OnSend( NetworkEvents.iErrorCode[FD_WRITE_BIT] );
|
|
|
|
if( NetworkEvents.lNetworkEvents & FD_OOB )
|
|
OnOutOfBandData( NetworkEvents.iErrorCode[FD_OOB_BIT] );
|
|
|
|
}
|
|
|
|
DWORD CLgSocket::ThreadProc()
|
|
{
|
|
__try
|
|
{
|
|
HANDLE hEvents[] = { CLgThread::GetThreadStopEvent(), GetSocketEventHandle() };
|
|
while( TRUE )
|
|
{
|
|
// wait for events
|
|
switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, INFINITE ) )
|
|
{
|
|
default:
|
|
case WAIT_OBJECT_0:
|
|
goto EXIT_THREAD;
|
|
|
|
case WAIT_OBJECT_0+1:
|
|
break;
|
|
}
|
|
|
|
// handle events
|
|
DoHandleEvents();
|
|
}
|
|
|
|
EXIT_THREAD:
|
|
return 0UL;
|
|
}
|
|
__except( EXCEPTION_CONTINUE_EXECUTION )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgSocket::ThreadProc"), _T("Exception 0x%08X"), _exception_code() );
|
|
}
|
|
return -1;
|
|
}
|
|
|