blob: fc6080e01cf8772ce105b88baa00a64c07cd88c8 [file] [log] [blame]
Taylor Cramer096c1522023-07-25 18:44:16 +00001// Copyright 2023 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#include "pw_async_bench/callback_impl.h"
16
17namespace pw::async_bench {
18
19pw::Status ProxyEchoImpl::Echo(EchoRequest request, EchoResponder responder) {
20 // NOTE: this requires that `pw::Function` can capture the full `responder`
21 // object as well as any associated state (including possibly `this`)
22 // required for further steps. This also doesn't give us a way to cancel the
23 // ongoing request if something in `ProxyEchoImpl` goes down.
24 //
25 // We're also assuming here that each `Echo` call stores some state in the
26 // RPC system in order to track the ongoing call and the `pw::Function` we
27 // gave it.
28
29 // We have to allocate because `responder` doesn't fit in pw::Function.
30 auto responder_alloc = std::make_unique<EchoResponder>(std::move(responder));
31 pw::Status status = remote_->Echo(
32 std::move(request),
33 [responder_alloc = std::move(responder_alloc)](
34 pw::Result<EchoResponse> response) mutable -> void {
35 // Ignore the result of the send.
36 //
37 // This, too, requires that the RPC system allocates storage for the
38 // ongoing call and advances it. We also don't (naiively) have a way
39 // to cancel the call without some kind of handle.
40 responder_alloc->Send(std::move(response), [](pw::Status) {})
41 .IgnoreError();
42 });
43 if (status.ok()) {
44 // WHat would we do here? We can't use `responder` if it has already been
45 // moved into the callback above-- this stinks :(
46 // responder.Send(std::move(status), [](pw::Status) {}).IgnoreError();
47 }
48 return pw::OkStatus();
49}
50
Rob Mohr24d02192023-09-21 21:14:28 +000051} // namespace pw::async_bench