這個計算機範例會說明如何建構 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 diagnostics support to both children. { dictionary: "diagnostics", from: "parent", to: [ "#client", "#server", ], }, ], }
接著,您可以使用任何支援的語言編寫用戶端和伺服器實作項目:
荒漠油廠
用戶端
// 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 通訊協定,這應該是相當常見的做法。
改善設計
以下各節將探討一種可能的原始設計疊代方式。每個案例都提供獨立的修改或改善方式,而非循序建構。