bluetoothengine/bthid/bthidserver/src/socketreader.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2008 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:  This is the implementation of application class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "sockets.pan"
       
    20 #include "socketreader.h"
       
    21 #include "socketobserver.h"
       
    22 #include "debug.h"
       
    23 
       
    24 CSocketReader* CSocketReader::NewL(TUint aSocketID,
       
    25         MSocketObserver& aObserver, TInt aInitialBufSize)
       
    26     {
       
    27     CSocketReader* self = CSocketReader::NewLC(aSocketID, aObserver,
       
    28             aInitialBufSize);
       
    29     CleanupStack::Pop(self);
       
    30     return self;
       
    31     }
       
    32 
       
    33 CSocketReader* CSocketReader::NewLC(TUint aSocketID,
       
    34         MSocketObserver& aObserver, TInt aInitialBufSize)
       
    35     {
       
    36     CSocketReader* self = new (ELeave) CSocketReader(aSocketID, aObserver);
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL(aInitialBufSize);
       
    39     return self;
       
    40     }
       
    41 
       
    42 CSocketReader::CSocketReader(TUint aSocketID, MSocketObserver& aObserver) :
       
    43     CActive(EPriorityStandard), iSocketID(aSocketID), iObserver(aObserver),
       
    44             iBufferPtr(0, 0)
       
    45     {
       
    46     }
       
    47 
       
    48 CSocketReader::~CSocketReader()
       
    49     {
       
    50     Cancel(); // Causes DoCancel
       
    51 
       
    52     delete iBuffer;
       
    53     }
       
    54 
       
    55 void CSocketReader::ConstructL(TInt aInitialBufSize)
       
    56     {
       
    57     iBuffer = HBufC8::NewL(aInitialBufSize);
       
    58     iBufferPtr.Set(iBuffer->Des());
       
    59 
       
    60     CActiveScheduler::Add(this);
       
    61     }
       
    62 
       
    63 void CSocketReader::DoCancel()
       
    64     {
       
    65     // Cancel asynchronous read request
       
    66     iSocket->CancelRead();
       
    67     }
       
    68 
       
    69 void CSocketReader::RunL()
       
    70     {
       
    71         // Active object request complete handler
       
    72         TRACE_INFO((_L("CSocketReader::RunL(), iStatus.Int()=%d, socketID=%d"),iStatus.Int(), iSocketID));
       
    73 
       
    74     switch (iStatus.Int())
       
    75         {
       
    76         case KErrNone:
       
    77             {
       
    78             // Data has been read from socket
       
    79             // Inform the observer and issue another read if we want
       
    80             // to continue listening
       
    81             if (iObserver.HandleDataReceived(iSocketID, iBufferPtr))
       
    82                 {
       
    83                 IssueRead();
       
    84                 }
       
    85             break;
       
    86             }
       
    87 
       
    88         default:
       
    89             {
       
    90             // A read error has occurred. Inform the observer the connection is lost
       
    91             iObserver.HandleSocketError(iSocketID, ETrue, iStatus.Int());
       
    92             break;
       
    93             }
       
    94         }
       
    95     }
       
    96 
       
    97 void CSocketReader::IssueRead()
       
    98     {
       
    99     // Initiate a new read from socket into iBuffer
       
   100     iSocket->Read(iBufferPtr, iStatus);
       
   101     SetActive();
       
   102     }
       
   103 
       
   104 void CSocketReader::StartReadingL(RSocket* aSocket, TInt aMTU)
       
   105     {
       
   106     // Initiate a new read from socket into iBuffer
       
   107     __ASSERT_DEBUG(!IsActive(),
       
   108             User::Panic(KPanicSocketsEngineRead, ESocketsBadState));
       
   109 
       
   110     if (aMTU > iBufferPtr.MaxLength())
       
   111         {
       
   112         // Realloc the buffer as it isn't large enough.
       
   113         iBufferPtr.Set(0, 0, 0);
       
   114         delete iBuffer;
       
   115         iBuffer = 0;
       
   116         iBuffer = HBufC8::NewL(aMTU);
       
   117         iBufferPtr.Set(iBuffer->Des());
       
   118         }
       
   119 
       
   120     iSocket = aSocket;
       
   121     IssueRead();
       
   122     }