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

📄 gprsclient.cpp

📁 一个symbian操作系统的
💻 CPP
字号:
/* Copyright (c) 2002, Nokia Mobile Phones. All rights reserved */

#include <aknquerydialog.h> 
#include <es_sock.h>
#include <aknnotewrappers.h>
#include <aknstaticnotedialog.h> 

#include "GPRSClient.h"



CGprsClient* CGprsClient::NewL(MDbObserver *aCallb)
{
    CGprsClient* self = NewLC(aCallb);
    CleanupStack::Pop(self);
    return self;
}

CGprsClient* CGprsClient::NewLC(MDbObserver *aCallb)
{
    CGprsClient* self = new (ELeave) CGprsClient(aCallb);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

CGprsClient::CGprsClient(MDbObserver *aCallb)
: CActive(CActive::EPriorityStandard),
  iState(ENotConnected),
  iCallb(aCallb)
{
    CActiveScheduler::Add(this);
}

CGprsClient::~CGprsClient()
{
	if (iSocket)
	{
		iSocket->Close();
		delete iSocket;
		iSocket=NULL;
	}
    if (IsActive()) Cancel();
	iSocketServer.Close();
}

void CGprsClient::ConstructL()
{
	User::LeaveIfError(iSocketServer.Connect());
	iSocket=NULL;
}

void CGprsClient::DoCancel()
{
}

//called when an event occurs
void CGprsClient::RunL()
{
    if (iStatus != KErrNone)
	{
		//error occured
        switch (iState)
		{
		case ENotConnected:
		case EGettingConnection:
			{
				iData.SetLength(0);
				FinishConnectionL();
				//MessageBox(_L("GPRS initialization error!"));
				if (iSocket)
				{
					iSocket->Close();
					delete iSocket;
					iSocket=NULL;
				}
				iState=ENotConnected;
			}
			break;
		case ESending:
			MessageBox(_L("KErr ESending!"));
			AppendToReceiveBuffer();
			FinishConnectionL();
			break;
		case EReceiving:
			MessageBox(_L("KErr EReceiving!"));
			FinishConnectionL();
			break;
		default:
			break;
		}
	}
    else 
	{
		//event occured
        switch (iState)
		{
		case ENotConnected:
			{
				//cannot happen
			} break;
		case EGettingConnection:
			{
				//connected, now send
				iState = ESending;
				DoSendL();
			} break;
		case ESending:
			{
				//sending finished
				iState=EReceiving;
				iData.SetLength(0);
				DoReceiveL();
			} break;
		case EReceiving:
			{
				iState = ENotConnected;
				AppendToReceiveBuffer();
				if (iData[iData.Length()-1]!=0)	DoReceiveL();
				else FinishConnectionL();
			} break;
		default:
			{
				//cannot happen
			} break;
		};
	}
}

//close the connection to the server either after a succesful or an unsuccesful operation
void CGprsClient::FinishConnectionL()
{
	if (iSocket)
	{
		iSocket->Close();
		delete iSocket;
		iSocket=NULL;
	}
	iCallb->ResponseReceived(iData);
}


//prepare for the sending operation, get the connection
void CGprsClient::SendL(TInetAddr &aIpAddress, const TDesC8& aDesc)
{
	iData=aDesc;

	iSocket = new RSocket();
	ASSERT(iState==ENotConnected);

	iState = EGettingConnection;
	User::LeaveIfError(iSocket->Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp));
	
	aIpAddress.SetPort(8080);
	
	iState = EGettingConnection;
	iSocket->Connect(aIpAddress, iStatus);
	SetActive();
}

//start sending the prepared packet
void CGprsClient::DoSendL()
{
	iSocket->Write(iData, iStatus);
    SetActive();
}


TBool CGprsClient::IsBusy()
{
    return IsActive();
}


//shows a message box to the user
void CGprsClient::MessageBox(const TDesC& aMessage)
{
				TRAPD(err, 
					CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
				informationNote->ExecuteLD(aMessage); );

}

//start receiving data
void CGprsClient::DoReceiveL()
{
	iRecvBuffer.SetLength(0);
	iSocket->RecvOneOrMore(iRecvBuffer, 0, iStatus, iLen);
	SetActive();
}

//append received data to buffer
void CGprsClient::AppendToReceiveBuffer()
{
	iData.Append(iRecvBuffer);
	iRecvBuffer.SetLength(0);
}

⌨️ 快捷键说明

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