hti/HtiCommPlugins/HtiIPCommPlugin/src/HtiIPCommServerClient.cpp
changeset 36 813b186005b6
parent 30 86a2e675b80a
child 41 838cdffd57ce
equal deleted inserted replaced
30:86a2e675b80a 36:813b186005b6
     1 /*
       
     2 * Copyright (c) 2009 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:  Implementation of the client side interface to HtiIPCommServer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32uid.h>
       
    21 #include <HtiLogging.h>
       
    22 
       
    23 #include "HtiIPCommServerCommon.h"
       
    24 #include "HtiIPCommServerClient.h"
       
    25 
       
    26 // CONSTANTS
       
    27 const TUint KIPCommServerDefaultMessageSlots = 4; // Read, write, readCancel, writeCancel
       
    28 
       
    29 //---------------------------------------------------------------------------
       
    30 static TInt StartServer()
       
    31     {
       
    32     const TUidType serverUid( KNullUid, KNullUid, KIPCommServerUid3 );
       
    33 
       
    34     RProcess server;
       
    35     TInt r = server.Create( KIPCommServerName, KNullDesC, serverUid);
       
    36     if ( r != KErrNone )
       
    37         return r;
       
    38 
       
    39     TRequestStatus stat;
       
    40     server.Rendezvous( stat );
       
    41     if ( stat != KRequestPending )
       
    42         server.Kill(0);     // abort startup
       
    43     else
       
    44         server.Resume();    // logon OK - start the server
       
    45 
       
    46     User::WaitForRequest( stat );   //wait for start or death
       
    47 
       
    48     // we can't use the 'exit reason' if the server panicked as this
       
    49     // is the panic 'reason' and may be '0' which cannot be distinguished
       
    50     // from KErrNone
       
    51     r = ( server.ExitType() == EExitPanic ) ? KErrGeneral : stat.Int();
       
    52     server.Close();
       
    53     return r;
       
    54     }
       
    55 
       
    56 //---------------------------------------------------------------------------
       
    57 EXPORT_C RHtiIPCommServer::RHtiIPCommServer()
       
    58     {
       
    59     }
       
    60 
       
    61 //---------------------------------------------------------------------------
       
    62 EXPORT_C TInt RHtiIPCommServer::Connect()
       
    63     {
       
    64     HTI_LOG_TEXT( "RHtiIPCommServer::Connect" );
       
    65 
       
    66     // Connect to the server, attempting to start it if necessary
       
    67     TInt retry=2;
       
    68     for (;;)
       
    69         {
       
    70         TInt r = CreateSession( KIPCommServerName,
       
    71                                 Version(),
       
    72                                 KIPCommServerDefaultMessageSlots );
       
    73         HTI_LOG_FORMAT( "CreateSession %d", r );
       
    74         if ( r != KErrNotFound && r != KErrServerTerminated )
       
    75             return r;
       
    76 
       
    77         if (--retry==0)
       
    78             return r;
       
    79 
       
    80         r = StartServer();
       
    81         HTI_LOG_FORMAT( "StartServer %d", r );
       
    82         if ( r != KErrNone && r != KErrAlreadyExists )
       
    83             return r;
       
    84         }
       
    85     }
       
    86 
       
    87 //---------------------------------------------------------------------------
       
    88 EXPORT_C void RHtiIPCommServer::Close()
       
    89     {
       
    90     HTI_LOG_FUNC_IN( "RHtiIPCommServer::Close" );
       
    91 
       
    92     // Use thread finder to find the server thread
       
    93     TFullName threadName;
       
    94     TFullName matchPattern;
       
    95     matchPattern.Append( _L( "*" ) );
       
    96     matchPattern.Append( KIPCommServerName );
       
    97     matchPattern.Append( _L( "*" ) );
       
    98 
       
    99     TFindThread threadFinder;
       
   100     threadFinder.Find( matchPattern );
       
   101     TInt err = threadFinder.Next( threadName );
       
   102     if ( err )
       
   103         {
       
   104         HTI_LOG_FORMAT( "RHtiIPCommServer::Close: Failed to find server thread: %d", err );
       
   105         return;
       
   106         }
       
   107 
       
   108     HTI_LOG_TEXT( "RHtiIPCommServer::Close: Found server thread:" );
       
   109     HTI_LOG_DES( threadName );
       
   110 
       
   111     RThread thread;
       
   112     err = thread.Open( threadName );
       
   113     if ( err )
       
   114         {
       
   115         HTI_LOG_FORMAT( "RHtiIPCommServer::Close: Failed to open server thread: %d", err );
       
   116         }
       
   117     else
       
   118         {
       
   119         // Close this session. Causes a call to ~CHtiIPCommServerSession which should
       
   120         // stop the active scheduler in IPCommServer
       
   121         RSessionBase::Close();
       
   122 
       
   123         // For clean server stop, wait for its death
       
   124         HTI_LOG_TEXT( "RHtiIPCommServer::Close: Waiting for server thread to die..." );
       
   125         TRequestStatus status;
       
   126         thread.Logon( status );
       
   127         User::WaitForRequest( status );
       
   128         HTI_LOG_TEXT( "RHtiIPCommServer::Close: Server thread dead." );
       
   129         }
       
   130 
       
   131     thread.Close();
       
   132     HTI_LOG_FUNC_OUT( "RHtiIPCommServer::Close" );
       
   133     }
       
   134 
       
   135 //---------------------------------------------------------------------------
       
   136 EXPORT_C TVersion RHtiIPCommServer::Version(void) const
       
   137     {
       
   138     return ( TVersion( KIPCommServerMajorVersionNumber,
       
   139                        KIPCommServerMinorVersionNumber,
       
   140                        KIPCommServerBuildVersionNumber ) );
       
   141 
       
   142     }
       
   143 
       
   144 //---------------------------------------------------------------------------
       
   145 EXPORT_C TInt RHtiIPCommServer::GetSendBufferSize() const
       
   146     {
       
   147     return KIPCommServerSendBufferMaxSize;
       
   148     }
       
   149 
       
   150 //---------------------------------------------------------------------------
       
   151 EXPORT_C TInt RHtiIPCommServer::GetReceiveBufferSize() const
       
   152     {
       
   153     return KIPCommServerReceiveBufferMaxSize;
       
   154     }
       
   155 
       
   156 //---------------------------------------------------------------------------
       
   157 EXPORT_C void RHtiIPCommServer::Receive(TDes8& aData,
       
   158                                         TRequestStatus& aStatus)
       
   159     {
       
   160     SendReceive( EIPCommServerRecv, TIpcArgs( &aData ), aStatus );
       
   161     }
       
   162 
       
   163 //---------------------------------------------------------------------------
       
   164 EXPORT_C void RHtiIPCommServer::Send(const TDesC8& aData,
       
   165                                         TRequestStatus& aStatus)
       
   166     {
       
   167     SendReceive( EIPCommServerSend, TIpcArgs( &aData ), aStatus );
       
   168     }
       
   169 
       
   170 //---------------------------------------------------------------------------
       
   171 EXPORT_C void RHtiIPCommServer::CancelReceive()
       
   172     {
       
   173     SendReceive( EIPCommServerCancelRecv, TIpcArgs( NULL ) );
       
   174     }
       
   175 
       
   176 //---------------------------------------------------------------------------
       
   177 EXPORT_C void RHtiIPCommServer::CancelSend()
       
   178     {
       
   179     SendReceive( EIPCommServerCancelSend, TIpcArgs( NULL ) );
       
   180     }
       
   181 // End of the file