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

00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include "threadclient.h"
00032 #include "threadserver.h"
00033 #include "threadclientserver.h"
00034 #include<e32debug.h>
00035 
00036 #define KMaxServerNameLength 256
00037 
00038 static const TUid KUidThreadServer = {0x10283039}; // Thread Server UID
00039 const TInt KCreateSessionRetryCount = 2;                        //CreateSession retry count
00040 const TInt KServerDefaultMessageSlots = 2;                      //default async message slots
00041 
00048 static TInt StartServer()
00049         {
00050         const TUidType serverUid(KNullUid,KNullUid,KUidThreadServer);
00051         TThreadFunction serverFunc = CThreadServer::StartThread;
00052         RThread server;
00053         
00054         //create a new thread
00055         TInt r = server.Create(_L(""), serverFunc, KThreadServerStackSize,
00056         #ifdef SYMBIAN_USE_SEPARATE_HEAPS
00057                                 KThreadServerInitHeapSize, KThreadServerMaxHeapSize,
00058         #else
00059                                                 &User::Heap(), //shared heap is now default
00060         #endif
00061                                 NULL,EOwnerProcess);
00062         
00063         if (r!=KErrNone)
00064                 return r;
00065         TRequestStatus stat;
00066         server.Rendezvous(stat);
00067         
00068         if (stat!=KRequestPending)
00069                 server.Kill(KErrCouldNotConnect);               // abort startup
00070         else
00071                 server.Resume();        // logon OK - start the server
00072         
00073         User::WaitForRequest(stat);             // wait for start or death
00074         // The server exit type may be a panic value and a panic value can be zero, which is the same value as KErrNone. 
00075         // So, the exit type is tested for a panic before being returned.
00076         r=(server.ExitType()==EExitPanic) ? KErrServerTerminated : stat.Int();
00077         server.Close();
00078         return r;
00079         }
00083 EXPORT_C TInt RThreadClient::Connect()
00084         {
00085         TInt retry=KCreateSessionRetryCount;
00086         FOREVER
00087                 {
00088                 // try to create a session with the server which has KServerDefaultMessageSlots async message slots
00089                 TInt r=CreateSession(KThreadServerName,
00090                                                          TVersion(KThreadServerVersion,
00091                                                                           KThreadServerMinorVersionNumber,
00092                                                                           KThreadServerBuildVersionNumber),
00093                                                          KServerDefaultMessageSlots);
00094                 if (r!=KErrNotFound && r!=KErrServerTerminated)
00095                         return r;
00096                 if (--retry==0)
00097                         return r;
00098                 r=StartServer();
00099                 if (r!=KErrNone && r!=KErrAlreadyExists)
00100                         return r;
00101                 // 1. r=KErrNone means Server starts up successfully
00102                 // 2. r=KErrAlreadyExists means duplicate server was trying to start up 
00103                 //              Note: if there are other functions called before CServer2::StartL() during construction,
00104                 //              other errors might happen when the duplicate server is trying to start
00105                 //              therefore, we recommend CServer2::StartL() is the first function in server construction
00106                 //              see CThreadServer::ConstructL()
00107         
00108                 // NOTE: If users would like to retry start up server after other scenarios happened, e.g. panic,then go 
00109                 //               through the following steps:
00110                 //                1. Increase the value of KCreateSessionRetryCount (only start up server once in this example)
00111                 //                2. Need another variable, e.g. TExitType, together with error code returned in StartServer()
00112                 //                      to distinguish these scenarios
00113                 //                3. modify the third if statement to stop exit when the required scenarios happens.
00114                 //           
00115                 }
00116         }
00121 EXPORT_C TInt RThreadClient::OpenDriver()
00122         {
00123         return SendReceive(EThreadServerOpenDriver);
00124         }
00130 EXPORT_C void RThreadClient::Send(const TDesC8& aBuf, TRequestStatus& aStatus)
00131         {
00132         SendReceive(EThreadServerSendData, TIpcArgs(&aBuf), aStatus);
00133         }
00137 EXPORT_C void RThreadClient::SendCancel()
00138         {
00139         SendReceive(EThreadServerSendDataCancel);
00140         }
00145 EXPORT_C TInt RThreadClient::UnloadDeviceDriver()
00146         {
00147         return SendReceive(EThreadServerUnloadDeviceDriver);
00148         }
00153 EXPORT_C TInt RThreadClient::LoadDeviceDriver()
00154         {
00155         return SendReceive(EThreadServerLoadDeviceDriver);
00156         }
00157 
00158 //EOF

Generated by  doxygen 1.6.2