examples/Base/IPC/ClientServer/Gettingstarted/transient/src/client.cpp

00001 // Copyright (c) 2000-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 // Transient server example - client interface implementation
00015 //
00016 
00017 #include <t-client.h>
00018 #include "clientserver.h"
00019 
00020 static TInt StartServer()
00021 //
00022 // Start the server process. Simultaneous launching
00023 // of two such processes should be detected when the second one attempts to
00024 // create the server object, failing with KErrAlreadyExists.
00025 //
00026         {
00027         const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
00028         RProcess server;
00029         TInt r=server.Create(KMyServerImg,KNullDesC,serverUid);
00030         if (r!=KErrNone)
00031                 return r;
00032         TRequestStatus stat;
00033         server.Rendezvous(stat);
00034         if (stat!=KRequestPending)
00035                 server.Kill(0);         // abort startup
00036         else
00037                 server.Resume();        // logon OK - start the server
00038         User::WaitForRequest(stat);             // wait for start or death
00039         // we can't use the 'exit reason' if the server panicked as this
00040         // is the panic 'reason' and may be '0' which cannot be distinguished
00041         // from KErrNone
00042         r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
00043         server.Close();
00044         return r;
00045         }
00046 
00047 EXPORT_C TInt RMySession::Connect()
00048 //
00049 // Connect to the server, attempting to start it if necessary
00050 //
00051         {
00052         TInt retry=2;
00053         for (;;)
00054                 {
00055                 TInt r=CreateSession(KMyServerName,TVersion(0,0,0),1);
00056                 if (r!=KErrNotFound && r!=KErrServerTerminated)
00057                         return r;
00058                 if (--retry==0)
00059                         return r;
00060                 r=StartServer();
00061                 if (r!=KErrNone && r!=KErrAlreadyExists)
00062                         return r;
00063                 }
00064         }
00065 
00066 EXPORT_C TInt RMySession::Send(const TDesC& aMessage)
00067         {
00068         if (aMessage.Length()>KMaxMyMessage)
00069                 return KErrArgument;
00070         return SendReceive(ESend,TIpcArgs(&aMessage));
00071         }
00072 
00073 EXPORT_C void RMySession::Receive(TRequestStatus& aStatus,TDes& aMessage)
00074         {
00075         SendReceive(EReceive,TIpcArgs(&aMessage,aMessage.MaxLength()),aStatus);
00076         }
00077 
00078 EXPORT_C void RMySession::CancelReceive()
00079         {
00080         SendReceive(ECancelReceive);
00081         }

Generated by  doxygen 1.6.2