natplugins/natptraversalcontroller/src/cnatbindingstunrefresher.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2007 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32math.h>
       
    20 #include "natfwstunbinding.h"
       
    21 #include "cnatbindingstunrefresher.h"
       
    22 #include "cnatbinding.h"
       
    23 
       
    24 const TInt KRefreshBaseValue = 900000; // microseconds
       
    25 const TInt KRefreshVariationRange = 200000; // microseconds
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CNATBindingSTUNRefresher::CNATBindingSTUNRefresher
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CNATBindingSTUNRefresher::CNATBindingSTUNRefresher(
       
    32     CDeltaTimer& aDeltaTimer,
       
    33     TInt aRefreshInterval,
       
    34     MNATBindingRefresherObserver& aObserver,
       
    35     CNATBinding& aNATBinding)
       
    36     : CNATBindingRefresher(aDeltaTimer,aObserver),
       
    37       iNATBinding(aNATBinding),
       
    38       iRefreshInterval(aRefreshInterval)
       
    39     {
       
    40     TTime time;
       
    41     time.HomeTime();
       
    42     iRandomSeed = time.Int64();
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CNATBindingSTUNRefresher::~CNATBindingSTUNRefresher
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CNATBindingSTUNRefresher::~CNATBindingSTUNRefresher()
       
    50     {
       
    51     CSTUNBinding* stunBinding = iNATBinding.Binding();
       
    52     if (stunBinding)
       
    53         {
       
    54         stunBinding->CancelRequest();
       
    55         }
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CNATBindingSTUNRefresher::HasSocket
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 TBool CNATBindingSTUNRefresher::HasSocket(const RSocket& aSocket) const
       
    63     {
       
    64     CSTUNBinding* stunBinding = iNATBinding.Binding();
       
    65     return stunBinding &&
       
    66            (aSocket.SubSessionHandle() ==
       
    67             stunBinding->Socket().SubSessionHandle());
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CNATBindingSTUNRefresher::RefreshInterval
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 TInt CNATBindingSTUNRefresher::RefreshInterval()
       
    75     {
       
    76     TInt interval = KErrUnderflow;
       
    77     if (iRefreshInterval > 0)
       
    78         {
       
    79         const TInt minValue = iRefreshInterval*KRefreshBaseValue;
       
    80         const TInt variantPart = iRefreshInterval*KRefreshVariationRange;
       
    81         const TInt rand = Math::Rand(iRandomSeed);
       
    82         interval = (minValue+(rand%variantPart));
       
    83         }
       
    84     return interval;
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CNATBindingSTUNRefresher::RefreshTimerExpiredL
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CNATBindingSTUNRefresher::RefreshTimerExpiredL()
       
    92     {
       
    93     __ASSERT_ALWAYS(iNATBinding.Binding(), User::Leave(KErrNotFound));
       
    94 
       
    95     iNATBinding.SendSTUNRequestL();
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CNATBindingSTUNRefresher::CancelRefresh
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CNATBindingSTUNRefresher::CancelRefresh()
       
   103     {
       
   104     CSTUNBinding* stunBinding = iNATBinding.Binding();
       
   105     if (stunBinding)
       
   106         {
       
   107         stunBinding->CancelRequest();
       
   108         }
       
   109     }