Summary: Asynchronous Remote Procedure Call (RPC) is an extension of the synchronous RPC mechanism This article discusses asynchronous RPC in detail and includes a comparison between the two (7 printed pages)..
Contents
What Is Async RPC? Sync vs. Async on the ClientSync vs. Async on the ServerTransport ProtocolsWhen To Use Async RPCFeatures and SemanticsChanging an Existing RPC Client or ServerOverhead and PerformanceCall Complete Notification with Async RPCConclusion
What is async rpc?
Asynchronous Remote Procedure Call (RPC) is an extension of the synchronous RPC mechanism. In synchronous RPC the thread that issued the RPC call blocks the client until the RPC call is complete. Async RPC allows the thread that issued the call to continue execution and pick up the results at a later time. Similarly, on an async RPC server the logical RPC call can continue even after the dispatched call returns from the application code into the RPC runtime (manager routine), as shown in Figure 2.
Figure 1. Synchronous RPC
Call is issued by the application. The RPC runtime sends the call to the server, on behalf of the client. Meanwhile, the client thread that issued the RPC call is stuck in the RPC runtime waiting for the call to complete. The call is dispatched by the server side RPC runtime. The server application then executes the remote call. Control returns back to the server RPC runtime. Server RPC runtime sends the reply to client. The client thread unblocks. The RPC call is complete.
Figure 2. Async RPC
The client submits Async RPC call. Control returns back to the client thread. The thread is free to continue with other work. RPC runtime sends request to the server, on behalf of the client. Request is dispatched to the server. The server application begins execution of the remote call. Control returns back to the server RPC runtime, but the server side call is not complete. Server completes the call. Reply is sent back to the client. Client is notified that reply has arrived. Client calls back into the RPC Runtime and Picks Up The Reply. At this Point, The Async RPC IS Complete.The Following Pseudocode (P-CODE) Illustrate The Difference Between The Two.
Sync vs. async on the client
Synchronous RPC
Function Callfoo ()
{
.
// Make the synchronous rpc call
X = foo (a, b, c);
// WHEN We get here, The Synchronous RPC CALL IS Complete.
// the return value and the [out] parameters are Valid.
}
Asynchronous RPC
Function Submitfoo ()
{
.
// Submit The Async RPC Call
// Initialize the async handle
RpcasyncinitializeHandle (Pasync);
FOO (Pasync, A, B, C);
.
// DO Other Work
}
Function Foocomplete ()
{
// when the app is notified the Above RPC Call is Complete
Status = rpcasync.completecall (Pasync, & x);
// Return Value and Out Parameters Are Valid Here.
}
Sync vs. async on the server
Synchronous RPC
Foomanagerroutine ()
{
// this is Remote Application Code.
// Do The Work and Fall Back Into The RPC Runtime.
}
Asynchronous RPC
FooasyncManagerRoutine (Pasync)
{
// this is where the logical call starts
// do some work
// Fall Back Into The RPC Runtime. The Logical RPC CALL IS STILL NOT COMPLETE
//
}
CompletefooasyncCall ()
{
// the logical call is about to be complete
Status = rpcasynccompletecall (pasync, & retval); // The logical call is complete. The async handle HAS Become Invalid
}
Transport Protocols
ASYNC RPC is supported on All Transport Protocols on Which Sync RPC is currently support.
When to use async rpc
Client
IT is useful to use async rpc on the client in The Following Cases:
The thread is a UI thread, and it can not afford to block on the RPC (because the UI will freeze). When a thread makes a synchronous RPC call, it is stuck in the RPC runtime until the RPC call completes. If the Thread That Is Making The RPC Call Can't Be Making A Synchronous RPC Call. for Example, IF The Thread Is A Ui Thread (That IS, IT HAS A Message Pump, IT Cannot Afford To block in an RPC. If it does, the UI will hang until the RPC is complete. The remote procedure call will take a while to complete and the client can do other work on the thread before it needs the results of this RPC. The client needs to make simultaneous calls to one or more servers. Using async RPC is much better than spinning off threads to do the same. For example, if a client wants to make simultaneous synchronous RPC calls to four servers, it can not do so with one thread . It has to spin off off at Least Three Threads and make an rpc call in each thread. However, if it is using async .
Server
IT is useful to use async rpc on the server in The Following Cases:
The processing of the call will take a long time to complete (especially if it blocks). Instead of just processing the call in the dispatch (as with sync RPC), the app can use async RPC on the server. When the call is received , it can be added to a work queue. When the work is complete, the async RPC can be completed (see Figure 2). If synchronous RPC is used, the server application will have a thread used up for every dispatched RPC call. The processing of the call depends on another asynchronous task. Instead of synchronously waiting for the task (as with synchronous RPC), the application can use async RPC. When an async RPC call is dispatched, the application can initiate the asynchronous task. The app can The use the completion of the asynchronous task to drive the completion of the async rpc.features and semantics
ASYNC RPC Supports All Features Supported by Synchronous RPC. However, There Area Some New / Different Semantics Associated with Using Async RPC, INCLUDING:
Cancels in async RPC work differently. (See the Asynchronous RPC section in the Platform SDK located in the MSDN Library for details.) Distributed computing environment (DCE) pipes work differently on async RPC. (See the Asynchronous RPC section in the Platform SDK located in the MSDN Library for details.) Async RPC enforces causal ordering of remote procedure calls. If a thread issues three async RPC calls-A, B, C, in that order-the remote procedure call runtime will make sure they are dispatched in the Order A, B, C on The Server. This Does Not Apply to Synchronous Calls Because Sync RPCS Are Blocking. The Causeal Order Semantics Can Be Turned Off.
Changing an existing rpc client or server
Async RPC is Wire-Compatible with Synchronous RPC. You can change the client or the server to asking clients and servers.overhead and perform
The overhead associated with each outstanding async RPC call is minimal. An async RPC call is 20-30 percent slower than a sync call. Async RPC should not be used as a direct replacement for sync RPC. It should be used when the situation warrants its use (see the section When To use Async RPC earlier in this article). Although async RPC is slower in terms of time taken for a single call, overall throughput can be improved significantly by efficient usage (for example, with overlapped calls).
Call Complete Notification with Async RPC
Async RPC Offers Five DiffERENT METHODS for Getting Call Complete Notification. The Choice of Which One To Use Depends on The Design of the Application Using Async RPC. The Five Methods Include:
Window handlesThis method is useful when the app has a UI thread and it is already handling different kinds of events in its WndProc, the function that handles dispatched window messages. APCs (asynchronous procedure calls) If the application is going to be in a wait state on the thread that issued the async RPC call, using APCs might be good. PollingThis method is useful if the thread is actively doing work and does not want to block. EventsThis method is useful when the application want to do some work and block on one Or More Async RPCS. I / O Completion Portsthis Method Is Useful When THE APP IS ALREADY USING I / O Completion Ports, And Wants To Be Notified of Call Completions On That Port.
Conclusion