natfw/natfwstunturnclient/src/cstuntimeruser.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2005 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 
       
    20 
       
    21 #include "stunassert.h"
       
    22 #include "cstuntimeruser.h"
       
    23 #include "stunutils.h"
       
    24 #include "stunturnclientlogs.h"
       
    25 
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CSTUNTimerUser::CSTUNTimerUser
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CSTUNTimerUser::CSTUNTimerUser( CDeltaTimer& aDeltaTimer ) :
       
    34     iDeltaTimer( aDeltaTimer )
       
    35     {    
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CSTUNTimerUser::CSTUNTimerUser
       
    40 // Dummy implementation. Default constructor is declared private and not used.
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 CSTUNTimerUser::CSTUNTimerUser() : iDeltaTimer( *( CDeltaTimer* )0x1 )
       
    44     {
       
    45     }
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // CSTUNTimerUser::CSTUNTimerUser
       
    49 // Dummy implementation, as copy constructor is declared private and not used.
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CSTUNTimerUser::CSTUNTimerUser( const CSTUNTimerUser& aTimerUser ) :
       
    53     CBase(),
       
    54     iDeltaTimer( aTimerUser.iDeltaTimer )
       
    55     {    
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CSTUNTimerUser::~CSTUNTimerUser
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CSTUNTimerUser::~CSTUNTimerUser()
       
    63     {
       
    64     StopTimer();
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CSTUNTimerUser::StartTimer
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 void CSTUNTimerUser::StartTimer( TInt aDuration )
       
    72     {
       
    73     __STUNTURNCLIENT_INT1( "CSTUNTimerUser::StartTimer, duration = ", aDuration )
       
    74     __STUN_ASSERT_RETURN( !iIsRunning, KErrNotReady );
       
    75 
       
    76     TCallBack callback( TimerExpired, this );
       
    77     iTimerEntry.Set( callback );
       
    78 
       
    79     iDeltaTimer.Queue( TimerDuration( aDuration ), iTimerEntry );
       
    80     iIsRunning = ETrue;
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CSTUNTimerUser::StopTimer
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void CSTUNTimerUser::StopTimer()
       
    88     {
       
    89     __STUNTURNCLIENT( "CSTUNTimerUser::StopTimer" )
       
    90     iDeltaTimer.Remove( iTimerEntry );
       
    91     iIsRunning = EFalse;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CSTUNTimerUser::TimerExpired
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 TInt CSTUNTimerUser::TimerExpired( TAny *aPtr )
       
    99     {
       
   100     __STUNTURNCLIENT( "CSTUNTimerUser::TimerExpired" )
       
   101     __STUN_ASSERT_RETURN_VALUE( aPtr, KErrArgument );
       
   102 
       
   103     CSTUNTimerUser* handler = reinterpret_cast<CSTUNTimerUser*>( aPtr );
       
   104     handler->iIsRunning = EFalse;
       
   105 
       
   106     TRAPD( err, handler->TimerExpiredL() );
       
   107     
       
   108     //Even if TimerExpiredL made an asynchronous callback, the actual callback
       
   109     //function has not been called yet, so application can't have deleted
       
   110     //handler yet.
       
   111     //Binding must be implemented so that it if has deleted the transaction,
       
   112     //TimerExpiredL will not leave after that!
       
   113     if ( err != KErrNone )
       
   114         {
       
   115         handler->LeaveFromTimerExpired( err );
       
   116         }
       
   117 
       
   118     return KErrNone;
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CSTUNTimerUser::DeltaTimer
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 CDeltaTimer& CSTUNTimerUser::DeltaTimer()
       
   126     {
       
   127     return iDeltaTimer;
       
   128     }
       
   129     
       
   130 // -----------------------------------------------------------------------------
       
   131 // CSTUNTimerUser::IsRunning
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 TBool CSTUNTimerUser::IsRunning() const
       
   135     {
       
   136     __STUNTURNCLIENT_INT1( "CSTUNTimerUser::IsRunning, isRunning = ", iIsRunning )
       
   137     return iIsRunning;
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CSTUNTimerUser::TimerDuration
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 TTimeIntervalMicroSeconds32 CSTUNTimerUser::TimerDuration( TInt aMilliseconds )
       
   145     {
       
   146     __STUNTURNCLIENT( "CSTUNTimerUser::TimerDuration" )
       
   147     const TInt KMicrosecInMillisec = 1000;
       
   148     const TInt KMaxMilliseconds = KMaxTInt32 / KMicrosecInMillisec;
       
   149 
       
   150     __ASSERT_DEBUG( aMilliseconds <= KMaxMilliseconds,
       
   151                     User::Panic( _L( "STUN:Timer overflow" ), KErrOverflow ) );
       
   152     __ASSERT_ALWAYS( aMilliseconds <= KMaxMilliseconds,
       
   153                      aMilliseconds = KMaxMilliseconds );
       
   154 
       
   155     TTimeIntervalMicroSeconds32 microSec( aMilliseconds * KMicrosecInMillisec );
       
   156     return microSec;
       
   157     }