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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
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_prepare (GSource *source,
53 pollable_source_check (GSource *source)
59 pollable_source_dispatch (GSource *source,
63 GPollableSourceFunc func = (GPollableSourceFunc)callback;
64 GPollableSource *pollable_source = (GPollableSource *)source;
66 return (*func) (pollable_source->stream, user_data);
70 pollable_source_finalize (GSource *source)
72 GPollableSource *pollable_source = (GPollableSource *)source;
74 g_object_unref (pollable_source->stream);
78 pollable_source_closure_callback (GObject *stream,
81 GClosure *closure = data;
83 GValue param = G_VALUE_INIT;
84 GValue result_value = G_VALUE_INIT;
87 g_value_init (&result_value, G_TYPE_BOOLEAN);
89 g_value_init (¶m, G_TYPE_OBJECT);
90 g_value_set_object (¶m, stream);
92 g_closure_invoke (closure, &result_value, 1, ¶m, NULL);
94 result = g_value_get_boolean (&result_value);
95 g_value_unset (&result_value);
96 g_value_unset (¶m);
101 static GSourceFuncs pollable_source_funcs =
103 pollable_source_prepare,
104 pollable_source_check,
105 pollable_source_dispatch,
106 pollable_source_finalize,
107 (GSourceFunc)pollable_source_closure_callback,
111 * g_pollable_source_new:
112 * @pollable_stream: the stream associated with the new source
114 * Utility method for #GPollableInputStream and #GPollableOutputStream
115 * implementations. Creates a new #GSource that expects a callback of
116 * type #GPollableSourceFunc. The new source does not actually do
117 * anything on its own; use g_source_add_child_source() to add other
118 * sources to it to cause it to trigger.
120 * Return value: (transfer full): the new #GSource.
125 g_pollable_source_new (GObject *pollable_stream)
128 GPollableSource *pollable_source;
130 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream) ||
131 G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream), NULL);
133 source = g_source_new (&pollable_source_funcs, sizeof (GPollableSource));
134 g_source_set_name (source, "GPollableSource");
135 pollable_source = (GPollableSource *)source;
136 pollable_source->stream = g_object_ref (pollable_stream);
142 * g_pollable_source_new_full:
143 * @pollable_stream: (type GObject): the stream associated with the
145 * @child_source: (allow-none): optional child source to attach
146 * @cancellable: (allow-none): optional #GCancellable to attach
148 * Utility method for #GPollableInputStream and #GPollableOutputStream
149 * implementations. Creates a new #GSource, as with
150 * g_pollable_source_new(), but also attaching @child_source (with a
151 * dummy callback), and @cancellable, if they are non-%NULL.
153 * Return value: (transfer full): the new #GSource.
158 g_pollable_source_new_full (gpointer pollable_stream,
159 GSource *child_source,
160 GCancellable *cancellable)
164 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream) ||
165 G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream), NULL);
167 source = g_pollable_source_new (pollable_stream);
170 g_source_set_dummy_callback (child_source);
171 g_source_add_child_source (source, child_source);
175 GSource *cancellable_source = g_cancellable_source_new (cancellable);
177 g_source_set_dummy_callback (cancellable_source);
178 g_source_add_child_source (source, cancellable_source);
179 g_source_unref (cancellable_source);
186 * g_pollable_stream_read:
187 * @stream: a #GInputStream
188 * @buffer: a buffer to read data into
189 * @count: the number of bytes to read
190 * @blocking: whether to do blocking I/O
191 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
192 * @error: location to store the error occurring, or %NULL to ignore
194 * Tries to read from @stream, as with g_input_stream_read() (if
195 * @blocking is %TRUE) or g_pollable_input_stream_read_nonblocking()
196 * (if @blocking is %FALSE). This can be used to more easily share
197 * code between blocking and non-blocking implementations of a method.
199 * If @blocking is %FALSE, then @stream must be a
200 * #GPollableInputStream for which g_pollable_input_stream_can_poll()
201 * returns %TRUE, or else the behavior is undefined. If @blocking is
202 * %TRUE, then @stream does not need to be a #GPollableInputStream.
204 * Returns: the number of bytes read, or -1 on error.
209 g_pollable_stream_read (GInputStream *stream,
213 GCancellable *cancellable,
218 return g_input_stream_read (stream,
224 return g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (stream),
231 * g_pollable_stream_write:
232 * @stream: a #GOutputStream.
233 * @buffer: (array length=count) (element-type guint8): the buffer
234 * containing the data to write.
235 * @count: the number of bytes to write
236 * @blocking: whether to do blocking I/O
237 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
238 * @error: location to store the error occurring, or %NULL to ignore
240 * Tries to write to @stream, as with g_output_stream_write() (if
241 * @blocking is %TRUE) or g_pollable_output_stream_write_nonblocking()
242 * (if @blocking is %FALSE). This can be used to more easily share
243 * code between blocking and non-blocking implementations of a method.
245 * If @blocking is %FALSE, then @stream must be a
246 * #GPollableOutputStream for which
247 * g_pollable_output_stream_can_poll() returns %TRUE or else the
248 * behavior is undefined. If @blocking is %TRUE, then @stream does not
249 * need to be a #GPollableOutputStream.
251 * Returns: the number of bytes written, or -1 on error.
256 g_pollable_stream_write (GOutputStream *stream,
260 GCancellable *cancellable,
265 return g_output_stream_write (stream,
271 return g_pollable_output_stream_write_nonblocking (G_POLLABLE_OUTPUT_STREAM (stream),
278 * g_pollable_stream_write_all:
279 * @stream: a #GOutputStream.
280 * @buffer: (array length=count) (element-type guint8): the buffer
281 * containing the data to write.
282 * @count: the number of bytes to write
283 * @blocking: whether to do blocking I/O
284 * @bytes_written: (out): location to store the number of bytes that was
285 * written to the stream
286 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
287 * @error: location to store the error occurring, or %NULL to ignore
289 * Tries to write @count bytes to @stream, as with
290 * g_output_stream_write_all(), but using g_pollable_stream_write()
291 * rather than g_output_stream_write().
293 * On a successful write of @count bytes, %TRUE is returned, and
294 * @bytes_written is set to @count.
296 * If there is an error during the operation (including
297 * %G_IO_ERROR_WOULD_BLOCK in the non-blocking case), %FALSE is
298 * returned and @error is set to indicate the error status,
299 * @bytes_written is updated to contain the number of bytes written
300 * into the stream before the error occurred.
302 * As with g_pollable_stream_write(), if @blocking is %FALSE, then
303 * @stream must be a #GPollableOutputStream for which
304 * g_pollable_output_stream_can_poll() returns %TRUE or else the
305 * behavior is undefined. If @blocking is %TRUE, then @stream does not
306 * need to be a #GPollableOutputStream.
308 * Return value: %TRUE on success, %FALSE if there was an error
313 g_pollable_stream_write_all (GOutputStream *stream,
317 gsize *bytes_written,
318 GCancellable *cancellable,
321 gsize _bytes_written;
325 while (_bytes_written < count)
327 res = g_pollable_stream_write (stream,
328 (char *)buffer + _bytes_written,
329 count - _bytes_written,
335 *bytes_written = _bytes_written;
340 g_warning ("Write returned zero without error");
342 _bytes_written += res;
346 *bytes_written = _bytes_written;