此计算器示例展示了如何构建具有最低限度功能的 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", }, ], }
领域
// 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++ (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 协议,这应该是一种相当常见的做法。
改进设计
以下各部分分别探讨了对原始设计进行迭代的一种潜在方法。它们各自提供了一种独立的方式来修改或改进上述基本示例,而不是依次构建。