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_
32 template <
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";
77 codec->DecodeMethodCall(message, message_size);
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 =
107 result = codec->EncodeErrorEnvelope(
error->error_code,
108 error->error_message,
109 error->error_details.get());
111 result = codec->EncodeSuccessEnvelope();
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());
120 result = codec->EncodeErrorEnvelope(
error->error_code,
121 error->error_message,
122 error->error_details.get());
124 result = codec->EncodeSuccessEnvelope();
126 is_listening =
false;
128 result = codec->EncodeErrorEnvelope(
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_;
181 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_