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

📄 tmsession.cpp

📁 c++下s60终端对终端传输协议
💻 CPP
字号:
/* Copyright (c) 2005, Forum Nokia. All rights reserved */


// INCLUDE FILES
#include <e32svr.h>
#include <s32file.h>
#include <s32crypt.h>
#include "TmSession.h"

// ========================= MEMBER FUNCTIONS ==================================

// -----------------------------------------------------------------------------
// CTmServerSession::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTmServerSession* CTmServerSession::NewL( RThread& aClient,
                                              CTmServer& aServer )
    {
    CTmServerSession* self = CTmServerSession::NewLC( aClient, aServer );
    CleanupStack::Pop( self );
    return self;
    }

// -----------------------------------------------------------------------------
// CTmServerSession::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTmServerSession* CTmServerSession::NewLC( RThread& aClient,
                                               CTmServer& aServer )
    {
    CTmServerSession* self = new ( ELeave ) CTmServerSession( aClient,
                                                                  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());
    // Create the example directory (might already be created by a previous session)
    iFs.MkDir(KExampleDir);  
	iPassword = _L("TMPassword");  // A password for file encryption/decryption
    }

// -----------------------------------------------------------------------------
// CTmServerSession::CTmServerSession()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CTmServerSession::CTmServerSession( RThread& aClient, CTmServer& aServer )
: CSession( aClient ), iServer( aServer )
    {
    }

// -----------------------------------------------------------------------------
// CTmServerSession::~CTmServerSession()
// Destructor.
// -----------------------------------------------------------------------------
//
CTmServerSession::~CTmServerSession()
    {
    iFs.Close();
    iServer.DecrementSessions();
    }

// -----------------------------------------------------------------------------
// CTmServerSession::ServiceL()
// Service request from client.
// -----------------------------------------------------------------------------
//
void CTmServerSession::ServiceL( const RMessage& 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;

        default:
            PanicClient( EBadRequest );
            break;
        }
    aMessage.Complete( KErrNone );
    }

// -----------------------------------------------------------------------------
// CTmServerSession::RequestAddressL()
// Called as a result of the client requesting the server address.
// -----------------------------------------------------------------------------
//

void CTmServerSession::RequestAddressL()
    {
    const TAny* clientsDescriptor = Message().Ptr0();
    ReadFileL(clientsDescriptor, KAddressFileName);
    }
    
// -----------------------------------------------------------------------------
// CTmServerSession::SetAddressL()
// Called as a result of the client requesting to set the server address.
// -----------------------------------------------------------------------------
//

void CTmServerSession::SetAddressL()
    {
    const TAny* clientsDescriptor = Message().Ptr0();
    WriteFileL(clientsDescriptor, KAddressFileName);
    }

// -----------------------------------------------------------------------------
// CTmServerSession::RequestPortL()
// Called as a result of the client requesting the server port.
// -----------------------------------------------------------------------------
//

void CTmServerSession::RequestPortL()
    {
    const TAny* clientsDescriptor = Message().Ptr0();
    ReadFileL(clientsDescriptor, KPortFileName);
    }
    
// -----------------------------------------------------------------------------
// CTmServerSession::SetPortL()
// Called as a result of the client requesting to set the server port.
// -----------------------------------------------------------------------------
//

void CTmServerSession::SetPortL()
    {
    const TAny* clientsDescriptor = Message().Ptr0();
    WriteFileL(clientsDescriptor, KPortFileName);
    }

// -----------------------------------------------------------------------------
// CTmServerSession::RequestTypeL()
// Called as a result of the client requesting the server type.
// -----------------------------------------------------------------------------
//

void CTmServerSession::RequestTypeL()
    {
    const TAny* clientsDescriptor = Message().Ptr0();
    ReadFileL(clientsDescriptor, KTypeFileName);
    }
    
// -----------------------------------------------------------------------------
// CTmServerSession::SetTypeL()
// Called as a result of the client requesting to set the server type.
// -----------------------------------------------------------------------------
//

void CTmServerSession::SetTypeL()
    {
    const TAny* clientsDescriptor = Message().Ptr0();
    WriteFileL(clientsDescriptor, KTypeFileName);
    }

// -----------------------------------------------------------------------------
// CTmServerSession::WriteFileL()
// Encrypts the given descriptor and writes it to a file with the given file name
// -----------------------------------------------------------------------------
//

void CTmServerSession::WriteFileL( const TAny* aClientsDescriptor, const TDesC& aFileName )
	{
    TBuf<KTmMaxClientDescriptorLength> contents;
    Message().ReadL(aClientsDescriptor, contents);
	
    RFileWriteStream writeStream;
    writeStream.PushL();

    TInt err = writeStream.Replace(iFs, aFileName, EFileWrite);
  	if (err == KErrNone) 
		{
	    // Set up the encryption engine with our password
    	CBoundedSecurityBase* encrypter = Security::NewL();
    	CleanupStack::PushL( encrypter );
    	encrypter->SetL( KNullDesC, iPassword );

	    // Write the security data, used in the decryption process to
	    // verify the password
	    writeStream << encrypter->SecurityData(); // Must use streaming api
                                                   // not WriteL

    	// Write the encrypted data
    	REncryptStream encryptedFileStream;
    	encryptedFileStream.AttachLC( writeStream, *encrypter, KNullDesC8 );
    	encryptedFileStream << contents;
    	encryptedFileStream.CommitL();
    	encryptedFileStream.Close();
    	encryptedFileStream.Pop();

 	   // Clean up
	    CleanupStack::PopAndDestroy( encrypter );
		}

    writeStream.Pop();
    writeStream.Release();
	}

// -----------------------------------------------------------------------------
// CTmServerSession::WriteFileL()
// 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( const TAny* aClientsDescriptor, const TDesC& aFileName )
	{
    RFileReadStream readStream;
    readStream.PushL();

    TInt err = readStream.Open(iFs, aFileName, EFileRead);
  	if (err == KErrNone) 
		{
	    // Read the security data
	    HBufC8* securityData = HBufC8::NewLC( readStream, KMaxKeyDataLength );

    	// Create security object, using the read security data
    	CBoundedSecurityBase* decrypter = Security::NewL( *securityData );
    	CleanupStack::PushL( decrypter );

    	// Authenticate password (should never fail in this case!)
    	TRAPD( err, decrypter->PrepareL( iPassword ) );
    	if ( err == KErrNone )
        	{
	        // Read the encrypted data
	        RDecryptStream decryptedFileStream;
	        decryptedFileStream.AttachLC( readStream, *decrypter, KNullDesC8 );
        	CleanupClosePushL( decryptedFileStream );
    		
    		TBuf<KTmMaxClientDescriptorLength> temp;
    		decryptedFileStream >> temp;
    		Message().WriteL( aClientsDescriptor, temp );

	        // Clean up
	        CleanupStack::PopAndDestroy(); // Close decryptedFileStream
	        decryptedFileStream.Pop(); // Removes item placed on cleanup
    	                               // stack by AttachLC
	        }
	    else // Password authentication failed (shoud never happen in this case)
	    	{
    		// Write an empty string to the client's address space
    		Message().WriteL( aClientsDescriptor, _L("") );
	    	}

    	CleanupStack::PopAndDestroy( decrypter );
    	CleanupStack::PopAndDestroy( securityData );
		}
	else
		{
    		// Opening read stream failed, write an empty string to client's address space
    		Message().WriteL( aClientsDescriptor, _L("") );
		}

    readStream.Pop();
    readStream.Release();
	}

// -----------------------------------------------------------------------------
// CTmServerSession::PanicClient()
// Causes the client thread to panic.
// -----------------------------------------------------------------------------
//
void CTmServerSession::PanicClient( TInt aPanic ) const
    {
    Panic( KTMExampleServer,aPanic ); // Note: this panics the client thread,
                                   // not server
    }

// End of File

⌨️ 快捷键说明

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