⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manager.cpp

📁 http代理程序
💻 CPP
字号:

#include "StdAfx.h"

#include <sys/stat.h>

#include <algorithm>
using namespace std;

#include "manager.h"
#include "common.h"

// Function name	: manager::add_tunnel_constructor
// Description	    : 
// Return type		: void 
// Argument         : a_ptr< tunnel_constructor > pTunnelConstructor
//##ModelId=3B79EDE600AE
void manager::add_tunnel_constructor( a_ptr< tunnel_constructor > pTunnelConstructor )
{
	// Protection
	a_mutex::generic_lock lock( m_mtxManager );

	m_lstTunnelConstructors.push_back( pTunnelConstructor );
}


// Function name	: manager::add_tunnel
// Description	    : 
// Return type		: void 
// Argument         : a_ptr< tunnel > pTunnel
//##ModelId=3B79EDE600A4
void manager::add_tunnel( a_ptr< tunnel > pTunnel )
{
	// Protection
	a_mutex::generic_lock lock( m_mtxManager );

	m_lstTunnels.push_back( pTunnel );
}


// Function name	: manager::add_tunnel_request_listener
// Description	    : 
// Return type		: void 
// Argument         : a_ptr< tunnel_request_listener > pTunnelRequestListener
//##ModelId=3B79EDE60091
void manager::add_tunnel_request_listener( a_ptr< tunnel_request_listener > pTunnelRequestListener )
{
	// Protection
	a_mutex::generic_lock lock( m_mtxManager );

	m_lstTunnelRequestListeners.push_back( pTunnelRequestListener );
}

// Function name	: manager::run
// Description	    : 
// Return type		: unsigned long 
//##ModelId=3B79EDE600CC
unsigned long manager::run()
{
	while( !m_bTerminateRequest )
	{
		struct stat s;
		::memset( &s, 0, sizeof( s ) );

		if( ::stat( m_strCfgFileName.c_str(), &s ) == 0 )
			if( m_tLastModification != s.st_mtime )
			{
				try
				{
					update_configuration( m_strCfgFileName.c_str() );
					m_tLastModification = s.st_mtime;
				}
				catch( ... )
				{
					gpEventLog->log_event( event_log::event_type_warning, "Error occured then reading from configuration file" );
				}
			}

		// Data transfer
		{			
			// Protection
			a_mutex::generic_lock lock( m_mtxManager );

			list< a_ptr< tunnel > >::iterator i = m_lstTunnels.begin();
			while( i != m_lstTunnels.end() )
			{
				(*i)->do_data_transfer();
				i++;
			}
		}

		// Looking for non valid tunnels
		{			
			// Protection
			a_mutex::generic_lock lock( m_mtxManager );

			list< a_ptr< tunnel > >::iterator i = m_lstTunnels.begin();
			while( i != m_lstTunnels.end() )
			{
				if( !(*i)->is_active() )
				{
					gpEventLog->log_event( event_log::event_type_information, "Tunnel %#.8LX removed.", (*i)->m_lID );
					i = m_lstTunnels.erase( i );
					continue;
				}				
				i++;
			}
		}

		// Looking for nan valid tunnel listeners
		{
			// Protection
			a_mutex::generic_lock lock( m_mtxManager );

			list< a_ptr< tunnel_request_listener > >::iterator i = m_lstTunnelRequestListeners.begin();
			while( i != m_lstTunnelRequestListeners.end() )
			{
				if( !(*i)->is_running() )
				{
					i = m_lstTunnelRequestListeners.erase( i );
					continue;
				}
				i++;
			}
		}
		

		// Looking for non-valid tunnel request listeners

		thread::sleep( 100 );
	}
	return 0;
}


// Function name	: manager::update_configuration
// Description	    : 
// Return type		: void 
// Argument         : const char* szFileName
//##ModelId=3B79EDE600B8
void manager::update_configuration( const char* szFileName )
	throw( configuration_stream_exception )
{
	fstream fsCfg( szFileName );
	if( fsCfg.fail() )
		{throw configuration_stream_exception( configuration_stream_exception::exception_code_generic, "Failed to open configuration file", __FILE__, __LINE__ );};

	configuration cfgNew;	
	fsCfg >> cfgNew;
	fsCfg.close();
	
	list< a_ptr< tunnel_request_listener > >::iterator i;
	
	// Iterate over all listeners to remove those that are not in the configuration file anymore
	i = m_lstTunnelRequestListeners.begin();
	while( i != m_lstTunnelRequestListeners.end() )
	{
		list< tunnel_cfg >::iterator q = find( cfgNew.m_lstTunnels.begin(), cfgNew.m_lstTunnels.end(), static_cast< tunnel_cfg >( **i ) );

		// If the configuration entry does not match a listener, we remve it
		if( q == cfgNew.m_lstTunnels.end() )
		{
			(*i)->stop();
			i = m_lstTunnelRequestListeners.erase( i );
			continue;
		}

		// If there is already a listenre, remove the entry from the configuration file
		cfgNew.m_lstTunnels.erase( q );

		i++;
	}

	// For remaining items we must construct listeners
	list< tunnel_cfg >::iterator j = cfgNew.m_lstTunnels.begin();
	while( j != cfgNew.m_lstTunnels.end() )
	{
		a_ptr< tunnel_request_listener > pListener = new tunnel_request_listener( this, *j );
		pListener->resume();
		m_lstTunnelRequestListeners.push_back( pListener );
		j++;
	}
}


// Function name	: manager::manager
// Description	    : 
// Return type		: 
//##ModelId=3B79EDE60090
manager::manager()
{
	m_strCfgFileName = "HTTPTunneling.cfg";
	m_tLastModification = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -