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

📄 fileserver.cpp

📁 Symbian Client_Server_Application
💻 CPP
字号:
/*
* ==============================================================================
*  Name        : FileServer.cpp
*  Part of     : clientserversync
*  Interface   : 
*  Description : 
*  Version     : 
* ==============================================================================
*/


// INCLUDE FILES
#include <e32svr.h>

#include "FileServer.h"
#include "ClientServerCommon.h"
#include "FileSession.h"

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

// -----------------------------------------------------------------------------
// CFileServer::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CFileServer* CFileServer::NewL()
    {
    CFileServer* FileServer = CFileServer::NewLC();
    CleanupStack::Pop( FileServer );
    return FileServer;
    }

// -----------------------------------------------------------------------------
// CFileServer::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CFileServer* CFileServer::NewLC()
    {
    CFileServer* FileServer = new ( ELeave ) CFileServer( EPriorityNormal );
    CleanupStack::PushL( FileServer );
    FileServer->ConstructL();
    return FileServer;
    }

// -----------------------------------------------------------------------------
// CFileServer::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CFileServer::ConstructL()
    {
    StartL( KFileServerName );
    }

// -----------------------------------------------------------------------------
// CFileServer::CFileServer()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CFileServer::CFileServer( TInt aPriority )
: CServer2( aPriority )
    {
    // Implementation not required
    }

// -----------------------------------------------------------------------------
// CFileServer::NewSessionL()
// Creates a File server session.
// -----------------------------------------------------------------------------
//
CSession2* CFileServer::NewSessionL( const TVersion& aVersion,const RMessage2& /*aMessage*/ ) const
    {
    // Check we are the right version
    if ( !User::QueryVersionSupported( TVersion( KFileServMajorVersionNumber,
                                                 KFileServMinorVersionNumber,
                                                 KFileServBuildVersionNumber ),
                                       aVersion ) )
        {
        User::Leave( KErrNotSupported );
        }

   CFileServerSession* mysession = CFileServerSession::NewL();
    CONST_CAST( CFileServer*, this )->IncrementSessions();
    return mysession;
    }

// -----------------------------------------------------------------------------
// CFileServer::IncrementSessions()
// Increments the count of the active sessions for this server.
// -----------------------------------------------------------------------------
//
void CFileServer::IncrementSessions()
    {
    iSessionCount++;
    }

// -----------------------------------------------------------------------------
// CFileServer::DecrementSessions()
// Decrements the count of the active sessions for this server.
// -----------------------------------------------------------------------------
//
void CFileServer::DecrementSessions()
    {
    iSessionCount--;
    if ( iSessionCount <= 0 )
        {
        CActiveScheduler::Stop();
        }
    }

// -----------------------------------------------------------------------------
// CFileServer::RunError()
// Processes any errors.
// -----------------------------------------------------------------------------
//
TInt CFileServer::RunError( TInt aError )
    {
    if ( aError == KErrBadDescriptor )
        {
        // A bad descriptor error implies a badly programmed client,
        // so panic it; otherwise report the error to the client
        PanicClient( Message(), EBadDescriptor );
        }
    else
        {
        Message().Complete( aError );
        }

    // The leave will result in an early return from CServer::RunL(), skipping
    // the call to request another message. So do that now in order to keep the
    // server running.
    ReStart();

    return KErrNone;    // Handled the error fully
    }

// -----------------------------------------------------------------------------
// CFileServer::PanicClient()
// Panics the client.
// -----------------------------------------------------------------------------
//
void CFileServer::PanicClient( const RMessage2& aMessage, TFileServPanic aPanic )
    {
    aMessage.Panic( KCSSyncServer, aPanic );
    }

// -----------------------------------------------------------------------------
// CFileServer::PanicServer()
// Panics the server.
// -----------------------------------------------------------------------------
//
void CFileServer::PanicServer( TFileServPanic aPanic )
    {
    User::Panic( KCSSyncServer, aPanic );
    }

// -----------------------------------------------------------------------------
// CFileServer::ThreadFunctionL()
// Second stage startup for the server thread.
// -----------------------------------------------------------------------------
//
void CFileServer::ThreadFunctionL()
    {
    // Construct active scheduler
    CActiveScheduler* activeScheduler = new ( ELeave ) CActiveScheduler;
    CleanupStack::PushL( activeScheduler );

    // Install active scheduler
    // We don't need to check whether an active scheduler is already installed
    // as this is a new thread, so there won't be one
    CActiveScheduler::Install( activeScheduler );

    // Construct our server
    CFileServer::NewLC();    // Anonymous

    RSemaphore semaphore;
    User::LeaveIfError( semaphore.OpenGlobal( KFileServerSemaphoreName ) );

    // Semaphore opened ok
    semaphore.Signal();
    semaphore.Close();

    // Start handling requests
    CActiveScheduler::Start();

    CleanupStack::PopAndDestroy( 2, activeScheduler ); //Anonymous CFileServer
    }

// -----------------------------------------------------------------------------
// CFileServer::ThreadFunction()
// Main function for the server thread.
// -----------------------------------------------------------------------------
//
TInt CFileServer::ThreadFunction( TAny* /*aNone*/ )
    {
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    if ( !( cleanupStack ) )
        {
        PanicServer( ECreateTrapCleanup );
        }

    TRAPD( err, ThreadFunctionL() );
    if ( err != KErrNone )
        {
        PanicServer( ESrvCreateServer );
        }

    delete cleanupStack;
    cleanupStack = NULL;

    return KErrNone;
    }
// -----------------------------------------------------------------------------
// E32Main()
// Entry point function for Symbian Applications
// -----------------------------------------------------------------------------
//


GLDEF_C TInt E32Main()
	{
	return CFileServer::ThreadFunction( NULL );
	}

⌨️ 快捷键说明

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