Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



How to implement a server interface with subsessions

A server side subsession is represented by an instance of a class derived from CObject.

The following sections refer to an example server, known as the count server.

In the example, a count server session is represented by the CSession derived class CCountServSession. The server session can have any number of subsessions, referred to as counters in the example. The subsessions are instances of the CCounter class and these are derived from CObject.

Unlike the implementation for a simple server interface, the functions to service client requests, i.e. Increase(), are in class CCounter rather than CCountServSession.

The important points to note are:


See also

[Top]


Server session representation

The CCountServSession class is defined as:

class CCountServSession : public CSession
 {
public:
 static CCountServSession* NewL(RThread& aClient, CCountServServer* aServer);
 CCountServSession(RThread& aClient);
 void ConstructL(CCountServServer* aServer);
 void CloseSession();
 CCounter* CounterFromHandle(TUint aHandle);
 void ServiceL(const RMessage& aMessage);
 void DispatchMessageL(const RMessage& aMessage);
 void NewCounterL();
 void DeleteCounter(TUint aHandle);
 void NumResources();
 TInt CountResources();
 void Write(const TAny* aPtr,const TDesC8& aDes,TInt anOffset=0);
 void PanicClient(TInt aPanic) const;
private:
 CObjectCon *iContainer;
 CObjectIx* iCounters;
 CCountServServer *iCountSvr;
 TInt iResourceCount;
 };


Notes

The function NewCounterL() has the following implementation:

void CCountServSession::NewCounterL()
 {
 CCounter* counter=CCounter::NewL(this);
 iContainer->AddL(counter);
 TInt handle=iCounters->AddL(counter);
 TPckg<TInt> handlePckg(handle);
 TRAPD(res,WriteL(Message().Ptr3(),handlePckg));
 if (res!=KErrNone)
  {
  iCounters->Remove(handle);
  PanicClient(EBadDescriptor);
  return;
  }
 iResourceCount++;
 }


Notes

[Top]


Subsession object representation

The CCounter class which represents the subsession is defined as:

class CCounter : public CObject
 {
public:
 static CCounter * NewL(CCountServSession* aSession);
 void ConstructL(CCountServSession *aSession);
 void SetFromString(const RMessage &aMessage);
 void Increase();
 void IncreaseBy();
 void Decrease();
 void DecreaseBy();
 void Reset();
 void CounterValue();
 void CloseCounter();
 const RMessage& Message() const;
protected:
 CCountServSession *iSession;
private:
  TInt iCount;
 };


Notes

[Top]


Implementing a subsession request

Subsession requests are handled in a similar way to session requests.

A subsession request is initially handled by the associated session, i.e. it is passed to the appropriate CSession::ServiceL().

void CCountServSession::ServiceL(const RMessage& aMessage)
 {
 TRAPD(err,DispatchMessageL(aMessage));
 aMessage.Complete(err);
 }

The appropriate service function is called via DispatchMessageL() and the asynchronous request is completed with aMessage.Complete(). This applies to messages targeted at sessions and subsessions.


DispatchMessageL()

The following code fragment shows important parts of this function:

void CCountServSession::DispatchMessageL(const RMessage& aMessage)
 {
 switch (aMessage.Function())
  {
 case ECountServCreateSubSession:// Request to create a subsession
  NewCounterL();
  return;
 case ECountServCloseSession: // Request to delete a subsession
  CloseSession();
  return;
  ...
  }
                             // Must be a subsession request
                             // Find out Which subsession and
                             // forward the request to it.
 CCounter* counter=CounterFromHandle(aMessage.Int3());
 switch (aMessage.Function())
  {
  ...
 case ECountServValue:
  counter->CounterValue();
  return;
 default:
  PanicClient(EBadRequest);
  return;
  }

}

CCounter* CCountServSession::CounterFromHandle(TUint aHandle)
 {
 CCounter* counter = (CCounter*)iCounters->At(aHandle);
 if (counter == NULL)
  {
  ... // bad subsession handle
  }
 return counter;
 }

Notes