jsonrpc-lean icon indicating copy to clipboard operation
jsonrpc-lean copied to clipboard

Asyncronous Server

Open semmel opened this issue 7 years ago • 1 comments

We use jsonrpc-lean to expose a C++ VoIP and SIP client via a Unix domain socket to a Node.js client on the same machine. The number of methods which need to be wired through JSON-RPC Api and the total count of calls can be reduced if longer running methods (like playing a WAV file over VoIP) are permitted to run as background threads thus not blocking the JSON-RPC server for parallel commands and without polling responding to the JSON-RPC request after the time the operation completed.

In single-threaded JavaScript one uses callbacks which are nicely wrapped by Promises. C++ Futures with TS for Concurrency for C++14 together with C++ Promises wrap C++ threads very similar to JS Promises. Both make creating and consuming background computations easy. Unfortunately the JSPromise-like C++ Future::then callback interface is still on the roadmap for the C++ standard. Thus the introduced dependency on the "polyfill" in boost::thread makes the setup of jsonrpc-lean more complicated. I.e. boost::thread needs to be available as a library.

The implementation of asynchronous server request offered here is far from perfect. Mostly because I couldn't figure out easy how the intricate template machinery for storing the strongly typed methods in their return values and parameters by the Dispatcher works. However, I attempted to minimise the changes to the public and internal jsonrpc-lean API. Except for the problem of future-returning Lambdas (see the section in the Readme) I just added the single new method Dispatcher::asyncHandleRequest.

semmel avatar Jul 27 '18 13:07 semmel

Thia is indeed a nice addition. I will dive in all the changes you posted to see what I can merge, without breaking the basic requirements of this project. Thank you!

uskr avatar Jul 27 '18 14:07 uskr