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

📄 explorer.cpp

📁 简单的socket请求
💻 CPP
字号:
// Explorer.cpp: implementation of the CExplorer class.
//
//////////////////////////////////////////////////////////////////////

#include "Explorer.h"
#include <aknnotewrappers.h> 
#include "http.hrh"
#include "HttpAppView.h"
#include "CommDbConnPref.h"

_LIT(KStrNewLine,            "\r\n\r\n");
_LIT(KStrReturn,             "\r\n");
_LIT(KStrNotConnected,       "Not connected");
_LIT(KStrConnecting,         "Connecting");
_LIT(KStrConnected,          "Connected");
_LIT(KStrLookingUp,          "Looking up");

//////////////////////////////////////////////////////////////////////////
_LIT(KStrGet,       "GET ");
_LIT(KStrVersion,   " HTTP/1.1");
_LIT(KStrPost,       "POST ");
_LIT(KStrHost,         "HOST: ");
_LIT(KStrXOnlineHost,           "X-Online-Host: ");
_LIT(KStrPOSTHeaderContentLength, "Content-Length: 0");
_LIT(KAccept, "Accept: */*");
_LIT(KConnect, "Connection: Keep-Alive");
_LIT(KCacheControl, "Cache-Control: no-store");
_LIT(KUserAgent, "User-Agent: Nokia6681/2.0 (5.37.01) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0 Configuration/CLDC-1.1");
_LIT(KRange, "Range: bytes=");

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
CExplorer::CExplorer(MDisplayData& aMDisplayData):
CActive(EPriorityStandard),iMDisplayData(aMDisplayData)
{
	iIAPSelect = 0;
	iConnectType = 2;
	iNeedHost = EFalse;
	iInterval1 = 0;
	iInterval2 = 0;
	iStartPos = 0;
	iEndPos = 204799;
}
CExplorer::~CExplorer()
{
// 	iConnection.Close();
	delete iRecver;
	iRecver = NULL;
	delete iSender;
	iSender = NULL;
	Cancel();			
	if (iSocketServ.Handle() != 0)
	{
	    iSocketServ.Close();	
	}
	iLog.CloseLog();
	iLog.Close();
	
}

CExplorer* CExplorer::NewLC(MDisplayData& aMDisplayData)
{
	CExplorer* self = new(ELeave) CExplorer(aMDisplayData);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CExplorer* CExplorer::NewL(MDisplayData& aMDisplayData)
{
	CExplorer* self = CExplorer::NewLC(aMDisplayData);
	CleanupStack::Pop();
	return self;
}

void CExplorer::ConstructL()
{
	iLog.Connect();
	iLog.CreateLog(_L("System"), _L("logfile"), EFileLoggingModeAppend);
	ChangeStatus(ENotConnected);
	CActiveScheduler::Add(this);
}
void CExplorer::StartL()
{
	User::LeaveIfError(iSocketServ.Connect());
	User::LeaveIfError(iConnection.Open(iSocketServ));   
	TCommDbConnPref   pref;   
	pref.SetIapId(iIAPSelect);   //   IAP   ID   
	pref.SetDialogPreference(   ECommDbDialogPrefDoNotPrompt   );   //   不显示提示对话框   
	pref.SetDirection(   ECommDbConnectionDirectionOutgoing   );   
	iConnection.Start(pref);
	iRecver = CRecver::NewL(*this,iSocket);
	iSender = CSender::NewL(*this,iSocket);
	ConnectL();
}
void CExplorer::ConnectL()
{
	if (iEngineStatus == ENotConnected)
	{
		switch (iConnectType)
		{
		case 1:
			{//lvs
				iPort = 9003;
				iServerName.Copy(KDefaultServerName);
				iUrl.Copy(KDefaultUrl);
			}
			break;
		case 2:
			{//cmwap
				iPort = 80;
				iServerName.Copy(KDefaultServerName1);
				if (0 >= iStartPos)
				{
					iUrl.Copy(KDefaultUrl1);
				}
			}
			break;
		case 3:
			{//cmnet
				iPort = 8088;
				iServerName.Copy(KDefaultServerName1);
				iUrl.Copy(KDefaultUrl);
			}
			break;
		case 4:
			{//white
// 				iPort = 8067;
// 				iServerName.Copy(KDefaultServerName2);
// 				iUrl.Copy(KDefaultUrl2);
				iPort = 80;
				iServerName.Copy(KDefaultServerName1_);
				if (0 >= iStartPos)
				{
					iUrl.Copy(KDefaultUrl1_);
				}
			}
			break;
		default:
			break;
		}
		if(2 == iConnectType || 4 == iConnectType)
		{
			if (!iNeedHost)
			{
				iRecver->iPushReceived = EFalse;
			}
			else
			{
				iRecver->iPushReceived = ETrue;
			}
			_LIT(KAddress,"10.0.0.172");
			User::LeaveIfError(iAddr.Input(KAddress));
			iAddr.SetPort(80);
			iPort = 80;
			ConnectL(iAddr.Address());			
		}
		else
		{
			iRecver->iPushReceived = ETrue;
			if(iAddr.Input(iServerName)==KErrNone)
			{
				ConnectL(iAddr.Address());	
			}
			else
			{
				NULL;
			}
		}
	}
}

void CExplorer::ConnectL(TUint32 aAddr)
{
	if (iEngineStatus == ENotConnected)
	{
		iStartTime.HomeTime();
		TInt err = iSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp);
		User::LeaveIfError(err);
		iAddr.SetPort(iPort);
		iAddr.SetAddress(aAddr);
		iSocket.Connect(iAddr, iStatus);
		ChangeStatus(EConnecting);
		SetActive();
	}
}
void CExplorer::DisconnectL()
{
	if (iResolver.SubSessionHandle() != 0)
	{
		iResolver.Cancel();
		iResolver.Close();
	}
	if (iSocket.SubSessionHandle() != 0)
	{
		iSocket.CancelAll();
		iSocket.Close();
	}
	if (iSocketServ.Handle() != 0)
	{
		iSocketServ.Close();	
	}
	ChangeStatus(ENotConnected);
}

