hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/Ctcpportlistener.cpp
branchRCL_3
changeset 59 8ad140f3dd41
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:  Listens TCP port
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "Ctcpportlistener.h"
       
    21 #include "MTCPPortListenerObserver.h"
       
    22 #include <in_sock.h>
       
    23 
       
    24 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    25 #include "DebugPrint.h"
       
    26 
       
    27 // LOCAL CONSTANTS
       
    28 const TInt KMaxConnectionsInQueue = 8;
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CTCPPortListener::CTCPPortListener
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CTCPPortListener::CTCPPortListener( TInt aPort,
       
    37                                     MTCPPortListenerObserver* aObserver ) :
       
    38     CActive( EPriorityStandard ), iObserver( aObserver ), iPort( aPort )
       
    39     {
       
    40     __ASSERT_DEBUG( iObserver, User::Invariant() );
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CTCPPortListener::ConstructL
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 void CTCPPortListener::ConstructL()
       
    48     {
       
    49     //Message slots needed is two per socket (read and write) + 1 for
       
    50     //synchronous requests.
       
    51     User::LeaveIfError(
       
    52         iSocketServer.Connect( 2 * KMaxConnectionsInQueue + 1 ) );
       
    53 
       
    54     User::LeaveIfError( iListeningSocket.Open(
       
    55         iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
       
    56 
       
    57     iAcceptedSocket = new (ELeave) RSocket;
       
    58     User::LeaveIfError( iAcceptedSocket->Open( iSocketServer ) );
       
    59 
       
    60     TSockAddr addr;
       
    61     addr.SetPort( iPort );
       
    62     User::LeaveIfError( iListeningSocket.Bind( addr ) );
       
    63 
       
    64     CActiveScheduler::Add( this );
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CTCPPortListener::NewL
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CTCPPortListener* CTCPPortListener::NewL( TInt aPort,
       
    72                                           MTCPPortListenerObserver* aObserver )
       
    73     {
       
    74     CTCPPortListener* self = CTCPPortListener::NewLC( aPort,
       
    75                                                       aObserver );
       
    76     CleanupStack::Pop();
       
    77 
       
    78     return self;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CTCPPortListener::NewLC
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CTCPPortListener* CTCPPortListener::NewLC( TInt aPort,
       
    86                                            MTCPPortListenerObserver* aObserver )
       
    87     {
       
    88     CTCPPortListener* self = new( ELeave ) CTCPPortListener( aPort,
       
    89                                                              aObserver );
       
    90     CleanupStack::PushL( self );
       
    91 
       
    92     self->ConstructL();
       
    93     return self;
       
    94     }
       
    95 
       
    96 
       
    97 // Destructor
       
    98 CTCPPortListener::~CTCPPortListener()
       
    99     {
       
   100     Cancel();
       
   101     iListeningSocket.Close();
       
   102     if ( iAcceptedSocket )
       
   103         {
       
   104         iAcceptedSocket->Close();
       
   105         delete iAcceptedSocket;
       
   106         }
       
   107     iSocketServer.Close();
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CTCPPortListener::IssueListen
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CTCPPortListener::IssueListen()
       
   115     {
       
   116     __ASSERT_DEBUG( iAcceptedSocket, User::Invariant() );
       
   117 
       
   118     iListeningSocket.Listen( KMaxConnectionsInQueue );
       
   119     iListeningSocket.Accept( *iAcceptedSocket, iStatus );
       
   120     SetActive();
       
   121 
       
   122     DEBUG_PRINT( DEBUG_STRING(
       
   123         "CTCPPortListener::IssueListen completed, port %d" ), iPort );
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CTCPPortListener::Port
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 TInt CTCPPortListener::Port() const
       
   131     {
       
   132     return iPort;
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CTCPPortListener::RunL
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CTCPPortListener::RunL()
       
   140     {
       
   141     if ( iStatus.Int() == KErrNone )
       
   142         {
       
   143         // Allocate new RSocket first
       
   144         RSocket* next = new ( ELeave ) RSocket;
       
   145         CleanupStack::PushL( next );
       
   146         User::LeaveIfError( next->Open( iSocketServer ) );
       
   147         CleanupClosePushL( *next );
       
   148 
       
   149         // temp will be passed to observer
       
   150         RSocket* temp = iAcceptedSocket;
       
   151 
       
   152         // Takes ownership of temp immediately
       
   153         iObserver->ConnectionAcceptedL( temp );
       
   154 
       
   155         CleanupStack::Pop();        // *next
       
   156         CleanupStack::Pop( next );
       
   157         iAcceptedSocket = next;
       
   158 
       
   159         iListeningSocket.Accept( *iAcceptedSocket, iStatus );
       
   160         SetActive();
       
   161         DEBUG_PRINT( DEBUG_STRING(
       
   162             "CTCPPortListener::RunL(), connection accepted, port %d" ), iPort );
       
   163         }
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CTCPPortListener::DoCancel
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void CTCPPortListener::DoCancel()
       
   171     {
       
   172     iListeningSocket.CancelAccept();
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CTCPPortListener::RunError
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 TInt CTCPPortListener::RunError( TInt aError )
       
   180     {
       
   181     iObserver->ObserverLeaved( aError );
       
   182     return KErrNone;
       
   183     }
       
   184 
       
   185 
       
   186 //  End of File