FIDL 示例:计算器

此计算器示例显示如何使用 最基本的功能。您将通过修改 使用另一个 FIDL 协议编写 FIDL 协议, 并显示其他基元的用法。

使用入门

在此示例中,您将创建一个基本的计算器服务器,显示 先定义然后传送和使用 FIDL 协议所需的基本设置。

首先,您需要定义接口定义和自动化测试框架。通过 接口定义(.fidl 文件本身)是创建新的 API 时 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++(有线)

客户端

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

改进设计

以下每个部分都探讨了一种迭代方法, 原始设计。无需依序构建,每个 为上述基本案例提供了一种独立的方式, 修改或改进。