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: #GPollableInputStream / #GPollableOutputStream utilities
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
,
108 (GSourceDummyMarshal
)g_cclosure_marshal_generic
,
112 * g_pollable_source_new:
113 * @pollable_stream: the stream associated with the new source
115 * Utility method for #GPollableInputStream and #GPollableOutputStream
116 * implementations. Creates a new #GSource that expects a callback of
117 * type #GPollableSourceFunc. The new source does not actually do
118 * anything on its own; use g_source_add_child_source() to add other
119 * sources to it to cause it to trigger.
121 * Return value: (transfer full): the new #GSource.
126 g_pollable_source_new (GObject
*pollable_stream
)
129 GPollableSource
*pollable_source
;
131 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream
) ||
132 G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream
), NULL
);
134 source
= g_source_new (&pollable_source_funcs
, sizeof (GPollableSource
));
135 g_source_set_name (source
, "GPollableSource");
136 pollable_source
= (GPollableSource
*)source
;
137 pollable_source
->stream
= g_object_ref (pollable_stream
);
143 * g_pollable_source_new_full:
144 * @pollable_stream: (type GObject): the stream associated with the
146 * @child_source: (allow-none): optional child source to attach
147 * @cancellable: (allow-none): optional #GCancellable to attach
149 * Utility method for #GPollableInputStream and #GPollableOutputStream
150 * implementations. Creates a new #GSource, as with
151 * g_pollable_source_new(), but also attaching @child_source (with a
152 * dummy callback), and @cancellable, if they are non-%NULL.
154 * Return value: (transfer full): the new #GSource.
159 g_pollable_source_new_full (gpointer pollable_stream
,
160 GSource
*child_source
,
161 GCancellable
*cancellable
)
165 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream
) ||
166 G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream
), NULL
);
168 source
= g_pollable_source_new (pollable_stream
);
171 g_source_set_dummy_callback (child_source
);
172 g_source_add_child_source (source
, child_source
);
176 GSource
*cancellable_source
= g_cancellable_source_new (cancellable
);
178 g_source_set_dummy_callback (cancellable_source
);
179 g_source_add_child_source (source
, cancellable_source
);
180 g_source_unref (cancellable_source
);
187 * g_pollable_stream_read:
188 * @stream: a #GInputStream
189 * @buffer: a buffer to read data into
190 * @count: the number of bytes to read
191 * @blocking: whether to do blocking I/O
192 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
193 * @error: location to store the error occurring, or %NULL to ignore
195 * Tries to read from @stream, as with g_input_stream_read() (if
196 * @blocking is %TRUE) or g_pollable_input_stream_read_nonblocking()
197 * (if @blocking is %FALSE). This can be used to more easily share
198 * code between blocking and non-blocking implementations of a method.
200 * If @blocking is %FALSE, then @stream must be a
201 * #GPollableInputStream for which g_pollable_input_stream_can_poll()
202 * returns %TRUE, or else the behavior is undefined. If @blocking is
203 * %TRUE, then @stream does not need to be a #GPollableInputStream.
205 * Returns: the number of bytes read, or -1 on error.
210 g_pollable_stream_read (GInputStream
*stream
,
214 GCancellable
*cancellable
,
219 return g_input_stream_read (stream
,
225 return g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (stream
),
232 * g_pollable_stream_write:
233 * @stream: a #GOutputStream.
234 * @buffer: (array length=count) (element-type guint8): the buffer
235 * containing the data to write.
236 * @count: the number of bytes to write
237 * @blocking: whether to do blocking I/O
238 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
239 * @error: location to store the error occurring, or %NULL to ignore
241 * Tries to write to @stream, as with g_output_stream_write() (if
242 * @blocking is %TRUE) or g_pollable_output_stream_write_nonblocking()
243 * (if @blocking is %FALSE). This can be used to more easily share
244 * code between blocking and non-blocking implementations of a method.
246 * If @blocking is %FALSE, then @stream must be a
247 * #GPollableOutputStream for which
248 * g_pollable_output_stream_can_poll() returns %TRUE or else the
249 * behavior is undefined. If @blocking is %TRUE, then @stream does not
250 * need to be a #GPollableOutputStream.
252 * Returns: the number of bytes written, or -1 on error.
257 g_pollable_stream_write (GOutputStream
*stream
,
261 GCancellable
*cancellable
,
266 return g_output_stream_write (stream
,
272 return g_pollable_output_stream_write_nonblocking (G_POLLABLE_OUTPUT_STREAM (stream
),
279 * g_pollable_stream_write_all:
280 * @stream: a #GOutputStream.
281 * @buffer: (array length=count) (element-type guint8): the buffer
282 * containing the data to write.
283 * @count: the number of bytes to write
284 * @blocking: whether to do blocking I/O
285 * @bytes_written: (out): location to store the number of bytes that was
286 * written to the stream
287 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
288 * @error: location to store the error occurring, or %NULL to ignore
290 * Tries to write @count bytes to @stream, as with
291 * g_output_stream_write_all(), but using g_pollable_stream_write()
292 * rather than g_output_stream_write().
294 * On a successful write of @count bytes, %TRUE is returned, and
295 * @bytes_written is set to @count.
297 * If there is an error during the operation (including
298 * %G_IO_ERROR_WOULD_BLOCK in the non-blocking case), %FALSE is
299 * returned and @error is set to indicate the error status,
300 * @bytes_written is updated to contain the number of bytes written
301 * into the stream before the error occurred.
303 * As with g_pollable_stream_write(), if @blocking is %FALSE, then
304 * @stream must be a #GPollableOutputStream for which
305 * g_pollable_output_stream_can_poll() returns %TRUE or else the
306 * behavior is undefined. If @blocking is %TRUE, then @stream does not
307 * need to be a #GPollableOutputStream.
309 * Return value: %TRUE on success, %FALSE if there was an error
314 g_pollable_stream_write_all (GOutputStream
*stream
,
318 gsize
*bytes_written
,
319 GCancellable
*cancellable
,
322 gsize _bytes_written
;
326 while (_bytes_written
< count
)
328 res
= g_pollable_stream_write (stream
,
329 (char *)buffer
+ _bytes_written
,
330 count
- _bytes_written
,
336 *bytes_written
= _bytes_written
;
341 g_warning ("Write returned zero without error");
343 _bytes_written
+= res
;
347 *bytes_written
= _bytes_written
;