| Flutter Linux Embedder |

Functions | Variables
fl_basic_message_channel.h File Reference
`#include <gio/gio.h>`
`#include <glib-object.h>`
`#include <gmodule.h>`
`#include "fl_binary_messenger.h"`
`#include "fl_message_codec.h"`

Go to the source code of this file.

## Functions
G_BEGIN_DECLS G_MODULE_EXPORT | G_DECLARE_FINAL_TYPE (FlBasicMessageChannel, fl_basic_message_channel, FL, BASIC_MESSAGE_CHANNEL, GObject) G_MODULE_EXPORT G_DECLARE_FINAL_TYPE(FlBasicMessageChannelResponseHandle
: a channel name.
fl_basic_message_channel_new: @messenger: an #FlBinaryMessenger. @codec: the message codec. Creates a basic message channel. @codec must match the codec used on the Dart end of the channel. Returns: a new #FlBasicMessageChannel.
FlBasicMessageChannel * | fl_basic_message_channel_new (FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
void | fl_basic_message_channel_set_message_handler (FlBasicMessageChannel *channel, FlBasicMessageChannelMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
gboolean | fl_basic_message_channel_respond (FlBasicMessageChannel *channel, FlBasicMessageChannelResponseHandle *response_handle, FlValue *message, GError **error)
void | fl_basic_message_channel_send (FlBasicMessageChannel *channel, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
FlValue * | fl_basic_message_channel_send_finish (FlBasicMessageChannel *channel, GAsyncResult *result, GError **error)

## Variables
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_BEGIN_DECLS G_MODULE_EXPORT | FL
G_BEGIN_DECLS G_MODULE_EXPORT | BASIC_MESSAGE_CHANNEL_RESPONSE_HANDLE

## Function Documentation

## ◆ fl_basic_message_channel_new()

FlBasicMessageChannel* fl_basic_message_channel_new | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | name,
|  | FlMessageCodec * | codec
| ) |  |

Definition at line 154 of file fl_basic_message_channel.cc.

157 {
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);
161
162 FlBasicMessageChannel* self = FL_BASIC_MESSAGE_CHANNEL(
163 g_object_new(fl_basic_message_channel_get_type(), nullptr));
164
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));
168
169 fl_binary_messenger_set_message_handler_on_channel(
170 self->messenger, self->name, message_cb, g_object_ref(self),
171 channel_closed_cb);
172
173 return self;
174 }
message_cb
static void message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
Definition: fl_basic_message_channel.cc:74
channel_closed_cb
static void channel_closed_cb(gpointer user_data)
Definition: fl_basic_message_channel.cc:111
fl_binary_messenger_set_message_handler_on_channel
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)
Definition: fl_binary_messenger.cc:408

References channel_closed_cb(), fl_binary_messenger_set_message_handler_on_channel(), and message_cb().

Referenced by fl_accessibility_channel_new(), fl_key_event_channel_new(), fl_settings_channel_new(), and TEST().

## ◆ fl_basic_message_channel_respond()

gboolean fl_basic_message_channel_respond | ( | FlBasicMessageChannel * | channel,
|  | FlBasicMessageChannelResponseHandle * | response_handle,
|  | FlValue * | message,
|  | GError ** | error
| ) |  |

fl_basic_message_channel_respond: @channel: an #FlBasicMessageChannel. @response_handle: handle that was provided in a FlBasicMessageChannelMessageHandler. @message: (allow-none): message response to send or NULL for an empty response. @error: (allow-none): #GError location to store the error occurring, or NULL to ignore. If `error` is not NULL, `*error` must be initialized (typically NULL, but an error from a previous call using GLib error handling is explicitly valid).

Responds to a message.

Returns: TRUE on success.

Definition at line 204 of file fl_basic_message_channel.cc.

208 {
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);
212
213 g_autoptr(GBytes) data =
214 fl_message_codec_encode_message(self->codec, message, error);
215 if (data == nullptr) {
216 return FALSE;
217 }
218
219 gboolean result = fl_binary_messenger_send_response(
220 self->messenger, response_handle->response_handle, data, error);
221 g_clear_object(&response_handle->response_handle);
222
223 return result;
224 }
message
const char * message
Definition: fl_accessibility_handler.cc:21
g_autoptr
g_autoptr(FlEngine) engine
fl_binary_messenger_send_response
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
Definition: fl_binary_messenger.cc:422
fl_message_codec_encode_message
G_MODULE_EXPORT GBytes * fl_message_codec_encode_message(FlMessageCodec *self, FlValue *message, GError **error)
Definition: fl_message_codec.cc:17
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40

References error, fl_binary_messenger_send_response(), fl_message_codec_encode_message(), g_autoptr(), and message.

Referenced by message_cb(), and TEST().

## ◆ fl_basic_message_channel_send()

void fl_basic_message_channel_send | ( | FlBasicMessageChannel * | channel,
|  | FlValue * | message,
|  | GCancellable * | cancellable,
|  | GAsyncReadyCallback | callback,
|  | gpointer | user_data
| ) |  |

fl_basic_message_channel_send: @channel: an #FlBasicMessageChannel. @message: (allow-none): message to send, must match what the #FlMessageCodec supports. @cancellable: (allow-none): a #GCancellable or NULL. @callback: (scope async): (allow-none): a #GAsyncReadyCallback to call when the request is satisfied or NULL to ignore the response. @user_data: (closure): user data to pass to @callback.

Asynchronously sends a message.

Definition at line 226 of file fl_basic_message_channel.cc.

230 {
231 g_return_if_fail(FL_IS_BASIC_MESSAGE_CHANNEL(self));
232
233 g_autoptr(GTask) task =
234 callback != nullptr ? g_task_new(self, cancellable, callback, user_data)
235 : nullptr;
236
237 g_autoptr(GError) error = nullptr;
238 g_autoptr(GBytes) data =
239 fl_message_codec_encode_message(self->codec, message, &error);
240 if (data == nullptr) {
241 if (task != nullptr) {
242 g_task_return_error(task, g_error_copy(error));
243 }
244 return;
245 }
246
247 fl_binary_messenger_send_on_channel(
248 self->messenger, self->name, data, cancellable,
249 callback != nullptr ? message_response_cb : nullptr,
250 g_steal_pointer(&task));
251 }
user_data
const char FlTextDirection FlAssertiveness gpointer user_data
Definition: fl_accessibility_handler.cc:24
message_response_cb
static void message_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_basic_message_channel.cc:103
fl_binary_messenger_send_on_channel
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_binary_messenger.cc:435

References error, fl_binary_messenger_send_on_channel(), fl_message_codec_encode_message(), g_autoptr(), message, message_response_cb(), and user_data.

Referenced by fl_key_event_channel_send(), fl_settings_channel_send(), and TEST().

## ◆ fl_basic_message_channel_send_finish()

FlValue* fl_basic_message_channel_send_finish | ( | FlBasicMessageChannel * | channel,
|  | GAsyncResult * | result,
|  | GError ** | error
| ) |  |

fl_basic_message_channel_send_finish: @channel: an #FlBasicMessageChannel.

Returns
  : a #GAsyncResult. @error: (allow-none): #GError location to store the error occurring, or NULL to ignore. If `error` is not NULL, `*error` must be initialized (typically NULL, but an error from a previous call using GLib error handling is explicitly valid).

Completes request started with fl_basic_message_channel_send().

Returns: message response on success or NULL on error.

Definition at line 253 of file fl_basic_message_channel.cc.

256 {
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);
259
260 GTask* task = G_TASK(result);
261 g_autoptr(GAsyncResult) r =
262 G_ASYNC_RESULT(g_task_propagate_pointer(task, error));
263 if (r == nullptr) {
264 return nullptr;
265 }
266
267 g_autoptr(GBytes) message =
268 fl_binary_messenger_send_on_channel_finish(self->messenger, r, error);
269 if (message == nullptr) {
270 return nullptr;
271 }
272
273 return fl_message_codec_decode_message(self->codec, message, error);
274 }
fl_binary_messenger_send_on_channel_finish
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
Definition: fl_binary_messenger.cc:449
fl_message_codec_decode_message
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
Definition: fl_message_codec.cc:33

References error, fl_binary_messenger_send_on_channel_finish(), fl_message_codec_decode_message(), g_autoptr(), and message.

Referenced by fl_key_event_channel_send_finish(), and TEST().

## ◆ fl_basic_message_channel_set_message_handler()

void fl_basic_message_channel_set_message_handler | ( | FlBasicMessageChannel * | channel,
|  | FlBasicMessageChannelMessageHandler | handler,
|  | gpointer | user_data,
|  | GDestroyNotify | destroy_notify
| ) |  |

fl_basic_message_channel_set_message_handler: @channel: an #FlBasicMessageChannel. @handler: (allow-none): function to call when a message is received on this channel or NULL to disable the handler. @user_data: (closure): user data to pass to @handler. @destroy_notify: (allow-none): a function which gets called to free @user_data, or NULL.

Sets the function called when a message is received from the Dart side of the channel. See FlBasicMessageChannelMessageHandler for details on how to respond to messages.

