以非同步方式回應要求

必要條件

本教學課程以入門教學課程為基礎。

總覽

本教學課程的完整程式碼範例位於 //examples/fidl/cpp/server_async_completer

在初始伺服器教學課程Echo 實作中,伺服器程式碼使用完成器回應 EchoString 要求:

void EchoString(EchoStringRequest& request, EchoStringCompleter::Sync& completer) override {
  // Call |Reply| to reply synchronously with the request value.
  completer.Reply({{.response = request.value()}});
}

請注意,完成者的型別結尾為 ::Sync。同步完成器必須在處理常式傳回前使用。強制執行這項操作可進行最佳化,因為用於回覆的簿記中繼資料可堆疊分配。

非同步回應

在許多情況下,同步回應並不可行。舉例來說,回覆可能需要其他非同步呼叫的結果。如要非同步回應,我們必須使用 ToAsync 從同步完成程式取得非同步完成程式:

EchoStringCompleter::Async async_completer = completer.ToAsync();

產生的 async_completer 會公開與 completer 相同的 API,但可以移開,以便日後使用。

在範例程式碼中,伺服器會在延遲的工作中建立回覆,模擬伺服器必須先執行長時間執行的工作,才能傳送回應的情境:

void EchoString(EchoStringRequest& request, EchoStringCompleter::Sync& completer) override {
  async::PostDelayedTask(
      dispatcher_,
      [value = request.value(), completer = completer.ToAsync()]() mutable {
        completer.Reply({{.response = value}});
      },
      zx::duration(ZX_SEC(1)));
}

伺服器會等到目前的處理常式方法傳回後,才會開始處理任何新要求。從 EchoString 傳回後,伺服器會監控端點是否有新的 FIDL 訊息,並在 1 秒後排定回覆時間。也就是說,如果用戶端快速連續傳送多個EchoString要求,我們可能會有相同數量的並行非同步延遲工作正在執行。

在伺服器中以非同步方式回應,並說出線路網域物件

當伺服器會說線路網域物件時,您會使用相同的 ToAsync 作業,但請特別注意物件生命週期。具體來說,提供給方法處理常式的要求檢視區塊不擁有要求訊息。如果非同步工作需要在 EchoString 方法傳回後使用要求參數,我們需要將相關欄位複製到自有型別:

class EchoImpl : public fidl::WireServer<fuchsia_examples::Echo> {
 public:
  void EchoString(EchoStringRequestView request, EchoStringCompleter::Sync& completer) override {
    // Copy the contents of |request->value| (a fidl::StringView) to a string.
    std::string value_owned{request->value.get()};
    async::PostDelayedTask(
        dispatcher_,
        [value = value_owned, completer = completer.ToAsync()]() mutable {
          completer.Reply(fidl::StringView::FromExternal(value));
        },
        zx::duration(ZX_SEC(1)));
  }

  // ...
};

如要進一步瞭解記憶體擁有權,請參閱「線路網域物件的記憶體擁有權」。

執行範例

如要讓用戶端和伺服器使用 Echo 通訊協定進行通訊,元件架構必須將 fuchsia.examples.Echo 能力從伺服器路由至用戶端。本教學課程提供

  1. 設定建構作業,以便納入提供的套件,其中包含 echo 領域、伺服器和用戶端:

    fx set core.x64 --with //examples/fidl/cpp/server_async_completer:echo-cpp-async
  2. 建構 Fuchsia 映像檔:

    fx build
  3. 執行 echo_realm 元件。這會建立用戶端和伺服器元件例項,並將功能路徑導向:

    ffx component run /core/ffx-laboratory:echo_realm fuchsia-pkg://fuchsia.com/echo-cpp-async#meta/echo_realm.cm
  4. 啟動 echo_client 執行個體:

    ffx component start /core/ffx-laboratory:echo_realm/echo_client

用戶端嘗試連線至 Echo 通訊協定時,伺服器元件就會啟動。裝置記錄檔中應會顯示類似以下的輸出內容 (ffx log)。最左欄是時間戳記:

[21611.962][echo_server][I] Running C++ echo server with natural types
[21611.965][echo_server][I] Incoming connection for fuchsia.examples.Echo
[21612.998][echo_client][I] (Natural types) got response: hello
[21613.999][echo_client][I] (Natural types) got response: hello
[21614.000][echo_client][I] (Natural types) got event: hello
[21615.002][echo_client][I] (Wire types) got response: hello
[21615.003][echo_client][I] (Natural types) got event: hello
[21615.003][echo_server][I] Client disconnected

請注意,Incoming connection for fuchsia.examples.Echo(Natural types) got response: hello 之間大約會間隔一秒,因為伺服器已設定為非同步延遲一秒才傳送回應。

終止領域元件,停止執行作業並清除元件例項:

ffx component destroy /core/ffx-laboratory:echo_realm