natfw/natfwstunplugin/src/cstunasynccallback.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 
       
    20 
       
    21 #include "mnatfwpluginobserver.h"
       
    22 #include "cstunasynccallback.h"
       
    23 
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // CStunAsyncCallback::NewL
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 CStunAsyncCallback* CStunAsyncCallback::NewL( 
       
    30     const CNATFWPluginApi& aStunPlugin,
       
    31     MNATFWPluginObserver& aObserver )
       
    32     {
       
    33     CStunAsyncCallback* self 
       
    34         = new (ELeave) CStunAsyncCallback( aStunPlugin, aObserver );
       
    35     CActiveScheduler::Add( self );
       
    36     self->WaitForRequestsL();
       
    37     return self;
       
    38     }
       
    39 
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CStunAsyncCallback::CStunAsyncCallback
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CStunAsyncCallback::CStunAsyncCallback( 
       
    46     const CNATFWPluginApi& aStunPlugin, 
       
    47     MNATFWPluginObserver& aObserver ) 
       
    48     :
       
    49     CActive( EPriorityStandard ),
       
    50     iStunPlugin( aStunPlugin ),
       
    51     iObserver( aObserver ),
       
    52     iPendingCallbacks( _FOFF( TStunPluginCallbackInfo, iLink ) )
       
    53     {
       
    54     }
       
    55 
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CStunAsyncCallback::CStunAsyncCallback
       
    59 // Dummy implementation. Default constructor is declared private and not used.
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CStunAsyncCallback::CStunAsyncCallback() :
       
    63     CActive( EPriorityStandard ),
       
    64     iStunPlugin( *( CNATFWPluginApi* )0x1 ),
       
    65     iObserver( *( MNATFWPluginObserver* )0x1 )
       
    66     {
       
    67     }
       
    68 
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // CStunAsyncCallback::CStunAsyncCallback
       
    72 // Dummy implementation, as copy constructor is declared private and not used.
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CStunAsyncCallback::CStunAsyncCallback( 
       
    76     const CStunAsyncCallback& aAsyncCallback ) :
       
    77     CActive( EPriorityStandard ),
       
    78     iStunPlugin( *( CNATFWPluginApi* )0x1 ),
       
    79     iObserver( aAsyncCallback.iObserver )
       
    80     {    
       
    81     }
       
    82 
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // CStunAsyncCallback::~CStunAsyncCallback
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CStunAsyncCallback::~CStunAsyncCallback()
       
    89     {
       
    90     Cancel();
       
    91 
       
    92     TStunPluginCallbackInfo* callback = iPendingCallbacks.First();
       
    93     while ( iPendingCallbacks.IsFirst( callback ) &&
       
    94             !iPendingCallbacks.IsEmpty() )
       
    95         {
       
    96         iPendingCallbacks.Remove( *callback );
       
    97         delete callback;
       
    98         callback = iPendingCallbacks.First();
       
    99         }
       
   100     }
       
   101 
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // CStunAsyncCallback::DoCancel
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 void CStunAsyncCallback::DoCancel()
       
   108     {
       
   109     if ( iStatus == KRequestPending )
       
   110         {
       
   111         TRequestStatus* status = &iStatus;    
       
   112         User::RequestComplete( status, KErrCancel );
       
   113         }
       
   114     }
       
   115 
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // CStunAsyncCallback::RunL
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 void CStunAsyncCallback::RunL()
       
   122     {
       
   123     WaitForRequestsL();
       
   124     ExecuteFirstCallback();
       
   125     }
       
   126 
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CStunAsyncCallback::RunError
       
   130 // Currently RunL cannot leave.
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 TInt CStunAsyncCallback::RunError( TInt aError )
       
   134     {
       
   135     if ( aError == KErrNoMemory )
       
   136         {
       
   137         return aError;
       
   138         }
       
   139     return KErrNone;
       
   140     }
       
   141 
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CStunAsyncCallback::MakeCallbackL
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CStunAsyncCallback::MakeCallbackL(
       
   148     TStunPluginCallbackInfo::TFunction aFunction,
       
   149     TUint aStreamId,
       
   150     TInt aErrorCode,
       
   151     TAny* aEventData )
       
   152     {    
       
   153     TStunPluginCallbackInfo* callback = 
       
   154         new ( ELeave ) TStunPluginCallbackInfo( iStunPlugin,
       
   155                                                 iObserver,
       
   156                                                 aFunction,
       
   157                                                 aStreamId,
       
   158                                                 aErrorCode,
       
   159                                                 aEventData );
       
   160     CleanupStack::PushL( callback );
       
   161     __ASSERT_ALWAYS( callback->Validate(), User::Leave( KErrArgument ) );
       
   162     CleanupStack::Pop( callback );
       
   163 
       
   164     // CActive::iActive won't tell whether User::RequestComplete has already
       
   165     // been called, so iStatus is inspected to see whether a previous call to
       
   166     // AddDeleteRequestL has already called User::RequestComplete.
       
   167     
       
   168     if ( iStatus == KRequestPending )
       
   169         {
       
   170         Wakeup();
       
   171         }
       
   172 
       
   173     iPendingCallbacks.AddLast( *callback );
       
   174     }
       
   175 
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // CStunAsyncCallback::CancelCallback
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 void CStunAsyncCallback::CancelCallback( TUint aStreamId )
       
   182     {
       
   183     TSglQueIter<TStunPluginCallbackInfo> iter( iPendingCallbacks );
       
   184 
       
   185     for ( TStunPluginCallbackInfo* callback = iter++; 
       
   186           callback; callback = iter++ )
       
   187         {
       
   188         if ( callback->iStreamId == aStreamId )
       
   189             {
       
   190             iPendingCallbacks.Remove( *callback );
       
   191             delete callback;
       
   192             }
       
   193         }
       
   194     }
       
   195 
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CStunAsyncCallback::ExecuteFirstCallback
       
   199 // Handle one callback request and wait for the next one. If there are more
       
   200 // callbacks, complete the asynchronous request so that RunL will soon be 
       
   201 // called again. Don't call more than one callback, since upper layer 
       
   202 // might've deleted STUN plugin, causing CStunAsyncCallback to be deleted too.
       
   203 // For the same reason nothing is done after executing the callback.
       
   204 // ---------------------------------------------------------------------------
       
   205 //    
       
   206 void CStunAsyncCallback::ExecuteFirstCallback()
       
   207     {
       
   208     TStunPluginCallbackInfo* callback = iPendingCallbacks.First();
       
   209     if ( iPendingCallbacks.IsFirst( callback ) &&
       
   210          !iPendingCallbacks.IsEmpty() )
       
   211         {
       
   212         iPendingCallbacks.Remove( *callback );
       
   213         
       
   214         if ( !iPendingCallbacks.IsEmpty() )
       
   215             {
       
   216             Wakeup();
       
   217             }
       
   218 
       
   219         callback->Execute();
       
   220         delete callback;
       
   221         }
       
   222     }
       
   223 
       
   224 
       
   225 // ---------------------------------------------------------------------------
       
   226 // CStunAsyncCallback::WaitForRequestsL
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 void CStunAsyncCallback::WaitForRequestsL()
       
   230     {
       
   231     __ASSERT_ALWAYS( !IsActive(), User::Leave( KErrNotReady ) );
       
   232 
       
   233     iStatus = KRequestPending;
       
   234     SetActive();
       
   235     }
       
   236 
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // CStunAsyncCallback::Wakeup
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void CStunAsyncCallback::Wakeup()
       
   243     {
       
   244     TRequestStatus* status = &iStatus;
       
   245     User::RequestComplete( status, KErrNone );
       
   246     }