//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgRemoteServerTcpIpSession.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgRemoteServerTcpIpSession.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "..\Version.h" #include "LgRemoteServerTcpIpSession.h" CLgRemoteServerTcpIpSession::CLgRemoteServerTcpIpSession( INT nSessionID, BYTE eSessionType ) : m_nSessionID( nSessionID ), m_eSessionType( eSessionType ) { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteServerTcpIpSession::CLgRemoteServerTcpIpSession" ); } CLgRemoteServerTcpIpSession::~CLgRemoteServerTcpIpSession() { SessionToConnectionMasterLinkP::Unlink(); SessionToConnectionSlaveLinkP::Unlink(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteServerTcpIpSession::~CLgRemoteServerTcpIpSession" ); } BOOL CLgRemoteServerTcpIpSession::AddConnection( CLgRemoteServerTcpIpConnection* pConnection, BYTE eConnectionType ) { if( !pConnection ) return FALSE; switch( eConnectionType ) { case LG_REMOTE_E_CONNECT_TYPE_MASTER: return SessionToConnectionMasterLinkP::Link( pConnection ); case LG_REMOTE_E_CONNECT_TYPE_SLAVE: return SessionToConnectionSlaveLinkP::Link( pConnection ); } return FALSE; } BOOL CLgRemoteServerTcpIpSession::RemoveConnection( CLgRemoteServerTcpIpConnection* pConnection ) { if( !pConnection ) return FALSE; if( pConnection->IsMaster() ) { if( SessionToConnectionMasterLinkP::Unlink( pConnection ) ) return TRUE; } else { if( SessionToConnectionSlaveLinkP::Unlink( pConnection ) ) return TRUE; } return FALSE; } BOOL CLgRemoteServerTcpIpSession::Dispatch( CLgRemoteServerTcpIpConnection* pFromConnection, PLG_TCP_PACKET pPacket ) { if( !pFromConnection ) return FALSE; if( pFromConnection->IsMaster() ) { // protect link CLgLocker Lock( &SessionToConnectionSlaveLinkP::GetLock() ); // dispatch packet switch( pPacket->bType ) { case LG_TCP_PACKET::TYPE_VIDEO: { INT nConnectionID = ((PLG_TCP_PACKET_VIDEO)pPacket)->nConnectionID; // find and send into exact connection BOOL bFound = FALSE; for( INT i = 0; i < SessionToConnectionSlaveLinkP::GetCount(); i++ ) { CLgRemoteServerTcpIpConnection *pToConnection = *SessionToConnectionSlaveLinkP::GetAt(i); if( pToConnection->GetConnectionID() == nConnectionID ) { if( pToConnection->Send( pPacket, pPacket->nLength ) == -1 ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("pToConnection->Send 1 failed") ); } // mark that connection found bFound = TRUE; break; } } if( !bFound ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("Connection %d was not found for LG_TCP_PACKET::TYPE_VIDEO"), nConnectionID ); } } break; case LG_TCP_PACKET::TYPE_REPORT: { INT nConnectionID = ((PLG_TCP_PACKET_REPORT)pPacket)->nConnectionID; // find and send into exact connection BOOL bFound = FALSE; for( INT i = 0; i < SessionToConnectionSlaveLinkP::GetCount(); i++ ) { CLgRemoteServerTcpIpConnection *pToConnection = *SessionToConnectionSlaveLinkP::GetAt(i); if( pToConnection->GetConnectionID() == nConnectionID ) { if( pToConnection->Send( pPacket, pPacket->nLength ) == -1 ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("pToConnection->Send 2 failed") ); } // mark that connection found bFound = TRUE; break; } } if( !bFound ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("Connection %d was not found for LG_TCP_PACKET::TYPE_REPORT"), nConnectionID ); } } break; default: // broadcast { for( INT i = 0; i < SessionToConnectionSlaveLinkP::GetCount(); i++ ) { CLgRemoteServerTcpIpConnection *pToConnection = *SessionToConnectionSlaveLinkP::GetAt(i); if( pToConnection->Send( pPacket, pPacket->nLength ) == -1 ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("pToConnection->Send 3 failed") ); } } } break; } } else { // protect link CLgLocker Lock( &SessionToConnectionMasterLinkP::GetLock() ); // dispatch into master connection only if( !SessionToConnectionMasterLinkP::GetCount() ) return FALSE; // substitute connection id switch( pPacket->bType ) { case LG_TCP_PACKET::TYPE_VIDEO: ((PLG_TCP_PACKET_VIDEO)pPacket)->nConnectionID = pFromConnection->GetConnectionID(); break; case LG_TCP_PACKET::TYPE_REPORT:((PLG_TCP_PACKET_REPORT)pPacket)->nConnectionID = pFromConnection->GetConnectionID(); break; } // master connection pointer CLgRemoteServerTcpIpConnection *pToConnection = *SessionToConnectionMasterLinkP::GetAt(0); if( pToConnection->Send( pPacket, pPacket->nLength ) == -1 ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("pToConnection->Send 4 failed") ); } } return TRUE; }