vpnengine/ikesocket/src/sender.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     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:  Sender for UDP data
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "sender.h"
       
    20 #include "ikesocketdefs.h"
       
    21 #include "ikedebug.h"
       
    22 
       
    23 using namespace IkeSocket;
       
    24 
       
    25 // ======== MEMBER FUNCTIONS ========
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // Two-phased constructor.
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CSender* CSender::NewL( RSocket& aSocket,
       
    32                         RSocket& aSocketNAT,
       
    33                         RSocket& aSocketNokiaNAT,
       
    34                         MSenderCallback& aCallback,                                     
       
    35                         MIkeDebug& aDebug  )
       
    36     {
       
    37     CSender* self = new (ELeave) CSender( aSocket,
       
    38                                           aSocketNAT,
       
    39                                           aSocketNokiaNAT,
       
    40                                           aCallback,
       
    41                                           aDebug );  
       
    42     CleanupStack::PushL(self);
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop(self);    
       
    45     return self;            
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Destructor.
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CSender::~CSender()
       
    53     {
       
    54     DEBUG_LOG( _L("CSender::~CSender") );
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Constructor.
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CSender::CSender( RSocket& aSocket,
       
    62                   RSocket& aSocketNAT,
       
    63                   RSocket& aSocketNokiaNAT,
       
    64                   MSenderCallback& aCallback,
       
    65                   MIkeDebug& aDebug )
       
    66  : CActive( EPriorityStandard ),
       
    67    iSocket( aSocket ),
       
    68    iSocketNAT( aSocketNAT ),
       
    69    iSocketNokiaNAT( aSocketNokiaNAT ),
       
    70    iCallback( aCallback ),
       
    71    iDebug( aDebug )
       
    72     {
       
    73     CActiveScheduler::Add( this ); // Added to the Active Scheduler
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // Second phase construction.
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 void CSender::ConstructL()
       
    81     {
       
    82     DEBUG_LOG( _L("CSender::ConstructL") );
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // Sends UDP data.
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 TInt CSender::SendUdpData( const TInt aLocalPort,
       
    90                            const TInetAddr& aDestAddr,
       
    91                            const TDesC8& aUdpData,
       
    92                            const TUint aDscp )
       
    93     {
       
    94     RSocket* socket = NULL;
       
    95     iLocalPort = aLocalPort;
       
    96     iDestAddr = aDestAddr;
       
    97     
       
    98     if ( iLocalPort == 500 )
       
    99         {
       
   100         socket = &iSocket;
       
   101         }
       
   102     else if ( iLocalPort == 4500 )
       
   103         {
       
   104         socket = &iSocketNAT;
       
   105         }
       
   106     else 
       
   107         {        
       
   108         socket = &iSocketNokiaNAT;
       
   109         }
       
   110     
       
   111     TInt err = socket->SetOpt( KSoIpTOS, KSolInetIp, aDscp );
       
   112     
       
   113     if ( err == KErrNone )
       
   114         {
       
   115         err = socket->SetOpt( KSoUdpSynchronousSend, KSolInetUdp, 1 );
       
   116         }
       
   117     
       
   118     if ( err == KErrNone )
       
   119         {
       
   120         socket->SendTo( aUdpData, iDestAddr, 0, iStatus );    
       
   121         SetActive();
       
   122         }
       
   123 
       
   124 #ifdef _DEBUG    
       
   125     TBuf<100> txt_addr;
       
   126     iDestAddr.Output( txt_addr );
       
   127     TUint32 port = iDestAddr.Port();
       
   128     DEBUG_LOG3( _L("Sending UDP data, local port=%d, dest address:port=%S:%d"),
       
   129             iLocalPort, &txt_addr, port );
       
   130     DEBUG_LOG2( _L(" DSCP=%d, err=%d"), aDscp, err );
       
   131 #endif
       
   132     
       
   133     return err;
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // From CActive
       
   138 // Handles request completion event about sending.
       
   139 // ---------------------------------------------------------------------------
       
   140 //
       
   141 void CSender::RunL()
       
   142     {
       
   143     DEBUG_LOG1( _L("CSender::RunL, iStatus=%d"),
       
   144             iStatus.Int() );
       
   145     
       
   146     iCallback.SendCompleted( iStatus.Int() );
       
   147     }
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // From CActive
       
   151 // Implements cancellation of sending.
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 void CSender::DoCancel()
       
   155     {
       
   156     DEBUG_LOG1( _L("CSender::DoCancel, iLocalPort=%d"),
       
   157             iLocalPort );
       
   158     
       
   159     if ( iLocalPort == KIkePort500 )
       
   160         {
       
   161         iSocket.CancelSend();
       
   162         }
       
   163     else if ( iLocalPort == KIkePort4500 )
       
   164         {
       
   165         iSocketNAT.CancelSend();
       
   166         }
       
   167     else
       
   168         {
       
   169         iSocketNokiaNAT.CancelSend();
       
   170         }
       
   171     }