FIDL 示例:计算器

此计算器示例展示了如何使用基本功能构建 FIDL 协议。您将在此示例的基础上进一步构建:修改可能会返回错误的方法,将 FIDL 协议与另一种协议编写在一起,以及显示其他基元的使用情况。

使用入门

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

首先,您需要定义接口定义和自动化测试框架。接口定义(.fidl 文件本身)是任何新 FIDL 协议的起点。此外,该计算器包含创建客户端-服务器模式所需的 CML 和领域定义,可用作任意实现的项目基架。

有关 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",
        },
    ],
}

领域

// 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 logging support to all children.
        {
            protocol: [
                "fuchsia.inspect.InspectSink",
                "fuchsia.logger.LogSink",
            ],
            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++ (Wire)

客户端

// 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 协议,预计这是一种相当常见的做法。

改进设计

以下各部分探索了一种对原始设计进行迭代的潜在方式。它们不是依序构建,而是各具独立性,可用于修改或改进上述基本情况。