Flutter Linux Embedder
flutter::EventChannel< T > Class Template Reference

#include <event_channel.h>

Public Member Functions

 EventChannel (BinaryMessenger *messenger, const std::string &name, const MethodCodec< T > *codec)
 
 ~EventChannel ()=default
 
 EventChannel (EventChannel const &)=delete
 
EventChanneloperator= (EventChannel const &)=delete
 
void SetStreamHandler (std::unique_ptr< StreamHandler< T >> handler)
 

Detailed Description

template<typename T = EncodableValue>
class flutter::EventChannel< T >

Definition at line 33 of file event_channel.h.

Constructor & Destructor Documentation

◆ EventChannel() [1/2]

template<typename T = EncodableValue>
flutter::EventChannel< T >::EventChannel ( BinaryMessenger messenger,
const std::string &  name,
const MethodCodec< T > *  codec 
)
inline

Definition at line 37 of file event_channel.h.

40  : messenger_(messenger), name_(name), codec_(codec) {}

◆ ~EventChannel()

template<typename T = EncodableValue>
flutter::EventChannel< T >::~EventChannel ( )
default

◆ EventChannel() [2/2]

template<typename T = EncodableValue>
flutter::EventChannel< T >::EventChannel ( EventChannel< T > const &  )
delete

Member Function Documentation

◆ operator=()

template<typename T = EncodableValue>
EventChannel& flutter::EventChannel< T >::operator= ( EventChannel< T > const &  )
delete

◆ SetStreamHandler()

template<typename T = EncodableValue>
void flutter::EventChannel< T >::SetStreamHandler ( std::unique_ptr< StreamHandler< T >>  handler)
inline

Definition at line 54 of file event_channel.h.

54  {
55  if (!handler) {
56  messenger_->SetMessageHandler(name_, nullptr);
57  return;
58  }
59 
60  // std::function requires a copyable lambda, so convert to a shared pointer.
61  // This is safe since only one copy of the shared_pointer will ever be
62  // accessed.
63  std::shared_ptr<StreamHandler<T>> shared_handler(handler.release());
64  const MethodCodec<T>* codec = codec_;
65  const std::string channel_name = name_;
66  const BinaryMessenger* messenger = messenger_;
67  BinaryMessageHandler binary_handler =
68  [shared_handler, codec, channel_name, messenger,
69  // Mutable state to track the handler's listening status.
70  is_listening = bool(false)](const uint8_t* message,
71  const size_t message_size,
72  const BinaryReply& reply) mutable {
73  constexpr char kOnListenMethod[] = "listen";
74  constexpr char kOnCancelMethod[] = "cancel";
75 
76  std::unique_ptr<MethodCall<T>> method_call =
77  codec->DecodeMethodCall(message, message_size);
78  if (!method_call) {
79  std::cerr
80  << "Unable to construct method call from message on channel: "
81  << channel_name << std::endl;
82  reply(nullptr, 0);
83  return;
84  }
85 
86  const std::string& method = method_call->method_name();
87  if (method.compare(kOnListenMethod) == 0) {
88  if (is_listening) {
89  std::unique_ptr<StreamHandlerError<T>> error =
90  shared_handler->OnCancel(nullptr);
91  if (error) {
92  std::cerr << "Failed to cancel existing stream: "
93  << (error->error_code) << ", "
94  << (error->error_message) << ", "
95  << (error->error_details);
96  }
97  }
98  is_listening = true;
99 
100  std::unique_ptr<std::vector<uint8_t>> result;
101  auto sink = std::make_unique<EventSinkImplementation>(
102  messenger, channel_name, codec);
103  std::unique_ptr<StreamHandlerError<T>> error =
104  shared_handler->OnListen(method_call->arguments(),
105  std::move(sink));
106  if (error) {
107  result = codec->EncodeErrorEnvelope(error->error_code,
108  error->error_message,
109  error->error_details.get());
110  } else {
111  result = codec->EncodeSuccessEnvelope();
112  }
113  reply(result->data(), result->size());
114  } else if (method.compare(kOnCancelMethod) == 0) {
115  std::unique_ptr<std::vector<uint8_t>> result;
116  if (is_listening) {
117  std::unique_ptr<StreamHandlerError<T>> error =
118  shared_handler->OnCancel(method_call->arguments());
119  if (error) {
120  result = codec->EncodeErrorEnvelope(error->error_code,
121  error->error_message,
122  error->error_details.get());
123  } else {
124  result = codec->EncodeSuccessEnvelope();
125  }
126  is_listening = false;
127  } else {
128  result = codec->EncodeErrorEnvelope(
129  "error", "No active stream to cancel", nullptr);
130  }
131  reply(result->data(), result->size());
132  } else {
133  reply(nullptr, 0);
134  }
135  };
136  messenger_->SetMessageHandler(name_, std::move(binary_handler));
137  }

References error, method_call, result, and flutter::BinaryMessenger::SetMessageHandler().

Referenced by flutter::TEST().


The documentation for this class was generated from the following file:
flutter::BinaryMessenger::SetMessageHandler
virtual void SetMessageHandler(const std::string &channel, BinaryMessageHandler handler)=0
method_call
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
Definition: fl_method_channel.h:120
flutter::BinaryReply
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
Definition: binary_messenger.h:17
result
GAsyncResult * result
Definition: fl_text_input_handler.cc:106
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
flutter::BinaryMessageHandler
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
Definition: binary_messenger.h:24