Flutter Engine
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.

References flutter::EventChannel< T >::operator=(), and flutter::EventChannel< T >::~EventChannel().

40  : messenger_(messenger), name_(name), codec_(codec) {}
const char * name
Definition: fuchsia.cc:50

◆ ~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 50 of file event_channel.h.

References flutter::MethodCodec< T >::EncodeErrorEnvelope(), flutter::MethodCodec< T >::EncodeSuccessEnvelope(), error, event, method_call, name, flutter::EventChannel< T >::operator=(), flutter::BinaryMessenger::Send(), and flutter::BinaryMessenger::SetMessageHandler().

Referenced by flutter::TEST().

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

The documentation for this class was generated from the following file: