// ConnectManager.cpp: implementation of the CConnectManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GPRSDemo.h"
#include "ConnectManager.h"

#include<connmgr.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CConnectManager::CConnectManager()
{

}

CConnectManager::~CConnectManager()
{
	ReleaseConnection ();
}

BOOL CConnectManager::GetConnMgrAvailable()
{
	HANDLE hConnMgr = ConnMgrApiReadyEvent ();
	BOOL bAvailbale = FALSE;
	DWORD dwResult = ::WaitForSingleObject ( hConnMgr, 2000 );
	if ( dwResult == WAIT_OBJECT_0 )
	{
		bAvailbale = TRUE;
	}
	// 关闭
	if ( hConnMgr ) CloseHandle ( hConnMgr );

	return bAvailbale;
}

int CConnectManager::MapURLAndGUID ( LPCTSTR lpszURL, OUT GUID &guidNetworkObject, OUT CString *pcsDesc/*=NULL*/ )
{
	if ( !lpszURL || lstrlen(lpszURL) < 1 )
		return FALSE;

	memset ( &guidNetworkObject, 0, sizeof(GUID) );
	int nIndex = 0;
	HRESULT hResult = ConnMgrMapURL ( lpszURL, &guidNetworkObject, (DWORD*)&nIndex );
	if ( FAILED(hResult) )
	{
		nIndex = -1;
		DWORD dwLastError = GetLastError ();
		AfxMessageBox ( _T("Could not map a request to a network identifier") );
	}
	else
	{
		if ( pcsDesc )
		{
			CONNMGR_DESTINATION_INFO DestInfo = {0};
			if ( SUCCEEDED(ConnMgrEnumDestinations(nIndex, &DestInfo)) )
			{
				*pcsDesc = DestInfo.szDescription;
			}
		}
	}

	return nIndex;
}

void CConnectManager::EnumNetIdentifier ( OUT CStringArray &StrAry )
{
	CONNMGR_DESTINATION_INFO networkDestInfo = {0};

	// 得到网络列表
	for ( DWORD dwEnumIndex=0; ; dwEnumIndex++ )
	{
		memset ( &networkDestInfo, 0, sizeof(CONNMGR_DESTINATION_INFO) );
		if ( ConnMgrEnumDestinations ( dwEnumIndex, &networkDestInfo ) == E_FAIL )
		{
			break;
		}
		StrAry.Add ( networkDestInfo.szDescription );
	}
}

BOOL CConnectManager::EstablishConnection ( DWORD dwIndex )
{
	// 释放之前的连接
	ReleaseConnection ();

	// 得到正确的连接信息
	CONNMGR_DESTINATION_INFO DestInfo = {0};
	HRESULT hResult = ConnMgrEnumDestinations(dwIndex, &DestInfo);
	BOOL bRet = FALSE;
	if(SUCCEEDED(hResult))
	{
		// 初始化连接结构
		CONNMGR_CONNECTIONINFO ConnInfo;

		ZeroMemory(&ConnInfo, sizeof(ConnInfo));
		ConnInfo.cbSize = sizeof(ConnInfo);
		ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
		ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
		ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
		ConnInfo.guidDestNet = DestInfo.guid;
		ConnInfo.bExclusive	= FALSE; 
		ConnInfo.bDisabled = FALSE;

		DWORD dwStatus = 0;
		hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 10*1000, &dwStatus );
		if(FAILED(hResult))
		{
			m_hConnection = NULL;
		}
		else bRet = TRUE;
	}

	return bRet;
}

BOOL CConnectManager::EstablishConnection(GUID guid)
{
	// 释放之前的连接
	ReleaseConnection ();
	//判断GUID是否合法
	//……

	HRESULT hResult=NULL;
	BOOL bRet = FALSE;
	// 初始化连接结构
	CONNMGR_CONNECTIONINFO ConnInfo;
	
	ZeroMemory(&ConnInfo, sizeof(ConnInfo));
	ConnInfo.cbSize = sizeof(ConnInfo);
	ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
	ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
	//ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
	ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
	ConnInfo.guidDestNet = guid;
	ConnInfo.bExclusive	= FALSE; 
	ConnInfo.bDisabled = FALSE;
	
	DWORD dwStatus = 0;
	hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 10*1000, &dwStatus );
	if(FAILED(hResult))
	{
		m_hConnection = NULL;
	}
	else bRet = TRUE;

	return bRet;
}

BOOL CConnectManager::WaitForConnected ( int nTimeoutSec, DWORD *pdwStatus/*=NULL*/ )
{
	DWORD dwStartTime = GetTickCount ();
	BOOL bRet = FALSE;
	while ( GetTickCount ()-dwStartTime < (DWORD)nTimeoutSec * 1000 )
	{
		if ( m_hConnection )
		{
			DWORD dwStatus = 0;
			HRESULT hr = ConnMgrConnectionStatus ( m_hConnection, &dwStatus );
			if ( pdwStatus ) *pdwStatus = dwStatus;
			if ( SUCCEEDED(hr) )
			{
				if ( dwStatus == CONNMGR_STATUS_CONNECTED )
				{
					bRet = TRUE;
					break;
				}
			}
		}
		Sleep ( 100 );
	}

	return bRet;
}

void CConnectManager::ReleaseConnection ()
{
	if ( m_hConnection )
	{
		ConnMgrReleaseConnection(m_hConnection, FALSE);
		m_hConnection = NULL;
	}
}