|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgRemoteServerTcpIp.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of LgRemoteServerTcpIp.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "..\Version.h"
#include "LgRemoteServerTcpIp.h"
#include "LgRemoteServerTcpIpConnection.h"
#include "LgRemoteServerTcpIpSession.h"
#include "..\..\LgLib\LgImageJPEG.h"
CLgRemoteServerTcpIp::CLgRemoteServerTcpIp(){ LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteServerTcpIp::CLgRemoteServerTcpIp" );}
CLgRemoteServerTcpIp::~CLgRemoteServerTcpIp(){ Destroy();
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteServerTcpIp::~CLgRemoteServerTcpIp" );}
void CLgRemoteServerTcpIp::Destroy(){ CLgSocketServer<CLgRemoteSocket>::Destroy();}
CLgSocketServerConnection<CLgRemoteSocket>* CLgRemoteServerTcpIp::OnCreateConnection(){ // create new connection
CLgRemoteServerTcpIpConnection *pConnection = new CLgRemoteServerTcpIpConnection( this ); if( !pConnection ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::OnCreateConnection"), _T("new CLgRemoteServerTcpIpConnection failed") ); return NULL; }
// place into pending connection list
if( !m_ConnectionArray.Add( pConnection ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::OnCreateConnection"), _T("m_ConnectionArray.Add failed") ); }
return pConnection;} void CLgRemoteServerTcpIp::OnDeleteConnection( CLgSocketServerConnection<CLgRemoteSocket>* pConnection ){ if( !pConnection ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::OnDeleteConnection"), _T("pConnection is NULL") ); return; }
// cast to valid type
CLgRemoteServerTcpIpConnection *pRemoteConnection = (CLgRemoteServerTcpIpConnection*)pConnection;
// remove from pending connection list (if any)
m_ConnectionArray.Remove( m_ConnectionArray.Find( pRemoteConnection ) );
// delete connection
delete pRemoteConnection;}
BOOL CLgRemoteServerTcpIp::AssignSession( CLgRemoteServerTcpIpConnection* pConnection, PINT pSessionID, BYTE eConnectionType, BYTE eSessionType ){ CLgLocker Lock( &m_SessionArray );
// try to assign to existing sessions
for( INT i = 0; i < m_SessionArray.GetCount(); i++ ) if( m_SessionArray[i]->GetSessionID() == *pSessionID ) return m_SessionArray[i]->AddConnection( pConnection, eConnectionType );
// if session is not new one
if( *pSessionID != -1 ) return FALSE;
if( eConnectionType == LG_REMOTE_E_CONNECT_TYPE_MASTER ) { static INT gSessionID = 0;
// allocate new session
CLgRemoteServerTcpIpSession *pSession = new CLgRemoteServerTcpIpSession( gSessionID, eSessionType ); if( !pSession ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("new CLgRemoteServerTcpIpSession failed") ); return FALSE; }
// add master connection into session and place session into session list
if( !pSession->AddConnection( pConnection, eConnectionType ) || !m_SessionArray.Add( pSession ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("pSession->AddConnection or m_SessionArray.Add failed") ); delete pSession; return FALSE; } // store id of new session;
*pSessionID = gSessionID;
// increase session id
gSessionID++;
return TRUE; } else { // if no master sessions
if( !m_SessionArray.GetCount() ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("There are no any master sessions") ); return FALSE; }
// default behaviour: taking first one
CLgRemoteServerTcpIpSession *pSession = NULL;
// find appropriate session
CLgLocker Lock( &m_SessionArray ); for( INT i = 0; i < m_SessionArray.GetCount(); i++ ) { CLgRemoteServerTcpIpSession *pTempSession = m_SessionArray[i]; if( pTempSession->GetSessionType() == eSessionType && pTempSession->GetSessionID() == *pSessionID ) { pSession = pTempSession; break; } }
if( !pSession ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("Can't find appropriate session") ); return FALSE; }
// add master connection into session and place session into session list
if( !pSession->AddConnection( pConnection, eConnectionType ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("pSession->AddConnection failed") ); return FALSE; }
// store id of new session;
*pSessionID = pSession->GetSessionID();
return TRUE; }
return FALSE;}
BOOL CLgRemoteServerTcpIp::GetSessionList( BYTE eSessionType, LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA* ppSessionInfo, PINT pSessionInfoSize ){ if( !ppSessionInfo || !pSessionInfoSize ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::GetSessionList"), _T("Parameters are wrong (%p,%p)"), ppSessionInfo, pSessionInfoSize ); return FALSE; }
CLgLocker Lock( &m_SessionArray );
LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA pSessionInfo = NULL; INT nSessionInfoSize = 0;
// prepare list
for( INT i = 0; i < m_SessionArray.GetCount(); i++ ) { CLgRemoteServerTcpIpSession *pSession = m_SessionArray[i]; if( pSession->GetSessionType() == eSessionType ) { // allocate memory for additional info
pSessionInfo = (LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA) realloc( pSessionInfo, sizeof(LG_TCP_PACKET_SESSION_INFO::SESSION_INFO_DATA) * (1 + nSessionInfoSize) ); if( pSessionInfo ) { pSessionInfo[nSessionInfoSize].nSessionID = pSession->GetSessionID(); pSessionInfo[nSessionInfoSize].nLength = sizeof(LG_TCP_PACKET_SESSION_INFO::SESSION_INFO_DATA); nSessionInfoSize ++; } } }
// return parameters
*ppSessionInfo = pSessionInfo; *pSessionInfoSize = nSessionInfoSize * sizeof(LG_TCP_PACKET_SESSION_INFO::SESSION_INFO_DATA);
return TRUE;}
|