bluetoothcommsprofiles/btpan/bnep/CSocketWriter.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <bluetooth/logger.h>
       
    22 #include "CBnepLink.h"
       
    23 #include "CSocketWriter.h"
       
    24 #include "bneputils.h"
       
    25 
       
    26 #ifdef __FLOG_ACTIVE
       
    27 _LIT8(KLogComponent, LOG_COMPONENT_PAN_BNEP);
       
    28 #endif
       
    29 
       
    30 CSocketWriter::CSocketWriter (RInternalSocket& aSocket, CBnepLink& aOwner)
       
    31         : CActive(EPriorityHigh), iSocket(aSocket), iBnepLink(aOwner)
       
    32     {
       
    33     LOG_FUNC
       
    34     CActiveScheduler::Add(this); 
       
    35     }
       
    36 
       
    37 /**
       
    38    Cancels any pending writes.
       
    39    @internalComponent
       
    40 */
       
    41 CSocketWriter::~CSocketWriter()
       
    42     {
       
    43     LOG_FUNC
       
    44     Cancel();
       
    45     
       
    46     iPendingWriteData.Free();
       
    47     }
       
    48 
       
    49 /**
       
    50    Create the socket writer
       
    51    @internalComponent
       
    52 */ 
       
    53 CSocketWriter* CSocketWriter::NewL (RInternalSocket& aSocket, CBnepLink& aOwner)
       
    54     {
       
    55     LOG_STATIC_FUNC
       
    56     CSocketWriter* self = new(ELeave) CSocketWriter(aSocket, aOwner);
       
    57     CleanupStack::PushL(self);
       
    58     self->ConstructL();
       
    59     CleanupStack::Pop(self);
       
    60     return self; 
       
    61     }
       
    62 
       
    63 /**
       
    64    Attempt to write a buffer.
       
    65    @return KErrNone if successful or a system-wide error code
       
    66    @internalComponent
       
    67 */
       
    68 TInt CSocketWriter::Write (RMBufChain& aChain)
       
    69     {
       
    70     LOG_FUNC
       
    71     if (aChain.Length() > KBnepMTU)
       
    72         {
       
    73         LOG(_L8("Length greater than expected MTU!"));
       
    74         aChain.Free();
       
    75         return KErrTooBig;
       
    76         }
       
    77     if (IsActive())
       
    78         {
       
    79         if(iQueueSize < KMaxSocketWriterQueueSize)
       
    80             {
       
    81             iQueue.Append(aChain);
       
    82             iQueueSize++;
       
    83             return KErrNone;
       
    84             }
       
    85         else
       
    86             {
       
    87             LOG(_L8("Bnep::ESocketWriterQueueOverflow"));
       
    88             aChain.Free();
       
    89             return KErrOverflow;
       
    90             }
       
    91         }
       
    92     LOG1(_L8("Length %d"), aChain.Length());
       
    93     
       
    94     __ASSERT_DEBUG(iPendingWriteData.IsEmpty(), BnepUtils::Panic(Bnep::ESocketWriterQueueOverflow));
       
    95     iPendingWriteData.Assign(aChain);
       
    96     iSocket.Write(iPendingWriteData, iStatus);
       
    97 
       
    98     SetActive();
       
    99     return KErrNone; 
       
   100     }
       
   101 
       
   102 /**
       
   103    Does nothing
       
   104    @internalComponent
       
   105 */ 
       
   106 void CSocketWriter::ConstructL ()
       
   107     {
       
   108     LOG_FUNC
       
   109     }
       
   110 
       
   111 /**
       
   112    Checks iQueue for outstanding buffers and writes the first
       
   113    if it exists.
       
   114    @internalComponent
       
   115 */ 
       
   116 void CSocketWriter::RunL ()
       
   117     {
       
   118     LOG_FUNC
       
   119 	// Check if the completed write was a success.
       
   120 	if(iStatus.Int() != KErrNone)
       
   121 		{
       
   122 		// The write failed - clear the pending write data.
       
   123 	    LOG1(_L8("CSocketWriter::RunL completed with error: %d"), iStatus.Int());
       
   124 		iPendingWriteData.Free();
       
   125 		}
       
   126 
       
   127     __ASSERT_DEBUG(iPendingWriteData.IsEmpty(), BnepUtils::Panic(Bnep::ESocketWriterQueueOverflow));
       
   128 
       
   129     if(!iQueue.IsEmpty())
       
   130         {
       
   131         RMBufChain item;
       
   132         //Check on return value of iQueue.Remove() is unnecessary, since we've checked iQueue.IsEmpty()
       
   133         static_cast<void>(iQueue.Remove(item));
       
   134         iQueueSize--;
       
   135         Write(item);
       
   136         }
       
   137     else
       
   138         {
       
   139         iQueueSize = 0; // To make sure that we are always in sync
       
   140         }
       
   141     }
       
   142 
       
   143 /**
       
   144    Cancel write.
       
   145    @internalComponent
       
   146 */
       
   147 void CSocketWriter::DoCancel ()
       
   148     {
       
   149     LOG_FUNC
       
   150     iSocket.CancelWrite();
       
   151     
       
   152     // Free the pending data buffer to aviod asserting when the next Write operation
       
   153     // is performed.
       
   154     
       
   155     iPendingWriteData.Free();
       
   156     }