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>
34 G_DEFINE_TYPE (GIOStream, g_io_stream, G_TYPE_OBJECT);
38 * @short_description: Base class for implementing read/write streams
40 * @see_also: #GInputStream, #GOutputStream
42 * GIOStream represents an object that has both read and write streams.
43 * Generally the two streams acts as separate input and output streams,
44 * but they share some common resources and state. For instance, for
45 * seekable streams they may use the same position in both streams.
47 * Examples of #GIOStream objects are #GSocketConnection which represents
48 * a two-way network connection, and #GFileIOStream which represent a
49 * file handle opened in read-write mode.
51 * To do the actual reading and writing you need to get the substreams
52 * with g_io_stream_get_input_stream() and g_io_stream_get_output_stream().
54 * The #GIOStream object owns the input and the output streams, not the other
55 * way around, so keeping the substreams alive will not keep the #GIOStream
56 * object alive. If the #GIOStream object is freed it will be closed, thus
57 * closing the substream, so even if the substreams stay alive they will
58 * always just return a %G_IO_ERROR_CLOSED for all operations.
60 * To close a stream use g_io_stream_close() which will close the common
61 * stream object and also the individual substreams. You can also close
62 * the substreams themselves. In most cases this only marks the
63 * substream as closed, so further I/O on it fails. However, some streams
64 * may support "half-closed" states where one direction of the stream
65 * is actually shut down.
78 struct _GIOStreamPrivate {
81 GAsyncReadyCallback outstanding_callback;
84 static gboolean g_io_stream_real_close (GIOStream *stream,
85 GCancellable *cancellable,
87 static void g_io_stream_real_close_async (GIOStream *stream,
89 GCancellable *cancellable,
90 GAsyncReadyCallback callback,
92 static gboolean g_io_stream_real_close_finish (GIOStream *stream,
97 g_io_stream_finalize (GObject *object)
99 G_OBJECT_CLASS (g_io_stream_parent_class)->finalize (object);
103 g_io_stream_dispose (GObject *object)
107 stream = G_IO_STREAM (object);
109 if (!stream->priv->closed)
110 g_io_stream_close (stream, NULL, NULL);
112 G_OBJECT_CLASS (g_io_stream_parent_class)->dispose (object);
116 g_io_stream_init (GIOStream *stream)
118 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
124 g_io_stream_get_property (GObject *object,
129 GIOStream *stream = G_IO_STREAM (object);
134 g_value_set_boolean (value, stream->priv->closed);
137 case PROP_INPUT_STREAM:
138 g_value_set_object (value, g_io_stream_get_input_stream (stream));
141 case PROP_OUTPUT_STREAM:
142 g_value_set_object (value, g_io_stream_get_output_stream (stream));
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 g_io_stream_set_property (GObject *object,
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164 g_io_stream_class_init (GIOStreamClass *klass)
166 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
168 g_type_class_add_private (klass, sizeof (GIOStreamPrivate));
170 gobject_class->finalize = g_io_stream_finalize;
171 gobject_class->dispose = g_io_stream_dispose;
172 gobject_class->set_property = g_io_stream_set_property;
173 gobject_class->get_property = g_io_stream_get_property;
175 klass->close_fn = g_io_stream_real_close;
176 klass->close_async = g_io_stream_real_close_async;
177 klass->close_finish = g_io_stream_real_close_finish;
179 g_object_class_install_property (gobject_class, PROP_CLOSED,
180 g_param_spec_boolean ("closed",
182 P_("Is the stream closed"),
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
187 g_param_spec_object ("input-stream",
189 P_("The GInputStream to read from"),
191 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
193 g_param_spec_object ("output-stream",
195 P_("The GOutputStream to write to"),
196 G_TYPE_OUTPUT_STREAM,
197 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
201 * g_io_stream_is_closed:
202 * @stream: a #GIOStream
204 * Checks if a stream is closed.
206 * Returns: %TRUE if the stream is closed.
211 g_io_stream_is_closed (GIOStream *stream)
213 g_return_val_if_fail (G_IS_IO_STREAM (stream), TRUE);
215 return stream->priv->closed;
219 * g_io_stream_get_input_stream:
220 * @stream: a #GIOStream
222 * Gets the input stream for this object. This is used
225 * Returns: (transfer none): a #GInputStream, owned by the #GIOStream.
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: (transfer none): a #GOutputStream, owned by the #GIOStream.
255 g_io_stream_get_output_stream (GIOStream *stream)
257 GIOStreamClass *klass;
259 klass = G_IO_STREAM_GET_CLASS (stream);
261 g_assert (klass->get_output_stream != NULL);
262 return klass->get_output_stream (stream);
266 * g_io_stream_has_pending:
267 * @stream: a #GIOStream
269 * Checks if a stream has pending actions.
271 * Returns: %TRUE if @stream has pending actions.
276 g_io_stream_has_pending (GIOStream *stream)
278 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
280 return stream->priv->pending;
284 * g_io_stream_set_pending:
285 * @stream: a #GIOStream
286 * @error: a #GError location to store the error occuring, or %NULL to
289 * Sets @stream to have actions pending. If the pending flag is
290 * already set or @stream is closed, it will return %FALSE and set
293 * Return value: %TRUE if pending was previously unset and is now set.
298 g_io_stream_set_pending (GIOStream *stream,
301 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
303 if (stream->priv->closed)
305 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
306 _("Stream is already closed"));
310 if (stream->priv->pending)
312 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
313 /* Translators: This is an error you get if there is
314 * already an operation running against this stream when
315 * you try to start one */
316 _("Stream has outstanding operation"));
320 stream->priv->pending = TRUE;
325 * g_io_stream_clear_pending:
326 * @stream: a #GIOStream
328 * Clears the pending flag on @stream.
333 g_io_stream_clear_pending (GIOStream *stream)
335 g_return_if_fail (G_IS_IO_STREAM (stream));
337 stream->priv->pending = FALSE;
341 g_io_stream_real_close (GIOStream *stream,
342 GCancellable *cancellable,
347 res = g_output_stream_close (g_io_stream_get_output_stream (stream),
350 /* If this errored out, unset error so that we don't report
351 further errors, but still do the following ops */
352 if (error != NULL && *error != NULL)
355 res &= g_input_stream_close (g_io_stream_get_input_stream (stream),
363 * @stream: a #GIOStream
364 * @cancellable: optional #GCancellable object, %NULL to ignore
365 * @error: location to store the error occuring, or %NULL to ignore
367 * Closes the stream, releasing resources related to it. This will also
368 * closes the individual input and output streams, if they are not already
371 * Once the stream is closed, all other operations will return
372 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
375 * Closing a stream will automatically flush any outstanding buffers
378 * Streams will be automatically closed when the last reference
379 * is dropped, but you might want to call this function to make sure
380 * resources are released as early as possible.
382 * Some streams might keep the backing store of the stream (e.g. a file
383 * descriptor) open after the stream is closed. See the documentation for
384 * the individual stream for details.
386 * On failure the first error that happened will be reported, but the
387 * close operation will finish as much as possible. A stream that failed
388 * to close will still return %G_IO_ERROR_CLOSED for all operations.
389 * Still, it is important to check and report the error to the user,
390 * otherwise there might be a loss of data as all data might not be written.
392 * If @cancellable is not NULL, then the operation can be cancelled by
393 * triggering the cancellable object from another thread. If the operation
394 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
395 * Cancelling a close will still leave the stream closed, but some streams
396 * can use a faster close that doesn't block to e.g. check errors.
398 * The default implementation of this method just calls close on the
399 * individual input/output streams.
401 * Return value: %TRUE on success, %FALSE on failure
406 g_io_stream_close (GIOStream *stream,
407 GCancellable *cancellable,
410 GIOStreamClass *class;
413 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
415 class = G_IO_STREAM_GET_CLASS (stream);
417 if (stream->priv->closed)
420 if (!g_io_stream_set_pending (stream, error))
424 g_cancellable_push_current (cancellable);
428 res = class->close_fn (stream, cancellable, error);
431 g_cancellable_pop_current (cancellable);
433 stream->priv->closed = TRUE;
434 g_io_stream_clear_pending (stream);
440 async_ready_close_callback_wrapper (GObject *source_object,
444 GIOStream *stream = G_IO_STREAM (source_object);
446 stream->priv->closed = TRUE;
447 g_io_stream_clear_pending (stream);
448 if (stream->priv->outstanding_callback)
449 (*stream->priv->outstanding_callback) (source_object, res, user_data);
450 g_object_unref (stream);
454 * g_io_stream_close_async:
455 * @stream: a #GIOStream
456 * @io_priority: the io priority of the request
457 * @callback: callback to call when the request is satisfied
458 * @user_data: the data to pass to callback function
459 * @cancellable: optional cancellable object
461 * Requests an asynchronous close of the stream, releasing resources
462 * related to it. When the operation is finished @callback will be
463 * called. You can then call g_io_stream_close_finish() to get
464 * the result of the operation.
466 * For behaviour details see g_io_stream_close().
468 * The asynchronous methods have a default fallback that uses threads
469 * to implement asynchronicity, so they are optional for inheriting
470 * classes. However, if you override one you must override all.
475 g_io_stream_close_async (GIOStream *stream,
477 GCancellable *cancellable,
478 GAsyncReadyCallback callback,
481 GIOStreamClass *class;
482 GSimpleAsyncResult *simple;
483 GError *error = NULL;
485 g_return_if_fail (G_IS_IO_STREAM (stream));
487 if (stream->priv->closed)
489 simple = g_simple_async_result_new (G_OBJECT (stream),
492 g_io_stream_close_async);
493 g_simple_async_result_complete_in_idle (simple);
494 g_object_unref (simple);
498 if (!g_io_stream_set_pending (stream, &error))
500 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
504 g_error_free (error);
508 class = G_IO_STREAM_GET_CLASS (stream);
509 stream->priv->outstanding_callback = callback;
510 g_object_ref (stream);
511 class->close_async (stream, io_priority, cancellable,
512 async_ready_close_callback_wrapper, user_data);
516 * g_io_stream_close_finish:
517 * @stream: a #GIOStream
518 * @result: a #GAsyncResult
519 * @error: a #GError location to store the error occuring, or %NULL to
524 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
529 g_io_stream_close_finish (GIOStream *stream,
530 GAsyncResult *result,
533 GSimpleAsyncResult *simple;
534 GIOStreamClass *class;
536 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
537 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
539 if (G_IS_SIMPLE_ASYNC_RESULT (result))
541 simple = G_SIMPLE_ASYNC_RESULT (result);
542 if (g_simple_async_result_propagate_error (simple, error))
545 /* Special case already closed */
546 if (g_simple_async_result_get_source_tag (simple) == g_io_stream_close_async)
550 class = G_IO_STREAM_GET_CLASS (stream);
551 return class->close_finish (stream, result, error);
556 close_async_thread (GSimpleAsyncResult *res,
558 GCancellable *cancellable)
560 GIOStreamClass *class;
561 GError *error = NULL;
564 /* Auto handling of cancelation disabled, and ignore cancellation,
565 * since we want to close things anyway, although possibly in a
566 * quick-n-dirty way. At least we never want to leak open handles
568 class = G_IO_STREAM_GET_CLASS (object);
571 result = class->close_fn (G_IO_STREAM (object), cancellable, &error);
574 g_simple_async_result_set_from_error (res, error);
575 g_error_free (error);
581 g_io_stream_real_close_async (GIOStream *stream,
583 GCancellable *cancellable,
584 GAsyncReadyCallback callback,
587 GSimpleAsyncResult *res;
589 res = g_simple_async_result_new (G_OBJECT (stream),
592 g_io_stream_real_close_async);
594 g_simple_async_result_set_handle_cancellation (res, FALSE);
596 g_simple_async_result_run_in_thread (res,
600 g_object_unref (res);
604 g_io_stream_real_close_finish (GIOStream *stream,
605 GAsyncResult *result,
608 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
609 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
610 g_io_stream_real_close_async);