Google is committed to advancing racial equity for Black communities. See how.

Responding to requests asynchronously in LLCPP

Prerequisites

This tutorial builds on the LLCPP getting started tutorials.

Overview

In the Echo implementation from the server tutorial, the server code responded to EchoString requests using the completer.

// An implementation of the Echo protocol. Protocols are implemented in LLCPP by
// creating a subclass of the ::Interface class for the protocol.
class EchoImpl final : public fidl::WireServer<fuchsia_examples::Echo> {
 public:
  // Bind this implementation to a channel.
  void Bind(std::unique_ptr<EchoImpl> self, async_dispatcher_t* dispatcher,
            fidl::ServerEnd<fuchsia_examples::Echo> request) {
    // Destroy |self| when the unbound handler completes.
    fidl::OnUnboundFn<EchoImpl> unbound_handler =
        [self = std::move(self)](EchoImpl* impl, fidl::UnbindInfo info,
                                 fidl::ServerEnd<fuchsia_examples::Echo> server_end) {
          switch (info.reason()) {
            case fidl::Reason::kClose:
            case fidl::Reason::kUnbind:
              // These are initiated by ourself.
              break;
            default:
              std::cerr << "server error: " << info << std::endl;
          }
        };
    binding_ = fidl::BindServer(dispatcher, std::move(request), this, std::move(unbound_handler));
  }

  // Handle a SendString request by sending on OnString event with the request value. For
  // fire and forget methods, the completer can be used to close the channel with an epitaph.
  void SendString(SendStringRequestView request, SendStringCompleter::Sync& completer) override {
    if (binding_) {
      binding_.value()->OnString(request->value);
    }
  }

  // Handle an EchoString request by responding with the request value. For two-way
  // methods, the completer is also used to send a response.
  void EchoString(EchoStringRequestView request, EchoStringCompleter::Sync& completer) override {
    completer.Reply(request->value);
  }

 private:
  // A reference back to the Binding that this class is bound to, which is used
  // to send events to the client.
  cpp17::optional<fidl::ServerBindingRef<fuchsia_examples::Echo>> binding_;
};

Notice that the type for the completer has ::Sync. This indicates the default mode of operation: the server must synchronously make a reply before returning from the handler function. Enforcing this allows optimizations since the bookkeeping metadata for making a reply can be stack-allocated.

This tutorial provides an example of how to respond to requests asynchronously, by converting the sync completer into an async completer.

The full example code for this tutorial is located at //examples/fidl/llcpp/async_completer.

The Echo protocol

This example uses the Echo protocol from the examples library:

@discoverable
protocol Echo {
    EchoString(struct {
        value string:MAX_STRING_LENGTH;
    }) -> (struct {
        response string:MAX_STRING_LENGTH;
    });
    SendString(struct {
        value string:MAX_STRING_LENGTH;
    });
    -> OnString(struct {
        response string:MAX_STRING_LENGTH;
    });
};

As part of this tutorial, you will implement a client that makes multiple EchoString requests in succession. The server will respond to these requests asynchronously, emulating a scenario where the server must execute a long running task before sending a response. By using an async completer, these long running tasks can be completed asynchronously.

Implement the client

The client code is mostly similar to the code from the client tutorial. The differences are highlighted in this section.

After connecting to the server, the client will make multiple EchoString requests inside of a for loop:

int main(int argc, const char** argv) {
  async::Loop loop(&kAsyncLoopConfigAttachToCurrentThread);
  async_dispatcher_t* dispatcher = loop.dispatcher();
  int num_responses = 0;

  auto client_end = service::Connect<fuchsia_examples::Echo>();
  ZX_ASSERT(client_end.is_ok());
  fidl::WireClient client(std::move(*client_end), dispatcher);

  auto start = time(nullptr);

  // Make |kNumEchoes| EchoString requests to the server, and print the result when
  // it is received.
  for (int i = 0; i < kNumEchoes; i++) {
    client->EchoString(
        "hello", [&](fidl::WireResponse<fuchsia_examples::Echo::EchoString>* response) {
          std::string reply(response->response.data(), response->response.size());
          std::cout << "Got response after " << time(nullptr) - start << " seconds" << std::endl;
          if (++num_responses == kNumEchoes) {
            loop.Quit();
          }
        });
  }

  loop.Run();
  return num_responses == kNumEchoes ? 0 : 1;
}

