os/security/cryptomgmtlibs/securitycommonutils/source/scsclient/scsclientbase.cpp
Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Generic client-side code for client-server interaction.
16 * Attempts to establish a connection to a session counting server,
17 * starting the process if required.
26 #include <scs/scsclient.h>
28 #include <scs/scscommon.h>
30 using namespace ScsImpl;
33 EXPORT_C RScsClientBase::RScsClientBase()
35 This constructor is protected to ensure this class is not
36 instantiated directly.
43 EXPORT_C void RScsClientBase::Close()
45 This method should be used in preference to RScsSessionBase::Close
46 because it sends a message to cancel any outstanding requests on the
47 session or its subsessions.
52 RSessionBase::SendReceive(EPreCloseSession);
53 RSessionBase::Close();
57 TInt RScsClientBase::StartServerProcess(const TDesC& aExeName, const TUidType& aFullExeUid)
59 This function is defined for the convenience of subclasses which need to start
60 a server process before they can connect to the server.
62 @param aExeName Executable which hosts the server.
63 @param aFullExeUid The server executable's full UID. This is used to ensure the
64 intended executable is started, and not another executable
66 @return Symbian OS error code. KErrNone indicates success, and any other
67 value indicates failure.
68 @pre This function should only be called by Connect(const TVersion&) if the server is
73 TInt r = pr.Create(aExeName, /* aCommand */ KNullDesC, aFullExeUid);
79 if (rs != KRequestPending)
84 User::WaitForRequest(rs);
85 if (rs.Int()==KErrAlreadyExists)
88 r = (pr.ExitType() == EExitPending) ? rs.Int() : KErrGeneral;
95 EXPORT_C TInt RScsClientBase::Connect(
96 const TDesC& aSvrName, const TVersion& aReqVer, const TDesC& aExeName, const TUidType& aFullExeUid)
98 Attempt to connect to the named server. If the server is not available then attempt
99 to start its hosting process.
101 @param aSvrName Name of server to connect to.
102 @param aReqVer Required server version.
103 @param aExeName Executable which hosts the server. This function will launch this
104 executable if the server is not running.
105 @param aFullExeUid The server executable's full UID. This ensures the intended
106 executable is started, and not another executable with the same name.
107 @return Symbian OS error code. KErrNone indicates success,
108 and any other value indicates failure.
111 TInt retries = 2; // number of remaining retries
115 TInt r = CreateSession(aSvrName, aReqVer);
117 // if connected then finished
121 // if any reason other than server not available then abort
122 if (r != KErrNotFound && r != KErrServerTerminated)
128 r = StartServerProcess(aExeName, aFullExeUid);
129 if (r != KErrNone && r != KErrAlreadyExists)
134 // -------- server heap checking --------
136 EXPORT_C TInt RScsClientBase::SetServerHeapFail(TInt aRate)
138 Start marking the server heap and set a deterministic
139 fail rate. This should matched with a call to EndServerHeapFail.
141 This function is empty in release builds.
143 @param aRate Number of allocations after which allocation
144 should fail on the server heap.
145 @see EndServerHeapFail
155 return RSessionBase::SendReceive(ScsImpl::EUHeapSetFail, ipc);
159 EXPORT_C TInt RScsClientBase::ResetServerHeapFail()
161 Finish marking the server heap and reset the failure rate.
162 This should match a previous call to SetServerHeapFail.
164 If there is a heap imbalance, then the server will be panicked.
166 This function is empty in release builds.
168 @see SetServerHeapFail
174 return RSessionBase::SendReceive(ScsImpl::EUHeapResetFail);
180 // -------- passing arguments to a server-side session --------
182 EXPORT_C TInt RScsClientBase::ShutdownServer()
184 DEBUG USE ONLY - Tells the server to shutdown down ASAP, and block
185 until it has done so. This also closes the current session.
187 If the server is not configured to use a inactivity shutdown timer,
188 this will fail with KErrNotSupported.
190 nb. You may still need to call the Close function of a derived class
191 to ensure it gets to cleanup...
193 @return Symbian OS error code where KErrNone indicates
194 success and any other value indicates failure.
198 TPckgBuf<TProcessId> idBuf;
199 TIpcArgs args(&idBuf);
200 TInt r = RSessionBase::SendReceive(ScsImpl::EGetServerPid, args);
201 if(r != KErrNone) return r;
203 // Open a handle for the server thread
205 r = server.Open(idBuf(), EOwnerThread);
206 if(r != KErrNone) return r;
208 // Logon to the server process to spot when it exits
212 // Ask the server to exit ASAP
213 r = RSessionBase::SendReceive(ScsImpl::EShutdownAsap);
216 (void) server.LogonCancel(rs);
224 // Wait for the server to finish shutting down
225 User::WaitForRequest(rs); // nb. we do not care what code it shutdown with.
227 // Close our server process handle
233 // -------- passing arguments to a server-side session --------
235 EXPORT_C TInt RScsClientBase::CallSessionFunction(TInt aFunction) const
237 Send a command to the corresponding server-side session. The
238 subclass uses this function instead of directly calling
239 RSubSessionBase::SendReceive because it adds the SCS code
240 which marks this as an ordinary session call.
242 @param aFunction Function identifier. Bits 31:24 must be zero,
243 because they are reserved for SCS commands.
244 @return Error code with which the server completed the request.
247 __ASSERT_DEBUG(! ScsFieldUsed(aFunction), ClientSidePanic(EScsClNoArgsSessUsedScs));
249 TInt f = ECallSessionFunc | aFunction;
250 return RSessionBase::SendReceive(f);
253 EXPORT_C TInt RScsClientBase::CallSessionFunction(TInt aFunction, const TIpcArgs& aArgs) const
255 Send a command to the corresponding server-side session. The
256 subclass uses this function instead of directly calling
257 RSubSessionBase::SendReceive because it adds the SCS code which
258 marks this as an ordinary session call.
260 @param aFunction Session function identifier. Bits 31:24 must be zero,
261 because they are reserved for SCS commands.
262 @param aArgs Standard IPC arguments.
263 @return Error code with which the server completed the request.
266 __ASSERT_DEBUG(! ScsFieldUsed(aFunction), ClientSidePanic(EScsClArgsSessUsedScs));
268 TInt f = ECallSessionFunc | aFunction;
269 return RSessionBase::SendReceive(f, aArgs);
272 EXPORT_C void RScsClientBase::CallSessionFunction(TInt aFunction, const TIpcArgs& aArgs, TRequestStatus& aStatus) const
274 Send the supplied function identifier and arguments to the server-side
275 session. The subclass uses this function instead of directly calling
276 RSubSessionBase::SendReceive because it adds the SCS code which marks
277 this as an ordinary session call.
279 @param aFunction Session function identifier. Bits 31:24 must be zero,
280 because they are reserved for SCS commands.
281 @param aArgs Standard IPC arguments.
282 @param aStatus This will be completed by the server when it has
283 finished handling the function.
286 __ASSERT_DEBUG(! ScsFieldUsed(aFunction), ClientSidePanic(EScsClArgsSessAsyncUsedScs));
288 TInt f = ECallSessionFunc | aFunction;
289 RSessionBase::SendReceive(f, aArgs, aStatus);
292 EXPORT_C void RScsClientBase::CancelSessionFunction(TInt aFunction) const
294 Cancel an outstanding session request. This has no effect if the
295 request is not outstanding.
297 @param aFunction Implementation function. This must be the
298 same value that was supplied to CallSessionFunction.
301 __ASSERT_DEBUG(! ScsFieldUsed(aFunction), ClientSidePanic(EScsClCancelSessUsedScs));
303 TInt f = ECancelSessionFunc | aFunction;
304 RSessionBase::SendReceive(f);