hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/CLocalHostConnection.cpp
changeset 36 813b186005b6
parent 30 86a2e675b80a
child 41 838cdffd57ce
equal deleted inserted replaced
30:86a2e675b80a 36:813b186005b6
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Local TCP connection for emulator testing
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CLocalHostConnection.h"
       
    22 #include "MHostConnectionObserver.h"
       
    23 
       
    24 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    25 #include "DebugPrint.h"
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CLocalHostConnection::CLocalHostConnection
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CLocalHostConnection::CLocalHostConnection(
       
    34     RSocketServ& aSocketServer,  TInt aPort ) :
       
    35     CActive( EPriorityStandard ),
       
    36     iSocketServer( aSocketServer )
       
    37     {
       
    38     DEBUG_PRINT( DEBUG_STRING(
       
    39         "CLocalHostConnection::CLocalHostConnection() aPort=%d" ), aPort );
       
    40 
       
    41     iAddr.SetPort( aPort );
       
    42     iAddr.SetAddress( KInetAddrLoop );
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CLocalHostConnection::ConstructL
       
    47 // Symbian 2nd phase constructor can leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 void CLocalHostConnection::ConstructL()
       
    51     {
       
    52 
       
    53     User::LeaveIfError( iClientSocket.Open(
       
    54         iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
       
    55     CActiveScheduler::Add( this );
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CLocalHostConnection::NewL
       
    60 // Two-phased constructor.
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CLocalHostConnection* CLocalHostConnection::NewL(
       
    64     RSocketServ& aSocketServer,  TInt aPort  )
       
    65     {
       
    66     CLocalHostConnection* self =
       
    67         CLocalHostConnection::NewLC( aSocketServer, aPort );
       
    68     CleanupStack::Pop();
       
    69 
       
    70     return self;
       
    71     }
       
    72 
       
    73 CLocalHostConnection* CLocalHostConnection::NewLC(
       
    74     RSocketServ& aSocketServer,  TInt aPort  )
       
    75     {
       
    76     CLocalHostConnection* self =
       
    77         new( ELeave ) CLocalHostConnection( aSocketServer, aPort );
       
    78     CleanupStack::PushL( self );
       
    79 
       
    80     self->ConstructL();
       
    81     return self;
       
    82     }
       
    83 
       
    84 // Destructor
       
    85 CLocalHostConnection::~CLocalHostConnection()
       
    86     {
       
    87     Cancel();
       
    88     iClientSocket.Close();
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CLocalHostConnection::RunL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CLocalHostConnection::RunL()
       
    96     {
       
    97     DEBUG_PRINT( DEBUG_STRING(
       
    98         "CLocalHostConnection::RunL(), iStatus=%d" ),
       
    99         iStatus.Int() );
       
   100 
       
   101     if ( iStatus.Int() == KErrNone )
       
   102         {
       
   103         switch ( iState )
       
   104             {
       
   105             case ELCStateConnecting:
       
   106                 {
       
   107                 DEBUG_PRINT( DEBUG_STRING(
       
   108                     "CLocalHostConnection::RunL(), ELCStateConnecting" ) );
       
   109                 iState = ELCStateConnected;
       
   110                 iObserver->ConnectionEstablishedL();
       
   111                 break;
       
   112                 }
       
   113             case ELCStateDisconnecting:
       
   114                 {
       
   115                 DEBUG_PRINT( DEBUG_STRING(
       
   116                     "CLocalHostConnection::RunL(), ELCStateDisconnecting" ) );
       
   117                 iState = ELCStateDisconnected;
       
   118 
       
   119                 iClientSocket.Close();
       
   120                 User::LeaveIfError( iClientSocket.Open(
       
   121                     iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
       
   122                 break;
       
   123                 }
       
   124             }
       
   125         }
       
   126     else
       
   127         {
       
   128         iObserver->HostConnectionErrorL( iStatus.Int() );
       
   129         iClientSocket.Close();
       
   130         User::LeaveIfError( iClientSocket.Open(
       
   131             iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
       
   132         }
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CLocalHostConnection::DoCancel
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CLocalHostConnection::DoCancel()
       
   140     {
       
   141     iClientSocket.CancelConnect();
       
   142     }
       
   143 
       
   144 TInt CLocalHostConnection::RunError( TInt aError )
       
   145     {
       
   146     DEBUG_PRINT( DEBUG_STRING(
       
   147         "CLocalHostConnection::RunError( %d )" ), aError );
       
   148 
       
   149     iObserver->HostConnectionObserverLeaved( aError );
       
   150     return KErrNone;
       
   151     }
       
   152 
       
   153 TInt CLocalHostConnection::Port()
       
   154     {
       
   155     return iAddr.Port();
       
   156     }
       
   157 
       
   158 void CLocalHostConnection::IssueConnectL()
       
   159     {
       
   160     DEBUG_PRINT( DEBUG_STRING(
       
   161         "CLocalHostConnection::IssueConnectL()" ) );
       
   162 
       
   163     iClientSocket.Connect( iAddr, iStatus );
       
   164     SetActive();
       
   165     iState = ELCStateConnecting;
       
   166     }
       
   167 
       
   168 void CLocalHostConnection::IssueDisconnect()
       
   169     {
       
   170     DEBUG_PRINT( DEBUG_STRING(
       
   171         "CLocalHostConnection::IssueDisconnect()" ) );
       
   172     iClientSocket.CancelAll();
       
   173     Cancel();
       
   174     iClientSocket.Shutdown( RSocket::ENormal, iStatus );
       
   175     SetActive();
       
   176     iState = ELCStateDisconnecting;
       
   177     }
       
   178 
       
   179 void CLocalHostConnection::SetObserver( MHostConnectionObserver* aObserver )
       
   180     {
       
   181     iObserver = aObserver;
       
   182     }
       
   183 
       
   184 RSocket* CLocalHostConnection::Socket()
       
   185     {
       
   186     return &iClientSocket;
       
   187     }
       
   188 
       
   189 TBool CLocalHostConnection::IsConnected()
       
   190     {
       
   191     return ( iState == ELCStateConnected || iState == ELCStateDisconnecting );
       
   192     }
       
   193 
       
   194 //  End of File