1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "gpollableinputstream.h"
26 #include "gasynchelper.h"
30 * SECTION:gpollableutils
31 * @short_description: Utilities for pollable streams
34 * Utility functions for #GPollableInputStream and
35 * #GPollableOutputStream implementations.
45 pollable_source_dispatch (GSource *source,
49 GPollableSourceFunc func = (GPollableSourceFunc)callback;
50 GPollableSource *pollable_source = (GPollableSource *)source;
52 return (*func) (pollable_source->stream, user_data);
56 pollable_source_finalize (GSource *source)
58 GPollableSource *pollable_source = (GPollableSource *)source;
60 g_object_unref (pollable_source->stream);
64 pollable_source_closure_callback (GObject *stream,
67 GClosure *closure = data;
69 GValue param = G_VALUE_INIT;
70 GValue result_value = G_VALUE_INIT;
73 g_value_init (&result_value, G_TYPE_BOOLEAN);
75 g_value_init (¶m, G_TYPE_OBJECT);
76 g_value_set_object (¶m, stream);
78 g_closure_invoke (closure, &result_value, 1, ¶m, NULL);
80 result = g_value_get_boolean (&result_value);
81 g_value_unset (&result_value);
82 g_value_unset (¶m);
87 static GSourceFuncs pollable_source_funcs =
91 pollable_source_dispatch,
92 pollable_source_finalize,
93 (GSourceFunc)pollable_source_closure_callback,
98 * g_pollable_source_new:
99 * @pollable_stream: the stream associated with the new source
101 * Utility method for #GPollableInputStream and #GPollableOutputStream
102 * implementations. Creates a new #GSource that expects a callback of
103 * type #GPollableSourceFunc. The new source does not actually do
104 * anything on its own; use g_source_add_child_source() to add other
105 * sources to it to cause it to trigger.
107 * Returns: (transfer full): the new #GSource.
112 g_pollable_source_new (GObject *pollable_stream)
115 GPollableSource *pollable_source;
117 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream) ||
118 G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream), NULL);
120 source = g_source_new (&pollable_source_funcs, sizeof (GPollableSource));
121 g_source_set_static_name (source, "GPollableSource");
122 pollable_source = (GPollableSource *)source;
123 pollable_source->stream = g_object_ref (pollable_stream);
129 * g_pollable_source_new_full:
130 * @pollable_stream: (type GObject): the stream associated with the
132 * @child_source: (nullable): optional child source to attach
133 * @cancellable: (nullable): optional #GCancellable to attach
135 * Utility method for #GPollableInputStream and #GPollableOutputStream
136 * implementations. Creates a new #GSource, as with
137 * g_pollable_source_new(), but also attaching @child_source (with a
138 * dummy callback), and @cancellable, if they are non-%NULL.
140 * Returns: (transfer full): the new #GSource.
145 g_pollable_source_new_full (gpointer pollable_stream,
146 GSource *child_source,
147 GCancellable *cancellable)
151 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream) ||
152 G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream), NULL);
154 source = g_pollable_source_new (pollable_stream);
157 g_source_set_dummy_callback (child_source);
158 g_source_add_child_source (source, child_source);
162 GSource *cancellable_source = g_cancellable_source_new (cancellable);
164 g_source_set_dummy_callback (cancellable_source);
165 g_source_add_child_source (source, cancellable_source);
166 g_source_unref (cancellable_source);
173 * g_pollable_stream_read:
174 * @stream: a #GInputStream
175 * @buffer: (array length=count) (element-type guint8): a buffer to
177 * @count: the number of bytes to read
178 * @blocking: whether to do blocking I/O
179 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
180 * @error: location to store the error occurring, or %NULL to ignore
182 * Tries to read from @stream, as with g_input_stream_read() (if
183 * @blocking is %TRUE) or g_pollable_input_stream_read_nonblocking()
184 * (if @blocking is %FALSE). This can be used to more easily share
185 * code between blocking and non-blocking implementations of a method.
187 * If @blocking is %FALSE, then @stream must be a
188 * #GPollableInputStream for which g_pollable_input_stream_can_poll()
189 * returns %TRUE, or else the behavior is undefined. If @blocking is
190 * %TRUE, then @stream does not need to be a #GPollableInputStream.
192 * Returns: the number of bytes read, or -1 on error.
197 g_pollable_stream_read (GInputStream *stream,
201 GCancellable *cancellable,
206 return g_input_stream_read (stream,
212 return g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (stream),
219 * g_pollable_stream_write:
220 * @stream: a #GOutputStream.
221 * @buffer: (array length=count) (element-type guint8): the buffer
222 * containing the data to write.
223 * @count: the number of bytes to write
224 * @blocking: whether to do blocking I/O
225 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
226 * @error: location to store the error occurring, or %NULL to ignore
228 * Tries to write to @stream, as with g_output_stream_write() (if
229 * @blocking is %TRUE) or g_pollable_output_stream_write_nonblocking()
230 * (if @blocking is %FALSE). This can be used to more easily share
231 * code between blocking and non-blocking implementations of a method.
233 * If @blocking is %FALSE, then @stream must be a
234 * #GPollableOutputStream for which
235 * g_pollable_output_stream_can_poll() returns %TRUE or else the
236 * behavior is undefined. If @blocking is %TRUE, then @stream does not
237 * need to be a #GPollableOutputStream.
239 * Returns: the number of bytes written, or -1 on error.
244 g_pollable_stream_write (GOutputStream *stream,
248 GCancellable *cancellable,
253 return g_output_stream_write (stream,
259 return g_pollable_output_stream_write_nonblocking (G_POLLABLE_OUTPUT_STREAM (stream),
266 * g_pollable_stream_write_all:
267 * @stream: a #GOutputStream.
268 * @buffer: (array length=count) (element-type guint8): the buffer
269 * containing the data to write.
270 * @count: the number of bytes to write
271 * @blocking: whether to do blocking I/O
272 * @bytes_written: (out): location to store the number of bytes that was
273 * written to the stream
274 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
275 * @error: location to store the error occurring, or %NULL to ignore
277 * Tries to write @count bytes to @stream, as with
278 * g_output_stream_write_all(), but using g_pollable_stream_write()
279 * rather than g_output_stream_write().
281 * On a successful write of @count bytes, %TRUE is returned, and
282 * @bytes_written is set to @count.
284 * If there is an error during the operation (including
285 * %G_IO_ERROR_WOULD_BLOCK in the non-blocking case), %FALSE is
286 * returned and @error is set to indicate the error status,
287 * @bytes_written is updated to contain the number of bytes written
288 * into the stream before the error occurred.
290 * As with g_pollable_stream_write(), if @blocking is %FALSE, then
291 * @stream must be a #GPollableOutputStream for which
292 * g_pollable_output_stream_can_poll() returns %TRUE or else the
293 * behavior is undefined. If @blocking is %TRUE, then @stream does not
294 * need to be a #GPollableOutputStream.
296 * Returns: %TRUE on success, %FALSE if there was an error
301 g_pollable_stream_write_all (GOutputStream *stream,
305 gsize *bytes_written,
306 GCancellable *cancellable,
309 gsize _bytes_written;
313 while (_bytes_written < count)
315 res = g_pollable_stream_write (stream,
316 (char *)buffer + _bytes_written,
317 count - _bytes_written,
323 *bytes_written = _bytes_written;
328 g_warning ("Write returned zero without error");
330 _bytes_written += res;
334 *bytes_written = _bytes_written;