The handler is removed if the channel is closed or is replaced by another handler, set @destroy_notify if you want to detect this.

Definition at line 176 of file fl_basic_message_channel.cc.

180 {
181 g_return_if_fail(FL_IS_BASIC_MESSAGE_CHANNEL(self));
182
183 // Don't set handler if channel closed.
184 if (self->channel_closed) {
185 if (handler != nullptr) {
186 g_warning(
187 "Attempted to set message handler on a closed FlBasicMessageChannel");
188 }
189 if (destroy_notify != nullptr) {
190 destroy_notify(user_data);
191 }
192 return;
193 }
194
195 if (self->message_handler_destroy_notify != nullptr) {
196 self->message_handler_destroy_notify(self->message_handler_data);
197 }
198
199 self->message_handler = handler;
200 self->message_handler_data = user_data;
201 self->message_handler_destroy_notify = destroy_notify;
202 }

References user_data.

Referenced by fl_accessibility_channel_new(), and TEST().

## ◆ G_DECLARE_FINAL_TYPE()

G_BEGIN_DECLS G_MODULE_EXPORT G_DECLARE_FINAL_TYPE | ( | FlBasicMessageChannel | ,
|  | fl_basic_message_channel | ,
|  | FL | ,
|  | BASIC_MESSAGE_CHANNEL | ,
|  | GObject |
| ) |  |

## Variable Documentation

## ◆ BASIC_MESSAGE_CHANNEL_RESPONSE_HANDLE

G_BEGIN_DECLS G_MODULE_EXPORT BASIC_MESSAGE_CHANNEL_RESPONSE_HANDLE

Definition at line 32 of file fl_basic_message_channel.h.

## ◆ FL

G_BEGIN_DECLS G_MODULE_EXPORT FL

Definition at line 31 of file fl_basic_message_channel.h.

## ◆ fl_basic_message_channel_response_handle

G_BEGIN_DECLS G_MODULE_EXPORT fl_basic_message_channel_response_handle

Definition at line 30 of file fl_basic_message_channel.h.

## ◆ FlBasicMessageChannelMessageHandler

G_BEGIN_DECLS G_MODULE_EXPORT GObject typedef void(* FlBasicMessageChannelMessageHandler) (FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data) | ( | FlBasicMessageChannel * | channel,
|  | FlValue * | message,
|  | FlBasicMessageChannelResponseHandle * | response_handle,
|  | gpointer | user_data
| ) |  |

FlBasicMessageChannel:

#FlBasicMessageChannel is an object that allows sending and receiving messages to/from Dart code over platform channels.

The following example shows how to send messages on a channel:

|[ static FlBasicMessageChannel *channel = NULL;

static void message_cb (FlBasicMessageChannel* channel, FlValue* message, FlBasicMessageChannelResponseHandle* response_handle, gpointer user_data) { g_autoptr(FlValue) response = handle_message (message); g_autoptr(GError) error = NULL; if (!fl_basic_message_channel_respond (channel, response_handle, response, &error)) g_warning ("Failed to send channel response: %s", error->message); }

static void message_response_cb (GObject *object, GAsyncResult *result, gpointer user_data) { g_autoptr(GError) error = NULL; g_autoptr(FlValue) response = fl_basic_message_channel_send_finish (FL_BASIC_MESSAGE_CHANNEL (object), result, &error); if (response == NULL) { g_warning ("Failed to send message: %s", error->message); return; }

handle_response (response); }

static void setup_channel () { g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new (); channel = fl_basic_message_channel_new (messenger, "flutter/foo", FL_MESSAGE_CODEC (codec)); fl_basic_message_channel_set_message_handler (channel, message_cb, NULL, NULL);

g_autoptr(FlValue) message = fl_value_new_string ("Hello World"); fl_basic_message_channel_send (channel, message, NULL, message_response_cb, NULL); } ]|

#FlBasicMessageChannel matches the BasicMessageChannel class in the Flutter services library. FlBasicMessageChannelResponseHandle:

#FlBasicMessageChannelResponseHandle is an object used to send responses with. FlBasicMessageChannelMessageHandler: @channel: an #FlBasicMessageChannel. @message: message received. @response_handle: a handle to respond to the message with. @user_data: (closure): data provided when registering this handler.

Function called when a message is received. Call fl_basic_message_channel_respond() to respond to this message. If the response is not occurring in this callback take a reference to @response_handle and release that once it has been responded to. Failing to respond before the last reference to @response_handle is dropped is a programming error.

Definition at line 110 of file fl_basic_message_channel.h.