這個計算機範例顯示如何建構具有最低限度功能的 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; }); };
行銷長
用戶端
// 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.
HTTP 即時串流 (HLP)
用戶端
// TODO(https://fxbug.dev/42063075): HLCPP implementation.
伺服器
// TODO(https://fxbug.dev/42063075): HLCPP implementation.
對特定開發人員 (例如平台開發人員) 來說,從頭開始建立 FIDL 通訊協定可能會是較常見的情況,如本例所示。然而,其他類型的開發人員也能受益於學習如何建構 FIDL 通訊協定,即使他們通常不太願意使用。這可協助您瞭解 FIDL 的一切如何相輔相成,包括語法、文法、語言功能、提供和使用特定 FIDL 通訊協定的方式,以及建構系統的運作方式。後續步驟可參考這個基準的範例,說明如何擴充現有的 FIDL 通訊協定,這是相當常見的做法。
改善設計
以下各節將探索您可以對原始設計疊代的一種可能方式。並非依序建構,每個項目代表了可修改或改進上述基礎案例的獨立方式。