FIDL 示例:计算器

此计算器示例展示了如何构建具有最基本功能的 FIDL 协议。您将在此示例的基础上进行构建,方法是修改可能会返回错误的方法、将一个 FIDL 协议与另一个协议组合在一起,以及展示其他原语的用法。

使用入门

在此示例中,您将创建一个基本的计算器服务器和客户端,其中展示了首先定义 FIDL 协议,然后提供和使用该协议所需的基本设置。

首先,您将定义接口定义和测试框架。接口定义(即 .fidl 文件本身)是任何新的 FIDL 协议的起点。此外,该计算器还包含必要的 CML 和 realm 定义,以创建可作为任意实现的工程脚手架的客户端-服务器模式。

以下是 FIDL 代码:

FIDL

// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// The namespace for this FIDL protocol. This namespace is how both consumers (clients) and providers (servers) reference this protocol.
library examples.calculator.baseline;

// @discoverable indicates 'Calculator' is a protocol that will be served under the examples.calculator.baseline libarary namespace. https://fuchsia.dev/fuchsia-src/reference/fidl/language/attributes#discoverable . If @discoverable is missing, it will lead to a compile time error when trying to import the library.
@discoverable
// A limited-functionality calculator 'protocol' that adds and subtracts integers.
open protocol Calculator {
    // Takes as input a struct with two integers, and returns their sum: (a+b)=sum.  This method is infallible (no errors can be generated) as two int32's cannot overflow a result type of int64.
    flexible Add(struct {
        a int32;
        b int32;
    }) -> (struct {
        sum int64;
    });
    // Takes as input a struct with two integers, and returns their difference: (a-b)=difference.  This method is infallible (no errors can be generated) as two int32's cannot overflow a result type of int64.
    flexible Subtract(struct {
        a int32;
        b int32;
    }) -> (struct {
        difference int64;
    });
};

CML

客户端

// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{
    include: [ "syslog/client.shard.cml" ],
    program: {
        runner: "elf",
        binary: "bin/client_bin",
    },
    use: [
        { protocol: "examples.calculator.baseline.Calculator" },
    ],
    config: {},
}

服务器

// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{
    include: [ "syslog/client.shard.cml" ],
    program: {
        runner: "elf",
        binary: "bin/server_bin",
    },
    capabilities: [
        { protocol: "examples.calculator.baseline.Calculator" },
    ],
    expose: [
        {
            protocol: "examples.calculator.baseline.Calculator",
            from: "self",
        },
    ],
}

Realm

// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{
    children: [
        {
            name: "client",
            url: "#meta/client.cm",
        },
        {
            name: "server",
            url: "#meta/server.cm",
        },
    ],
    offer: [
        // Route the protocol under test from the server to the client.
        {
            protocol: "examples.calculator.baseline.Calculator",
            from: "#server",
            to: "#client",
        },

        // Route diagnostics support to both children.
        {
            dictionary: "diagnostics",
            from: "parent",
            to: [
                "#client",
                "#server",
            ],
        },
    ],
}

然后,您可以使用任何支持的语言编写客户端和服务器实现:

Rust

客户端

// TODO(https://fxbug.dev/42063075): Rust implementation.

服务器

// TODO(https://fxbug.dev/42063075): Rust implementation.

C++(自然)

客户端

// TODO(https://fxbug.dev/42063075): C++ (Natural) implementation.

服务器

// TODO(https://fxbug.dev/42063075): C++ (Natural) implementation.

C++(线框)

客户端

// TODO(https://fxbug.dev/42063075): C++ (Wire) implementation.

服务器

// TODO(https://fxbug.dev/42063075): C++ (Wire) implementation.

HLCPP

客户端

// TODO(https://fxbug.dev/42063075): HLCPP implementation.

服务器

// TODO(https://fxbug.dev/42063075): HLCPP implementation.

从头开始创建 FIDL 协议(如本例所示)对于某些开发者(例如平台开发者)来说可能是一种更常见的方案。不过,即使其他类型的开发者通常不会构建 FIDL 协议,学习如何构建 FIDL 协议也会让他们受益。这有助于您了解 FIDL 的各个方面如何协同工作,包括语法、语法规则、语言功能、如何提供和使用给定的 FIDL 协议,以及构建系统如何运作。对于后续步骤,此基准之后的示例展示了如何扩展现有的 FIDL 协议,这预计会是一种相当常见的做法。

改进设计

以下各部分将探讨一种可能的方法,让您能够对原始设计进行迭代。每个示例并非依次构建,而是分别展示了如何修改或改进上述基本情况。