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

📄 tmserver.cpp

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


// INCLUDE FILES
#include <e32svr.h>
#include <e32math.h>

#include "TmServer.h"
#include "ClientServerCommon.h"
#include "TmSession.h"

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

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

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

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

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

// -----------------------------------------------------------------------------
// CTmServer::NewSessionL()
// Creates a tm server session.
// -----------------------------------------------------------------------------
//
CSharableSession* CTmServer::NewSessionL( const TVersion& aVersion ) const
    {
    // Check we are the right version
    if ( !User::QueryVersionSupported( TVersion( KTmServMajorVersionNumber,
                                                 KTmServMinorVersionNumber,
                                                 KTmServBuildVersionNumber ),
                                       aVersion ) )
        {
        User::Leave( KErrNotSupported );
        }

    // Make new session
    RThread client = Message().Client();
    return CTmServerSession::NewL( client,
                                     *const_cast<CTmServer*> ( this ) );
    }

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

// -----------------------------------------------------------------------------
// CTmServer::DecrementSessions()
// Decrements the count of the active sessions for this server.
// -----------------------------------------------------------------------------
//
void CTmServer::DecrementSessions()
    {
    iSessionCount--;
    if ( iSessionCount <= 0 )
        {
        // There could also be a timer that would allow the server to be running
        // and waiting for new sessions e.g. for a few minutes after the last 
        // session has ended. For simplicity's sake we do not implement a timer here.
        CActiveScheduler::Stop();
        }
    }

// -----------------------------------------------------------------------------
// CTmServer::RunError()
// Processes any errors.
// -----------------------------------------------------------------------------
//
TInt CTmServer::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
    }

// -----------------------------------------------------------------------------
// CTmServer::PanicClient()
// Panics the client.
// -----------------------------------------------------------------------------
//
void CTmServer::PanicClient( const RMessage& aMessage, TTmServPanic aPanic )
    {
    aMessage.Panic( KTMExampleServer, aPanic );
    }

// -----------------------------------------------------------------------------
// CTmServer::PanicServer()
// Panics the server.
// -----------------------------------------------------------------------------
//
void CTmServer::PanicServer( TTmServPanic aPanic )
    {
    User::Panic( KTMExampleServer, aPanic );
    }

// -----------------------------------------------------------------------------
// CTmServer::ThreadFunctionL()
// Second stage startup for the server thread.
// -----------------------------------------------------------------------------
//
void CTmServer::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
    CTmServer::NewLC();    // Anonymous

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

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

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

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

// -----------------------------------------------------------------------------
// CTmServer::ThreadFunction()
// Main function for the server thread.
// -----------------------------------------------------------------------------
//
TInt CTmServer::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;
    }

// ========================== OTHER EXPORTED FUNCTIONS =========================

#ifdef __WINS__

// -----------------------------------------------------------------------------
// WinsMain()
// Returns a pointer to CTmServer::ThreadFunction.
// -----------------------------------------------------------------------------
//
IMPORT_C TInt WinsMain();
EXPORT_C TInt WinsMain()
    {
    return reinterpret_cast<TInt>( &CTmServer::ThreadFunction );
    }

// -----------------------------------------------------------------------------
// E32Dll()
// Entry point function for Symbian Apps.
// -----------------------------------------------------------------------------
//
GLDEF_C TInt E32Dll( TDllReason )
    {
    return KErrNone;
    }

#else  // __ARMI__

// -----------------------------------------------------------------------------
// E32Main()
// Provides the API for the operating system to start the executable.
// Returns the address of the function to be called.
// -----------------------------------------------------------------------------
//
TInt E32Main()
    {
    return CTmServer::ThreadFunction( NULL );
    }

#endif

// End of File

⌨️ 快捷键说明

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