bluetoothengine/bthid/bthidserver/src/socketwriter.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 "socketwriter.h"
       
    21 #include "socketobserver.h"
       
    22 #include "timeouttimer.h"
       
    23 
       
    24 // ----------------------------------------------------------------------
       
    25 
       
    26 // Initial size of the buffers for data to write
       
    27 const TInt KWriteBufferInitialSize = 20;
       
    28 
       
    29 // 5 seconds socket write time-out
       
    30 const TInt CSocketWriter::KTimeOut = 5000000;
       
    31 
       
    32 CSocketWriter* CSocketWriter::NewL(TUint aSocketID,
       
    33         MSocketObserver& aObserver)
       
    34     {
       
    35     CSocketWriter* self = CSocketWriter::NewLC(aSocketID, aObserver);
       
    36     CleanupStack::Pop(self);
       
    37     return self;
       
    38     }
       
    39 
       
    40 CSocketWriter* CSocketWriter::NewLC(TUint aSocketID,
       
    41         MSocketObserver& aObserver)
       
    42     {
       
    43     CSocketWriter* self = new (ELeave) CSocketWriter(aSocketID, aObserver);
       
    44     CleanupStack::PushL(self);
       
    45     self->ConstructL();
       
    46     return self;
       
    47     }
       
    48 
       
    49 CSocketWriter::CSocketWriter(TUint aSocketID, MSocketObserver& aObserver) :
       
    50     CActive(EPriorityStandard), iSocketID(aSocketID), iObserver(aObserver)
       
    51     {
       
    52     }
       
    53 
       
    54 CSocketWriter::~CSocketWriter()
       
    55     {
       
    56     Cancel();
       
    57     delete iTimer;
       
    58     delete iWriteBuffer[0];
       
    59     delete iWriteBuffer[1];
       
    60     }
       
    61 
       
    62 void CSocketWriter::DoCancel()
       
    63     {
       
    64     // Cancel asynchronous write request
       
    65     iSocket->CancelWrite();
       
    66 
       
    67     iTimer->Cancel();
       
    68 
       
    69     // Clear the buffers
       
    70     *iWriteBuffer[0] = KNullDesC8;
       
    71     *iWriteBuffer[1] = KNullDesC8;
       
    72     }
       
    73 
       
    74 void CSocketWriter::ConstructL()
       
    75     {
       
    76     CActiveScheduler::Add(this);
       
    77 
       
    78     iTimer = CTimeOutTimer::NewL(CActive::EPriorityStandard, *this);
       
    79 
       
    80     iWriteBuffer[0] = HBufC8::NewL(KWriteBufferInitialSize);
       
    81     iWriteBuffer[1] = HBufC8::NewL(KWriteBufferInitialSize);
       
    82     iCurrentBuffer = 0;
       
    83     }
       
    84 
       
    85 void CSocketWriter::TimerExpired()
       
    86     {
       
    87     Cancel();
       
    88 
       
    89     // Clear the buffers
       
    90     *iWriteBuffer[0] = KNullDesC8;
       
    91     *iWriteBuffer[1] = KNullDesC8;
       
    92 
       
    93     iObserver.HandleSocketError(iSocketID, EFalse, KErrTimedOut);
       
    94     }
       
    95 
       
    96 void CSocketWriter::RunL()
       
    97     {
       
    98     iTimer->Cancel();
       
    99 
       
   100     // Remember the current status
       
   101     TInt status = iStatus.Int();
       
   102 
       
   103     // Clear the buffer that has been written
       
   104     *iWriteBuffer[iCurrentBuffer] = KNullDesC8;
       
   105 
       
   106     // If the other buffer has data, write it now
       
   107     if (iWriteBuffer[1 - iCurrentBuffer]->Length() > 0)
       
   108         {
       
   109         iCurrentBuffer = 1 - iCurrentBuffer;
       
   110         DoWrite();
       
   111         }
       
   112 
       
   113     // Handle status of completed write
       
   114     if (KErrNone == status)
       
   115         {
       
   116         iObserver.HandleWriteComplete(iSocketID);
       
   117         }
       
   118     else
       
   119         {
       
   120         // Error: pass it up to user interface
       
   121         iObserver.HandleSocketError(iSocketID, EFalse, status);
       
   122         }
       
   123     }
       
   124 
       
   125 void CSocketWriter::Initialise(RSocket* aSocket)
       
   126     {
       
   127     __ASSERT_ALWAYS(!IsActive(),
       
   128             User::Panic(KPanicSocketsEngineWrite, ESocketsBadState));
       
   129 
       
   130     iSocket = aSocket;
       
   131     }
       
   132 
       
   133 void CSocketWriter::IssueWriteL(const TDesC8& aData)
       
   134     {
       
   135     if (IsActive())
       
   136         {
       
   137         // Put the data in the other buffer
       
   138         StoreDataL(aData, 1 - iCurrentBuffer);
       
   139         }
       
   140     else
       
   141         {
       
   142         // Put the data in the current buffer and write immediately
       
   143         StoreDataL(aData, iCurrentBuffer);
       
   144         DoWrite();
       
   145         }
       
   146     }
       
   147 
       
   148 void CSocketWriter::DoWrite()
       
   149     {
       
   150     // If we aren't in the correct state for a write, panic.
       
   151     __ASSERT_ALWAYS(!IsActive(),
       
   152             User::Panic(KPanicSocketsEngineWrite, ESocketsBadState));
       
   153 
       
   154     // Initiate actual write
       
   155     iSocket->Write(*iWriteBuffer[iCurrentBuffer], iStatus);
       
   156 
       
   157     // Request timeout
       
   158     if (!iTimer->IsActive())
       
   159         iTimer->After(KTimeOut);
       
   160 
       
   161     SetActive();
       
   162     }
       
   163 
       
   164 void CSocketWriter::StoreDataL(const TDesC8& aData, TInt aBuffer)
       
   165     {
       
   166     __ASSERT_ALWAYS(iWriteBuffer[aBuffer]->Length() == 0,
       
   167             User::Panic(KPanicSocketsEngineWrite, ESocketsBadState));
       
   168 
       
   169     // Reallocate the buffer if it's too small
       
   170     if (aData.Length() > iWriteBuffer[aBuffer]->Des().MaxLength())
       
   171         {
       
   172         // Make sure we have a new buffer before deleting the old one
       
   173         HBufC8* newBuffer = HBufC8::NewL(aData.Length());
       
   174 
       
   175         delete iWriteBuffer[aBuffer];
       
   176         iWriteBuffer[aBuffer] = newBuffer;
       
   177         }
       
   178 
       
   179     *iWriteBuffer[aBuffer] = aData;
       
   180     }