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.
 
 
 
 
 

274 lines
6.6 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 06/11/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgReportSocket.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of LgReportSocket.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "LgReportSocket.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CLgReportSocket::CLgReportSocket()
{
m_pBuffer = new BYTE[1<<20];
m_nCount = 0;
m_OutputStream.Create();
m_liTransferTX.QuadPart = m_liTransferRX.QuadPart = 0;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgReportSocket::CLgReportSocket" );
}
CLgReportSocket::~CLgReportSocket()
{
m_OutputStream.Destroy();
if( m_pBuffer )
{
delete m_pBuffer;
m_pBuffer = 0;
}
m_nCount = 0;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgReportSocket::~CLgReportSocket" );
}
void CLgReportSocket::OnClose( int nErrorCode )
{
CLgSocket::OnClose( nErrorCode );
// clear internal buffers
m_OutputStream.Clear();
m_nCount = 0;
}
void CLgReportSocket::OnReceive( int nErrorCode )
{
if( nErrorCode )
{
m_nCount = 0;
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::OnReceive"), _T("Error %d received"), nErrorCode );
return;
}
CLgSocket::OnReceive( nErrorCode );
if( !m_pBuffer )
return;
while( TRUE )
{
// read packet header
const INT cDescriptionSize = sizeof(LG_REPORT_PACKET);
if( m_nCount < cDescriptionSize )
{
INT nSize = cDescriptionSize - m_nCount;
INT nRead = CLgSocket::Receive( m_pBuffer + m_nCount, nSize, 0 );
if( nRead < 0 )
{
CLgSocket::OnReceive( nErrorCode );
return;
}
// store statistic
m_liTransferRX.QuadPart += nRead;
m_nCount += nRead;
if( nRead < nSize )
{
CLgSocket::OnReceive( nErrorCode );
return;
}
}
// read packet
INT nPacketSize = ((PLG_REPORT_PACKET)m_pBuffer)->nLength;
if( m_nCount < nPacketSize )
{
INT nSize = nPacketSize - m_nCount;
INT nRead = CLgSocket::Receive( m_pBuffer + m_nCount, nSize, 0 );
if( nRead < 0 )
{
CLgSocket::OnReceive( nErrorCode );
return;
}
// store statistic
m_liTransferRX.QuadPart += nRead;
m_nCount += nRead;
if( nRead < nSize )
{
CLgSocket::OnReceive( nErrorCode );
return;
}
}
Dispatch( (PLG_REPORT_PACKET)m_pBuffer );
m_nCount = 0;
}
}
void CLgReportSocket::OnSend( int nErrorCode )
{
CLgLocker Locker( &m_OutputStream );
// write into socket
while( TRUE )
{
// read from stream
BYTE pBuffer[MAX_PATH];
INT nCount = m_OutputStream.Peek( pBuffer, MAX_PATH );
if( !nCount )
break;
// write to stream
INT nWritten = CLgSocket::Send( pBuffer, nCount );
if( nWritten <= 0 )
break;
// validate buffer
m_OutputStream.Skip( nWritten );
// store statistic info
m_liTransferTX.QuadPart += nWritten;
}
CLgSocket::OnSend( nErrorCode );
}
int CLgReportSocket::Send( const void* lpBuf, int nBufLen, int nFlags )
{
CLgLocker Locker( &m_OutputStream );
// check availible space
if( m_OutputStream.GetFree() < nBufLen )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send1"), _T("Buffer space is not enough for new data (free %d, but it needs %d)"), m_OutputStream.GetFree(), nBufLen );
return -1;
}
// write into internal buffer
INT nWritten = m_OutputStream.Put( (PBYTE)lpBuf, nBufLen );
if( nWritten != nBufLen )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send2"), _T("m_OutputStream.Put failed (%d instead of %d)"), nWritten, nBufLen );
return -1;
}
// write into socket
INT nSend = 0;
while( TRUE )
{
// read from stream
BYTE pBuffer[MAX_PATH];
INT nCount = m_OutputStream.Peek( pBuffer, MAX_PATH );
if( !nCount )
break;
// write to stream
nWritten = CLgSocket::Send( pBuffer, nCount );
if( nWritten <= 0 )
{
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, _T("CLgReportSocket::Send"), _T("CLgSocket::Send failed (%d instead of %d)"), nWritten, nCount );
break;
}
// validate buffer
m_OutputStream.Skip( nWritten );
nSend += nWritten;
}
// store statistic info
m_liTransferTX.QuadPart += nSend;
return nSend;
}
int CLgReportSocket::Send2( const void* lpBuf1, int nBufLen1, const void* lpBuf2, int nBufLen2 )
{
CLgLocker Locker( &m_OutputStream );
// check availible space
if( m_OutputStream.GetFree() < (nBufLen1 + nBufLen2) )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send2"), _T("Buffer space is not enough for new data (free %d, but it needs %d)"), m_OutputStream.GetFree(), nBufLen1 + nBufLen2 );
return -1;
}
// write first block into internal buffer
INT nWritten1 = m_OutputStream.Put( (PBYTE)lpBuf1, nBufLen1 );
if( nWritten1 != nBufLen1 )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send2"), _T("m_OutputStream.Put for block 1 failed (%d instead of %d)"), nWritten1, nBufLen1 );
return -1;
}
// write second block into internal buffer
INT nWritten2 = m_OutputStream.Put( (PBYTE)lpBuf2, nBufLen2 );
if( nWritten2 != nBufLen2 )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send2"), _T("m_OutputStream.Put for block 2 failed (%d instead of %d)"), nWritten2, nBufLen2 );
return -1;
}
// write into socket
INT nSend = 0;
while( TRUE )
{
// read from stream
BYTE pBuffer[MAX_PATH];
INT nCount = m_OutputStream.Peek( pBuffer, MAX_PATH );
if( !nCount )
break;
// write to stream
INT nWritten = CLgSocket::Send( pBuffer, nCount );
if( nWritten <= 0 )
{
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, _T("CLgReportSocket::Send2"), _T("CLgSocket::Send failed (%d instead of %d)"), nWritten, nCount );
break;
}
// validate buffer
m_OutputStream.Skip( nWritten );
nSend += nWritten;
}
// store statistic info
m_liTransferTX.QuadPart += nSend;
return nSend;
}
BOOL CLgReportSocket::GetTransferInfo( PLARGE_INTEGER pTX, PLARGE_INTEGER pRX )
{
if( pTX )
*pTX = m_liTransferTX;
if( pRX )
*pRX = m_liTransferRX;
return TRUE;
}
void CLgReportSocket::Dispatch( PLG_REPORT_PACKET pPacket )
{
if( !pPacket )
return;
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Dispatch"), _T("Received packed with unknown type %d"), pPacket->bType );
}