How to implement a simple client interface

Provides code snippets to help you to implement a simple client interface.

Client side session with a server

A client side session is represented by an instance of a class derived from RSessionBase which provides the behaviour for connecting to the server and sending messages to it.

In the following code fragment, taken from the example that can be found at ...\examples\Base\IPC\ClientServer\simple, the class RCountServSession, derived from RSessionBase, represents the client side session with a server. In the example, this may be referred to as the "count server". Note that sessions can not be shared in this example, and it just shows the basic mechanics of the client/server interaction.

class RCountServSession : public RSessionBase
    {
public:
    RCountServSession();
    TInt Connect();
    TVersion Version() const;
    TInt UnsupportedRequest();
    TInt SetFromString(const TDesC& aString);
    void Increase();
    void Decrease();
    void IncreaseBy(TInt anInt);
    void DecreaseBy(TInt anInt);
    void Reset();
    TInt CounterValue();
    void BadRequest();
    void Close();
private:
    RThread iServerThread;
    };

The important points are:

  • Use Connect() to start the count server. This calls RSessionBase::CreateSession() to create a session with the server. Note that in this simple example, the server is implemented as a separate thread, which is started by the example executable.

  • Use a client interface function, such as Increase(), to send a specific message to the server. The client interface function builds the message using the appropriate operation code and assembling a suitable TIpcArgs object, i.e. the object containing the integers and/or the descriptor pointers to the message arguments in the client address space.

As an example, the function SetFromString() is implemented as follows:

TInt RCountServSession::SetFromString(const TDesC& aString)
    {
    TIpcArgs args(&aString);
    return SendReceive(ECountServSetFromString, args);
    }

SendReceive() is called, specifying an operation code ECountServSetFromString and a TIpcArgs object containing argument values. In this case, there is only one argument - a pointer to a TDesC object containing the string to be passed to the server. Typically, operation codes are enum values defined in a header file visible to both the client interface and the server. Note that the client descriptor must remain in existence until the server request completes.