hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/CSocketReader.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:  Socket reader
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "Csocketreader.h"
       
    22 #include "MSocketReaderObserver.h"
       
    23 #include <badesca.h>
       
    24 
       
    25 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    26 #include "DebugPrint.h"
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CSocketReader::CSocketReader
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CSocketReader::CSocketReader( RSocket& aSocket,
       
    35         TInt aUDPRemotePort /*= -1*/ ) :
       
    36     CActive( EPriorityStandard ),
       
    37     iSocket( aSocket ),
       
    38     iUDPRemotePort( aUDPRemotePort )
       
    39     {
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CSocketReader::ConstructL
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 void CSocketReader::ConstructL()
       
    47     {
       
    48     CActiveScheduler::Add( this );
       
    49     IssueRead();
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CSocketReader::NewL
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CSocketReader* CSocketReader::NewL( RSocket& aSocket,
       
    57         TInt aUDPRemotePort /*= -1*/ )
       
    58     {
       
    59     CSocketReader* self = CSocketReader::NewLC( aSocket, aUDPRemotePort );
       
    60     CleanupStack::Pop();
       
    61 
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CSocketReader::NewLC
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CSocketReader* CSocketReader::NewLC( RSocket& aSocket,
       
    70         TInt aUDPRemotePort /*= -1*/ )
       
    71     {
       
    72     CSocketReader* self = new( ELeave ) CSocketReader( aSocket, aUDPRemotePort );
       
    73     CleanupStack::PushL( self );
       
    74 
       
    75     self->ConstructL();
       
    76     return self;
       
    77     }
       
    78 
       
    79 
       
    80 // Destructor
       
    81 CSocketReader::~CSocketReader()
       
    82     {
       
    83     Cancel();
       
    84     }
       
    85 
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CSocketReader::SetObserver
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CSocketReader::SetObserver( MSocketReaderObserver* aObserver )
       
    92     {
       
    93     iObserver = aObserver;
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CSocketReader::IssueRead
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CSocketReader::IssueRead()
       
   101     {
       
   102     DEBUG_PRINT( DEBUG_STRING(
       
   103         "CSocketReader::IssueRead +" ) );
       
   104 
       
   105     __ASSERT_ALWAYS( !IsActive() || iStatus != KRequestPending,
       
   106         User::Panic( _L ( "s-reader" ), 1 ) );
       
   107 
       
   108     TProtocolDesc desc;
       
   109     TInt err = iSocket.Info( desc );
       
   110     DEBUG_PRINT( DEBUG_STRING(
       
   111         "CSocketReader::IssueRead, err = %d" ), err );
       
   112     DEBUG_PRINT( DEBUG_STRING(
       
   113         "desc.iProtocol = %d, iUDPRemotePort=%d" ), desc.iProtocol, iUDPRemotePort );
       
   114 
       
   115     if ( desc.iProtocol == KProtocolInetUdp && iUDPRemotePort > -1 )
       
   116         {
       
   117         // UDP
       
   118         DEBUG_PRINT( DEBUG_STRING(
       
   119             "CSocketReader::IssueRead(), UDP, local port=%d" ), iUDPRemotePort );
       
   120         iSocket.RecvFrom( iReadBuffer, iUDPRemoteAddr, 0, iStatus );
       
   121         }
       
   122     else
       
   123         {
       
   124         // TCP
       
   125         DEBUG_PRINT( DEBUG_STRING(
       
   126             "CSocketReader::IssueRead(), TCP, local port=%d" ), iSocket.LocalPort() );
       
   127         iSocket.RecvOneOrMore( iReadBuffer, 0, iStatus, iReceivedDataLength );
       
   128         }
       
   129 
       
   130     SetActive();
       
   131     DEBUG_PRINT( DEBUG_STRING(
       
   132         "CSocketReader::IssueRead() -" ) );
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CSocketReader::Start
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CSocketReader::Start()
       
   140     {
       
   141     DEBUG_PRINT( DEBUG_STRING(
       
   142         "CSocketReader::Start" ) );
       
   143     if ( !IsActive() )
       
   144         {
       
   145         IssueRead();
       
   146         }
       
   147     }
       
   148 // -----------------------------------------------------------------------------
       
   149 // CSocketReader::RunL
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CSocketReader::RunL()
       
   153     {
       
   154     TInt status = iStatus.Int();
       
   155     if ( status == KErrNone )
       
   156         {
       
   157         iObserver->DataReceivedL( iReadBuffer );
       
   158         iReadBuffer.SetLength( 0 );
       
   159         IssueRead();
       
   160         }
       
   161     else
       
   162         {
       
   163         iObserver->ReaderErrorL( status );
       
   164         }
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CSocketReader::DoCancel
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 void CSocketReader::DoCancel()
       
   172     {
       
   173     iSocket.CancelRead();
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CSocketReader::RunError
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 TInt CSocketReader::RunError( TInt aError )
       
   181     {
       
   182     iObserver->ObserverLeaved( aError );
       
   183     iReadBuffer.SetLength( 0 );
       
   184     IssueRead();
       
   185     return KErrNone;
       
   186     }
       
   187