hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/Csocketwriter.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
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 writer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CSocketWriter.h"
       
    22 #include "MSocketWriterObserver.h"
       
    23 #include <in_sock.h>
       
    24 #include <badesca.h>
       
    25 
       
    26 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    27 #include "DebugPrint.h"
       
    28 
       
    29 
       
    30 const TInt KServerBusyWaiting = 200000;  //200 ms delay
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CSocketWriter::CSocketWriter
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CSocketWriter::CSocketWriter( RSocket& aSocket,
       
    39         TInt aUDPRemotePort /*= -1*/ ) :
       
    40     CActive( EPriorityStandard ),
       
    41     iSocket( aSocket ),
       
    42     iUDPRemotePort( aUDPRemotePort )
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CSocketWriter::ConstructL
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 void CSocketWriter::ConstructL()
       
    51     {
       
    52     iTransferBufferArray = new (ELeave) CDesC8ArraySeg( 10 );
       
    53     User::LeaveIfError( iTimer.CreateLocal() );
       
    54     CActiveScheduler::Add( this );
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CSocketWriter::NewL
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 CSocketWriter* CSocketWriter::NewL( RSocket& aSocket,
       
    62         TInt aUDPRemotePort /* = -1 */ )
       
    63     {
       
    64     CSocketWriter* self = CSocketWriter::NewLC( aSocket,
       
    65         aUDPRemotePort );
       
    66     CleanupStack::Pop();
       
    67 
       
    68     return self;
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CSocketWriter::NewLC
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CSocketWriter* CSocketWriter::NewLC( RSocket& aSocket,
       
    76         TInt aUDPRemotePort /* = -1 */ )
       
    77     {
       
    78     CSocketWriter* self = new( ELeave ) CSocketWriter( aSocket,
       
    79         aUDPRemotePort );
       
    80     CleanupStack::PushL( self );
       
    81 
       
    82     self->ConstructL();
       
    83     return self;
       
    84     }
       
    85 
       
    86 
       
    87 // Destructor
       
    88 CSocketWriter::~CSocketWriter()
       
    89     {
       
    90     Cancel();
       
    91     iTimer.Close();
       
    92     delete iTransferBufferArray;
       
    93     }
       
    94 
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CSocketWriter::SetObserver
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CSocketWriter::SetObserver( MSocketWriterObserver* aObserver )
       
   101     {
       
   102     iObserver = aObserver;
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CSocketWriter::IssueWriteL
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CSocketWriter::IssueWriteL( const TDesC8& aData )
       
   110     {
       
   111     // slice the size of data if bigger than KWriteBufferSize
       
   112 
       
   113     TInt dsize = aData.Size();
       
   114 
       
   115     for ( TInt i = 0; i < dsize; i+=KWriteBufferSize )
       
   116         {
       
   117         if ( i + KWriteBufferSize - 1 >= dsize )
       
   118             {
       
   119             __ASSERT_DEBUG( i + aData.Mid( i ).Size() == dsize ,
       
   120                 User::Panic( _L( "writer" ), 100 ) );
       
   121             iTransferBufferArray->AppendL( aData.Mid(i) );
       
   122             }
       
   123         else
       
   124             {
       
   125             iTransferBufferArray->AppendL( aData.Mid( i, KWriteBufferSize ) );
       
   126             }
       
   127         }
       
   128 
       
   129     if ( !IsActive() )
       
   130         {
       
   131         IssueWrite();
       
   132         }
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CSocketWriter::IssueWrite
       
   137 // -----------------------------------------------------------------------------
       
   138 void CSocketWriter::IssueWrite()
       
   139     {
       
   140     DEBUG_PRINT( DEBUG_STRING(
       
   141         "CSocketWriter::IssueWrite" ) );
       
   142     iWriteBuffer = (*iTransferBufferArray)[ 0 ];
       
   143 
       
   144     TProtocolDesc desc;
       
   145     iSocket.Info( desc );
       
   146 
       
   147     if ( desc.iProtocol == KProtocolInetUdp && iUDPRemotePort > -1 )
       
   148         {
       
   149         // UDP
       
   150         DEBUG_PRINT( DEBUG_STRING(
       
   151             "CSocketWriter::IssueWrite(), UDP, remote port=%d" ),
       
   152             iUDPRemotePort );
       
   153 
       
   154         TInetAddr addrLocalHost( KInetAddrLoop, iUDPRemotePort );
       
   155         iSocket.SendTo( iWriteBuffer, addrLocalHost, 0, iStatus );
       
   156         }
       
   157     else
       
   158         {
       
   159         // TCP
       
   160         iSocket.Write( iWriteBuffer, iStatus );
       
   161         }
       
   162 
       
   163     SetActive();
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CSocketWriter::ContinueAfterError
       
   168 // -----------------------------------------------------------------------------
       
   169 void CSocketWriter::ContinueAfterError()
       
   170     {
       
   171     if ( iTransferBufferArray->Count() > 0 )
       
   172         {
       
   173         IssueWrite();
       
   174         }
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CSocketWriter::ResetTransferBuffer
       
   179 // -----------------------------------------------------------------------------
       
   180 void CSocketWriter::ResetTransferBuffer()
       
   181     {
       
   182     iTransferBufferArray->Reset();
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CSocketWriter::RunL
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 void CSocketWriter::RunL()
       
   190     {
       
   191     TInt status = iStatus.Int();
       
   192     if ( status == KErrNone )
       
   193         {
       
   194         if ( iWaiting )
       
   195             {
       
   196             DEBUG_PRINT( DEBUG_STRING(
       
   197                 "Trying to write again..." ) );
       
   198 
       
   199             iWaiting = EFalse;
       
   200             IssueWrite();
       
   201             }
       
   202         else
       
   203             {
       
   204             iTransferBufferArray->Delete( 0 );
       
   205             if ( iTransferBufferArray->Count() > 0 )
       
   206                 {
       
   207                 IssueWrite();
       
   208                 }
       
   209             else
       
   210                 {
       
   211                 iObserver->BufferUnderrunL();
       
   212                 }
       
   213             }
       
   214         }
       
   215     else
       
   216         {
       
   217         DEBUG_PRINT( DEBUG_STRING(
       
   218             "CSocketWriter::RunL(), iStatus=%d" ), status );
       
   219         iObserver->WriterErrorL( status );
       
   220         //If Socket server is busy, wait for a while and try again
       
   221         if ( status == KErrServerBusy )
       
   222             {
       
   223             DEBUG_PRINT( DEBUG_STRING(
       
   224                 "Socket server busy. Waiting for a while..." ) );
       
   225             iWaiting = ETrue;
       
   226             iTimer.After( iStatus, KServerBusyWaiting );
       
   227             SetActive();
       
   228             }
       
   229         }
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CSocketWriter::DoCancel
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 void CSocketWriter::DoCancel()
       
   237     {
       
   238     iTimer.Cancel();
       
   239     iSocket.CancelWrite();
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CSocketWriter::RunError
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 TInt CSocketWriter::RunError( TInt aError )
       
   247     {
       
   248     iObserver->ObserverLeaved( aError );
       
   249     return KErrNone;
       
   250     }
       
   251