5#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_
6#define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_
32template <
typename T = EncodableValue>
38 const std::string&
name,
40 : messenger_(messenger), name_(
name), codec_(codec) {}
63 std::shared_ptr<StreamHandler<T>> shared_handler(handler.release());
65 const std::string channel_name = name_;
68 [shared_handler, codec, channel_name, messenger,
70 is_listening = bool(
false)](
const uint8_t*
message,
71 const size_t message_size,
73 constexpr char kOnListenMethod[] =
"listen";
74 constexpr char kOnCancelMethod[] =
"cancel";
80 <<
"Unable to construct method call from message on channel: "
81 << channel_name << std::endl;
86 const std::string& method =
method_call->method_name();
87 if (method.compare(kOnListenMethod) == 0) {
89 std::unique_ptr<StreamHandlerError<T>>
error =
90 shared_handler->OnCancel(
nullptr);
92 std::cerr <<
"Failed to cancel existing stream: "
93 << (
error->error_code) <<
", "
94 << (
error->error_message) <<
", "
95 << (
error->error_details);
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 =
108 error->error_message,
109 error->error_details.get());
114 }
else if (method.compare(kOnCancelMethod) == 0) {
115 std::unique_ptr<std::vector<uint8_t>>
result;
117 std::unique_ptr<StreamHandlerError<T>>
error =
118 shared_handler->OnCancel(
method_call->arguments());
121 error->error_message,
122 error->error_details.get());
126 is_listening =
false;
129 "error",
"No active stream to cancel",
nullptr);
140 class EventSinkImplementation :
public EventSink<T> {
143 const std::string&
name,
145 : messenger_(messenger), name_(
name), codec_(codec) {}
146 ~EventSinkImplementation() =
default;
149 EventSinkImplementation(EventSinkImplementation
const&) =
delete;
150 EventSinkImplementation&
operator=(EventSinkImplementation
const&) =
delete;
153 const BinaryMessenger* messenger_;
154 const std::string name_;
155 const MethodCodec<T>* codec_;
158 void SuccessInternal(
const T*
event =
nullptr)
override {
159 auto result = codec_->EncodeSuccessEnvelope(
event);
160 messenger_->Send(name_,
result->data(),
result->size());
163 void ErrorInternal(
const std::string& error_code,
164 const std::string& error_message,
165 const T* error_details)
override {
167 codec_->EncodeErrorEnvelope(error_code, error_message, error_details);
168 messenger_->Send(name_,
result->data(),
result->size());
171 void EndOfStreamInternal()
override { messenger_->Send(name_,
nullptr, 0); }
174 BinaryMessenger* messenger_;
175 const std::string name_;
176 const MethodCodec<T>* codec_;
virtual void SetMessageHandler(const std::string &channel, BinaryMessageHandler handler)=0
EventChannel & operator=(EventChannel const &)=delete
void SetStreamHandler(std::unique_ptr< StreamHandler< T > > handler)
EventChannel(EventChannel const &)=delete
EventChannel(BinaryMessenger *messenger, const std::string &name, const MethodCodec< T > *codec)
std::unique_ptr< MethodCall< T > > DecodeMethodCall(const uint8_t *message, size_t message_size) const
std::unique_ptr< std::vector< uint8_t > > EncodeErrorEnvelope(const std::string &error_code, const std::string &error_message="", const T *error_details=nullptr) const
std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelope(const T *result=nullptr) const
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
const uint8_t uint32_t uint32_t GError ** error
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
DEF_SWITCHES_START aot vmservice shared library name