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 llcpp::fuchsia::examples::Echo::Interface {
 public:
  // Bind this implementation to a channel.
  zx_status_t Bind(async_dispatcher_t* dispatcher, zx::channel request) {
    auto result = fidl::BindServer(dispatcher, std::move(request), this);
    if (!result.is_ok()) {
      return result.error();
    }
    binding_ = result.take_value();
    return ZX_OK;
  }

  // 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(fidl::StringView value, SendStringCompleter::Sync& completer) override {
    if (binding_) {
      binding_.value()->OnString(std::move(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(fidl::StringView value, EchoStringCompleter::Sync& completer) override {
    completer.Reply(std::move(value));
  }

  // A reference back to the Binding that this class is bound to, which is used
  // to send events to the client.
  fit::optional<fidl::ServerBindingRef<llcpp::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(string:MAX_STRING_LENGTH value) -> (string:MAX_STRING_LENGTH response);
    SendString(string:MAX_STRING_LENGTH value);
    -> OnString(string:MAX_STRING_LENGTH response);
};

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 svc = get_svc_directory();

  zx::channel server_end, client_end;
  ZX_ASSERT(zx::channel::create(0, &client_end, &server_end) == ZX_OK);
  ZX_ASSERT(fdio_service_connect_at(svc.get(), "fuchsia.examples.Echo", server_end.release()) ==
            ZX_OK);
  fidl::Client<llcpp::fuchsia::examples::Echo> 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::StringView resp) {
      std::string reply(resp.data(), resp.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 llcpp::fuchsia::examples::Echo::Interface {
 public:
  explicit EchoImpl(async_dispatcher_t* dispatcher) : dispatcher_(dispatcher) {}
  // SendString is not used in this example, so requests are just ignored.
  void SendString(fidl::StringView value, SendStringCompleter::Sync& completer) override {}
  void EchoString(fidl::StringView value, 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 lambda capturing `completer` must be marked mutable, because making a
        // reply using the completer mutates it such that duplicate repies will panic.
        [value = std::move(value), completer = completer.ToAsync()]() mutable {
          completer.Reply(std::move(value));
        },
        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.

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.