examples/Base/IPC/AdvancedClientServerExample/ThreadClient/src/threadclient.cpp

00001 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include "threadclient.h"
00017 #include "threadserver.h"
00018 #include "threadclientserver.h"
00019 #include<e32debug.h>
00020 
00021 #define KMaxServerNameLength 256
00022 
00023 static const TUid KUidThreadServer = {0x10283039}; // Thread Server UID
00024 const TInt KCreateSessionRetryCount = 2;                        //CreateSession retry count
00025 const TInt KServerDefaultMessageSlots = 2;                      //default async message slots
00026 
00033 static TInt StartServer()
00034         {
00035         const TUidType serverUid(KNullUid,KNullUid,KUidThreadServer);
00036         TThreadFunction serverFunc = CThreadServer::StartThread;
00037         RThread server;
00038         
00039         //create a new thread
00040         TInt r = server.Create(_L(""), serverFunc, KThreadServerStackSize,
00041         #ifdef SYMBIAN_USE_SEPARATE_HEAPS
00042                                 KThreadServerInitHeapSize, KThreadServerMaxHeapSize,
00043         #else
00044                                                 &User::Heap(), //shared heap is now default
00045         #endif
00046                                 NULL,EOwnerProcess);
00047         
00048         if (r!=KErrNone)
00049                 return r;
00050         TRequestStatus stat;
00051         server.Rendezvous(stat);
00052         
00053         if (stat!=KRequestPending)
00054                 server.Kill(KErrCouldNotConnect);               // abort startup
00055         else
00056                 server.Resume();        // logon OK - start the server
00057         
00058         User::WaitForRequest(stat);             // wait for start or death
00059         // The server exit type may be a panic value and a panic value can be zero, which is the same value as KErrNone. 
00060         // So, the exit type is tested for a panic before being returned.
00061         r=(server.ExitType()==EExitPanic) ? KErrServerTerminated : stat.Int();
00062         server.Close();
00063         return r;
00064         }
00068 EXPORT_C TInt RThreadClient::Connect()
00069         {
00070         TInt retry=KCreateSessionRetryCount;
00071         FOREVER
00072                 {
00073                 // try to create a session with the server which has KServerDefaultMessageSlots async message slots
00074                 TInt r=CreateSession(KThreadServerName,
00075                                                          TVersion(KThreadServerVersion,
00076                                                                           KThreadServerMinorVersionNumber,
00077                                                                           KThreadServerBuildVersionNumber),
00078                                                          KServerDefaultMessageSlots);
00079                 if (r!=KErrNotFound && r!=KErrServerTerminated)
00080                         return r;
00081                 if (--retry==0)
00082                         return r;
00083                 r=StartServer();
00084                 if (r!=KErrNone && r!=KErrAlreadyExists)
00085                         return r;
00086                 // 1. r=KErrNone means Server starts up successfully
00087                 // 2. r=KErrAlreadyExists means duplicate server was trying to start up 
00088                 //              Note: if there are other functions called before CServer2::StartL() during construction,
00089                 //              other errors might happen when the duplicate server is trying to start
00090                 //              therefore, we recommend CServer2::StartL() is the first function in server construction
00091                 //              see CThreadServer::ConstructL()
00092         
00093                 // NOTE: If users would like to retry start up server after other scenarios happened, e.g. panic,then go 
00094                 //               through the following steps:
00095                 //                1. Increase the value of KCreateSessionRetryCount (only start up server once in this example)
00096                 //                2. Need another variable, e.g. TExitType, together with error code returned in StartServer()
00097                 //                      to distinguish these scenarios
00098                 //                3. modify the third if statement to stop exit when the required scenarios happens.
00099                 //           
00100                 }
00101         }
00106 EXPORT_C TInt RThreadClient::OpenDriver()
00107         {
00108         return SendReceive(EThreadServerOpenDriver);
00109         }
00115 EXPORT_C void RThreadClient::Send(const TDesC8& aBuf, TRequestStatus& aStatus)
00116         {
00117         SendReceive(EThreadServerSendData, TIpcArgs(&aBuf), aStatus);
00118         }
00122 EXPORT_C void RThreadClient::SendCancel()
00123         {
00124         SendReceive(EThreadServerSendDataCancel);
00125         }
00130 EXPORT_C TInt RThreadClient::UnloadDeviceDriver()
00131         {
00132         return SendReceive(EThreadServerUnloadDeviceDriver);
00133         }
00138 EXPORT_C TInt RThreadClient::LoadDeviceDriver()
00139         {
00140         return SendReceive(EThreadServerLoadDeviceDriver);
00141         }
00142 
00143 //EOF

Generated by  doxygen 1.6.2