void CExplorer::DoCancel()
{
	if (iConnection.SubSessionHandle() != 0)
	{
		iConnection.Close();
	}
	if (iResolver.SubSessionHandle() != 0)
	{
		iResolver.Cancel();
		iResolver.Close();
	}
	if (iSocket.SubSessionHandle() != 0)
	{
		iSocket.CancelAll();
		iSocket.Close();
	}
    ChangeStatus(ENotConnected);	
}

void CExplorer::RunL()
{
	switch(iEngineStatus)
	{
	case EConnecting:
		if (iStatus == KErrNone)
		{
			ChangeStatus(EConnected);
			SetHttpHeader();
			WriteL();
			ReadL();
		}
		else
		{
			iSocket.Close();			
			ChangeStatus(ENotConnected);
		}
		break;
	case ELookingUp:
		iResolver.Close();
		if (iStatus == KErrNone)
		{
			iNameRecord = iNameEntry();			
			
			TBuf<15> ipAddr;
			TInetAddr::Cast(iNameRecord.iAddr).Output(ipAddr);
			
			ChangeStatus(ENotConnected);
			ConnectL(TInetAddr::Cast(iNameRecord.iAddr).Address());
		}
		else
		{	
			ChangeStatus(ENotConnected);
		}
		break;
	default:
		break;		
	};
}

void CExplorer::SetServerName(TDesC aName)
{
	iServerName.Copy(aName);	
}
void CExplorer::SetPort(TInt aPort)
{
	iPort = aPort;
}

void CExplorer::ChangeStatus(TSocketsEngineState aNewStatus)
{	
    iEngineStatus = aNewStatus;
}

void CExplorer::WriteL()
{
	if (iEngineStatus == EConnected)
	{
        iSender->IssueSend(iBuffer);
	}
}

void CExplorer::ReadL()
{
	if (iEngineStatus == EConnected) 
	{
		iRecver->Start(iStartPos);
	}
}

void CExplorer::SetRequestMethod(TInt aMethod)
{
	iRequestType = aMethod;
}

void CExplorer::SetHttpHeader()
{	
	if(iRequestType == REQUEST_METHOD_GET)
	{
		iHttpHeader = KStrGet;
		iHttpHeader.Append(iUrl);
		iHttpHeader.Append(KStrVersion);
		iHttpHeader.Append(KStrReturn);
		iHttpHeader.Append(KAccept);
		iHttpHeader.Append(KStrReturn);
		iHttpHeader.Append(KConnect);
		iHttpHeader.Append(KStrReturn);
		iHttpHeader.Append(KCacheControl);
		iHttpHeader.Append(KStrReturn);
		iHttpHeader.Append(KCacheControl);
		iHttpHeader.Append(KStrReturn);
		iHttpHeader.Append(KUserAgent);
		iHttpHeader.Append(KStrReturn);
		iHttpHeader.Append(KStrHost);
		iHttpHeader.Append(iServerName);
		iHttpHeader.Append(KStrReturn);
		if (2 == iConnectType || 4 == iConnectType)
		{
			iHttpHeader.Append(KRange);
			iHttpHeader.AppendNum(iStartPos);
			iHttpHeader.Append(_L("-"));
			iHttpHeader.AppendNum(iEndPos);
		}
		iHttpHeader.Append(KStrNewLine);
	}
	else
	{
	}
	iBuffer.Copy(iHttpHeader);
}

