hti/HtiCommPlugins/HtiBtCommPlugin/BtEngine/src/socketsreader.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:  Reads from socket.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "BtSerialEngine.pan"
       
    21 #include "socketsreader.h"
       
    22 #include "SocketObserver.h"
       
    23 
       
    24 // -----------------------------------------------------------------------------
       
    25 CSocketsReader* CSocketsReader::NewL( MSocketObserver& aEngineNotifier,
       
    26                                       RSocket& aSocket )
       
    27     {
       
    28     CSocketsReader* self = new ( ELeave ) CSocketsReader( aEngineNotifier,
       
    29                                                           aSocket );
       
    30     CleanupStack::PushL( self );
       
    31     self->ConstructL();
       
    32     CleanupStack::Pop( self );
       
    33     return self;
       
    34     }
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 CSocketsReader::CSocketsReader( MSocketObserver& aObserver,
       
    38                                 RSocket& aSocket )
       
    39 : CActive( EPriorityStandard ),
       
    40   iSocket( aSocket ),
       
    41   iObserver( aObserver )
       
    42     {
       
    43     // No implementation required
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 void CSocketsReader::ConstructL()
       
    48     {
       
    49     CActiveScheduler::Add( this );
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 CSocketsReader::~CSocketsReader()
       
    54     {
       
    55     Cancel();
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 void CSocketsReader::DoCancel()
       
    60     {
       
    61     // Cancel asychronous read request
       
    62     iSocket.CancelRead();
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 void CSocketsReader::RunL()
       
    67     {
       
    68     switch ( iStatus.Int() )
       
    69         {
       
    70         case KErrNone:
       
    71             iObserver.NewData(iBuffer);
       
    72 //            IssueRead(); // Immediately start another read
       
    73             break;
       
    74         case KErrDisconnected:
       
    75             iObserver.ReportError( MSocketObserver::EDisconnected,
       
    76                                    iStatus.Int() );
       
    77             break;
       
    78         default:
       
    79             iObserver.ReportError( MSocketObserver::EGeneralReadError,
       
    80                                    iStatus.Int() );
       
    81             break;
       
    82         }
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 void CSocketsReader::IssueRead()
       
    87     {
       
    88     // Initiate a new read from socket into iBuffer
       
    89     __ASSERT_ALWAYS( !IsActive(), Panic(EBTSerialEngineReadSocketBadState) );
       
    90     iSocket.RecvOneOrMore( iBuffer, 0, iStatus, iLen );
       
    91     SetActive();
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 void CSocketsReader::ReadAsync()
       
    96     {
       
    97     // Initiate a new read from socket into iBuffer
       
    98     if (!IsActive())
       
    99         {
       
   100         IssueRead();
       
   101         }
       
   102     }
       
   103 
       
   104 // End of File