36 G_DEFINE_TYPE(FlBasicMessageChannel, fl_basic_message_channel, G_TYPE_OBJECT)
41 static
void fl_basic_message_channel_response_handle_dispose(GObject*
object) {
42 FlBasicMessageChannelResponseHandle*
self =
43 FL_BASIC_MESSAGE_CHANNEL_RESPONSE_HANDLE(
object);
45 g_clear_object(&self->response_handle);
47 G_OBJECT_CLASS(fl_basic_message_channel_response_handle_parent_class)
52 FlBasicMessageChannelResponseHandleClass* klass) {
53 G_OBJECT_CLASS(klass)->dispose =
54 fl_basic_message_channel_response_handle_dispose;
58 FlBasicMessageChannelResponseHandle*
self) {}
60 static FlBasicMessageChannelResponseHandle*
62 FlBinaryMessengerResponseHandle* response_handle) {
63 FlBasicMessageChannelResponseHandle*
self =
64 FL_BASIC_MESSAGE_CHANNEL_RESPONSE_HANDLE(g_object_new(
65 fl_basic_message_channel_response_handle_get_type(),
nullptr));
67 self->response_handle =
68 FL_BINARY_MESSENGER_RESPONSE_HANDLE(g_object_ref(response_handle));
77 FlBinaryMessengerResponseHandle* response_handle,
79 FlBasicMessageChannel*
self = FL_BASIC_MESSAGE_CHANNEL(
user_data);
81 if (self->message_handler ==
nullptr) {
87 g_autoptr(GError)
error =
nullptr;
88 g_autoptr(
FlValue) message_value =
90 if (message_value ==
nullptr) {
91 g_warning(
"Failed to decode message: %s",
error->message);
96 g_autoptr(FlBasicMessageChannelResponseHandle) handle =
98 self->message_handler(
self, message_value, handle,
99 self->message_handler_data);
104 GAsyncResult* result,
106 g_autoptr(GTask) task = G_TASK(
user_data);
107 g_task_return_pointer(task, g_object_ref(result), g_object_unref);
112 g_autoptr(FlBasicMessageChannel)
self = FL_BASIC_MESSAGE_CHANNEL(
user_data);
114 self->channel_closed =
TRUE;
117 if (self->message_handler_destroy_notify !=
nullptr) {
118 self->message_handler_destroy_notify(self->message_handler_data);
120 self->message_handler =
nullptr;
121 self->message_handler_data =
nullptr;
122 self->message_handler_destroy_notify =
nullptr;
126 FlBasicMessageChannel*
self = FL_BASIC_MESSAGE_CHANNEL(
object);
128 if (self->messenger !=
nullptr) {
130 self->messenger, self->name,
nullptr,
nullptr,
nullptr);
133 g_clear_object(&self->messenger);
134 g_clear_pointer(&self->name, g_free);
135 g_clear_object(&self->codec);
137 if (self->message_handler_destroy_notify !=
nullptr) {
138 self->message_handler_destroy_notify(self->message_handler_data);
140 self->message_handler =
nullptr;
141 self->message_handler_data =
nullptr;
142 self->message_handler_destroy_notify =
nullptr;
144 G_OBJECT_CLASS(fl_basic_message_channel_parent_class)->dispose(
object);
148 FlBasicMessageChannelClass* klass) {
155 FlBinaryMessenger* messenger,
157 FlMessageCodec* codec) {
158 g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger),
nullptr);
159 g_return_val_if_fail(name !=
nullptr,
nullptr);
160 g_return_val_if_fail(FL_IS_MESSAGE_CODEC(codec),
nullptr);
162 FlBasicMessageChannel*
self = FL_BASIC_MESSAGE_CHANNEL(
163 g_object_new(fl_basic_message_channel_get_type(),
nullptr));
165 self->messenger = FL_BINARY_MESSENGER(g_object_ref(messenger));
166 self->name = g_strdup(name);
167 self->codec = FL_MESSAGE_CODEC(g_object_ref(codec));
170 self->messenger, self->name,
message_cb, g_object_ref(
self),
177 FlBasicMessageChannel*
self,
180 GDestroyNotify destroy_notify) {
181 g_return_if_fail(FL_IS_BASIC_MESSAGE_CHANNEL(
self));
184 if (self->channel_closed) {
185 if (handler !=
nullptr) {
187 "Attempted to set message handler on a closed FlBasicMessageChannel");
189 if (destroy_notify !=
nullptr) {
195 if (self->message_handler_destroy_notify !=
nullptr) {
196 self->message_handler_destroy_notify(self->message_handler_data);
199 self->message_handler = handler;
201 self->message_handler_destroy_notify = destroy_notify;
205 FlBasicMessageChannel*
self,
206 FlBasicMessageChannelResponseHandle* response_handle,
209 g_return_val_if_fail(FL_IS_BASIC_MESSAGE_CHANNEL(
self), FALSE);
210 g_return_val_if_fail(response_handle !=
nullptr, FALSE);
211 g_return_val_if_fail(response_handle->response_handle !=
nullptr, FALSE);
213 g_autoptr(GBytes) data =
215 if (data ==
nullptr) {
220 self->messenger, response_handle->response_handle, data,
error);
221 g_clear_object(&response_handle->response_handle);
228 GCancellable* cancellable,
229 GAsyncReadyCallback callback,
231 g_return_if_fail(FL_IS_BASIC_MESSAGE_CHANNEL(
self));
233 g_autoptr(GTask) task =
234 callback !=
nullptr ? g_task_new(
self, cancellable, callback,
user_data)
237 g_autoptr(GError)
error =
nullptr;
238 g_autoptr(GBytes) data =
240 if (data ==
nullptr) {
241 if (task !=
nullptr) {
242 g_task_return_error(task, g_error_copy(
error));
248 self->messenger, self->name, data, cancellable,
250 g_steal_pointer(&task));
254 FlBasicMessageChannel*
self,
255 GAsyncResult* result,
257 g_return_val_if_fail(FL_IS_BASIC_MESSAGE_CHANNEL(
self),
nullptr);
258 g_return_val_if_fail(g_task_is_valid(result,
self),
nullptr);
260 GTask* task = G_TASK(result);
261 g_autoptr(GAsyncResult) r =
262 G_ASYNC_RESULT(g_task_propagate_pointer(task,
error));
267 g_autoptr(GBytes) message =
269 if (message ==
nullptr) {
static void fl_basic_message_channel_class_init(FlBasicMessageChannelClass *klass)
static void message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
G_MODULE_EXPORT void fl_basic_message_channel_set_message_handler(FlBasicMessageChannel *self, FlBasicMessageChannelMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
G_MODULE_EXPORT gboolean fl_basic_message_channel_respond(FlBasicMessageChannel *self, FlBasicMessageChannelResponseHandle *response_handle, FlValue *message, GError **error)
static void message_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static void fl_basic_message_channel_response_handle_init(FlBasicMessageChannelResponseHandle *self)
static void fl_basic_message_channel_init(FlBasicMessageChannel *self)
static FlBasicMessageChannelResponseHandle * fl_basic_message_channel_response_handle_new(FlBinaryMessengerResponseHandle *response_handle)
static void fl_basic_message_channel_response_handle_class_init(FlBasicMessageChannelResponseHandleClass *klass)
static void fl_basic_message_channel_dispose(GObject *object)
static void channel_closed_cb(gpointer user_data)
G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel *self, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
G_BEGIN_DECLS G_MODULE_EXPORT GObject typedef void(* FlBasicMessageChannelMessageHandler)(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
G_BEGIN_DECLS G_MODULE_EXPORT fl_basic_message_channel_response_handle
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
G_MODULE_EXPORT void fl_binary_messenger_set_message_handler_on_channel(FlBinaryMessenger *self, const gchar *channel, FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
G_MODULE_EXPORT GBytes * fl_message_codec_encode_message(FlMessageCodec *self, FlValue *message, GError **error)
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
const uint8_t uint32_t uint32_t GError ** error
typedefG_BEGIN_DECLS struct _FlValue FlValue
FlBasicMessageChannelMessageHandler message_handler
gpointer message_handler_data
FlBinaryMessenger * messenger
GDestroyNotify message_handler_destroy_notify
FlBinaryMessengerResponseHandle * response_handle