GPRS開發(fā)系列文章之進階篇
HRESULT WINAPI ConnMgrReleaseConnection( HANDLE hConnection,BOOL bCache );
【連接管理API大致使用步驟】:
首先我們利用ConnMgrApiReadyEvent()函數(shù)來確認是否有可用連接,如果有可用連接我們則利用ConnMgrEnumDestinations()函數(shù)枚舉所有可用連接,然后遍歷所有連接調(diào)用我們的同步或異步連接方法ConnMgrEstablishConnectionSync()與ConnMgrEstablishConnection()來發(fā)起連接,一旦連接成功后我們就可以進行我們偉大的下一步了,就是和我們的服務器進行通信。
【GPRS demo效果圖】
【GPRSDemo介紹】
GPRSDemo主要利用了上述的幾個重要的API函數(shù)來獲取當前可用連接,并自動選擇一個最佳的連接途徑,然后啟用這個連接,在連接啟動成功以后再用socket 進行網(wǎng)絡連接,與公網(wǎng)服務器進行通信。
首先檢查是否有可用連接
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;
}
然后枚舉所有可用連接:
void CConnectManager::EnumNetIdentifier ( OUT CStringArray StrAry )
{
CONNMGR_DESTINATION_INFO networkDestInfo = {0};
// 得到網(wǎng)絡列表
for ( DWORD dwEnumIndex=0; ; dwEnumIndex++ )
{
memset ( networkDestInfo, 0, sizeof(CONNMGR_DESTINATION_INFO) );
if ( ConnMgrEnumDestinations ( dwEnumIndex, networkDestInfo ) == E_FAIL )
{
break;
}
StrAry.Add ( networkDestInfo.szDescription );
}
}
接下來找到“Internet”這個連接,可用遠程URL映射的方式來完成,這樣可以讓系統(tǒng)自動選取一個最好的連接。
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;
}
最后啟用指定編號的連接并檢查連接狀態(tài)
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;
}
檢測連接狀態(tài)
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;
}
}
2. 客戶端與服務器端進行socket通信APIsocket通信相關開發(fā)API在Winsock2.h.文件中定義,因為SOCKET通信不是本文的重點但是又是必須要涉及的
評論