Usage of TwoPartyClient() for client callback mechanism

46 views
Skip to first unread message

Bhavya R

unread,
Oct 17, 2023, 7:37:10 AM10/17/23
to Cap'n Proto
Hello All,

I am trying to pass client capability to sever, client should receive  notify back  on any updates.
In my application, Service is sending statusHearbeat continuously, but client receives callback only when  it is waiting on any promise. Could anyone please guide on how to receives callback from server? My client application is running on windows.

For e.g  only duration when i am blocked on wait() call, i see callback gets received and dispatchCall is triggered

This call is done from client -> server
        auto request = connect->directorRef->versionRequest();
        auto responsePromise = request.send();
       auto response = responsePromise.wait(connect->clientRef->getWaitScope());

e.g interface FaTfServerToClient {
     statusHeartbeat   @0 () -> ();
}

interface FaTfClientToServer {
 version @0 () -> (version :Base.Version);
}

Client connection---> I am passing client bootstrap TransformEventHandler object


     auto address = connect->ioContext.provider->getNetwork()
                .parseAddress(ipAddress, port).wait(connect->ioContext.waitScope);
        connect->connection = address->connect().wait(connect->ioContext.waitScope);
        connect->clientRef = std::make_shared<capnp::TwoPartyClient>(*(connect->connection), kj::heap<TransformEventHandler>(observer),
            capnp::schemas::Side_9fd69ebc87b9719c::CLIENT);
        connect->directorRef = std::make_shared<FaTfClientToServer::Client>(connect->clientRef->bootstrap().castAs< FaTfClientToServer >());


class TransformEventHandler : public FaTfServerToClient::Server, public EventHandler {

::kj::Promise<void> statusHeartbeat(StatusHeartbeatContext context) override {
std::cout << "status heartbeat" << std::endl;
return kj::READY_NOW;
}
     }

I tried adding another iocontext, client in different thread and ran wait() on that. It doesnt receive nothing.
Looks like i haven't understood basic mechanism!! Any pointers would be very helpful. Thanks

Kenton Varda

unread,
Oct 24, 2023, 4:17:08 PM10/24/23
to Bhavya R, Cap'n Proto
This is expected. The KJ event loop runs when you call `.wait()`. The event loop checks for network events and delivers callbacks. If you haven't called .wait(), then the event loop has no way to run. KJ does not create any background threads for this purpose; the thread that created the event loop must call .wait() on a promise in order to run it. Usually, large programs will actually have a single top-level `.wait()` call that runs the entire program -- this might literally be `kj::NEVER_DONE.wait(waitScope)` to wait forever. In such programs, all actual work is done in `.then()` callbacks.

If you need to be able to receive KJ events at the same time as executing other code (especially bulk CPU-heavy work), then you will have to create a separate thread yourself, and figure out how to communicate between them. The KJ event loop is tied to a specific thread and cannot be directly accessed from other threads; however, you can use the `kj::Executor` interface to schedule work on another thread's event loop, or kj::newPromiseAndCrossThreadFulfiller() to create a promise/fulfiller pair that can be fulfilled from a different thread.

-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+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/97c4321a-fcf7-4904-a9fc-11ece2bc6aecn%40googlegroups.com.

Bhavya R

unread,
Oct 31, 2023, 6:13:12 AM10/31/23
to Cap'n Proto
Hello Kenton,

Thanks a lot for the input. kj::newPromiseAndCrossThreadFulfiller() approach works for me.
Reply all
Reply to author
Forward
0 new messages