本計算機範例說明如何使用 建構 FIDL 通訊協定 功能最基本在這個例子中,您將修改 可能傳回錯誤、組合 FIDL 通訊協定與其他程式 並展示其他基元的用法
開始使用
在這個範例中,您將建立基本的計算機伺服器用戶端會顯示用來 如要先定義並提供及使用 FIDL 通訊協定,則須有基本設定。
首先,請定義介面定義並測試控管工具。
介面定義 (.fidl
檔案本身) 是任何新資料的起點
FIDL 通訊協定。此外,計算機還包括必要的 CML 和領域
建立可用於專案的用戶端與伺服器模式的定義
進行任意實作的 Scaffold
以下為 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", ], }, ], }
用戶端和伺服器實作項目現在可以使用任何支援的語言編寫:
荒漠油廠
用戶端
// 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 通訊協定 以及每個 VM 的運作原理如要瞭解後續步驟,請參考這個基準線的範例, 擴充現有的 FIDL 通訊協定,在預期發生類似情況時 練習。
改善設計
以下各節將探索一種可能的疊代方式 原始設計。每個學習路徑都不必依序建構 的論述獨立方式, 遭到修改或改良