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

📄 controller.cpp

📁 《SymbianOSC手机应用开发》源码
💻 CPP
字号:
// controller.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.
//

#include "controller.h"
#include "appview.h"

#include <eikenv.h>
#include <eiklabel.h>
#include <gdp.h>

enum TPanic {
	ESetupMeNotBlank,
	EInitiateNotBlank,
	EListenNotBlank,
	EReceiveWhenBlank,
	ESendNotBound,
	};

void Panic(TInt aPanic)
	{
	_LIT(KPanicCategory,"GSDPCHAT control");
	User::Panic(KPanicCategory, aPanic);
	}

// Controller

void CGameController::ConstructL(CGameEngine* aEngine, CEikAppUi* aAppUi)
	{
	// store cached pointers
	iEngine=aEngine;
	iAppUi=aAppUi;
	iGsdp.ConnectL(*this);
	iEnv=CEikonEnv::Static();
	// construct app view
    iAppView=new(ELeave) CGameAppView;
    iAppView->ConstructL(iEngine, iAppUi->ClientRect());
	iAppUi->AddToStackL(iAppView);
	}

CGameController::~CGameController()
	{
	iGsdp.Close();
	iAppUi->RemoveFromStack(iAppView);
    delete iAppView;
	}

// session control

void CGameController::Reset() // set to blank, loopback, my-address from document
	{
	if (iEngine->State()==CGameEngine::EBlank)
		return;
	iGsdp.StopListening();
	iEngine->Reset();
	iEngine->SetGdpProtocol(KGdpLoopbackUid);
	SetEngineL(iEngine);
	}

void CGameController::SetupMeL(TUid aProtocol)
	{
	__ASSERT_ALWAYS(iEngine->IsBlank(), Panic(ESetupMeNotBlank));
	iGsdp.SetGdpProtocol(aProtocol);
	iEngine->SetGdpProtocol(aProtocol);
	}

// The underlying protocol uses 8 bit descriptors. We exchange ascii text messages
// and ascii text network addresses, so we use a simple scheme to convert between the
// two sizes. Within the app & engine we keep cached copies of info in the native size
HBufC16* NewHBufC16LC(const TDesC8& aDataBuffer)
	{
	HBufC16* buffer = HBufC16::NewLC(aDataBuffer.Length());
	TPtr16 buffer_ptr(buffer->Des());
	buffer_ptr.Copy(aDataBuffer);
	return buffer;
	}


HBufC8* NewHBufC8LC(const TDesC16& aDataBuffer)
	{
	HBufC8* buffer = HBufC8::NewLC(aDataBuffer.Length());
	TPtr8 buffer_ptr(buffer->Des());
	buffer_ptr.Copy(aDataBuffer);
	return buffer;
	}

void CGameController::InitiateL(const TDesC& aOtherAddress, const TDesC& aSentString)
	{
	__ASSERT_ALWAYS(iEngine->IsBlank(), Panic(EInitiateNotBlank));
	TUint32 myPort=iGsdp.AllocMyNextPort();

	// the app UI and engine works with 16bit descriptors
	HBufC8* sent8 = NewHBufC8LC(aSentString);
	HBufC8* addr8 = NewHBufC8LC(aOtherAddress);
	
	iGsdp.SetOtherAddress(*addr8);
	iGsdp.Send(*sent8);
	iGsdp.Listen();

	iEngine->SetInitiatingL(myPort, aOtherAddress, aSentString);
	iAppView->iLastSentLabel->SetTextL(aSentString);
	iAppView->DrawNow();

	CleanupStack::PopAndDestroy(2);
	}

void CGameController::Listen()
	{
	__ASSERT_ALWAYS(iEngine->IsBlank(), Panic(EListenNotBlank));
	iGsdp.Listen();
	iEngine->SetListeningL();
	}

void CGameController::ReceiveAll()
	{
	iGsdp.ReceiveAll();
	}

// model update

void CGameController::SetEngineL(CGameEngine* aEngine)
	{
	iEngine=aEngine;
	iGsdp.StopListening();
	iGsdp.SetGdpProtocol(iEngine->GetGdpProtocol());
	iGsdp.SetMyPort(iEngine->MyPort());

	HBufC8* other_address = NewHBufC8LC(iEngine->OtherAddress());
	
	iGsdp.SetOtherAddress(*other_address);
	iGsdp.SetOtherPort(iEngine->OtherPort());
	if (!iEngine->IsBlank())
		iGsdp.Listen();
	iAppView->SetEngineL(iEngine);

	CleanupStack::PopAndDestroy();
	}

// MGameCommsCmdHandler stuff

void CGameController::GsdpHandleL(const TDesC8& aString)
	{
	__ASSERT_ALWAYS(!iEngine->IsBlank(), Panic(EReceiveWhenBlank));
	User::Beep(440,250000);
	// complete initiation if necessary
	if (iEngine->IsInitiating())
		{
		// ... ?
		TUint32 otherPort=iGsdp.GetOtherPort();
		iEngine->BindAfterInitiatingL(otherPort);
		}
	// complete listening if necessary
	if (iEngine->IsListening())
		{
		TUint32 myPort=iGsdp.AllocMyNextPort();
		TBuf8<KMaxGsdpAddress> otherAddress;
		iGsdp.GetOtherAddress(otherAddress);
		TUint32 otherPort=iGsdp.GetOtherPort();
		HBufC16* addr16 = NewHBufC16LC(otherAddress);
		iEngine->BindAfterListeningL(myPort, *addr16, otherPort);
		CleanupStack::PopAndDestroy();
		}
	// in any case, handle receive

	HBufC* buffer=HBufC::NewLC(aString.Length());
	TPtr buffer_ptr(buffer->Des());
	buffer_ptr.Copy(aString);

	iEngine->SetLastReceivedL(buffer_ptr);
	iAppView->iLastReceivedLabel->SetTextL(buffer_ptr);
	iAppView->DrawNow();

	CleanupStack::PopAndDestroy();
	}

// app UI commands

void CGameController::CmdSendMessageL(const TDesC& aString)
	{
	__ASSERT_ALWAYS(iEngine->IsBound(), Panic(ESendNotBound));
	iEngine->SetLastSentL(aString);
	iAppView->iLastSentLabel->SetTextL(aString);
	iAppView->DrawNow();
	
	HBufC8* buffer = HBufC8::NewLC(aString.Length());
	TPtr8 buffer_ptr(buffer->Des());
	buffer_ptr.Copy(aString);

	iGsdp.Send(buffer_ptr);

	CleanupStack::PopAndDestroy();
	}

⌨️ 快捷键说明

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