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

📄 irclienttoserver.cpp

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

#include "infraredtransport.h"


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

CIrClientToServer* CIrClientToServer::NewL(MTransportObserver& aObserver, const TDesC& aProtocolName)
/**
	Factory function allocates infrared transport.

	@param	aObserver		Observer to notify about transport events.
							This is managed by the CTransport superclass.
	@param	aProtocolName	Which protocol is used to communicate with the
							remote device.  Supported values are "IrTinyTP"
							and "Irmux".
	@return					New, connected instance of CIrClientToServer.
							This is owned by the caller.
 */
	{
	CIrClientToServer* self = new(ELeave) CIrClientToServer(aObserver);
	CleanupStack::PushL(self);
	self->ConstructL(aProtocolName);
	CleanupStack::Pop(self);
	return self;
	}

CIrClientToServer::CIrClientToServer(MTransportObserver& aObserver)
/**
	This constructor 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.
 */
:	CInfraredTransport(aObserver)
	{
	// empty.
	}

void CIrClientToServer::ConstructL(const TDesC& aProtocolName)
/**
	Connect to the remote host.  This function returns when the connection
	has been made, or has failed.

	@param	aProtocolName	Which protocol is used to communicate with the
							remote device.  Supported values are "IrTinyTP"
							and "Irmux".
 */
	{
	TRAN_LOG1(">CIrClientToServer::ConstructL,pr=\"%S\"", &aProtocolName);
	
	TInt r;
	
	CInfraredTransport::ConstructL(aProtocolName);
	
	// get information about the remote device

	RHostResolver hr;
	r = hr.Open(iSocketServ, iProtoDesc.iAddrFamily, iProtoDesc.iProtocol);
	TRAN_LOG1("-CIrClientToServer::ConstructL,hropen=%d", r);
	User::LeaveIfError(r);
	
	TNameEntry log;			// wraps a TNameRecord
	r = hr.GetByName(KNullDesC, log);
	TRAN_LOG2("-CIrClientToServer::ConstructL,gbn=%d,iName=\"%S\"", r, &log().iName);
	hr.Close();
	User::LeaveIfError(r);
	
	// attempt to connect to the remote device.  Local port is auto-bound.
	
	r = iSocket.Open(iSocketServ, iProtoDesc.iAddrFamily, iProtoDesc.iSockType, iProtoDesc.iProtocol);
	User::LeaveIfError(r);
	
	TIrdaSockAddr irdaSockAddr(log().iAddr);
	irdaSockAddr.SetPort(KIrSocketPortNum);		// remote port number, local on server
	iSocket.Connect(irdaSockAddr, iStatus);
	SetActive();
	
	// dialog will be dismissed by the user or by RunL
	r = (! iObserver.StartedConnectingToServiceL()) ? KErrCancel : iConnectError;
	TRAN_LOG1("-CInfraredTransport::ConstructL,connect=%d", r);
	User::LeaveIfError(r);

	CTransport::ConstructL(/*aInitListen*/ ETrue);

	TRAN_LOG0("<CIrClientToServer::ConstructL");
	}

CIrClientToServer::~CIrClientToServer()
/**
	Cancel any pending operations and free used resources.
 */
	{
	Cancel();
	CloseDataSocket();
	}


// -------- implement CActive / override CTransport --------

void CIrClientToServer::RunL()
/**
	Implement CActive and override CTransport by handling connection event.
	If the socket is already connected then delegate to CInfraredTransport which
	will convert the payload from 8 bit to native width.
 */
	{
	TRAN_LOG2(">CIrClientToServer::RunL,st=%d,conn=%d", iStatus.Int(), iConnected);

	if (iConnected)
		CInfraredTransport::RunL();
	else
		{
		iConnectError = iStatus.Int();
		iConnected = ETrue;
		iObserver.StoppedConnectingToService();
		}
	
	TRAN_LOG0("<CIrClientToServer::RunL");
	}

void CIrClientToServer::DoCancel()
/**
	Implement CActive and override CInfraredTransport by
	cancelling any pending connect.  If this device is already
	connected then CInfraredTransport cancels any pending read
	or write.
 */
	{
	if (! iConnected)
		iSocket.CancelConnect();
	else
		CInfraredTransport::DoCancel();
	}

⌨️ 快捷键说明

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