这两天写程序中,遇到了RHTTPSession与RHttpDownloadMgr共享网络连接RConnection的问题,折腾了好久,最后终于解决。
大体上的流程是先在appui中建立connection,然后分别设置RHTTPSession与RHttpDownloadMgr
我的方法是:
在APPUI中定义:
RConnection iConnect;
RSocketServ iSocketServ;
然后再初始化时:
InitConnectL()
{
User::LeaveIfError(iSocketServ.Connect());
User::LeaveIfError(iConnect.Open(iSocketServ));
User::LeaveIfError(iConnect.Start());
iClient->SetConnection(&(iConnect), &(iSocketServ));
iDLMgr->SetConnection(&iConnect);
iDLMgr->AfterConnectL();
}
iClient->SetConnection(&(iConnect), &(iSocketServ));中的代码是:
//set the sessions connection info
RStringPool strPool = iSession.StringPool();
RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
//to use our socket server and connection
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ,
RHTTPSession::GetTable() ), THTTPHdrVal (iSocketServ->Handle()) );
connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection,
RHTTPSession::GetTable() ),
THTTPHdrVal (REINTERPRET_CAST(TInt, (iConnection))) );
}
iDLMgr->SetConnection(&iConnect);
iDLMgr->AfterConnectL();
中的代码是:
void CDownloadMgr::AfterConnectL()
{
LOG(_L("CDownloadMgr::AfterConnectL"));
TUint connCount = 0;
// Count active connections
iConnect->EnumerateConnections(connCount);
if (connCount)
{
for(TUint i = 1; i <= connCount; i++)
{
TPckgBuf<TConnectionInfo> connInfo;
// Get connection info
User::LeaveIfError(iConnect->GetConnectionInfo(i, connInfo));
//return connInfo().iIapId;
User::LeaveIfError( iDMgr.SetIntAttribute( EDlMgrIap, (TInt32)connInfo().iIapId ) );
break;
//connInfo().iNetId;
}
}
}