在计算器实例中,我在capnp文件中添加了一个新接口,如下所示:
interface Main {
getCalculator @0 () -> (calculator :Calculator);
}我的目标是通过一个实现Main使用多个接口。我怎样才能做到这一点?额外的客户端代码如下(当我尝试使用calculator时,它抛出一个空功能异常):
capnp::EzRpcClient client1(argv[1]);
Main::Client main = client1.getMain<Main>();
auto& waitScope1 = client1.getWaitScope();
auto request1 = main.getCalculatorRequest();
auto evalPromise1 = request1.send();
auto response1 = evalPromise1.wait(waitScope1);
auto calculator = response1.getCalculator();
auto request = calculator.evaluateRequest();
request.getExpression().setLiteral(123);
// Send it, which returns a promise for the result (without blocking).
auto evalPromise = request.send();
// Using the promise, create a pipelined request to call read() on the
// returned object, and then send that.
auto readPromise = evalPromise.getValue().readRequest().send();
// Now that we've sent all the requests, wait for the response. Until this
// point, we haven't waited at all!
auto response = readPromise.wait(waitScope1);
KJ_ASSERT(response.getValue() == 123);服务器的主要实现文件的更改如下:
class MainImpl : public Main::Server {
public:
kj::Promise<void> getCalculator(GetCalculatorContext context) override {
context.getResults();
return kj::READY_NOW;
}
};服务器的主要功能是提供MainImpl服务。我确实得到了计算器对象,但是如何进一步调用该对象的方法呢?
发布于 2022-10-06 11:12:20
服务器端的getCalculator()函数现在什么也不做。如果希望Calculator返回到客户端,则需要实际设置结果。这就是为什么您会得到“空功能异常”。
就像这样:
class MainImpl : public Main::Server {
public:
kj::Promise<void> getCalculator(GetCalculatorContext context) override {
context.getResults().setCalculator(<the calculator capability>);
return kj::READY_NOW;
}
};这将要求您在服务器端创建Calculator客户机(即不再在客户端执行Calculator::Client calculator = client.getMain<Calculator>();,因为calculator将来自getCalculator())。
https://stackoverflow.com/questions/73971865
复制相似问题