?? tmsession.cpp
字號:
/* Copyright (c) 2005, Forum Nokia. All rights reserved */
// INCLUDE FILES
#include <e32svr.h>
#include <s32file.h>
#include "TmSession.h"
_LIT(KEmpty, "<empty>");
// ========================= MEMBER FUNCTIONS =================================
// ----------------------------------------------------------------------------
// CTmServerSession::NewL()
// Two-phased constructor.
// ----------------------------------------------------------------------------
//
CTmServerSession* CTmServerSession::NewL( CTmServer& aServer )
{
CTmServerSession* self = CTmServerSession::NewLC( aServer );
CleanupStack::Pop( self );
return self;
}
// ----------------------------------------------------------------------------
// CTmServerSession::NewLC()
// Two-phased constructor.
// ----------------------------------------------------------------------------
//
CTmServerSession* CTmServerSession::NewLC( CTmServer& aServer )
{
CTmServerSession* self = new ( ELeave ) CTmServerSession( aServer );
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
// ----------------------------------------------------------------------------
// CTmServerSession::ConstructL()
// Symbian 2nd phase constructor can leave.
// ----------------------------------------------------------------------------
//
void CTmServerSession::ConstructL()
{
iServer.IncrementSessions();
User::LeaveIfError(iFs.Connect());
// Set session to private (files will be stored in the private data cage).
User::LeaveIfError( iFs.CreatePrivatePath( EDriveC ) );
User::LeaveIfError( iFs.SetSessionToPrivate( EDriveC ) );
}
// ----------------------------------------------------------------------------
// CTmServerSession::CTmServerSession()
// C++ default constructor can NOT contain any code, that might leave.
// ----------------------------------------------------------------------------
//
CTmServerSession::CTmServerSession(CTmServer& aServer): iServer( aServer )
{
}
// ----------------------------------------------------------------------------
// CTmServerSession::~CTmServerSession()
// Destructor.
// ----------------------------------------------------------------------------
//
CTmServerSession::~CTmServerSession()
{
iFs.Close();
iServer.DecrementSessions();
}
// ----------------------------------------------------------------------------
// CTmServerSession::ServiceL()
// Service request from client.
// ----------------------------------------------------------------------------
//
//void CTmServerSession::ServiceL( const RMessage& aMessage )
void CTmServerSession::ServiceL( const RMessage2& aMessage )
{
iClientMsg = aMessage;
switch ( aMessage.Function() )
{
case ETmServRequestAddress :
RequestAddressL();
break;
case ETmServSetAddress :
SetAddressL();
break;
case ETmServRequestPort :
RequestPortL();
break;
case ETmServSetPort :
SetPortL();
break;
case ETmServRequestType :
RequestTypeL();
break;
case ETmServSetType :
SetTypeL();
break;
case ETmServRequestAll :
RequestAllL();
break;
case ETmServSetAll :
SetAllL();
break;
default:
PanicClient( EBadRequest );
break;
}
aMessage.Complete( KErrNone );
}
// ----------------------------------------------------------------------------
// CTmServerSession::RequestAddressL()
// Called as a result of the client requesting the server address.
// ----------------------------------------------------------------------------
//
void CTmServerSession::RequestAddressL()
{
ReadFileL(0, KAddressFileName);
}
// ----------------------------------------------------------------------------
// CTmServerSession::SetAddressL()
// Called as a result of the client requesting to set the server address.
// ----------------------------------------------------------------------------
//
void CTmServerSession::SetAddressL()
{
WriteFileL(0, KAddressFileName);
}
// ----------------------------------------------------------------------------
// CTmServerSession::RequestPortL()
// Called as a result of the client requesting the server port.
// ----------------------------------------------------------------------------
//
void CTmServerSession::RequestPortL()
{
ReadFileL(0, KPortFileName);
}
// ----------------------------------------------------------------------------
// CTmServerSession::SetPortL()
// Called as a result of the client requesting to set the server port.
// ----------------------------------------------------------------------------
//
void CTmServerSession::SetPortL()
{
WriteFileL(0, KPortFileName);
}
// ----------------------------------------------------------------------------
// CTmServerSession::RequestTypeL()
// Called as a result of the client requesting the server type.
// ----------------------------------------------------------------------------
//
void CTmServerSession::RequestTypeL()
{
ReadFileL(0, KTypeFileName);
}
// ----------------------------------------------------------------------------
// CTmServerSession::SetTypeL()
// Called as a result of the client requesting to set the server type.
// ----------------------------------------------------------------------------
//
void CTmServerSession::SetTypeL()
{
WriteFileL(0, KTypeFileName);
}
// ----------------------------------------------------------------------------
// CTmServerSession::RequestAllL()
// Called as a result of the client requesting the server address, port
// and type.
// ----------------------------------------------------------------------------
//
void CTmServerSession::RequestAllL()
{
ReadFileL(0, KAddressFileName);
ReadFileL(1, KPortFileName);
ReadFileL(2, KTypeFileName);
}
// ----------------------------------------------------------------------------
// CTmServerSession::SetAllL()
// Called as a result of the client requesting to set the server address, port
// and type.
// ----------------------------------------------------------------------------
//
void CTmServerSession::SetAllL()
{
WriteFileL(0, KAddressFileName);
WriteFileL(1, KPortFileName);
WriteFileL(2, KTypeFileName);
}
// ----------------------------------------------------------------------------
// CTmServerSession::WriteFileL()
// Encrypts the given descriptor and writes it to a file with the given file
// name
// ----------------------------------------------------------------------------
//
void CTmServerSession::WriteFileL( TInt aMsgIndex, const TDesC& aFileName )
{
// An RBuf could also be used here (as an instance variable) instead of a
// TBuf, in which case the client's message could be of arbitrary length.
if (iClientMsg.GetDesLength(aMsgIndex) > KTmMaxClientDescriptorLength)
{
iClientMsg.Panic(KTMExampleServer, KErrBadDescriptor);
}
TBuf<KTmMaxClientDescriptorLength> contents;
TRAPD(err2, iClientMsg.ReadL(aMsgIndex, contents));
if (err2 != KErrNone)
{
iClientMsg.Panic(KTMExampleServer, KErrBadDescriptor);
}
RFileWriteStream writeStream;
writeStream.PushL();
TInt err = writeStream.Replace(iFs, aFileName, EFileWrite);
if (err == KErrNone)
{
writeStream << contents;
writeStream.CommitL();
}
writeStream.Pop();
writeStream.Release();
}
// ----------------------------------------------------------------------------
// CTmServerSession::ReadFileL()
// Reads and decrypts a descriptor found in a file with the given file name.
// The decrypted descriptor is written to the client's address space via
// aClientsDescriptor.
// ----------------------------------------------------------------------------
//
void CTmServerSession::ReadFileL( TInt aMsgIndex, const TDesC& aFileName )
{
RFileReadStream readStream;
readStream.PushL();
TInt err = readStream.Open(iFs, aFileName, EFileRead);
if (err == KErrNone)
{
TBuf<KTmMaxClientDescriptorLength> temp;
readStream >> temp;
iClientMsg.WriteL( aMsgIndex, temp );
}
else
{
// Opening read stream failed, write a dummy string to client's address space
iClientMsg.WriteL(aMsgIndex, KEmpty);
}
readStream.Pop();
readStream.Release();
}
// -----------------------------------------------------------------------------
// CTmServerSession::PanicClient()
// Causes the client thread to panic.
// -----------------------------------------------------------------------------
//
void CTmServerSession::PanicClient( TInt aPanic ) const
{
// Note: this panics the client thread, not the server
iClientMsg.Panic( KTMExampleServer,aPanic );
}
// End of File
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -