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

📄 iapconnectdemoengine.cpp

📁 这是一个手机编程s60 基于c++的关于手机连接internet的数据库协议
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
* ============================================================================
*  Name     : CIAPConnectDemoEngine from IAPConnectDemoEngine.cpp
*  Part of  : Internet Access Points Example v2.0
*  Created  : 01.09.2006 by Forum Nokia
*  Version  : 2.0
*  Copyright: Forum Nokia
* ============================================================================
*/

// INCLUDE FILES
#include <eikgted.h>
#include <eikenv.h>
#include <utf.h> //converter
#include <commdbconnpref.h>
#include "IAPConnectDemoDataNotification.h"
#include "IAPConnectDemoEngine.h"
#include "IAPConnectDemoSocketsReader.h"
#include "IAPConnectDemoSocketsWriter.h"
#include "IAPConnectDemoSettings.h"

// CONSTANTS
const TInt KTimeOut = 30000000; // 30 seconds time-out
const TInt KDefaultPortNumber = 80;
const TInt KBufSize = 20;
const TInt KMaxRequestLength = 100;
const TInt KMaxIpAddrLength = 15;
_LIT(KStatusStarting, "*Starting demo");
_LIT(KStatusConnecting, "*Connecting to server");
_LIT(KStatusConnected, "*Connected");
_LIT(KStatusLookingUp, "*Looking up");
_LIT(KHttpGet, "Get http://");
_LIT(KHttpVersion, " HTTP/1.0\r\n\r\n");
_LIT(KErrorDisconnected, "Disconnected");
_LIT(KErrorConnectionFailed, "Connection failed");
_LIT(KErrorTimeOut, "Time out");
_LIT(KErrorSocketError, "Socket error");

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

// Constructor
CIAPConnectDemoEngine::CIAPConnectDemoEngine( 
    MIAPConnectDemoEngineObserver* aObserver, RConnection& aConnection,
    RSocketServ& aSocketServ )
    : CActive( EPriorityStandard ), iSocketServ(aSocketServ),
    iObserver ( aObserver ), iConnection(aConnection)
    {
    }
    
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::NewL()
// Two-phased constructor.
// ---------------------------------------------------------------------------
//
CIAPConnectDemoEngine* CIAPConnectDemoEngine::NewL(
    MIAPConnectDemoEngineObserver* aObserver, RConnection& aConnection,
    RSocketServ& aSocketServ )
    {
    CIAPConnectDemoEngine* self = CIAPConnectDemoEngine::NewLC( 
        aObserver, aConnection, aSocketServ );
    CleanupStack::Pop( self );
    return self;
    }
    
// Destructor
CIAPConnectDemoEngine::~CIAPConnectDemoEngine()
    {
    Cancel();
    if (iSocketsReader)
    	{
    	delete iSocketsReader;    	
    	iSocketsReader = NULL;
    	}
    if (iSocketsWriter)
    	{
    	delete iSocketsWriter;
    	iSocketsWriter = NULL;
    	}
    if (iSentDataNotification)
        {
        delete iSentDataNotification;
        iSentDataNotification = NULL;
        }
    if (iReceivedDataNotification)
        {
        delete iReceivedDataNotification;
        iReceivedDataNotification = NULL;
	    }
    }

// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::NewLC()
// Two-phased constructor.
// ---------------------------------------------------------------------------
//
CIAPConnectDemoEngine* CIAPConnectDemoEngine::NewLC( 
    MIAPConnectDemoEngineObserver* aObserver, RConnection& aConnection,
    RSocketServ& aSocketServ )
    {
    CIAPConnectDemoEngine* self = 
        new ( ELeave ) CIAPConnectDemoEngine( aObserver, aConnection, 
            aSocketServ );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }


// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::ConstructL()
// Symbian 2nd phase constructor can leave.
// ---------------------------------------------------------------------------
void CIAPConnectDemoEngine::ConstructL()
    {
    CActiveScheduler::Add( this );
    
    // Create socket read and write active objects
    iSocketsReader = CIAPConnectDemoSocketsReader::NewL( *this, iSocket );
    iSocketsWriter = CIAPConnectDemoSocketsWriter::NewL( *this, iSocket );
    }

// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::StartDemoL(CIAPConnectDemoSettings* aSettings)
// Initiates the demo
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::StartDemoL( CIAPConnectDemoSettings* aSettings )
    {
    
    if (IsActive())
    	{
    	return;
    	}
    
    iSettings = aSettings;
   	ResolveAddressL();	
    }

// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::StopDemo()
// Stops the demo
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::StopDemoL()
    {
	DisconnectL();
    }


// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::ResolveAddressL()
// Resolves the IP address from server name.
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::ResolveAddressL ()
	{
    iObserver->SetStatusL( KStatusStarting );
    TInetAddr addr;
    
	HBufC* server = NULL;
    if ( iSettings -> GetSetting( EIAPConnectDemoServerName, server))
	    {
	    User::Leave( KErrNotFound );	
	    }
    if ( addr.Input( *server ) == 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,
                                            KProtocolInetIcmp, iConnection ));
        // DNS request for name resolution
        iResolver.GetByName( *server , iNameEntry, iStatus );
        ChangeStatusL( ELookingUp );
        SetActive();
        }
    }

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

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

        // Initiate socket connection
        iSocket.Connect( iAddress, iStatus );
        SetActive();
        }
    }

// -----------------------------------------------------------------------------
// CIAPConnectDemoEngine::Disconnect()
// Disconnects socket.
// -----------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::DisconnectL()
    {
    // Cancel all outstanding operations
    if (iSocketsReader)
	    {
        if (iSocketsReader->IsActive())
    	    iSocketsReader->Cancel();	
	    }
    if (iSocketsWriter)
	    {
        if (iSocketsWriter->IsActive())
    	   	iSocketsWriter->Cancel();
	    }
    if (iSentDataNotification)
        {
        delete iSentDataNotification;
        iSentDataNotification = NULL;
        }
    if (iReceivedDataNotification)
        {
        delete iReceivedDataNotification;
        iReceivedDataNotification = NULL;
	    }
	iSocket.Close();
    ChangeStatusL( ENotConnected );
    }

// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::DoCancel()
// Cancels any outstanding operation.
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::DoCancel()
    {
    // Cancel appropriate request to socket
    switch ( iEngineStatus )
        {
        case EConnecting:
	        {
            iSocket.CancelConnect();
            iSocket.Close();
            break;
	        }
        case ELookingUp:
        	{
            // Cancel look up attempt

⌨️ 快捷键说明

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