natfw/natfwclient/src/cnatfwasynccallback.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 "cnatfwasynccallback.h"
       
    22 
       
    23 
       
    24 // ---------------------------------------------------------------------------
       
    25 // CNatFwAsyncCallback::NewL
       
    26 // ---------------------------------------------------------------------------
       
    27 //
       
    28 CNatFwAsyncCallback* CNatFwAsyncCallback::NewL()
       
    29     {
       
    30     CNatFwAsyncCallback* self
       
    31         = new (ELeave) CNatFwAsyncCallback();
       
    32     CActiveScheduler::Add( self );
       
    33     self->WaitForRequestsL();
       
    34     return self;
       
    35     }
       
    36 
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CNatFwAsyncCallback::CNatFwAsyncCallback
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CNatFwAsyncCallback::CNatFwAsyncCallback() :
       
    43     CActive( EPriorityStandard ),
       
    44     iPendingCallbacks( _FOFF( CNatFwCallbackInfo, iLink ) )
       
    45     {
       
    46 
       
    47     }
       
    48 
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CNatFwAsyncCallback::~CNatFwAsyncCallback
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CNatFwAsyncCallback::~CNatFwAsyncCallback()
       
    55     {
       
    56     Cancel();
       
    57 
       
    58     CNatFwCallbackInfo* callback = iPendingCallbacks.First();
       
    59     while ( iPendingCallbacks.IsFirst( callback ) &&
       
    60             !iPendingCallbacks.IsEmpty() )
       
    61         {
       
    62         iPendingCallbacks.Remove( *callback );
       
    63         delete callback;
       
    64         callback = iPendingCallbacks.First();
       
    65         }
       
    66     }
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // CNatFwAsyncCallback::DoCancel
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 void CNatFwAsyncCallback::DoCancel()
       
    74     {
       
    75     if ( iStatus == KRequestPending )
       
    76         {
       
    77         TRequestStatus* status = &iStatus;
       
    78         User::RequestComplete( status, KErrCancel );
       
    79         }
       
    80     }
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CNatFwAsyncCallback::RunL
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CNatFwAsyncCallback::RunL()
       
    88     {
       
    89     WaitForRequestsL();
       
    90     ExecuteFirstCallback();
       
    91     }
       
    92 
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // CNatFwAsyncCallback::RunError
       
    96 // Currently RunL cannot leave.
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 TInt CNatFwAsyncCallback::RunError( TInt aError )
       
   100     {
       
   101     if ( aError == KErrNoMemory )
       
   102         {
       
   103         return aError;
       
   104         }
       
   105 
       
   106     return KErrNone;
       
   107     }
       
   108 
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // CNatFwAsyncCallback::MakeCallbackL
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CNatFwAsyncCallback::MakeCallbackL(
       
   115     MNATFWRegistrationController& aController,
       
   116     CNatFwCallbackInfo::TFunction aFunction,
       
   117     TUint aSessionId,
       
   118     TUint aStreamId,
       
   119     TInt aErrorCode,
       
   120     TAny* aEventData )
       
   121     {
       
   122     CNatFwCallbackInfo* callback =
       
   123         new ( ELeave ) CNatFwCallbackInfo( aController,
       
   124                                            aFunction,
       
   125                                            aSessionId,
       
   126                                            aStreamId,
       
   127                                            aErrorCode,
       
   128                                            aEventData );
       
   129     CleanupStack::PushL( callback );
       
   130     __ASSERT_ALWAYS( callback->Validate(), User::Leave( KErrArgument ) );
       
   131     CleanupStack::Pop( callback );
       
   132 
       
   133     // CActive::iActive won't tell whether User::RequestComplete has already
       
   134     // been called, so iStatus is inspected to see whether a previous call to
       
   135     // AddDeleteRequestL has already called User::RequestComplete.
       
   136 
       
   137     if ( iStatus == KRequestPending )
       
   138         {
       
   139         Wakeup();
       
   140         }
       
   141 
       
   142     iPendingCallbacks.AddLast( *callback );
       
   143     }
       
   144 
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // CNatFwAsyncCallback::CancelCallback
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 void CNatFwAsyncCallback::CancelCallback( TUint aStreamId )
       
   151     {
       
   152     TSglQueIter<CNatFwCallbackInfo> iter( iPendingCallbacks );
       
   153 
       
   154     for ( CNatFwCallbackInfo* callback = iter++; callback; callback = iter++ )
       
   155         {
       
   156         if ( callback->iStreamId == aStreamId )
       
   157             {
       
   158             iPendingCallbacks.Remove( *callback );
       
   159             delete callback;
       
   160             }
       
   161         }
       
   162     }
       
   163 
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CNatFwAsyncCallback::ExecuteFirstCallback
       
   167 // Handle one callback request and wait for the next one. If there are more
       
   168 // callbacks, complete the asynchronous request so that RunL will soon be
       
   169 // called again.
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 void CNatFwAsyncCallback::ExecuteFirstCallback()
       
   173     {
       
   174     CNatFwCallbackInfo* callback = iPendingCallbacks.First();
       
   175     if ( iPendingCallbacks.IsFirst( callback ) &&
       
   176          !iPendingCallbacks.IsEmpty() )
       
   177         {
       
   178         iPendingCallbacks.Remove( *callback );
       
   179 
       
   180         if ( !iPendingCallbacks.IsEmpty() )
       
   181             {
       
   182             Wakeup();
       
   183             }
       
   184 
       
   185         callback->Execute();
       
   186         delete callback;
       
   187         }
       
   188     }
       
   189 
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // CNatFwAsyncCallback::WaitForRequestsL
       
   193 // ---------------------------------------------------------------------------
       
   194 //
       
   195 void CNatFwAsyncCallback::WaitForRequestsL()
       
   196     {
       
   197     __ASSERT_ALWAYS( !IsActive(), User::Leave( KErrNotReady ) );
       
   198 
       
   199     iStatus = KRequestPending;
       
   200     SetActive();
       
   201     }
       
   202 
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // CNatFwAsyncCallback::Wakeup
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CNatFwAsyncCallback::Wakeup()
       
   209     {
       
   210     TRequestStatus* status = &iStatus;
       
   211     User::RequestComplete( status, KErrNone );
       
   212     }