TBool CExplorer::Isconnected()
{
	return iEngineStatus == EConnected;
}

void CExplorer::KeyEventL(TInt aCode)
{
	iMDisplayData.KeyEventL(aCode);
}

void CExplorer::SetStartWrite()
{
	iMDisplayData.SetStartWrite();
}

void CExplorer::WriteToFile(TDesC8& aData)
{
	iMDisplayData.WriteToFile(aData);
}

void CExplorer::SetEditText(TDesC8 aData)
{
	iMDisplayData.SetEditText(aData);
}

void CExplorer::DisplayText(TDesC8& aData)
{
	iMDisplayData.DisplayText(aData);
}

void CExplorer::SetIAP(TUint32 aIAP)
{
	iIAPSelect = aIAP;
}

void CExplorer::WriteLog(const TDesC16& aData)
{
	iLog.Write(aData);
}
void CExplorer::WriteLogInt(const TDesC16& aData, TInt aCount)
{
	iLog.WriteFormat(aData, aCount);
}
void CExplorer::SetUrl(TDesC8& aData)
{
	iUrl.Copy(aData);
	iMDisplayData.SetUrl(aData);
	iNeedHost = ETrue;
	if (iResolver.SubSessionHandle() != 0)
	{
		iResolver.Cancel();
		iResolver.Close();
	}
	if (iSocket.SubSessionHandle() != 0)
	{
		iSocket.CancelAll();
		iSocket.Close();
	}		
	if (iSocketServ.Handle() != 0)
	{
		iSocketServ.Close();	
	}
	ChangeStatus(ENotConnected);
	delete iRecver;
	iRecver = NULL;
	delete iSender;
	iSender = NULL;
	StartL();
}
void CExplorer::RecvBegin()
{
	iRecvTime.HomeTime();
	TTimeIntervalMicroSeconds interval;
	interval = iRecvTime.MicroSecondsFrom(iStartTime);
	iInterval1 = I64LOW(interval.Int64());
}
void CExplorer::RecvContinue(TInt aDownLen, TInt aTotalLen)
{
	TTime ct;
	ct.HomeTime();
	TTimeIntervalMicroSeconds interval;
	interval = ct.MicroSecondsFrom(iRecvTime);
	iInterval2 = I64LOW(interval.Int64());
	iLog.WriteFormat(_L("aDownLen=%d,aTotalLen=%d,id=%d,rt=%d,ft=%d"),aDownLen, aTotalLen, iConnectType, iInterval1, iInterval2);
	iInterval1 = 0;
	iInterval2 = 0;

    iStartPos += aDownLen;
	iEndPos = iStartPos + 204799;
	if (iEndPos >= aTotalLen)
	{
		iEndPos = aTotalLen - 1;
	}
	if (iResolver.SubSessionHandle() != 0)
	{
		iResolver.Cancel();
		iResolver.Close();
	}
	if (iSocket.SubSessionHandle() != 0)
	{
		iSocket.CancelAll();
		iSocket.Close();
	}		
	if (iSocketServ.Handle() != 0)
	{
		iSocketServ.Close();	
	}
	ChangeStatus(ENotConnected);
	delete iRecver;
	iRecver = NULL;
	delete iSender;
	iSender = NULL;
	StartL();
}
void CExplorer::RecvFinish()
{
		TTime ct;
		ct.HomeTime();
		TTimeIntervalMicroSeconds interval;
		interval = ct.MicroSecondsFrom(iRecvTime);
		iInterval2 = I64LOW(interval.Int64());
		iLog.WriteFormat(_L("id=%d,rt=%d,ft=%d"), iConnectType, iInterval1, iInterval2);
	iInterval1 = 0;
	iInterval2 = 0;
	iMDisplayData.NextConnect();
}
void CExplorer::ChangeState()
{
	iEngineStatus = EConnected;
}
void CExplorer::NotifyState(TInt aTotalLen, TInt aDownLen)
{
	iMDisplayData.NotifyState(aTotalLen, aDownLen);
}

⌨️ 快捷键说明

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