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.
155 lines
3.8 KiB
155 lines
3.8 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LgNetSocketMD.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of LgNetSocketMD.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
#include "..\Version.h"
|
|
#include "LgNetSocketMD.h"
|
|
#include "LgNetDeviceLGS.h"
|
|
#include "..\Comm\LGSm.h"
|
|
|
|
CLgNetSocketMD::CLgNetSocketMD()
|
|
{
|
|
m_bSpyMode = FALSE;
|
|
}
|
|
|
|
CLgNetSocketMD::~CLgNetSocketMD()
|
|
{
|
|
Destroy();
|
|
|
|
// unlink all linked devices
|
|
CLgLinkP<CLgNetSocket,CLgNetSocketDevice>::Unlink();
|
|
}
|
|
|
|
BOOL CLgNetSocketMD::Create( LPCTSTR pCommPort, INT nBaudRate, BOOL bSpyMode, BOOL bDemo )
|
|
{
|
|
Destroy();
|
|
|
|
if( !bDemo )
|
|
{
|
|
if( !m_Connection.Create( pCommPort, nBaudRate, 8, NOPARITY, ONESTOPBIT, CProtocolLGS::MAX_COMMAND_SIZE * 2, CProtocolLGS::MAX_COMMAND_SIZE * 2 ) )
|
|
return FALSE;
|
|
|
|
if( !m_Protocol.Create( &m_Connection ) )
|
|
return FALSE;
|
|
}
|
|
|
|
m_bSpyMode = bSpyMode;
|
|
|
|
if( !CLgNetSocket::Create( &m_Protocol ) )
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void CLgNetSocketMD::Destroy()
|
|
{
|
|
CLgNetSocket::Destroy();
|
|
m_Protocol.Destroy();
|
|
m_Connection.Destroy();
|
|
}
|
|
|
|
BOOL CLgNetSocketMD::SetBaudrate( DWORD nBaudrate )
|
|
{
|
|
DCB dcb;
|
|
if( !m_Connection.GetCommState( &dcb ) )
|
|
return FALSE;
|
|
|
|
if( dcb.BaudRate == nBaudrate )
|
|
return TRUE;
|
|
|
|
dcb.BaudRate = nBaudrate;
|
|
return m_Connection.SetCommState( &dcb );
|
|
}
|
|
|
|
BOOL CLgNetSocketMD::OnTransaction()
|
|
{
|
|
BOOL bResult = FALSE;
|
|
|
|
__try
|
|
{
|
|
bResult = OnTransaction_();
|
|
}
|
|
__except( EXCEPTION_EXECUTE_HANDLER )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgNetSocketMD::OnTransaction"), _T("Exception 0x%08X"), _exception_code() );
|
|
}
|
|
|
|
return bResult;
|
|
}
|
|
|
|
BOOL CLgNetSocketMD::OnTransaction_()
|
|
{
|
|
// if not in spy mode
|
|
if( !IsSpyMode() )
|
|
return CLgNetSocket::OnTransaction();
|
|
|
|
// process in spy mode
|
|
PBYTE pReadBuffer = NULL;
|
|
INT nReadCount = 0;
|
|
|
|
// read host command
|
|
if( !m_Protocol.Read( &pReadBuffer, &nReadCount, 100UL, FALSE ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, "CLgNetSocketMD::OnTransaction", "read host command failed" );
|
|
// accumulate error amount
|
|
m_liFailureCount.QuadPart++;
|
|
return FALSE;
|
|
}
|
|
|
|
// store address
|
|
INT nAddress = (INT)*pReadBuffer;
|
|
|
|
// found device for this operation
|
|
CLgLocker Locker( &CLgLinkP<CLgNetSocket,CLgNetSocketDevice>::GetLock() );
|
|
for( INT i = 0; i < CLgLinkP<CLgNetSocket,CLgNetSocketDevice>::GetCount(); i++ )
|
|
{
|
|
CLgNetDeviceMD *pDevice = static_cast<CLgNetDeviceMD*>( CLgLinkP<CLgNetSocket,CLgNetSocketDevice>::GetAt(i)->GetThis() );
|
|
if( pDevice->GetAddress() == nAddress )
|
|
{
|
|
// perform command reading
|
|
if( pDevice->DeviceProc( this, m_pProtocol ) )
|
|
m_liSuccessCount.QuadPart++;
|
|
else
|
|
{
|
|
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, "CLgNetSocketMD::OnTransaction", "read device command failed" );
|
|
// accumulate error amount
|
|
m_liFailureCount.QuadPart++;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
// log information
|
|
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, "CLgNetSocketMD::OnTransaction", "can't find device with address %d", nAddress );
|
|
return FALSE;
|
|
}
|
|
|
|
BOOL CLgNetSocketMD::Link( CLgLink<CLgNetSocketDevice,CLgNetSocket>* pLink )
|
|
{
|
|
// update spy mode
|
|
if( pLink )
|
|
static_cast<CLgNetDeviceMD*>(pLink->GetThis())->SetSpyMode( IsSpyMode() );
|
|
|
|
return CLgNetSocket::Link( pLink );
|
|
}
|
|
/*
|
|
BOOL CLgNetSocketMD::AttachDevice( CLgNetSocketDevice* pDevice )
|
|
{
|
|
// update spy mode
|
|
if( pDevice )
|
|
((CLgNetDeviceMD*)pDevice)->SetSpyMode( IsSpyMode() );
|
|
|
|
return CLgNetSocket::AttachDevice( pDevice );
|
|
}
|
|
*/
|