The loop is run kNumEchoes times (which is by default 3), and will print the time elapsed since the first request every time it receives a response. After it receives kNumEchoes responses, the code quits from the loop.

Implement the server

The main() function from the server code is the same as in the server tutorial. The difference lies in the implementation of Echo:

class EchoImpl final : public fidl::WireServer<fuchsia_examples::Echo> {
 public:
  explicit EchoImpl(async_dispatcher_t* dispatcher) : dispatcher_(dispatcher) {}
  // SendString is not used in this example, so requests are just ignored.
  void SendString(SendStringRequestView request, SendStringCompleter::Sync& completer) override {}
  void EchoString(EchoStringRequestView request, EchoStringCompleter::Sync& completer) override {
    // Respond to the request asynchronously by using ToAsync() and moving it into
    // a lambda capture. This allows multiple requests to EchoString to wait concurrently
    // rather than in sequence.
    async::PostDelayedTask(
        dispatcher_,
        [
            // The buffer referenced by the request view only lives until the end
            // of the |EchoString| call. Convert it to an owned value to use asynchronously.
            value_owned = std::string(request->value.get()),
            // The lambda capturing `completer` must be marked mutable, because making a
            // reply using the completer mutates it such that duplicate replies will panic.
            completer = completer.ToAsync()]() mutable {
          completer.Reply(fidl::StringView::FromExternal(value_owned));
        },
        zx::duration(ZX_SEC(5)));
  }

  // Contains a pointer to the dispatcher in order to wait asynchronously using
  // PostDelayedTask.
  async_dispatcher_t* dispatcher_;
};

When an EchoString request is received, the server calls async::PostDelayedTask. This function takes a dispatcher, a callback, and a duration, and will execute the callback at the end of the duration. This call emulates a long running task that returns its result through a callback when it is finished. The handler uses PostDelayedTask to wait 5 seconds before echoing the request value back to the client.

A key point is that the completer being moved into the lambda capture is the async completer. A ::Sync completer can be converted to the ::Async counterpart by using the ToAsync() method. For further information on the completer API, refer to the LLCPP bindings reference.

Another noteworthy aspect is that the request views provided to method handlers do not own the request message. In order to use the request parameters after the EchoString method returns, we need to copy relevant fields to an owned type, here value_owned in the lambda captures. For further information on the memory ownership, refer to the LLCPP Memory Management.

Run the example

First, build the code:

fx set core.x64 --with //examples/fidl/llcpp/async_completer/client --with //examples/fidl/llcpp/async_completer/server --with //examples/fidl/test:echo-launcher

fx build

Then run the example:

fx shell run fuchsia-pkg://fuchsia.com/echo-launcher#meta/launcher.cmx fuchsia-pkg://fuchsia.com/echo-llcpp-client-async#meta/echo-client.cmx fuchsia-pkg://fuchsia.com/echo-llcpp-server-async#meta/echo-server.cmx fuchsia.examples.Echo

You should see the following print output in the QEMU console (or using fx log):

[193539.863] 884542:884544> Running echo server
[193539.871] 884542:884544> echo_server_llcpp: Incoming connection for fuchsia.examples.Echo
[193544.899] 884632:884636> Got response after 5 seconds
[193544.899] 884632:884636> Got response after 5 seconds
[193544.899] 884632:884636> Got response after 5 seconds

By using the async completer, the client receives all 3 responses after 5 seconds, rather than in 5/10/15 seconds.