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

📄 socketsengine.cpp

📁 Symbian 手机网络通信程序 sockets-vc
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2004, Nokia. All rights reserved */


// INCLUDE FILES
#include <eikgted.h>
#include <eikenv.h>
#include <stringloader.h>
#include <Sockets.rsg>

#include "SocketsEngine.h"
#include "TimeOutTimer.h"
#include "SocketsReader.h"
#include "SocketsWriter.h"
#include "Sockets.pan"
#include "UINotifier.h"

// STATIC MEMBER INITIALISATIONS
const TInt CSocketsEngine::KTimeOut = 30000000; // 30 seconds time-out
const TInt CSocketsEngine::KDefaultPortNumber = 7;

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

// -----------------------------------------------------------------------------
// CSocketsEngine::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CSocketsEngine* CSocketsEngine::NewL( MUINotifier& aConsole )
    {
    CSocketsEngine* self = CSocketsEngine::NewLC( aConsole );
    CleanupStack::Pop( self );
    return self;
    }

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

// -----------------------------------------------------------------------------
// CSocketsEngine::CSocketsEngine()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CSocketsEngine::CSocketsEngine( MUINotifier& aConsole )
: CActive( EPriorityStandard ),
  iConsole( aConsole ),
  iPort( KDefaultPortNumber ),
  iServerName( KDefaultServerName )
    {
    // No implementation required
    }

// -----------------------------------------------------------------------------
// CSocketsEngine::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CSocketsEngine::ConstructL()
    {
    ChangeStatus( ENotConnected );

    // Start a timer
    iTimer = CTimeOutTimer::NewL( EPriorityHigh, *this );
    CActiveScheduler::Add( this );

    // Open channel to Socket Server
    User::LeaveIfError( iSocketServ.Connect() );

    // Create socket read and write active objects
    iSocketsReader = CSocketsReader::NewL( *this, iSocket );
    iSocketsWriter = CSocketsWriter::NewL( *this, iSocket );
    }

// -----------------------------------------------------------------------------
// CSocketsEngine::~CSocketsEngine()
// Destructor.
// -----------------------------------------------------------------------------
//
CSocketsEngine::~CSocketsEngine()
    {
    Cancel();

    delete iSocketsReader;
    iSocketsReader = NULL;

    delete iSocketsWriter;
    iSocketsWriter = NULL;

    delete iTimer;
    iTimer = NULL;

    iSocketServ.Close();
    }

// -----------------------------------------------------------------------------
// CSocketsEngine::ConnectL()
// Initiates connection of socket, using iServerName and iPort.
// -----------------------------------------------------------------------------
//
void CSocketsEngine::ConnectL()
    {
    // Initiate connection process
    if ( iEngineStatus == ENotConnected )
        {
        TInetAddr addr;
        if ( addr.Input( iServerName ) == KErrNone )
            {
            // server name is already a valid ip address
            ConnectL( addr.Address() );
            }
        else // need to look up name using dns
            {
            // Initiate DNS
            User::LeaveIfError( iResolver.Open( iSocketServ,
                                                KAfInet,
                                                KProtocolInetUdp ) );
            // DNS request for name resolution
            iResolver.GetByName( iServerName, iNameEntry, iStatus );

            ChangeStatus( ELookingUp );
            // Request time out
            iTimer->After( KTimeOut );
            SetActive();
            }
        }
    }

// -----------------------------------------------------------------------------
// CSocketsEngine::ConnectL()
// Initiates a connect operation on a socket.
// -----------------------------------------------------------------------------
//
void CSocketsEngine::ConnectL( TUint32 aAddr )
    {
    // Initiate attempt to connect to a socket by IP address
    if ( iEngineStatus == ENotConnected )
        {
        // Open a TCP socket
        User::LeaveIfError( iSocket.Open( iSocketServ,
                                          KAfInet,
                                          KSockStream,
                                          KProtocolInetTcp ) );

        // Set up address information
        iAddress.SetPort( iPort );
        iAddress.SetAddress( aAddr );

        // Initiate socket connection
        iSocket.Connect( iAddress, iStatus );
        ChangeStatus( EConnecting );

        // Start a timeout
        iTimer->After( KTimeOut );

        SetActive();
        }
    }

// -----------------------------------------------------------------------------
// CSocketsEngine::Disconnect()
// Disconnects socket.
// -----------------------------------------------------------------------------
//
void CSocketsEngine::Disconnect()
    {
    __ASSERT_ALWAYS( iEngineStatus == EConnected,
                     User::Panic( KPanicSocketsEngine, ESocketsBadState ) );

    // Cancel all outstanding operations
    // Since we are connected, the only possibilities are read and write
    iSocketsReader->Cancel();
    iSocketsWriter->Cancel();

    iSocket.Close();
    ChangeStatus( ENotConnected );
    }

// -----------------------------------------------------------------------------
// CSocketsEngine::DoCancel()
// Cancels any outstanding operation.
// -----------------------------------------------------------------------------
//
void CSocketsEngine::DoCancel()
    {
    iTimer->Cancel();

    // Cancel appropriate request to socket
    switch ( iEngineStatus )
        {
        case EConnecting:
            iSocket.CancelConnect();
            iSocket.Close();
            break;
        case ELookingUp:
            // Cancel look up attempt
            iResolver.Cancel();
            iResolver.Close();
            break;
        default:
            User::Panic( KPanicSocketsEngine, ESocketsBadStatus );
            break;
        }

    ChangeStatus( ENotConnected );
    }

// -----------------------------------------------------------------------------
// CSocketsEngine::WriteL()
// Writes data to socket.
// -----------------------------------------------------------------------------
//
void CSocketsEngine::WriteL( const TDesC8& aData )
    {
    if ( iEngineStatus == EConnected )
        {
        iSocketsWriter->IssueWriteL( aData );
        }
    }

// -----------------------------------------------------------------------------
// CSocketsEngine::Read()
// Initiates read of data from socket.
// -----------------------------------------------------------------------------
//
void CSocketsEngine::Read()
    {
    if ( ( iEngineStatus == EConnected ) && ( !iSocketsReader->IsActive() ) )
        {

⌨️ 快捷键说明

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