Asynchronous RPC calls using Cap'n Proto

344 views
Skip to first unread message

igor Nikolaychuk

unread,
Mar 5, 2017, 4:46:44 AM3/5/17
to Cap'n Proto
Is it possible to run Cap'n Proto RPC client and use it from another threads to perform asynchronous RPC calls?
I've written the following client behavior, how to make the code work if it's possible?

capnp::EzRpcClient client(argv[1], 5923);
auto& waitScope = client.getWaitScope();
Directory::Client cap = client.getMain<Directory>();

std::thread([&]() {
while(true) {
auto request = cap.openRequest();
request.setName("test");
auto promise = request.send();
promise.then([](capnp::Response<Directory::OpenResults>&& r) {
std::cout << r.getTest().cStr() << std::endl;
});
sleep(1);
}
}).detach();

kj::NEVER_DONE.wait(waitScope)

Also I didn't find any example of asynchronous responding. How to implement the following server behavior?

class DirectoryImpl final: public Directory::Server {
public:
kj::Promise<void> open(OpenContext context) override {
asio_io_service.post([=](){
             // Fetching information on another thread
              capnproto_io_service.post([=](){
                             context.getResults().setFile("lol");
                             //TODO: Sending the result to the client
              });
        });
}
};

Kenton Varda

unread,
Mar 12, 2017, 12:14:56 AM3/12/17
to igor Nikolaychuk, Cap'n Proto
Hi Igor,

Cap'n Proto RPC is currently single-threaded. You can only use a capability client in the thread that created it. If you want to make requests from multiple threads, each thread will need to form its own request to the server. (This is arguably more efficient anyway since it avoids synchronization costs from a shared connection.)

For the second part of your question, you may want to look at kj::newPromiseAndFulfiller().

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/capnproto.

Reply all
Reply to author
Forward
0 new messages