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

📄 chatbt.cpp

📁 一个聊天的软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
* ==============================================================================
*  Name        : ChatBt.cpp
*  Part of     : Chat
*  Interface   : 
*  Description : 
*  Version     : 
*
*  Copyright (c) 2005-2006 Nokia Corporation.
*  This material, including documentation and any related 
*  computer programs, is protected by copyright controlled by 
*  Nokia Corporation.
* ==============================================================================
*/

// INCLUDE FILES
#include <StringLoader.h>
#include <coemain.h>
#include <ChatEx.rsg>
#include "ChatServiceAdvertiser.h"
#include "ChatBt.h"
#include "ChatServiceSearcher.h"
#include "Chat.pan"
#include "Log.h"

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

// ----------------------------------------------------------------------------
// CChatBt::NewL()
// Two-phased constructor.
// ----------------------------------------------------------------------------
//
CChatBt* CChatBt::NewL( MLog& aLog )
    {
    CChatBt* self = NewLC( aLog );
    CleanupStack::Pop( self );
    return self;
    }

// ----------------------------------------------------------------------------
// CChatBt::NewLC()
// Two-phased constructor.
// ----------------------------------------------------------------------------
//
CChatBt* CChatBt::NewLC( MLog& aLog )
    {
    CChatBt* self = new ( ELeave ) CChatBt( aLog );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

// ----------------------------------------------------------------------------
// CChatBt::CChatBt()
// Constructor.
// ----------------------------------------------------------------------------
//
CChatBt::CChatBt( MLog& aLog ) : 
    CChatBase( aLog ),
    iLog( aLog )
    {
    SetState( EWaitingToGetDevice );
    }

// ----------------------------------------------------------------------------
// CChatBt::~CChatBt()
// Destructor.
// ----------------------------------------------------------------------------
//
CChatBt::~CChatBt()
    {
    // Close() will wait forever for Read to complete
    if ( State() == EConnected )
        {
        if ( iActiveSocket )
            {
            iActiveSocket->CancelRead();
            }
        }
    Cancel();

    iSocket.Close();
    iAcceptedSocket.Close();
    iSocketServer.Close();

    delete iMessage;
    iMessage = NULL;

    delete iServiceSearcher;
    iServiceSearcher = NULL;
    
    delete iAdvertiser;
    iAdvertiser = NULL;
    }

// ----------------------------------------------------------------------------
// CChatBt::ConstructL()
// Perform second phase construction of this object.
// ----------------------------------------------------------------------------
//
void CChatBt::ConstructL()
    {
    iServiceSearcher = CChatServiceSearcher::NewL( iLog );
    iAdvertiser = CChatServiceAdvertiser::NewL();
    User::LeaveIfError( iSocketServer.Connect() );
    }

// ----------------------------------------------------------------------------
// CChatBt::DoCancel()
// Cancel any outstanding requests.
// ----------------------------------------------------------------------------
//
void CChatBt::DoCancel()
    {
    // no implementation required
    }

// ----------------------------------------------------------------------------
// CChatBt::RunL()
// Respond to an event.
// ----------------------------------------------------------------------------
//
void CChatBt::RunL()
    {

    HBufC* textResource = NULL;
    TBuf<KMaxMarkLen> mark;
  
    if ( iStatus == KErrDisconnected )
        {
        // Disconnected
        HBufC* strDisconnected = StringLoader
            ::LoadLC ( R_CHAT_STR_DISCONNECTED );
        iLog.LogL( *strDisconnected );
        CleanupStack::PopAndDestroy( strDisconnected );
        StopL();
        return;
        }
        
    else if ( iStatus == KErrAbort )
        {
        HBufC* strDisconnected = StringLoader
            ::LoadLC ( R_CHAT_STR_DISCONNECTED );
        iLog.LogL( *strDisconnected );
        CleanupStack::PopAndDestroy( strDisconnected );
        StopL();
        return;
        }
  
    else if ( iStatus != KErrNone )
        {
        switch ( State() )
            {
            case EGettingDevice:
                if ( iStatus == KErrCancel )
                    {
                    textResource = StringLoader
                        ::LoadLC( R_CHAT_ERR_NO_DEVICE_SELECTED );
                    iLog.LogL( *textResource );
                    CleanupStack::PopAndDestroy( textResource );
                    }
                SetState( EWaitingToGetDevice );
                break;
                
            case EGettingService:
            
            case EGettingConnection:
                textResource = StringLoader
                    ::LoadLC( R_CHAT_ERR_CONNECTION_ERROR );
                iLog.LogL( *textResource, iStatus.Int() );
                CleanupStack::PopAndDestroy( textResource );
                SetState( EWaitingToGetDevice );
                break;
                
            case EConnected:
                textResource = StringLoader
                    ::LoadLC( R_CHAT_ERR_LOST_CONNECTION );
                iLog.LogL( *textResource, iStatus.Int() );
                DisconnectFromServerL();
                CleanupStack::PopAndDestroy( textResource );
                SetState( EDisconnecting );
                break;
                
            case ESendingMessage:
                textResource = StringLoader
                    ::LoadLC( R_CHAT_ERR_MESSAGE_FAILED );
                iLog.LogL( *textResource, iStatus.Int() );
                CleanupStack::PopAndDestroy( textResource );
                DisconnectFromServerL();
                SetState( EDisconnecting );
                break;
                
            case EDisconnecting:
                if ( iStatus == KErrDisconnected )
                    {
                    textResource = StringLoader
                        ::LoadLC( R_CHAT_DISCONNECT_COMPLETE );
                    iLog.LogL( *textResource, iStatus.Int() );
                    CleanupStack::PopAndDestroy( textResource );

                    StopL();
                    SetState( EWaitingToGetDevice );
                    }
                else
                    {
                    textResource = StringLoader
                        ::LoadLC( R_CHAT_ERR_FAILED_TO_DISCONNECT );
                    iLog.LogL( *textResource, iStatus.Int() );
                    CleanupStack::PopAndDestroy( textResource );

                    Panic( EChatUnableToDisconnect );
                    }
                break;
            
            case EWaitingToGetDevice:
                textResource = StringLoader
                    ::LoadLC( R_CHAT_STR_DISCONNECTED );
                iLog.LogL( *textResource );
                CleanupStack::PopAndDestroy( textResource );
                break;
                
            default:
                Panic( EChatInvalidLogicState );
                break;
            }
        }
        
    else 
        {
        switch ( State() )
            {
            case EGettingDevice:
                // found a device now search for a suitable service
                iLog.LogL( iServiceSearcher->ResponseParams().DeviceName() );
                SetState( EGettingService );
                iStatus = KRequestPending;  // this means that the RunL 
                                            // can not be called until
                                            // this program does something 
                                            // to iStatus
                iServiceSearcher->FindServiceL( iStatus );
                SetActive();
                break;
                
            case EConnecting:
                textResource = StringLoader::LoadLC ( R_CHAT_CONNECTED );
                iLog.LogL( *textResource );
                CleanupStack::PopAndDestroy( textResource );
              
                // do not accept any more connections
                iAdvertiser->UpdateAvailabilityL( EFalse );
                RequestData();
                SetState( EConnected );
                break;
                
            case EGettingService:
                textResource = StringLoader
                    ::LoadLC( R_CHAT_FOUND_SERVICE );
                iLog.LogL( *textResource );
                CleanupStack::PopAndDestroy( textResource );
                SetState( EGettingConnection );
                ConnectToServerL();
                break;
                
            case EGettingConnection:
                textResource = StringLoader
                    ::LoadLC( R_CHAT_CONNECTED );
                iLog.LogL( *textResource );
                CleanupStack::PopAndDestroy( textResource );
                SetState( EConnected );
                RequestData();
                break;
                
            case EConnected:
              
                mark.Append( '>' );
                textResource = HBufC::NewLC( iBuffer.Length() );
                textResource->Des().Copy( iBuffer );
                iLog.LogL( *textResource, mark );
                iBuffer.Zero();
                CleanupStack::PopAndDestroy( textResource );
                RequestData();
                break;
                
            case ESendingMessage:
                SetState( EConnected );
                RequestData();
                break;
                
            case EDisconnecting:
                textResource = StringLoader
                    ::LoadLC( R_CHAT_DISCONNECT_COMPLETE );
                iLog.LogL( *textResource );
                CleanupStack::PopAndDestroy ( textResource );
                iSocket.Close();
                SetState( EWaitingToGetDevice );
                break;
            
            case EWaitingToGetDevice:
                
                break;
                

⌨️ 快捷键说明

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