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

📄 transport.cpp

📁 Symbian OS C++ for Mobile Phones Volume 3 源码
💻 CPP
字号:
// Copyright (c) 2004 - 2007, Symbian Software Ltd. All rights reserved.

#include "transport.h"


// -------- (de)allocation --------


EXPORT_C CTransport::CTransport(MTransportObserver& aObserver)
/**
	This c'tor is defined to pass the supplied observer to the superclass.

	@param	aObserver		Observer to notify about transport events.
							This is managed by the CTransport superclass.
 */
:	iObserver(aObserver)
	{
	// this object is added to the active scheduler in the
	// CTransportInterface constructor.
	}

EXPORT_C void CTransport::ConstructL(TBool aInitListen)
/**
	If this object should start by listening for an incoming payload
	then launch the listen request from here.  This function should be
	called AFTER the derived class has set up the transport.

	@param	aInitListen		Whether the transport should listen for an incoming payload
							after it has been created.  If false, it should wait for its
							owner to give it a payload to send.
 */
	{
	iListening = aInitListen;
	if (iListening)
		{
		LaunchRead();
		}
	}


// -------- data transfer --------

EXPORT_C void CTransport::SendPayload(const TDesC& aPayload)
/**
	Implement CTransportInterface by asking the transport-specific
	subclass to send the supplied payload to the remote device.
	
	@param	aPayload		Text to send to remote device.  This function
							makes its own copy of the supplied data, so the
							does not have to maintain it.
 */
	{
	TRAN_LOG3(">CTransport::SendPayload,st=0x%x,fl=0x%x,txt=\"%S\"", iStatus.Int(), ((const TInt*)&iStatus)[1], &aPayload);
	__TRANS_ASSERT(! iListening, ESpListening);
	__TRANS_ASSERT(aPayload.Length() == KPayloadLen, ESpInvalidLength);
	
	iPayload.Copy(aPayload);
	TRAPD(r, DoSendPayloadL());
	CompleteSelfIfError(r);
	SetActive();
	TRAN_LOG0("<CTransport::SendPayload");
	}

void CTransport::LaunchRead()
/**
	Asks the transport-specific subclass to read an incoming payload.
	Sets this object to active.
 */
	{
	TRAN_LOG0(">CTransport::LaunchRead");
	DoLaunchRead();
	SetActive();
	TRAN_LOG0("<CTransport::LaunchRead");
	}


// -------- partially implement / override CActive --------

EXPORT_C void CTransport::RunL()
/**
	Implement CActive by notifying the observer when a read or write operation
	completes, successfully or otherwise.

	If a write completes successfully then it calls the observer's SentPayload function.
	
	If a read completes successfully then it calls the observer's ReceivedPayload function.
	
	If an error occurs then it calls the observer's LostConnection function via RunError.
 */
	{
	TRAN_LOG2(">CTransport::RunL,status=%d,listen=%d", iStatus.Int(), iListening);
	
	// notify user about error in RunError
	User::LeaveIfError(iStatus.Int());

	TBool wasListening = iListening;
	iListening = ! iListening;

	if (wasListening)
		iObserver.ReceivedPayload(iPayload);
	else
		{
		iObserver.SentPayload();
		LaunchRead();
		}
	
	TRAN_LOG0("<CTransport::RunL");
	}

EXPORT_C TInt CTransport::RunError(TInt aError)
/**
	Notify observer an error has occured.  Once this function
	has been called this transport should be destroyed.

	@param	aError			Leave code from RunL implementation.
	@return					KErrNone.  This tells the active scheduler
							that this object has handled the error and
							that the scheduler should continue as normal.
 */
	{
	TRAN_LOG1(">CTransport::RunError,%d", aError);
	
	iListening = EFalse;		// don't process any more incoming messages
	iObserver.LostConnection(aError);
	
	TRAN_LOG0("<CTransport::RunError");
	return KErrNone;
	}


// -------- AO management --------

EXPORT_C void CTransport::CompleteSelf(TInt aError)
/**
	This function is provided for the convenience of subclasses
	who want to complete this AO because an event has occured.

	This is useful when events do not complete this object's
	request status, such as receiving data over bluetooth, or
	receiving a message.

	@param	aError			Error code used to complete this object.
 */
	{
	TRequestStatus* rs = &iStatus;
	User::RequestComplete(rs, aError);
	}

EXPORT_C void CTransport::CompleteSelfIfError(TInt aError)
/**
	Complete this object with the supplied error code if it is not KErrNone.

	@param	aError			Symbian OS error code.  If this is KErrNone then
							this AO is not completed.
 */
	{
	if (aError != KErrNone)
		CompleteSelf(aError);
	}

⌨️ 快捷键说明

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