03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The message for a synchronous function must be completed synchronously by the server's<br />

handler function.<br />

We saw in the previous chapter that any asynchronous request must be completed precisely<br />

once – whatever the circumstances. The kernel guarantees that even if the server thread<br />

dies an asynchronous request by a client is completed as part of the kernel's thread-death<br />

cleanup.<br />

We also saw that any API that provides asynchronous request functions should also offer<br />

corresponding cancel functions: so cancel functions too, are often present in servers' client<br />

APIs.<br />

18.1.5 Ending a Session and Cleanup after Client Death<br />

When a client has finished with a client-server session, it should end it with<br />

RSessionBase::Close(). This sends a disconnect message to the server, which<br />

responds by simply destroying its end of the session: by calling the destructor of the<br />

CSharableSession-derived object. Then, the kernel destroys its representation of the<br />

session. The handle of the client-side RSessionBase is set to zero.<br />

It is safe for a client to call Close() on the RSessionBase again: when the handle is zero,<br />

Close() does nothing.<br />

If the client thread dies, the kernel will perform thread-death cleanup and send a disconnect<br />

message to the server end of all the sessions for which it was a client thread. Thus, the<br />

server-side end of the session is destroyed, even if the client thread dies.<br />

Servers must perform effective cleanup. When a CSession/CSharableSession object is<br />

destroyed, any resources associated with it should also be destroyed. Usually, this is just<br />

standard C++ destructor processing.<br />

18.1.6 Cleanup after Server Death<br />

Servers should be written with the greatest care, so they do not terminate prematurely. But if<br />

a server does die, then the kernel gives the opportunity for clients to recover. Any<br />

outstanding messages from asynchronous requests will be completed with the KErrDied<br />

return code.<br />

After a server has died, the client should clean up all RSession-Base objects relating to<br />

that server. Any SendReceive() issued on an RSessionBase to a dead server will result<br />

in a completion code of KErrServerTerminated. This will be the case even if a new<br />

instance of the server is started: old sessions will not be reconnected.<br />

18.1.7 Handling Multiple Objects from One Session<br />

Sometimes it is useful for a single client-side thread to have multiple sessions with a given<br />

server. A good example of this is the socket server; each socket created by the client could<br />

be a separate session. However, creating a new session consumes resources in the server<br />

and in the kernel to support it. Therefore, creating many sessions for one client thread may<br />

be inefficient. An alternative approach is provided by the use of subsessions (see Figure<br />

18.4). In general, if your client thread needs multiple sessions, try to use subsessions.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!