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

📄 engine.cpp

📁 symbian网络聊天
💻 CPP
字号:
// engine.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

#include "engine.h"
#include <gdp.h>

// construct/destruct

CGameEngine::~CGameEngine()
	{
	delete iOtherAddress;
	delete iLastSent;
	delete iLastReceived;
	}

// interrogate

TBool CGameEngine::IsBlank() const
	{
	return iState==EBlank;
	}

TBool CGameEngine::IsListening() const
	{
	return iState==EListening;
	}

TBool CGameEngine::IsInitiating() const
	{
	return iState==EInitiating;
	}

TBool CGameEngine::IsBound() const
	{
	return iState==EBound;
	}

CGameEngine::TState CGameEngine::State() const
	{
	return iState;
	}

_LIT(KNullString,"");

TUid CGameEngine::GetGdpProtocol() const // get current GDP protocol
	{
	return iGdpProtocol;
	}

TUint32 CGameEngine::MyPort() const
	{
	return iMyPort;
	}

const TDesC& CGameEngine::OtherAddress() const
	{
	if (iOtherAddress)
		return *iOtherAddress;
	else
		return KNullString;
	}

TUint32 CGameEngine::OtherPort() const
	{
	return iOtherPort;
	}

const TDesC& CGameEngine::LastSent() const
	{
	if (iLastSent)
		return *iLastSent;
	else
		return KNullString;
	}

const TDesC& CGameEngine::LastReceived() const
	{
	if (iLastReceived)
		return *iLastReceived;
	else
		return KNullString;
	}

// set

void CGameEngine::Reset() // resets everything
	{
	// state is blank
	iState=EBlank;
	// GDP protocol is loopback
	iGdpProtocol=KGdpLoopbackUid;
	// reset my address
	iMyPort=0;
	// reset other address
	delete iOtherAddress;
	iOtherAddress=0;
	iOtherPort=0;
	// reset message logs
	delete iLastSent;
	iLastSent=0;
	delete iLastReceived;
	iLastReceived=0;
	}

void CGameEngine::SetGdpProtocol(TUid aGdpProtocol)
	{
	iGdpProtocol=aGdpProtocol;
	}

void CGameEngine::SetBoundL(TUint32 aMyPort, const TDesC& aOtherAddress, TUint32 aOtherPort)
	{
	iState=EBound;
	SetMyPort(aMyPort);
	SetOtherAddressL(aOtherAddress);
	SetOtherPort(aOtherPort);
	}

void CGameEngine::SetInitiatingL(TUint32 aMyPort, const TDesC& aOtherAddress, const TDesC& aSentString)
	{
	iState=EInitiating;
	SetMyPort(aMyPort);
	SetOtherAddressL(aOtherAddress);
	SetLastSentL(aSentString);
	}

void CGameEngine::BindAfterInitiatingL(TUint32 aOtherPort)
	{
	iState=EBound;
	SetOtherPort(aOtherPort);
	}

void CGameEngine::SetListeningL()
	{
	iState=EListening;
	}

void CGameEngine::BindAfterListeningL(TUint32 aMyPort, const TDesC& aOtherAddress, TUint32 aOtherPort)
	{
	iState=EBound;
	SetMyPort(aMyPort);
	SetOtherAddressL(aOtherAddress);
	SetOtherPort(aOtherPort);
	}

void CGameEngine::SetLastSentL(const TDesC& aString)
	{
	delete iLastSent;
	iLastSent=0;
	iLastSent=aString.AllocL();
	}

void CGameEngine::SetLastReceivedL(const TDesC& aString)
	{
	delete iLastReceived;
	iLastReceived=0;
	iLastReceived=aString.AllocL();
	}

void CGameEngine::SetMyPort(TUint32 aPort)
	{
	iMyPort=aPort;
	}

void CGameEngine::SetOtherAddressL(const TDesC& aString)
	{
	delete iOtherAddress;
	iOtherAddress=0;
	if (aString.Size()==0)
		return;
	iOtherAddress=aString.AllocL();
	}

void CGameEngine::SetOtherPort(TUint32 aPort)
	{
	iOtherPort=aPort;
	}

// persistence

void CGameEngine::ExternalizeL(RWriteStream& aStream) const
	{
	// protocol
	aStream.WriteInt32L(iGdpProtocol.iUid);
	// state
	aStream.WriteInt32L(iState);
	// my port
	aStream.WriteInt32L(iMyPort);
	// other address
	if (iOtherAddress)
		aStream << *iOtherAddress;
	else aStream << KNullDesC;
	// other port
	aStream.WriteInt32L(iOtherPort);
	// last sent
	if (iLastSent)
		aStream << *iLastSent;
	else aStream << KNullDesC;
	// last received
	if (iLastReceived)
		aStream << *iLastReceived;
	else aStream << KNullDesC;
	}

void CGameEngine::InternalizeL(RReadStream& aStream)
	{
	Reset();
	HBufC* string;
	// protocol
	iGdpProtocol.iUid = aStream.ReadInt32L();
	// state
	iState=(TState) aStream.ReadInt32L();
	// my port
	iMyPort=aStream.ReadInt32L();
	// other address
	string=HBufC::NewL(aStream,10000);
	if (string->Length() > 0)
		iOtherAddress=string;
	else
		{
		iOtherAddress=0;
		delete string;
		}
	// other port
	iOtherPort=aStream.ReadInt32L();
	// last sent
	string=HBufC::NewL(aStream,10000);
	if (string->Length() > 0)
		iLastSent=string;
	else
		{
		iLastSent=0;
		delete string;
		}
	// last received
	string=HBufC::NewL(aStream,10000);
	if (string->Length() > 0)
		iLastReceived=string;
	else
		{
		iLastReceived=0;
		delete string;
		}
	}

TStreamId CGameEngine::StoreL(CStreamStore& aStore) const
	{
	RStoreWriteStream stream;
	TStreamId id = stream.CreateLC(aStore);
	ExternalizeL(stream);
	stream.CommitL();
	CleanupStack::PopAndDestroy(); // stream
	return id;
	}

void CGameEngine::RestoreL(const CStreamStore& aStore, TStreamId aStreamId)
	{
	RStoreReadStream stream;
	stream.OpenLC(aStore,aStreamId);
	InternalizeL(stream);
	CleanupStack::PopAndDestroy(); // stream
	}

⌨️ 快捷键说明

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