blob: 53ff65475ee8fe4ecb35f2ff8b35790dfb9067ab [file] [log] [blame]
// Copyright 2019 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 <lib/fidl/cpp/interface_request.h>
#include <lib/fidl/cpp/internal/message_sender.h>
#include <lib/zx/channel.h>
#include <utility>
namespace fidl {
// Sends events for |Interface| on a given channel.
// An |EventSender| lets its client send events on a given channel. This class
// differs from |Binding| in that |EventSender| does not listen for incoming
// messages on the channel, which allows |EventSender| to send messages from
// multiple threads safely.
// See also:
// * |Binding|, which can receive messages as well as send events.
template <typename Interface>
class EventSender final : public fidl::internal::MessageSender {
// Constructs an event sender that sends events through |channel|.
explicit EventSender(zx::channel channel) : channel_(std::move(channel)), stub_(nullptr) {
// Constructs an event sender that sends events through the underlying channel
// in |request|.
explicit EventSender(InterfaceRequest<Interface> request) : EventSender(request.TakeChannel()) {}
EventSender(const EventSender&) = delete;
EventSender& operator=(const EventSender&) = delete;
// The interface for sending events back to the client.
typename Interface::EventSender_& events() { return stub_; }
// The underlying channel.
const zx::channel& channel() const { return channel_; }
// Transfers ownership of the underlying channel to the caller.
zx::channel TakeChannel() { return std::move(channel_); }
zx_status_t Send(const fidl_type_t* type, Message message) final {
return fidl::internal::SendMessage(channel_, type, std::move(message));
zx::channel channel_;
typename Interface::Stub_ stub_;
} // namespace fidl