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, see <http://www.gnu.org/licenses/>.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
20 * Alexander Larsson <alexl@redhat.com>
27 #include "giostream.h"
28 #include "gasyncresult.h"
33 * @short_description: Base class for implementing read/write streams
35 * @see_also: #GInputStream, #GOutputStream
37 * GIOStream represents an object that has both read and write streams.
38 * Generally the two streams acts as separate input and output streams,
39 * but they share some common resources and state. For instance, for
40 * seekable streams they may use the same position in both streams.
42 * Examples of #GIOStream objects are #GSocketConnection which represents
43 * a two-way network connection, and #GFileIOStream which represent a
44 * file handle opened in read-write mode.
46 * To do the actual reading and writing you need to get the substreams
47 * with g_io_stream_get_input_stream() and g_io_stream_get_output_stream().
49 * The #GIOStream object owns the input and the output streams, not the other
50 * way around, so keeping the substreams alive will not keep the #GIOStream
51 * object alive. If the #GIOStream object is freed it will be closed, thus
52 * closing the substream, so even if the substreams stay alive they will
53 * always just return a %G_IO_ERROR_CLOSED for all operations.
55 * To close a stream use g_io_stream_close() which will close the common
56 * stream object and also the individual substreams. You can also close
57 * the substreams themselves. In most cases this only marks the
58 * substream as closed, so further I/O on it fails but common state in the
59 * #GIOStream may still be open. However, some streams may support
60 * "half-closed" states where one direction of the stream is actually shut down.
73 struct _GIOStreamPrivate {
78 static gboolean g_io_stream_real_close (GIOStream *stream,
79 GCancellable *cancellable,
81 static void g_io_stream_real_close_async (GIOStream *stream,
83 GCancellable *cancellable,
84 GAsyncReadyCallback callback,
86 static gboolean g_io_stream_real_close_finish (GIOStream *stream,
90 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GIOStream, g_io_stream, G_TYPE_OBJECT)
93 g_io_stream_dispose (GObject *object)
97 stream = G_IO_STREAM (object);
99 if (!stream->priv->closed)
100 g_io_stream_close (stream, NULL, NULL);
102 G_OBJECT_CLASS (g_io_stream_parent_class)->dispose (object);
106 g_io_stream_init (GIOStream *stream)
108 stream->priv = g_io_stream_get_instance_private (stream);
112 g_io_stream_get_property (GObject *object,
117 GIOStream *stream = G_IO_STREAM (object);
122 g_value_set_boolean (value, stream->priv->closed);
125 case PROP_INPUT_STREAM:
126 g_value_set_object (value, g_io_stream_get_input_stream (stream));
129 case PROP_OUTPUT_STREAM:
130 g_value_set_object (value, g_io_stream_get_output_stream (stream));
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 g_io_stream_class_init (GIOStreamClass *klass)
141 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
143 gobject_class->dispose = g_io_stream_dispose;
144 gobject_class->get_property = g_io_stream_get_property;
146 klass->close_fn = g_io_stream_real_close;
147 klass->close_async = g_io_stream_real_close_async;
148 klass->close_finish = g_io_stream_real_close_finish;
150 g_object_class_install_property (gobject_class, PROP_CLOSED,
151 g_param_spec_boolean ("closed",
153 P_("Is the stream closed"),
155 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
158 g_param_spec_object ("input-stream",
160 P_("The GInputStream to read from"),
162 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
164 g_param_spec_object ("output-stream",
166 P_("The GOutputStream to write to"),
167 G_TYPE_OUTPUT_STREAM,
168 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
172 * g_io_stream_is_closed:
173 * @stream: a #GIOStream
175 * Checks if a stream is closed.
177 * Returns: %TRUE if the stream is closed.
182 g_io_stream_is_closed (GIOStream *stream)
184 g_return_val_if_fail (G_IS_IO_STREAM (stream), TRUE);
186 return stream->priv->closed;
190 * g_io_stream_get_input_stream:
191 * @stream: a #GIOStream
193 * Gets the input stream for this object. This is used
196 * Returns: (transfer none): a #GInputStream, owned by the #GIOStream.
202 g_io_stream_get_input_stream (GIOStream *stream)
204 GIOStreamClass *klass;
206 klass = G_IO_STREAM_GET_CLASS (stream);
208 g_assert (klass->get_input_stream != NULL);
210 return klass->get_input_stream (stream);
214 * g_io_stream_get_output_stream:
215 * @stream: a #GIOStream
217 * Gets the output stream for this object. This is used for
220 * Returns: (transfer none): a #GOutputStream, owned by the #GIOStream.
226 g_io_stream_get_output_stream (GIOStream *stream)
228 GIOStreamClass *klass;
230 klass = G_IO_STREAM_GET_CLASS (stream);
232 g_assert (klass->get_output_stream != NULL);
233 return klass->get_output_stream (stream);
237 * g_io_stream_has_pending:
238 * @stream: a #GIOStream
240 * Checks if a stream has pending actions.
242 * Returns: %TRUE if @stream has pending actions.
247 g_io_stream_has_pending (GIOStream *stream)
249 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
251 return stream->priv->pending;
255 * g_io_stream_set_pending:
256 * @stream: a #GIOStream
257 * @error: a #GError location to store the error occurring, or %NULL to
260 * Sets @stream to have actions pending. If the pending flag is
261 * already set or @stream is closed, it will return %FALSE and set
264 * Returns: %TRUE if pending was previously unset and is now set.
269 g_io_stream_set_pending (GIOStream *stream,
272 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
274 if (stream->priv->closed)
276 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
277 _("Stream is already closed"));
281 if (stream->priv->pending)
283 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
284 /* Translators: This is an error you get if there is
285 * already an operation running against this stream when
286 * you try to start one */
287 _("Stream has outstanding operation"));
291 stream->priv->pending = TRUE;
296 * g_io_stream_clear_pending:
297 * @stream: a #GIOStream
299 * Clears the pending flag on @stream.
304 g_io_stream_clear_pending (GIOStream *stream)
306 g_return_if_fail (G_IS_IO_STREAM (stream));
308 stream->priv->pending = FALSE;
312 g_io_stream_real_close (GIOStream *stream,
313 GCancellable *cancellable,
318 res = g_output_stream_close (g_io_stream_get_output_stream (stream),
321 /* If this errored out, unset error so that we don't report
322 further errors, but still do the following ops */
323 if (error != NULL && *error != NULL)
326 res &= g_input_stream_close (g_io_stream_get_input_stream (stream),
334 * @stream: a #GIOStream
335 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
336 * @error: location to store the error occurring, or %NULL to ignore
338 * Closes the stream, releasing resources related to it. This will also
339 * closes the individual input and output streams, if they are not already
342 * Once the stream is closed, all other operations will return
343 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
346 * Closing a stream will automatically flush any outstanding buffers
349 * Streams will be automatically closed when the last reference
350 * is dropped, but you might want to call this function to make sure
351 * resources are released as early as possible.
353 * Some streams might keep the backing store of the stream (e.g. a file
354 * descriptor) open after the stream is closed. See the documentation for
355 * the individual stream for details.
357 * On failure the first error that happened will be reported, but the
358 * close operation will finish as much as possible. A stream that failed
359 * to close will still return %G_IO_ERROR_CLOSED for all operations.
360 * Still, it is important to check and report the error to the user,
361 * otherwise there might be a loss of data as all data might not be written.
363 * If @cancellable is not NULL, then the operation can be cancelled by
364 * triggering the cancellable object from another thread. If the operation
365 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
366 * Cancelling a close will still leave the stream closed, but some streams
367 * can use a faster close that doesn't block to e.g. check errors.
369 * The default implementation of this method just calls close on the
370 * individual input/output streams.
372 * Returns: %TRUE on success, %FALSE on failure
377 g_io_stream_close (GIOStream *stream,
378 GCancellable *cancellable,
381 GIOStreamClass *class;
384 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
386 class = G_IO_STREAM_GET_CLASS (stream);
388 if (stream->priv->closed)
391 if (!g_io_stream_set_pending (stream, error))
395 g_cancellable_push_current (cancellable);
399 res = class->close_fn (stream, cancellable, error);
402 g_cancellable_pop_current (cancellable);
404 stream->priv->closed = TRUE;
405 g_io_stream_clear_pending (stream);
411 async_ready_close_callback_wrapper (GObject *source_object,
415 GIOStream *stream = G_IO_STREAM (source_object);
416 GIOStreamClass *klass = G_IO_STREAM_GET_CLASS (stream);
417 GTask *task = user_data;
418 GError *error = NULL;
421 stream->priv->closed = TRUE;
422 g_io_stream_clear_pending (stream);
424 if (g_async_result_legacy_propagate_error (res, &error))
427 success = klass->close_finish (stream, res, &error);
430 g_task_return_error (task, error);
432 g_task_return_boolean (task, success);
434 g_object_unref (task);
438 * g_io_stream_close_async:
439 * @stream: a #GIOStream
440 * @io_priority: the io priority of the request
441 * @cancellable: (allow-none): optional cancellable object
442 * @callback: (scope async): callback to call when the request is satisfied
443 * @user_data: (closure): the data to pass to callback function
445 * Requests an asynchronous close of the stream, releasing resources
446 * related to it. When the operation is finished @callback will be
447 * called. You can then call g_io_stream_close_finish() to get
448 * the result of the operation.
450 * For behaviour details see g_io_stream_close().
452 * The asynchronous methods have a default fallback that uses threads
453 * to implement asynchronicity, so they are optional for inheriting
454 * classes. However, if you override one you must override all.
459 g_io_stream_close_async (GIOStream *stream,
461 GCancellable *cancellable,
462 GAsyncReadyCallback callback,
465 GIOStreamClass *class;
466 GError *error = NULL;
469 g_return_if_fail (G_IS_IO_STREAM (stream));
471 task = g_task_new (stream, cancellable, callback, user_data);
473 if (stream->priv->closed)
475 g_task_return_boolean (task, TRUE);
476 g_object_unref (task);
480 if (!g_io_stream_set_pending (stream, &error))
482 g_task_return_error (task, error);
483 g_object_unref (task);
487 class = G_IO_STREAM_GET_CLASS (stream);
489 class->close_async (stream, io_priority, cancellable,
490 async_ready_close_callback_wrapper, task);
494 * g_io_stream_close_finish:
495 * @stream: a #GIOStream
496 * @result: a #GAsyncResult
497 * @error: a #GError location to store the error occurring, or %NULL to
502 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
507 g_io_stream_close_finish (GIOStream *stream,
508 GAsyncResult *result,
511 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
512 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
514 return g_task_propagate_boolean (G_TASK (result), error);
519 close_async_thread (GTask *task,
520 gpointer source_object,
522 GCancellable *cancellable)
524 GIOStream *stream = source_object;
525 GIOStreamClass *class;
526 GError *error = NULL;
529 class = G_IO_STREAM_GET_CLASS (stream);
532 result = class->close_fn (stream,
533 g_task_get_cancellable (task),
537 g_task_return_error (task, error);
542 g_task_return_boolean (task, TRUE);
546 g_io_stream_real_close_async (GIOStream *stream,
548 GCancellable *cancellable,
549 GAsyncReadyCallback callback,
554 task = g_task_new (stream, cancellable, callback, user_data);
555 g_task_set_check_cancellable (task, FALSE);
556 g_task_set_priority (task, io_priority);
558 g_task_run_in_thread (task, close_async_thread);
559 g_object_unref (task);
563 g_io_stream_real_close_finish (GIOStream *stream,
564 GAsyncResult *result,
567 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
569 return g_task_propagate_boolean (G_TASK (result), error);
576 GIOStreamSpliceFlags flags;
578 GCancellable *cancellable;
580 GCancellable *op1_cancellable;
581 GCancellable *op2_cancellable;
587 splice_context_free (SpliceContext *ctx)
589 g_object_unref (ctx->stream1);
590 g_object_unref (ctx->stream2);
591 if (ctx->cancellable != NULL)
592 g_object_unref (ctx->cancellable);
593 g_object_unref (ctx->op1_cancellable);
594 g_object_unref (ctx->op2_cancellable);
595 g_clear_error (&ctx->error);
596 g_slice_free (SpliceContext, ctx);
600 splice_complete (GTask *task,
603 if (ctx->cancelled_id != 0)
604 g_cancellable_disconnect (ctx->cancellable, ctx->cancelled_id);
605 ctx->cancelled_id = 0;
607 if (ctx->error != NULL)
609 g_task_return_error (task, ctx->error);
613 g_task_return_boolean (task, TRUE);
617 splice_close_cb (GObject *iostream,
621 GTask *task = user_data;
622 SpliceContext *ctx = g_task_get_task_data (task);
623 GError *error = NULL;
625 g_io_stream_close_finish (G_IO_STREAM (iostream), res, &error);
629 /* Keep the first error that occurred */
630 if (error != NULL && ctx->error == NULL)
633 g_clear_error (&error);
635 /* If all operations are done, complete now */
636 if (ctx->completed == 4)
637 splice_complete (task, ctx);
639 g_object_unref (task);
643 splice_cb (GObject *ostream,
647 GTask *task = user_data;
648 SpliceContext *ctx = g_task_get_task_data (task);
649 GError *error = NULL;
651 g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
655 /* ignore cancellation error if it was not requested by the user */
657 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
658 (ctx->cancellable == NULL ||
659 !g_cancellable_is_cancelled (ctx->cancellable)))
660 g_clear_error (&error);
662 /* Keep the first error that occurred */
663 if (error != NULL && ctx->error == NULL)
666 g_clear_error (&error);
668 if (ctx->completed == 1 &&
669 (ctx->flags & G_IO_STREAM_SPLICE_WAIT_FOR_BOTH) == 0)
671 /* We don't want to wait for the 2nd operation to finish, cancel it */
672 g_cancellable_cancel (ctx->op1_cancellable);
673 g_cancellable_cancel (ctx->op2_cancellable);
675 else if (ctx->completed == 2)
677 if (ctx->cancellable == NULL ||
678 !g_cancellable_is_cancelled (ctx->cancellable))
680 g_cancellable_reset (ctx->op1_cancellable);
681 g_cancellable_reset (ctx->op2_cancellable);
684 /* Close the IO streams if needed */
685 if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM1) != 0)
687 g_io_stream_close_async (ctx->stream1,
688 g_task_get_priority (task),
689 ctx->op1_cancellable,
690 splice_close_cb, g_object_ref (task));
695 if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM2) != 0)
697 g_io_stream_close_async (ctx->stream2,
698 g_task_get_priority (task),
699 ctx->op2_cancellable,
700 splice_close_cb, g_object_ref (task));
705 /* If all operations are done, complete now */
706 if (ctx->completed == 4)
707 splice_complete (task, ctx);
710 g_object_unref (task);
714 splice_cancelled_cb (GCancellable *cancellable,
719 ctx = g_task_get_task_data (task);
720 g_cancellable_cancel (ctx->op1_cancellable);
721 g_cancellable_cancel (ctx->op2_cancellable);
725 * g_io_stream_splice_async:
726 * @stream1: a #GIOStream.
727 * @stream2: a #GIOStream.
728 * @flags: a set of #GIOStreamSpliceFlags.
729 * @io_priority: the io priority of the request.
730 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
731 * @callback: (scope async): a #GAsyncReadyCallback.
732 * @user_data: (closure): user data passed to @callback.
734 * Asyncronously splice the output stream of @stream1 to the input stream of
735 * @stream2, and splice the output stream of @stream2 to the input stream of
738 * When the operation is finished @callback will be called.
739 * You can then call g_io_stream_splice_finish() to get the
740 * result of the operation.
745 g_io_stream_splice_async (GIOStream *stream1,
747 GIOStreamSpliceFlags flags,
749 GCancellable *cancellable,
750 GAsyncReadyCallback callback,
755 GInputStream *istream;
756 GOutputStream *ostream;
758 if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
760 g_task_report_new_error (NULL, callback, user_data,
761 g_io_stream_splice_async,
762 G_IO_ERROR, G_IO_ERROR_CANCELLED,
763 "Operation has been cancelled");
767 ctx = g_slice_new0 (SpliceContext);
768 ctx->stream1 = g_object_ref (stream1);
769 ctx->stream2 = g_object_ref (stream2);
771 ctx->op1_cancellable = g_cancellable_new ();
772 ctx->op2_cancellable = g_cancellable_new ();
775 task = g_task_new (NULL, cancellable, callback, user_data);
776 g_task_set_task_data (task, ctx, (GDestroyNotify) splice_context_free);
778 if (cancellable != NULL)
780 ctx->cancellable = g_object_ref (cancellable);
781 ctx->cancelled_id = g_cancellable_connect (cancellable,
782 G_CALLBACK (splice_cancelled_cb), g_object_ref (task),
786 istream = g_io_stream_get_input_stream (stream1);
787 ostream = g_io_stream_get_output_stream (stream2);
788 g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
789 io_priority, ctx->op1_cancellable, splice_cb,
790 g_object_ref (task));
792 istream = g_io_stream_get_input_stream (stream2);
793 ostream = g_io_stream_get_output_stream (stream1);
794 g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
795 io_priority, ctx->op2_cancellable, splice_cb,
796 g_object_ref (task));
798 g_object_unref (task);
802 * g_io_stream_splice_finish:
803 * @result: a #GAsyncResult.
804 * @error: a #GError location to store the error occurring, or %NULL to
807 * Finishes an asynchronous io stream splice operation.
809 * Returns: %TRUE on success, %FALSE otherwise.
814 g_io_stream_splice_finish (GAsyncResult *result,
817 g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
819 return g_task_propagate_boolean (G_TASK (result), error);