dosservices/dosserver/src/doscliserver.cpp
changeset 0 4e1aa6a622a0
child 34 b2f9f823b5fb
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2002 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 "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 *    Implementation for the RDosServer class.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <e32svr.h>
       
    21 #include "DosSvrServices.h"
       
    22 #include "dosclientserver.h"
       
    23 #include "dosserver.h"
       
    24 #include "dos_debug.h"
       
    25 
       
    26 // CONSTANTS
       
    27 const TUid KDosServerUid = {0x101f6efa};
       
    28 const TInt KConnectionTries = 5;
       
    29 #if defined(__WINS__)
       
    30 _LIT( KDosSrvLibName, "DosSrv.dll" );
       
    31 const TInt KDosSrvThreadStartPos = 103;
       
    32 #endif
       
    33 
       
    34 // ================= MEMBER FUNCTIONS =======================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // RDosServer::StartServer
       
    38 // Creates the server thread/process
       
    39 // -----------------------------------------------------------------------------
       
    40 EXPORT_C TInt RDosServer::StartServer() const
       
    41 {
       
    42     API_TRACE_( "[DOSSERVER] RDosServer::StartServer()" );
       
    43 
       
    44     TInt ret(KErrNone);
       
    45 	TRequestStatus status;
       
    46     // IPCv2: TSignal no longer used, but still passed to threadfunction as 
       
    47 	// otherwise API change would be required.
       
    48 	CDosServer::TSignal signal( status );
       
    49 
       
    50 	// Create startup semaphore
       
    51 	RSemaphore startupSemaphore;
       
    52 	ret = startupSemaphore.CreateGlobal( KServerStartupSemaphoreName, 0 );
       
    53 
       
    54 	if ( ret == KErrAlreadyExists )
       
    55 		{
       
    56 		// The server is starting up, but has not yet started 
       
    57 		startupSemaphore.OpenGlobal( KServerStartupSemaphoreName );
       
    58 		startupSemaphore.Wait(); // wait until the server has started up.
       
    59 		startupSemaphore.Close();
       
    60 		return ret;
       
    61 		}
       
    62 
       
    63 	// launch server thread (emulator) or process (Target platform)
       
    64 
       
    65 	RProcess server;
       
    66 
       
    67 	ret = server.Create( KDosServerExe, signal.Get(),
       
    68                        TUidType( KNullUid, KNullUid, KDosServerUid ),
       
    69                        EOwnerThread );
       
    70 
       
    71 	if ( ret )
       
    72 	{
       
    73 		startupSemaphore.Close();
       
    74 		return ret;
       
    75 	}
       
    76 
       
    77 	server.SetPriority(EPriorityHigh);
       
    78 
       
    79 	server.Resume();
       
    80 	server.Close();
       
    81 
       
    82 	startupSemaphore.Wait();
       
    83 	startupSemaphore.Close();
       
    84 
       
    85 	return KErrNone;
       
    86 }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // RDosServer::Connect
       
    90 // -----------------------------------------------------------------------------
       
    91 EXPORT_C TInt RDosServer::Connect()
       
    92 {
       
    93     API_TRACE_( "[DOSSERVER] RDosServer::Connect()" );
       
    94 
       
    95     TInt err( KErrNone );
       
    96 
       
    97     for ( TInt tries = 0; tries < KConnectionTries ; tries++ )
       
    98     {
       
    99     COM_TRACE_1( "[DOSSERVER]\t RDosServer::Connect() tries: %d", tries );
       
   100 
       
   101         err = CreateSession( KDosServerName, TVersion(1,0,0));
       
   102         COM_TRACE_1( "[DOSSERVER]\t RDosServer::Connect()-CreateSession err: %d", err );
       
   103 
       
   104         // Break if connected to existing server or if the problem is
       
   105         // other than missing server.
       
   106 
       
   107         if ( err == KErrNone || 
       
   108            ( err != KErrNotFound && err != KErrServerTerminated ) )
       
   109         {
       
   110             break;
       
   111         }
       
   112 
       
   113         // do not try to start server on last round (since if it failed before with KErrNone
       
   114         // (i.e. failure in server side on launch),
       
   115         // it is likely to fail again and reset the error code to KErrNone)
       
   116         if ( tries < (KConnectionTries - 1) )
       
   117 		{
       
   118 	        err = StartServer();    // try start a new server
       
   119             COM_TRACE_1( "[DOSSERVER]\t RDosServer::Connect()-StartServer err: %d", err );
       
   120 
       
   121 			// If server launched ok or someone else got to launch it first,
       
   122 			// try connect again.
       
   123 
       
   124 			if ( err != KErrNone && err != KErrAlreadyExists )
       
   125 			{
       
   126 				break; // server not launched: don't cycle round again
       
   127 			}
       
   128 		}
       
   129     }
       
   130 
       
   131     return err;
       
   132 }
       
   133 
       
   134 // FLAGS STRUCTURE (It always goes in the third (2) position of the parameter array)
       
   135 //
       
   136 // KAutoComplete:
       
   137 // 
       
   138 // Position: 0 0 0 0 0 0 0 x
       
   139 // Values:	0 -> AutoComplete is not active.
       
   140 //			1 -> AutoComplete is active.
       
   141 
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // RDosServer::Close()
       
   145 // Closes the session.
       
   146 // -----------------------------------------------------------------------------
       
   147 
       
   148 EXPORT_C void RDosServer::Close() 
       
   149 {
       
   150     API_TRACE_( "[DOSSERVER] RDosServer::Close()" );
       
   151 
       
   152     RSessionBase::Close();  // deletes session object
       
   153 }
       
   154 
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // RDosServer::ServerShutdown()
       
   158 // Shutdowns the server
       
   159 // -----------------------------------------------------------------------------
       
   160 
       
   161 EXPORT_C void RDosServer::ServerShutdown() const
       
   162 {
       
   163     API_TRACE_( "[DOSSERVER] RDosServer::ServerShutdown()" );
       
   164 
       
   165     Send(EServerShutdown, TIpcArgs(TIpcArgs::ENothing, TIpcArgs::ENothing, KAutoComplete));
       
   166 }
       
   167