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

📄 gsdpsession.cpp

📁 Symbian mobile os C++ GSDP编程
💻 CPP
字号:
// gsdpsession.cpp
//
// Copyright (c) 2000-2002 Symbian Ltd.  All rights reserved.

#include "gsdpserver.h"

/*
	CGsdpSession
*/

// construct/destruct

CGsdpSession::CGsdpSession()
	{
	}

void CGsdpSession::ConstructL(CGsdpServer& aServer)
	{
	iGdpProtocol = aServer.GetProtocolL(KGdpLoopbackUid);
	}

CGsdpSession::~CGsdpSession()
	{
	// cancel any receive
	CancelReceive();
	// decrement session count, so server may terminate if no longer needed
	Server()->DecrementSessions();
	}

CGsdpServer* CGsdpSession::Server() const
	/**
	   Return the GSDP server.

	   This deliberately hides the CSharableSession version of this
	   function, to prevent lots of ugly casts around the code.
	*/
	{
	return const_cast<CGsdpServer*>(static_cast<const CGsdpServer*>(CSharableSession::Server()));
	}

// service dispatcher - from CSharableSession

void CGsdpSession::ServiceL(const RMessage& aMessage)
	{
	switch (aMessage.Function())
		{
	case EGsdpReqCountProtocols: // return TInt
		aMessage.Complete(Server()->CountProtocols());
		break;
	case EGsdpReqProtocolInfo:   // retrieves protocol info
		GetProtocolInfoL(aMessage.Int0(), aMessage.Ptr1());
		aMessage.Complete(KErrNone);
		break;
	case EGsdpReqSetGameProtocol: // TUint32 aProtocol
		SetGameProtocol(aMessage.Int0());
		aMessage.Complete(KErrNone);
		break;
	case EGsdpReqGetGameProtocol: // returns TUint32
		aMessage.Complete(GetGameProtocol());
		break;
	case EGsdpReqSetGdpProtocol: // RGsdpSession::GdpProtocol aProtocol
		SetGdpProtocolL(TUid::Uid(aMessage.Int0()));
		aMessage.Complete(KErrNone);
		break;
	case EGsdpReqGetGdpProtocol: // returns RGsdpSession::Protocol
		aMessage.Complete(GetGdpProtocol().iUid);
		break;
	case EGsdpReqGdpIsNetworked: // returns TBool
		aMessage.Complete(GdpIsNetworked());
		break;
	case EGsdpReqSetMyPort: // TUint32 aPort
		SetMyPort(aMessage.Int0());
		aMessage.Complete(0);
		break;
	case EGsdpReqGetMyPort: // returns TUint32
		aMessage.Complete(GetMyPort());
		break;
	case EGsdpReqAllocMyNextPort: // returns TUint32
		aMessage.Complete(AllocMyNextPort());
		break;
	case EGsdpReqSetOtherAddress: // const TAny& aAddress
		SetOtherAddress(aMessage.Ptr0());
		aMessage.Complete(0);
		break;
	case EGsdpReqGetOtherAddress: // TAny& aAddress
		GetOtherAddress(aMessage.Ptr0());
		aMessage.Complete(0);
		break;
	case EGsdpReqSetOtherPort: // TUint32 aPort
		SetOtherPort((TUint32) aMessage.Int0());
		aMessage.Complete(0);
		break;
	case EGsdpReqGetOtherPort: // returns TUint32
		aMessage.Complete(GetOtherPort());
		break;
	case EGsdpReqSend: // const TAny& aData
		aMessage.Complete(Send(aMessage.Ptr0()));
		break;
	case EGsdpReqReceiveAll:
		ReceiveAll();
		aMessage.Complete(0);
		break;
	case EGsdpReqReceive: // TAny& aBuffer - async
		Receive(aMessage.Ptr0());
		break;
	case EGsdpReqCancelReceive:
		CancelReceive();
		aMessage.Complete(0);
		break;
	default:
		Server()->PanicClient(EBadRequest);
		};
	}


// client service functions
void CGsdpSession::GetProtocolInfoL(TInt aProto, const TAny* aDes)
	/**
	   Write the protocol information back to the client.
	*/
	{
	TGdpProtocolInfo info;
	TPckg<TGdpProtocolInfo> buf(info);
	Server()->GetProtocolInfoL(aProto, info);
	Message().WriteL(aDes, buf);
	}

