?? gsdpclient.cpp
字號:
// gsdpclient.cpp
//
// Copyright (c) 2002 Symbian Ltd. All rights reserved.
#include "gsdpclient.h"
#include "gsdpserver.h"
void Panic(TInt aPanic)
{
_LIT(KPanicCategory,"GSDP Client");
User::Panic(KPanicCategory, aPanic);
}
/*
class RGsdpSession
*/
// open/close
EXPORT_C void RGsdpSession::ConnectL(MGsdpPacketHandler& aHandler)
{
// connect to server
TInt err=KErrNone;
for (TInt tries=0; tries<2; tries++)
{
err=CreateSession(KGsdpServerName, TVersion(0,0,0));
if (!err) break; // connected to existing server - ok
if (err!=KErrNotFound && err!=KErrServerTerminated)
break; // problems other than server not here - propagate error
err=CGsdpScheduler::LaunchFromClient();
if (!err) continue; // if server launched ok, try again to connect
if (err==KErrAlreadyExists)
continue; // if someone else got there first, try again to connect
break; // server not launched: don't cycle round again
}
User::LeaveIfError(err);
// create active object receive handler and add it to scheduler
CleanupClosePushL(*this); // Close if next operation leaves
iHandler=new (ELeave) CGsdpReceiveHandler(aHandler, *this);
CleanupStack::Pop(this);
CActiveScheduler::Add(iHandler);
}
EXPORT_C void RGsdpSession::Close()
{
// destroy receiver-handler
delete iHandler;
iHandler=0;
// destroy server session
RSessionBase::Close();
}
// game protocol
EXPORT_C void RGsdpSession::SetGameProtocol(TUint32 aProtocol)
{
TInt p[KMaxMessageArguments];
p[0]=(TInt) aProtocol;
SendReceive(EGsdpReqSetGameProtocol, p);
}
EXPORT_C TUint32 RGsdpSession::GetGameProtocol() const
{
return SendReceive(EGsdpReqGetGameProtocol, 0);
}
// set and get GDP protocol
EXPORT_C TInt RGsdpSession::SetGdpProtocol(TUid aProtocol)
{
TInt p[KMaxMessageArguments];
p[0]= aProtocol.iUid;
return SendReceive(EGsdpReqSetGdpProtocol, p);
}
EXPORT_C TUid RGsdpSession::GetGdpProtocol() const
{
return TUid::Uid(SendReceive(EGsdpReqGetGdpProtocol, 0));
}
EXPORT_C TBool RGsdpSession::GdpIsNetworked() const
{
return SendReceive(EGsdpReqGdpIsNetworked, 0);
}
EXPORT_C TInt RGsdpSession::CountGdpProtocols() const
{
return SendReceive(EGsdpReqCountProtocols, 0);
}
EXPORT_C TInt RGsdpSession::GetGdpProtocolInfo(TInt aProto, TGdpProtocolInfo& aInfo) const
{
TPckg<TGdpProtocolInfo> buf(aInfo);
TInt p[KMaxMessageArguments];
p[0] = aProto;
p[1] = reinterpret_cast<TInt>(&buf);
return SendReceive(EGsdpReqProtocolInfo, p);
}
// set and get my port
EXPORT_C void RGsdpSession::SetMyPort(TUint32 aPort)
{
TInt p[KMaxMessageArguments];
p[0]=(TInt) aPort;
SendReceive(EGsdpReqSetMyPort, p);
}
EXPORT_C TUint32 RGsdpSession::GetMyPort() const
{
return SendReceive(EGsdpReqGetMyPort, 0);
}
EXPORT_C TUint32 RGsdpSession::AllocMyNextPort()
{
return SendReceive(EGsdpReqAllocMyNextPort, 0);
}
// set and get other address and port
EXPORT_C void RGsdpSession::SetOtherAddress(const TDesC8& aAddress)
{
TInt p[KMaxMessageArguments];
p[0]=(TInt) &aAddress;
SendReceive(EGsdpReqSetOtherAddress, p);
}
EXPORT_C void RGsdpSession::GetOtherAddress(TDes8& aAddress) const
{
TInt p[KMaxMessageArguments];
p[0]=(TInt) &aAddress;
SendReceive(EGsdpReqGetOtherAddress, p);
}
EXPORT_C void RGsdpSession::SetOtherPort(TUint32 aPort)
{
TInt p[KMaxMessageArguments];
p[0]=(TInt) aPort;
SendReceive(EGsdpReqSetOtherPort, p);
}
EXPORT_C TUint32 RGsdpSession::GetOtherPort() const
{
return SendReceive(EGsdpReqGetOtherPort, 0);
}
// main protocol functions
EXPORT_C void RGsdpSession::Listen()
{
// kick off receive handler active object to start listening and start a receive
if (!iHandler->IsActive())
{
iHandler->Receive();
}
}
EXPORT_C void RGsdpSession::StopListening()
{
// tell receive handler to cancel receive and stop listening
iHandler->Cancel();
}
EXPORT_C void RGsdpSession::Send(const TDesC8& aData)
{
TInt p[KMaxMessageArguments];
p[0]=(TInt) &aData;
SendReceive(EGsdpReqSend, p);
}
// initiate receive-all for "pull" protocols
EXPORT_C void RGsdpSession::ReceiveAll() const
{
SendReceive(EGsdpReqReceiveAll, 0);
}
// private functions in conjunction with CGsdpReceiveHandler
void RGsdpSession::Receive(TDes8& aBuffer, TRequestStatus& aStatus)
{
TInt p[KMaxMessageArguments];
p[0]=(TInt) &aBuffer;
SendReceive(EGsdpReqReceive, p, aStatus);
}
void RGsdpSession::CancelReceive()
{
SendReceive(EGsdpReqCancelReceive, 0);
}
/*
CGsdpReceiveHandler
*/
// construct/destruct
CGsdpReceiveHandler::CGsdpReceiveHandler(MGsdpPacketHandler& aHandler, RGsdpSession& aSession)
: CActive(0), iSession(aSession), iHandler(aHandler)
{
}
CGsdpReceiveHandler::~CGsdpReceiveHandler()
{
Cancel();
}
// operation
void CGsdpReceiveHandler::Receive()
{
iSession.Receive(iBuffer, iStatus);
SetActive();
}
// from CActive
void CGsdpReceiveHandler::RunL()
{
iHandler.GsdpHandleL(iBuffer);
// initiate next receive
Receive();
}
void CGsdpReceiveHandler::DoCancel()
{
iSession.CancelReceive();
}
/*
DLL harness
*/
EXPORT_C TInt E32Dll(TDllReason)
{
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -