1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008 codethink
4 * Copyright © 2009 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Ryan Lortie <desrt@desrt.ca>
22 * Alexander Larsson <alexl@redhat.com>
29 #include "giostream.h"
30 #include <gio/gsimpleasyncresult.h>
31 #include <gio/gasyncresult.h>
35 G_DEFINE_TYPE (GIOStream, g_io_stream, G_TYPE_OBJECT);
39 * @short_description: Base class for implementing read/write streams
41 * @see_also: #GInputStream, #GOutputStream
43 * GIOStream represents an object that has both read and write streams.
44 * Generally the two streams acts as separate input and output streams,
45 * but they share some common resources and state. For instance, for
46 * seekable streams they may use the same position in both streams.
48 * Examples of #GIOStream objects are #GSocketConnection which represents
49 * a two-way network connection, and #GFileIOStream which represent a
50 * file handle opened in read-write mode.
52 * To do the actual reading and writing you need to get the substreams
53 * with g_io_stream_get_input_stream() and g_io_stream_get_output_stream().
55 * The #GIOStream object owns the input and the output streams, not the other
56 * way around, so keeping the substreams alive will not keep the #GIOStream
57 * object alive. If the #GIOStream object is freed it will be closed, thus
58 * closing the substream, so even if the substreams stay alive they will
59 * always just return a %G_IO_ERROR_CLOSED for all operations.
61 * To close a stream use g_io_stream_close() which will close the common
62 * stream object and also the individual substreams. You can also close
63 * the substreams themselves. In most cases this only marks the
64 * substream as closed, so further I/O on it fails. However, some streams
65 * may support "half-closed" states where one direction of the stream
66 * is actually shut down.
79 struct _GIOStreamPrivate {
82 GAsyncReadyCallback outstanding_callback;
85 static gboolean g_io_stream_real_close (GIOStream *stream,
86 GCancellable *cancellable,
88 static void g_io_stream_real_close_async (GIOStream *stream,
90 GCancellable *cancellable,
91 GAsyncReadyCallback callback,
93 static gboolean g_io_stream_real_close_finish (GIOStream *stream,
98 g_io_stream_finalize (GObject *object)
100 G_OBJECT_CLASS (g_io_stream_parent_class)->finalize (object);
104 g_io_stream_dispose (GObject *object)
108 stream = G_IO_STREAM (object);
110 if (!stream->priv->closed)
111 g_io_stream_close (stream, NULL, NULL);
113 G_OBJECT_CLASS (g_io_stream_parent_class)->dispose (object);
117 g_io_stream_init (GIOStream *stream)
119 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
125 g_io_stream_get_property (GObject *object,
130 GIOStream *stream = G_IO_STREAM (object);
135 g_value_set_boolean (value, stream->priv->closed);
138 case PROP_INPUT_STREAM:
139 g_value_set_object (value, g_io_stream_get_input_stream (stream));
142 case PROP_OUTPUT_STREAM:
143 g_value_set_object (value, g_io_stream_get_output_stream (stream));
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 g_io_stream_set_property (GObject *object,
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 g_io_stream_class_init (GIOStreamClass *klass)
167 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
169 g_type_class_add_private (klass, sizeof (GIOStreamPrivate));
171 gobject_class->finalize = g_io_stream_finalize;
172 gobject_class->dispose = g_io_stream_dispose;
173 gobject_class->set_property = g_io_stream_set_property;
174 gobject_class->get_property = g_io_stream_get_property;
176 klass->close_fn = g_io_stream_real_close;
177 klass->close_async = g_io_stream_real_close_async;
178 klass->close_finish = g_io_stream_real_close_finish;
180 g_object_class_install_property (gobject_class, PROP_CLOSED,
181 g_param_spec_boolean ("closed",
183 P_("Is the stream closed"),
185 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
188 g_param_spec_object ("input-stream",
190 P_("The GInputStream to read from"),
192 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
194 g_param_spec_object ("output-stream",
196 P_("The GOutputStream to write to"),
197 G_TYPE_OUTPUT_STREAM,
198 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
202 * g_io_stream_is_closed:
203 * @stream: a #GIOStream
205 * Checks if a stream is closed.
207 * Returns: %TRUE if the stream is closed.
212 g_io_stream_is_closed (GIOStream *stream)
214 g_return_val_if_fail (G_IS_IO_STREAM (stream), TRUE);
216 return stream->priv->closed;
220 * g_io_stream_get_input_stream:
221 * @stream: a #GIOStream
223 * Gets the input stream for this object. This is used
226 * Returns: a #GInputStream, owned by the #GIOStream. Do not free.
231 g_io_stream_get_input_stream (GIOStream *stream)
233 GIOStreamClass *klass;
235 klass = G_IO_STREAM_GET_CLASS (stream);
237 g_assert (klass->get_input_stream != NULL);
239 return klass->get_input_stream (stream);
243 * g_io_stream_get_output_stream:
244 * @stream: a #GIOStream
246 * Gets the output stream for this object. This is used for
249 * Returns: a #GOutputStream, owned by the #GIOStream. Do not free.
254 g_io_stream_get_output_stream (GIOStream *stream)
256 GIOStreamClass *klass;
258 klass = G_IO_STREAM_GET_CLASS (stream);
260 g_assert (klass->get_output_stream != NULL);
261 return klass->get_output_stream (stream);
265 * g_io_stream_has_pending:
266 * @stream: a #GIOStream
268 * Checks if a stream has pending actions.
270 * Returns: %TRUE if @stream has pending actions.
275 g_io_stream_has_pending (GIOStream *stream)
277 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
279 return stream->priv->pending;
283 * g_io_stream_set_pending:
284 * @stream: a #GIOStream
285 * @error: a #GError location to store the error occuring, or %NULL to
288 * Sets @stream to have actions pending. If the pending flag is
289 * already set or @stream is closed, it will return %FALSE and set
292 * Return value: %TRUE if pending was previously unset and is now set.
297 g_io_stream_set_pending (GIOStream *stream,
300 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
302 if (stream->priv->closed)
304 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
305 _("Stream is already closed"));
309 if (stream->priv->pending)
311 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
312 /* Translators: This is an error you get if there is
313 * already an operation running against this stream when
314 * you try to start one */
315 _("Stream has outstanding operation"));
319 stream->priv->pending = TRUE;
324 * g_io_stream_clear_pending:
325 * @stream: a #GIOStream
327 * Clears the pending flag on @stream.
332 g_io_stream_clear_pending (GIOStream *stream)
334 g_return_if_fail (G_IS_IO_STREAM (stream));
336 stream->priv->pending = FALSE;
340 g_io_stream_real_close (GIOStream *stream,
341 GCancellable *cancellable,
346 res = g_output_stream_close (g_io_stream_get_output_stream (stream),
349 /* If this errored out, unset error so that we don't report
350 further errors, but still do the following ops */
351 if (error != NULL && *error != NULL)
354 res &= g_input_stream_close (g_io_stream_get_input_stream (stream),
362 * @stream: a #GIOStream
363 * @cancellable: optional #GCancellable object, %NULL to ignore
364 * @error: location to store the error occuring, or %NULL to ignore
366 * Closes the stream, releasing resources related to it. This will also
367 * closes the individual input and output streams, if they are not already
370 * Once the stream is closed, all other operations will return
371 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
374 * Closing a stream will automatically flush any outstanding buffers
377 * Streams will be automatically closed when the last reference
378 * is dropped, but you might want to call this function to make sure
379 * resources are released as early as possible.
381 * Some streams might keep the backing store of the stream (e.g. a file
382 * descriptor) open after the stream is closed. See the documentation for
383 * the individual stream for details.
385 * On failure the first error that happened will be reported, but the
386 * close operation will finish as much as possible. A stream that failed
387 * to close will still return %G_IO_ERROR_CLOSED for all operations.
388 * Still, it is important to check and report the error to the user,
389 * otherwise there might be a loss of data as all data might not be written.
391 * If @cancellable is not NULL, then the operation can be cancelled by
392 * triggering the cancellable object from another thread. If the operation
393 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
394 * Cancelling a close will still leave the stream closed, but some streams
395 * can use a faster close that doesn't block to e.g. check errors.
397 * The default implementation of this method just calls close on the
398 * individual input/output streams.
400 * Return value: %TRUE on success, %FALSE on failure
405 g_io_stream_close (GIOStream *stream,
406 GCancellable *cancellable,
409 GIOStreamClass *class;
412 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
414 class = G_IO_STREAM_GET_CLASS (stream);
416 if (stream->priv->closed)
419 if (!g_io_stream_set_pending (stream, error))
423 g_cancellable_push_current (cancellable);
427 res = class->close_fn (stream, cancellable, error);
430 g_cancellable_pop_current (cancellable);
432 stream->priv->closed = TRUE;
433 g_io_stream_clear_pending (stream);
439 async_ready_close_callback_wrapper (GObject *source_object,
443 GIOStream *stream = G_IO_STREAM (source_object);
445 stream->priv->closed = TRUE;
446 g_io_stream_clear_pending (stream);
447 if (stream->priv->outstanding_callback)
448 (*stream->priv->outstanding_callback) (source_object, res, user_data);
449 g_object_unref (stream);
453 * g_io_stream_close_async:
454 * @stream: a #GIOStream
455 * @io_priority: the io priority of the request
456 * @callback: callback to call when the request is satisfied
457 * @user_data: the data to pass to callback function
458 * @cancellable: optional cancellable object
460 * Requests an asynchronous close of the stream, releasing resources
461 * related to it. When the operation is finished @callback will be
462 * called. You can then call g_io_stream_close_finish() to get
463 * the result of the operation.
465 * For behaviour details see g_io_stream_close().
467 * The asynchronous methods have a default fallback that uses threads
468 * to implement asynchronicity, so they are optional for inheriting
469 * classes. However, if you override one you must override all.
474 g_io_stream_close_async (GIOStream *stream,
476 GCancellable *cancellable,
477 GAsyncReadyCallback callback,
480 GIOStreamClass *class;
481 GSimpleAsyncResult *simple;
482 GError *error = NULL;
484 g_return_if_fail (G_IS_IO_STREAM (stream));
486 if (stream->priv->closed)
488 simple = g_simple_async_result_new (G_OBJECT (stream),
491 g_io_stream_close_async);
492 g_simple_async_result_complete_in_idle (simple);
493 g_object_unref (simple);
497 if (!g_io_stream_set_pending (stream, &error))
499 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
503 g_error_free (error);
507 class = G_IO_STREAM_GET_CLASS (stream);
508 stream->priv->outstanding_callback = callback;
509 g_object_ref (stream);
510 class->close_async (stream, io_priority, cancellable,
511 async_ready_close_callback_wrapper, user_data);
515 * g_io_stream_close_finish:
516 * @stream: a #GIOStream
517 * @result: a #GAsyncResult
518 * @error: a #GError location to store the error occuring, or %NULL to
523 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
528 g_io_stream_close_finish (GIOStream *stream,
529 GAsyncResult *result,
532 GSimpleAsyncResult *simple;
533 GIOStreamClass *class;
535 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
536 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
538 if (G_IS_SIMPLE_ASYNC_RESULT (result))
540 simple = G_SIMPLE_ASYNC_RESULT (result);
541 if (g_simple_async_result_propagate_error (simple, error))
544 /* Special case already closed */
545 if (g_simple_async_result_get_source_tag (simple) == g_io_stream_close_async)
549 class = G_IO_STREAM_GET_CLASS (stream);
550 return class->close_finish (stream, result, error);
555 close_async_thread (GSimpleAsyncResult *res,
557 GCancellable *cancellable)
559 GIOStreamClass *class;
560 GError *error = NULL;
563 /* Auto handling of cancelation disabled, and ignore cancellation,
564 * since we want to close things anyway, although possibly in a
565 * quick-n-dirty way. At least we never want to leak open handles
567 class = G_IO_STREAM_GET_CLASS (object);
570 result = class->close_fn (G_IO_STREAM (object), cancellable, &error);
573 g_simple_async_result_set_from_error (res, error);
574 g_error_free (error);
580 g_io_stream_real_close_async (GIOStream *stream,
582 GCancellable *cancellable,
583 GAsyncReadyCallback callback,
586 GSimpleAsyncResult *res;
588 res = g_simple_async_result_new (G_OBJECT (stream),
591 g_io_stream_real_close_async);
593 g_simple_async_result_set_handle_cancellation (res, FALSE);
595 g_simple_async_result_run_in_thread (res,
599 g_object_unref (res);
603 g_io_stream_real_close_finish (GIOStream *stream,
604 GAsyncResult *result,
607 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
608 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
609 g_io_stream_real_close_async);
613 #define __G_IO_STREAM_C__
614 #include "gioaliasdef.c"