void CGsdpSession::SetGdpProtocolL(TUid aProtocol)
	{
	iGdpProtocol = Server()->GetProtocolL(aProtocol);
	}

TUid CGsdpSession::GetGdpProtocol()
	{
	return iGdpProtocol->ProtocolUid();
	}

TBool CGsdpSession::GdpIsNetworked()
	{
	return iGdpProtocol->IsNetworked();
	}

void CGsdpSession::SetGameProtocol(TUint32 aProtocol)
	{
	// set protocol
	iGameProtocol=aProtocol;
	// check whether we can now receive anything
	if (iReceiveActive)
		Server()->iReceiveQueue->CheckPackets(this);
	}

TUint32 CGsdpSession::GetGameProtocol()
	{
	return iGameProtocol;
	}

void CGsdpSession::SetMyPort(TUint32 aPort)
	{
	// set port
	iMyPort=aPort;
	// check whether we can now receive anything
	if (iReceiveActive)
		Server()->iReceiveQueue->CheckPackets(this);
	}

TUint32 CGsdpSession::GetMyPort()
	{
	return iMyPort;
	}

TUint32 CGsdpSession::AllocMyNextPort()
	{
	iMyPort=Server()->MyNextPort();
	return iMyPort;
	}

void CGsdpSession::SetOtherAddress(const TAny* aAddress)
	{
	Message().ReadL(aAddress, iOtherAddress);
	}

void CGsdpSession::GetOtherAddress(const TAny* aAddress)
	{
	Message().WriteL(aAddress, iOtherAddress);
	}

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

TUint32 CGsdpSession::GetOtherPort()
	{
	return iOtherPort;
	}

TInt CGsdpSession::Send(const TAny* aData)
	{
	__ASSERT_ALWAYS(iMyPort!=0, Server()->PanicClient(ESendFromZeroPort));
	TBuf8<KMaxGsdpData> buffer;
	Message().ReadL(aData, buffer, 0);
	iGdpProtocol->SendL(iGameProtocol, iMyPort, iOtherAddress, iOtherPort, buffer);
	return 0;
	}

void CGsdpSession::ReceiveAll()
	{
	iGdpProtocol->ReceiveAll();
	}

void CGsdpSession::Receive(const TAny* aBuffer)
	{
	__ASSERT_DEBUG(!iReceiveActive, PanicServer(EReceiveReceiveAlreadyActive));
	// remember receive request
	iReceiveMessage=Message();
	iReceiveBuffer=aBuffer;
	iReceiveActive=ETrue;
	// check for immediate fulfilment
	Server()->iReceiveQueue->CheckPackets(this);
	}

void CGsdpSession::CancelReceive()
	{
	if (!iReceiveActive)
		return;
	iReceiveMessage.Complete(KErrCancel);
	iReceiveActive=EFalse;
	}

// receive queue support

TBool CGsdpSession::CanReceivePacket(const TGsdpPacket& aPacket) const
	{
	return iReceiveActive && (
		iMyPort==aPacket.iToPort && (
			iMyPort!=0 || // in session
			iMyPort==0 && iGameProtocol==aPacket.iGameProtocol // listening
			)
		);
	}

void CGsdpSession::ReceivePacket(TGsdpPacket& aPacket)
	{
	// decide whether to drop or to receive
	TBool drop=EFalse;
	if (aPacket.iGameProtocol!=iGameProtocol)
		drop=ETrue;
	if (iOtherPort!=0 && (aPacket.iAddress!=iOtherAddress || aPacket.iFromPort!=iOtherPort))
		drop=ETrue;
	// get remote's port and address information if we haven't already got it
	if (iOtherPort==0)
		{
		iOtherPort=aPacket.iFromPort;
		iOtherAddress=aPacket.iAddress;
		}
	// receive packet if we should do
	if (!drop)
		{
		iReceiveMessage.WriteL(iReceiveBuffer, aPacket.iData);
		iReceiveMessage.Complete(KErrNone);
		iReceiveActive=EFalse;
		}
	// in any case, tell the receive queue to free the packet for future use
	Server()->iReceiveQueue->FreePacket(aPacket);
	}

⌨️ 快捷键说明

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