On Tue, Oct 3, 2017 at 1:01 PM, Richard Petri <[email protected]> wrote:

> On 10/02/2017 08:45 PM, Kenton Varda wrote:
> > In particular you'll want to use kj::setupAsyncIo() to get your initial
> > I/O context, and then use capnp::TwoPartyServer and
> > capnp::TwoPartyClient in <capnp/rpc-twoparty.h>.
>
> With this hint, I came up with the following:
>
>     kj::AsyncIoContext asyncio = kj::setupAsyncIo();
>     auto& waitScope = asyncio.waitScope;
>     auto& ioprovider = *asyncio.provider;
>     auto& network = ioprovider.getNetwork();
>     auto addr = network.parseAddress(SOCKET_FILE).wait(waitScope);
>     auto listener = addr->listen();
>     capnp::TwoPartyServer server(kj::heap<helloworld::Te
> stInterfaceImpl>());
>     auto serverPromise = server.listen(*listener);
>     // Run until SIGTERM
>     kj::UnixEventPort::captureSignal(SIGTERM);
>     asyncio.unixEventPort.onSignal(SIGTERM).wait(waitScope);
>     std::cout << "Shutting down server..." << std::endl;
>
> Works as far as I can see, thanks! From what I understand, the server
> will close the socket if the serverPromise will be destroyed?
>

Yes.

That said, consider joining the promises:

    asyncio.unixEventPort.onSignal(SIGTERM)
        .exclusiveJoin(kj::mv(serverPromise))
        .wait(waitScope);

A promise formed by exclusiveJoin() will finish (or throw) as soon as
either of its inputs finishes (or throws), automatically cancelling the
other input.

This way, if serverPromise throws an exception, it will propagate up rather
than sitting in limbo.

In general, a promise that is just sitting without being consumed is always
risky since you won't find out if it throws.

-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 [email protected].
Visit this group at https://groups.google.com/group/capnproto.

Reply via email to