Asynchronous invocation of remote calls allow you to initate remote calls on one thread, and have them complete asynchronously on another thread.
Asynchronous invocation is implemented in RCF using the
RCF::Future<> class. If a
RCF::Future<> object is supplied as the return value, or as one of the parameters, of a remote call, the remote call is performed asynchronously.
For example, given this RCF interface:
, an asynchronous call would be initiated by the following code:
The thread that executes this code will return immediately. The code can subsequently poll for completion:
, or it can wait for completion:
Once the call is complete, the return values can be recovered from the relevant
If the call resulted in an error, the error will be thrown when the
Future<> instance is dereferenced, as above. Alternatively, the error can be retrieved by calling
Instead of polling or waiting, the thread that initiates an asynchronous remote call can provide a completion callback that will be called by the RCF runtime, on a background thread, when the call completes:
Notice that the
Future<> arguments are passed as arguments to the completion callback function.
Future<> objects are internally reference counted, and can be copied freely, while still referring to the same underlying value.
An asynchronous call that is in progress, can be canceled by disconnecting the client:
RcfClient is destroyed while an asynchronous call is in progress, the connection is automatically disconnected and any asynchronous operations are canceled.
On the server-side, RCF will normally dispatch a remote call on the same server thread that reads the remote call request from the transport. Asynchronous dispatching allows you to instead transfer the remote call over to other threads, freeing up the RCF server thread to proceed with other remote calls.
RCF::RemoteCallContext<> class is used to capture the server-side context of a remote call.
RCF::RemoteCallContext<> objects can be copied into queues and stored for later execution on arbitrary application threads.
RCF::RemoteCallContext<> objects are created from within the corresponding servant implementation method. For comparison, here is a non-asynchronously dispatched
To instead dispatch the call asynchronously, create a
RCF::RemoteCallContext<> object in
Print(), with template parameters corresponding to the method signature:
Once created, the
RCF::RemoteCallContext<> objects can be stored and copied like any other C++ object. When the
Print() function returns, RCF will not send a response to the client. The response will only be sent when
RCF::RemoteCallContext<>::commit() is called.
RCF::RemoteCallContext::parameters() provides access to all the parameters of the remote call, including the return value. The code sample above accesses an in-parameter:
, and subsequently sets an out-parameter (in this case the return value):