//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgRemoteClientTcpIp.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgRemoteClientTcpIp.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "..\Version.h" #include "LgRemoteClientTcpIp.h" CLgRemoteClientTcpIp::CLgRemoteClientTcpIp() { m_pHostAddress[0] = 0; m_nHostPort = 0; m_eConnectionType = LG_REMOTE_E_CONNECT_TYPE_SLAVE; m_eSessionType = LG_REMOTE_E_SESSION_TYPE_UNKNOWN; m_nSessionID = -1; m_nConnectionID = -1; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteClientTcpIp::CLgRemoteClientTcpIp" ); } CLgRemoteClientTcpIp::~CLgRemoteClientTcpIp() { Destroy(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteClientTcpIp::~CLgRemoteClientTcpIp" ); } BOOL CLgRemoteClientTcpIp::Create( LPCTSTR lpszHostAddress, UINT nHostPort, BYTE eSessionType, BYTE eConnectionType ) { TCHAR pLocalHostAddress[MAX_PATH]; // use current machine adress if( !lpszHostAddress ) { // repoint to another buffer lpszHostAddress = pLocalHostAddress; // get local address if( gethostname( (PCHAR)lpszHostAddress, MAX_PATH ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::Create"), _T("gethostname failed") ); return FALSE; } } // store properties m_eConnectionType = eConnectionType; m_eSessionType = eSessionType; _tcscpy( m_pHostAddress, lpszHostAddress ); m_nHostPort = nHostPort; if( !CLgSocketClient::Create( lpszHostAddress, nHostPort ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::Create"), _T("CLgSocketClient::Create failed") ); return FALSE; } switch( eConnectionType ) { case LG_REMOTE_E_CONNECT_TYPE_MASTER: // send connect packet if( !SendServerConnect( m_eSessionType, m_nSessionID, m_eConnectionType, m_nConnectionID ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::Create"), _T("SendServerConnect failed") ); } break; case LG_REMOTE_E_CONNECT_TYPE_SLAVE: // send connect packet if( !SendServerSessionList( m_eSessionType, NULL, 0 ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::Create"), _T("SendServerSessionList failed") ); } break; } return TRUE; } void CLgRemoteClientTcpIp::Destroy() { // unlink interface CLgRemoteInterfaceInfo::Unlink(); // destroy socket CLgSocketClient::Destroy(); m_pHostAddress[0] = 0; m_nHostPort = 0; } void CLgRemoteClientTcpIp::OnConnect( int nErrorCode ) { if( nErrorCode ) { // perform reconnect if( !CLgSocketClient::Create( m_pHostAddress, m_nHostPort ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnConnect"), _T("CLgSocketClient::Create failed") ); } CLgSocketClient::OnConnect( nErrorCode ); return; } switch( m_eConnectionType ) { case LG_REMOTE_E_CONNECT_TYPE_MASTER: // send connect packet if( !SendServerConnect( m_eSessionType, m_nSessionID, m_eConnectionType, m_nConnectionID ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnConnect"), _T("SendServerConnect failed") ); } break; case LG_REMOTE_E_CONNECT_TYPE_SLAVE: // send connect packet if( !SendServerSessionList( m_eSessionType, NULL, 0 ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnConnect"), _T("SendServerSessionList failed") ); } break; } // call base class handler CLgSocketClient::OnConnect( nErrorCode ); } void CLgRemoteClientTcpIp::OnClose( int nErrorCode ) { // perform reconnect CLgSocketClient::Create( m_pHostAddress, m_nHostPort ); CLgSocketClient::OnClose( nErrorCode ); OnRemoteInfoConnect( FALSE ); } void CLgRemoteClientTcpIp::OnPacketConnect( PLG_TCP_PACKET_CONNECT pReceivePacket ) { m_nSessionID = pReceivePacket->nSessionID; m_nConnectionID = pReceivePacket->nConnectionID; // notify interface about connection if( !OnRemoteInfoConnect( m_nSessionID != -1 && m_nConnectionID != -1 ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnPacketConnect"), _T("OnRemoteInfoConnect failed") ); } } void CLgRemoteClientTcpIp::OnPacketSessionInfo( PLG_TCP_PACKET_SESSION_INFO pReceivePacket ) { // error situation if( pReceivePacket->eSessionType != m_eSessionType || // no active sessions pReceivePacket->nLength <= sizeof(LG_TCP_PACKET_SESSION_INFO) ) { // perform 1 second pause ::WaitForSingleObject( CLgThread::GetThreadStopEvent(), 1000UL ); // make aditional request if( !SendServerSessionList( m_eSessionType, NULL, 0 ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnPacketSessionInfo"), _T("SendServerSessionList failed") ); } return; } LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA pSessionInfoData = (LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA)&pReceivePacket[1]; // send connect packet if( !SendServerConnect( m_eSessionType, pSessionInfoData->nSessionID, m_eConnectionType, m_nConnectionID ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnPacketSessionInfo"), _T("SendServerConnect failed") ); } } BOOL CLgRemoteClientTcpIp::DoRemoteInfoTransfer() { LARGE_INTEGER liRX, liTX; if( !CLgRemoteSocket::GetTransferInfo( &liTX, &liRX ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::DoRemoteInfoTransfer"), _T("CLgRemoteSocket::GetTransferInfo failed") ); return FALSE; } if( !OnRemoteInfoTransfer( &liTX, &liRX ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::DoRemoteInfoTransfer"), _T("OnRemoteInfoTransfer failed") ); } return TRUE; }