hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/Clocaltcpconnection.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     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 "CLocalTCPConnection.h"
       
    22 #include "MLocalTCPConnectionObserver.h"
       
    23 
       
    24 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    25 #include "DebugPrint.h"
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CLocalTCPConnection::CLocalTCPConnection
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CLocalTCPConnection::CLocalTCPConnection(
       
    34     MLocalTCPConnectionObserver* aObserver, TInt aPort ) :
       
    35     CActive( EPriorityStandard )
       
    36     {
       
    37     DEBUG_PRINT( DEBUG_STRING(
       
    38         "CLocalTCPConnection::CLocalTCPConnection() aPort=%d" ), aPort );
       
    39 
       
    40     iOwnsSocket = ETrue;
       
    41     iObserver = aObserver;
       
    42     iAddr.SetPort( aPort );
       
    43     iAddr.SetAddress( KInetAddrLoop );
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CLocalTCPConnection::ConstructL
       
    48 // Symbian 2nd phase constructor can leave.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void CLocalTCPConnection::ConstructL()
       
    52     {
       
    53     DEBUG_PRINT( DEBUG_STRING(
       
    54         "CLocalTCPConnection::ConstructL, opening socket..." ) );
       
    55 
       
    56     iSocket = new (ELeave) RSocket;
       
    57     User::LeaveIfError( iSocketServer.Connect() );
       
    58     User::LeaveIfError( iSocket->Open(
       
    59         iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
       
    60     DEBUG_PRINT( DEBUG_STRING(
       
    61         "CLocalTCPConnection::ConstructL, socket opened." ) );
       
    62     CActiveScheduler::Add( this );
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CLocalTCPConnection::NewL
       
    67 // Two-phased constructor.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CLocalTCPConnection* CLocalTCPConnection::NewL(
       
    71     MLocalTCPConnectionObserver* aObserver,
       
    72     TInt aPort  )
       
    73     {
       
    74     CLocalTCPConnection* self =
       
    75         CLocalTCPConnection::NewLC( aObserver, aPort );
       
    76     CleanupStack::Pop();
       
    77 
       
    78     return self;
       
    79     }
       
    80 
       
    81 CLocalTCPConnection* CLocalTCPConnection::NewLC(
       
    82     MLocalTCPConnectionObserver* aObserver,
       
    83     TInt aPort  )
       
    84     {
       
    85     CLocalTCPConnection* self =
       
    86         new( ELeave ) CLocalTCPConnection( aObserver, aPort );
       
    87     CleanupStack::PushL( self );
       
    88 
       
    89     self->ConstructL();
       
    90     return self;
       
    91     }
       
    92 
       
    93 // Destructor
       
    94 CLocalTCPConnection::~CLocalTCPConnection()
       
    95     {
       
    96     Cancel();
       
    97 
       
    98     if ( iOwnsSocket )
       
    99         {
       
   100         if ( iSocket )
       
   101             {
       
   102             iSocket->Close();
       
   103             delete iSocket;
       
   104             }
       
   105         }
       
   106 
       
   107     iSocketServer.Close();
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CLocalTCPConnection::RunL
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CLocalTCPConnection::RunL()
       
   115     {
       
   116     DEBUG_PRINT( DEBUG_STRING(
       
   117         "CLocalTCPConnection::RunL(), iStatus=%d" ),
       
   118         iStatus.Int() );
       
   119 
       
   120     if ( iStatus.Int() == KErrNone )
       
   121         {
       
   122         switch ( iState )
       
   123             {
       
   124             case ELTCStateConnecting:
       
   125                 {
       
   126                 DEBUG_PRINT( DEBUG_STRING(
       
   127                     "CLocalTCPConnection::RunL(), ELCStateConnecting" ) );
       
   128                 iState = ELTCStateConnected;
       
   129                 iObserver->LocalTCPConnectionEstablishedL( Port() );
       
   130                 break;
       
   131                 }
       
   132             case ELTCStateDisconnecting:
       
   133                 {
       
   134                 DEBUG_PRINT( DEBUG_STRING(
       
   135                     "CLocalTCPConnection::RunL(), ELCStateDisconnecting" ) );
       
   136                 iState = ELTCStateDisconnected;
       
   137 
       
   138                 iSocket->Close();
       
   139                 User::LeaveIfError( iSocket->Open(
       
   140                     iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
       
   141                 break;
       
   142                 }
       
   143             }
       
   144         }
       
   145     else
       
   146         {
       
   147         iObserver->LocalTCPConnectionErrorL( Port(), iStatus.Int() );
       
   148         iSocket->Close();
       
   149         User::LeaveIfError( iSocket->Open(
       
   150             iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
       
   151         }
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CLocalTCPConnection::DoCancel
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CLocalTCPConnection::DoCancel()
       
   159     {
       
   160     iSocket->CancelConnect();
       
   161     }
       
   162 
       
   163 TInt CLocalTCPConnection::RunError( TInt aError )
       
   164     {
       
   165     DEBUG_PRINT( DEBUG_STRING(
       
   166         "CLocalTCPConnection::RunError( %d )" ), aError );
       
   167 
       
   168     iObserver->LocalTCPConnectionObserverLeaved( Port(), aError );
       
   169     return KErrNone;
       
   170     }
       
   171 
       
   172 TInt CLocalTCPConnection::Port()
       
   173     {
       
   174     return iAddr.Port();
       
   175     }
       
   176 
       
   177 void CLocalTCPConnection::IssueConnectL()
       
   178     {
       
   179     DEBUG_PRINT( DEBUG_STRING(
       
   180         "CLocalTCPConnection::IssueConnectL()" ) );
       
   181 
       
   182     iSocket->Connect( iAddr, iStatus );
       
   183     SetActive();
       
   184     iState = ELTCStateConnecting;
       
   185     }
       
   186 
       
   187 void CLocalTCPConnection::IssueDisconnect()
       
   188     {
       
   189     DEBUG_PRINT( DEBUG_STRING(
       
   190         "CLocalTCPConnection::IssueDisconnect()" ) );
       
   191     iSocket->CancelAll();
       
   192     Cancel();
       
   193     iSocket->Shutdown( RSocket::ENormal, iStatus );
       
   194     SetActive();
       
   195     iState = ELTCStateDisconnecting;
       
   196     }
       
   197 
       
   198 void CLocalTCPConnection::SetObserver( MLocalTCPConnectionObserver* aObserver )
       
   199     {
       
   200     iObserver = aObserver;
       
   201     }
       
   202 
       
   203 RSocket* CLocalTCPConnection::Socket()
       
   204     {
       
   205     return iSocket;
       
   206     }
       
   207 
       
   208 void CLocalTCPConnection::SetSocketOwnership( TBool aOwns )
       
   209     {
       
   210     iOwnsSocket = aOwns;
       
   211     }
       
   212 
       
   213 
       
   214 TBool CLocalTCPConnection::IsConnected()
       
   215     {
       
   216     return ( iState == ELTCStateConnected || iState == ELTCStateDisconnecting );
       
   217     }
       
   218 
       
   219 //  End of File