這個計算機範例說明如何建構具備最基本功能的 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 通訊協定,這應該是相當常見的做法。
改善設計
以下各節將探討可用於原始設計的疊代方法。每個案例都會以獨立的方式呈現,而非依序建立,以便修改或改善上述基本案例。