cryptomgmtlibs/securitycommonutils/source/scsserver/asyncrequest.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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 the License "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 * Base class functionality for server-side asynchronous requests.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22 */
       
    23 
       
    24 
       
    25 #include <scs/scsserver.h>
       
    26 #include "scsserverconstants.h"
       
    27 
       
    28 EXPORT_C CAsyncRequest::CAsyncRequest(CScsSession* aSession, CScsSubsession* aSubsession, const RMessage2& aMessage)
       
    29 /**
       
    30 	Record the session, subsession, and function associated with this request.
       
    31 	These are required to complete or to cancel the request later.
       
    32 	
       
    33 	Adds this object to the active scheduler.
       
    34 
       
    35 	@param	aSession		Session used to launch this request.
       
    36 	@param	aSubsession		Subsession on which this request is queued.  This
       
    37 							should be NULL if the request is relative to a session,
       
    38 							instead of a subsession
       
    39 	@param	aMessage		Handle to the kernel-side request object, which
       
    40 							will be completed later.
       
    41  */
       
    42 :	CActive(CActive::EPriorityStandard),
       
    43 	iSession(aSession),
       
    44 	iSubsession(aSubsession),
       
    45 	iMessagePtr2(aMessage)
       
    46 	{
       
    47 	// extract implementation function
       
    48 	ScsImpl::ExtractScsAndImplFunctions(aMessage, NULL, &iFunction);
       
    49 	
       
    50 	CActiveScheduler::Add(this);
       
    51 	}
       
    52 
       
    53 EXPORT_C void CAsyncRequest::TransferToScsFrameworkL()
       
    54 /**
       
    55 	Adds this asynchronous request to the global collection.
       
    56 	This should be called after the subclass has performed its
       
    57 	own initialization, but before the active request has been
       
    58 	set up.
       
    59  */
       
    60 	{
       
    61 	iSession->iServer.AddAsyncRequestL(this);
       
    62 	}
       
    63 
       
    64 EXPORT_C void CAsyncRequest::RunL()
       
    65 /**
       
    66 	Implement CActive by completing the server client's request status
       
    67 	with the AO error code, i.e. iStatus.  This function also marks this
       
    68 	object for deletion.
       
    69 
       
    70 	If a subclass overrides this implementation then it must call
       
    71 	CompleteAndMarkForDeletion itself to complete the client-side
       
    72 	request.
       
    73 
       
    74 	@see CompleteAndMarkForDeletion
       
    75  */
       
    76 	{
       
    77 	TInt r = iStatus.Int();
       
    78 	CompleteAndMarkForDeletion(r);
       
    79 	}
       
    80 
       
    81 EXPORT_C TInt CAsyncRequest::RunError(TInt aError)
       
    82 /**
       
    83 	Override CActive by completing the request with the
       
    84 	supplied error code and marking this object for deletion.
       
    85 	The default implementation of RunL cannot leave, but the
       
    86 	subclass may override it with a more complex implementation.
       
    87 	
       
    88 	@param	aError			Error code with which aError left.
       
    89 	@return					KErrNone, meaning do not propagate
       
    90 							the error to the active scheduler.
       
    91  */
       
    92 	{
       
    93 	CompleteAndMarkForDeletion(aError);
       
    94 	return KErrNone;
       
    95 	}
       
    96 
       
    97 void CAsyncRequest::CancelCompleteAndMarkForDeletion()
       
    98 /**
       
    99 	Cancel this asynchronous request and complete the client
       
   100 	request with KErrCancel.  On exit, this object has been
       
   101 	marked for deletion.
       
   102  */
       
   103 	{
       
   104 	CompleteAndMarkForDeletion(KErrCancel);
       
   105 	}
       
   106 
       
   107 EXPORT_C void CAsyncRequest::CompleteAndMarkForDeletion(TInt aError)
       
   108 /**
       
   109 	Completes the client-side request associated with this
       
   110 	asynchronous request and marks this object for deletion.
       
   111 	
       
   112 	If the error was because the client passed a bad descriptor then
       
   113 	panick the client instead of completing the request for consistency
       
   114 	with synchronous requests.
       
   115 
       
   116 	@param	aError			Error code with which the client request
       
   117 							will be completed.
       
   118  */
       
   119 	{
       
   120 	if (aError == KErrBadDescriptor)
       
   121 		PanicClient(iMessagePtr2, ScsImpl::EScsClBadDesc);
       
   122 	else
       
   123 		iMessagePtr2.Complete(aError);
       
   124 	MarkForDeletion();
       
   125 	}
       
   126 
       
   127 void CAsyncRequest::MarkForDeletion()
       
   128 /**
       
   129 	Mark this outstanding request for deletion, so it is
       
   130 	cleaned up when the cleanup AO runs.
       
   131  */
       
   132 	{
       
   133 	DoCleanup();
       
   134 	// The deletion is processed at priority CActive::EPriorityHigh,
       
   135 	// so should happen ahead of any pending or new requests.
       
   136 	CAsyncCallBack* acb = iSession->iServer.iAsyncCleanup;
       
   137 	acb->CallBack();		// no effect if already queued
       
   138 	iSession = NULL;		// mark for deletion		
       
   139 	}
       
   140 
       
   141 EXPORT_C void CAsyncRequest::DoCleanup()
       
   142 /**
       
   143 	This virtual function is called whenever the SCS needs to cancel an operation.
       
   144 
       
   145 	If the asynchronous request implementation ALWAYS has an internal request active
       
   146 	for the duration of the client request, then this default implementation can be used, which
       
   147 	simply calls  CActive::Cancel which will call the derived classe's DoCancel (if, and only if
       
   148 	the object IsActive).
       
   149 
       
   150 	A more complex case is where the asynchronous requests may enter an internal queue where
       
   151 	the client request is (obviously still active), but the derived class has not outstanding request.
       
   152 	In this case the default implementation will cause a HANG, because Cancel will not call DoCancel if
       
   153 	the object is not active...
       
   154 
       
   155 	In the generic case, the derived class should provide an overload for the Cleanup method.
       
   156 	This function should call Cancel to cancel any outstanding operation, then perform whatever other
       
   157 	cleanup is required to cleanup the request (closing handles, removing from queues etc).
       
   158 	The framework will then complete the client request.
       
   159 
       
   160 	 nb. The destructor of the CAsyncRequest may not run until sometime after the client request has been
       
   161 	 completed.
       
   162  */
       
   163 {
       
   164 	Cancel();
       
   165 }
       
   166