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);
107 g_task_return_pointer(task,
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,
231 g_return_if_fail(FL_IS_BASIC_MESSAGE_CHANNEL(
self));
233 g_autoptr(GTask) task =
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,
error);
248 self->messenger, self->name, data, cancellable,
250 g_steal_pointer(&task));
254 FlBasicMessageChannel*
self,
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 g_autoptr(GTask) task = G_TASK(
result);
261 GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task,
nullptr));
263 g_autoptr(GBytes) message =
265 if (message ==
nullptr) {