1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "gpollableinputstream.h"
24 #include "gasynchelper.h"
28 * SECTION:gpollableutils
29 * @short_description: Utilities for pollable streams
32 * Utility functions for #GPollableInputStream and
33 * #GPollableOutputStream implementations.
43 pollable_source_dispatch (GSource *source,
47 GPollableSourceFunc func = (GPollableSourceFunc)callback;
48 GPollableSource *pollable_source = (GPollableSource *)source;
50 return (*func) (pollable_source->stream, user_data);
54 pollable_source_finalize (GSource *source)
56 GPollableSource *pollable_source = (GPollableSource *)source;
58 g_object_unref (pollable_source->stream);
62 pollable_source_closure_callback (GObject *stream,
65 GClosure *closure = data;
67 GValue param = G_VALUE_INIT;
68 GValue result_value = G_VALUE_INIT;
71 g_value_init (&result_value, G_TYPE_BOOLEAN);
73 g_value_init (¶m, G_TYPE_OBJECT);
74 g_value_set_object (¶m, stream);
76 g_closure_invoke (closure, &result_value, 1, ¶m, NULL);
78 result = g_value_get_boolean (&result_value);
79 g_value_unset (&result_value);
80 g_value_unset (¶m);
85 static GSourceFuncs pollable_source_funcs =
89 pollable_source_dispatch,
90 pollable_source_finalize,
91 (GSourceFunc)pollable_source_closure_callback,
95 * g_pollable_source_new:
96 * @pollable_stream: the stream associated with the new source
98 * Utility method for #GPollableInputStream and #GPollableOutputStream
99 * implementations. Creates a new #GSource that expects a callback of
100 * type #GPollableSourceFunc. The new source does not actually do
101 * anything on its own; use g_source_add_child_source() to add other
102 * sources to it to cause it to trigger.
104 * Returns: (transfer full): the new #GSource.
109 g_pollable_source_new (GObject *pollable_stream)
112 GPollableSource *pollable_source;
114 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream) ||
115 G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream), NULL);
117 source = g_source_new (&pollable_source_funcs, sizeof (GPollableSource));
118 g_source_set_name (source, "GPollableSource");
119 pollable_source = (GPollableSource *)source;
120 pollable_source->stream = g_object_ref (pollable_stream);
126 * g_pollable_source_new_full:
127 * @pollable_stream: (type GObject): the stream associated with the
129 * @child_source: (allow-none): optional child source to attach
130 * @cancellable: (allow-none): optional #GCancellable to attach
132 * Utility method for #GPollableInputStream and #GPollableOutputStream
133 * implementations. Creates a new #GSource, as with
134 * g_pollable_source_new(), but also attaching @child_source (with a
135 * dummy callback), and @cancellable, if they are non-%NULL.
137 * Returns: (transfer full): the new #GSource.
142 g_pollable_source_new_full (gpointer pollable_stream,
143 GSource *child_source,
144 GCancellable *cancellable)
148 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream) ||
149 G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream), NULL);
151 source = g_pollable_source_new (pollable_stream);
154 g_source_set_dummy_callback (child_source);
155 g_source_add_child_source (source, child_source);
159 GSource *cancellable_source = g_cancellable_source_new (cancellable);
161 g_source_set_dummy_callback (cancellable_source);
162 g_source_add_child_source (source, cancellable_source);
163 g_source_unref (cancellable_source);
170 * g_pollable_stream_read:
171 * @stream: a #GInputStream
172 * @buffer: a buffer to read data into
173 * @count: the number of bytes to read
174 * @blocking: whether to do blocking I/O
175 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
176 * @error: location to store the error occurring, or %NULL to ignore
178 * Tries to read from @stream, as with g_input_stream_read() (if
179 * @blocking is %TRUE) or g_pollable_input_stream_read_nonblocking()
180 * (if @blocking is %FALSE). This can be used to more easily share
181 * code between blocking and non-blocking implementations of a method.
183 * If @blocking is %FALSE, then @stream must be a
184 * #GPollableInputStream for which g_pollable_input_stream_can_poll()
185 * returns %TRUE, or else the behavior is undefined. If @blocking is
186 * %TRUE, then @stream does not need to be a #GPollableInputStream.
188 * Returns: the number of bytes read, or -1 on error.
193 g_pollable_stream_read (GInputStream *stream,
197 GCancellable *cancellable,
202 return g_input_stream_read (stream,
208 return g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (stream),
215 * g_pollable_stream_write:
216 * @stream: a #GOutputStream.
217 * @buffer: (array length=count) (element-type guint8): the buffer
218 * containing the data to write.
219 * @count: the number of bytes to write
220 * @blocking: whether to do blocking I/O
221 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
222 * @error: location to store the error occurring, or %NULL to ignore
224 * Tries to write to @stream, as with g_output_stream_write() (if
225 * @blocking is %TRUE) or g_pollable_output_stream_write_nonblocking()
226 * (if @blocking is %FALSE). This can be used to more easily share
227 * code between blocking and non-blocking implementations of a method.
229 * If @blocking is %FALSE, then @stream must be a
230 * #GPollableOutputStream for which
231 * g_pollable_output_stream_can_poll() returns %TRUE or else the
232 * behavior is undefined. If @blocking is %TRUE, then @stream does not
233 * need to be a #GPollableOutputStream.
235 * Returns: the number of bytes written, or -1 on error.
240 g_pollable_stream_write (GOutputStream *stream,
244 GCancellable *cancellable,
249 return g_output_stream_write (stream,
255 return g_pollable_output_stream_write_nonblocking (G_POLLABLE_OUTPUT_STREAM (stream),
262 * g_pollable_stream_write_all:
263 * @stream: a #GOutputStream.
264 * @buffer: (array length=count) (element-type guint8): the buffer
265 * containing the data to write.
266 * @count: the number of bytes to write
267 * @blocking: whether to do blocking I/O
268 * @bytes_written: (out): location to store the number of bytes that was
269 * written to the stream
270 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
271 * @error: location to store the error occurring, or %NULL to ignore
273 * Tries to write @count bytes to @stream, as with
274 * g_output_stream_write_all(), but using g_pollable_stream_write()
275 * rather than g_output_stream_write().
277 * On a successful write of @count bytes, %TRUE is returned, and
278 * @bytes_written is set to @count.
280 * If there is an error during the operation (including
281 * %G_IO_ERROR_WOULD_BLOCK in the non-blocking case), %FALSE is
282 * returned and @error is set to indicate the error status,
283 * @bytes_written is updated to contain the number of bytes written
284 * into the stream before the error occurred.
286 * As with g_pollable_stream_write(), if @blocking is %FALSE, then
287 * @stream must be a #GPollableOutputStream for which
288 * g_pollable_output_stream_can_poll() returns %TRUE or else the
289 * behavior is undefined. If @blocking is %TRUE, then @stream does not
290 * need to be a #GPollableOutputStream.
292 * Returns: %TRUE on success, %FALSE if there was an error
297 g_pollable_stream_write_all (GOutputStream *stream,
301 gsize *bytes_written,
302 GCancellable *cancellable,
305 gsize _bytes_written;
309 while (_bytes_written < count)
311 res = g_pollable_stream_write (stream,
312 (char *)buffer + _bytes_written,
313 count - _bytes_written,
319 *bytes_written = _bytes_written;
324 g_warning ("Write returned zero without error");
326 _bytes_written += res;
330 *bytes_written = _bytes_written;