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