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 {
76 GAsyncReadyCallback outstanding_callback;
79 static gboolean g_io_stream_real_close (GIOStream *stream,
80 GCancellable *cancellable,
82 static void g_io_stream_real_close_async (GIOStream *stream,
84 GCancellable *cancellable,
85 GAsyncReadyCallback callback,
87 static gboolean g_io_stream_real_close_finish (GIOStream *stream,
91 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GIOStream, g_io_stream, G_TYPE_OBJECT)
94 g_io_stream_dispose (GObject *object)
98 stream = G_IO_STREAM (object);
100 if (!stream->priv->closed)
101 g_io_stream_close (stream, NULL, NULL);
103 G_OBJECT_CLASS (g_io_stream_parent_class)->dispose (object);
107 g_io_stream_init (GIOStream *stream)
109 stream->priv = g_io_stream_get_instance_private (stream);
113 g_io_stream_get_property (GObject *object,
118 GIOStream *stream = G_IO_STREAM (object);
123 g_value_set_boolean (value, stream->priv->closed);
126 case PROP_INPUT_STREAM:
127 g_value_set_object (value, g_io_stream_get_input_stream (stream));
130 case PROP_OUTPUT_STREAM:
131 g_value_set_object (value, g_io_stream_get_output_stream (stream));
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 g_io_stream_class_init (GIOStreamClass *klass)
142 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144 gobject_class->dispose = g_io_stream_dispose;
145 gobject_class->get_property = g_io_stream_get_property;
147 klass->close_fn = g_io_stream_real_close;
148 klass->close_async = g_io_stream_real_close_async;
149 klass->close_finish = g_io_stream_real_close_finish;
151 g_object_class_install_property (gobject_class, PROP_CLOSED,
152 g_param_spec_boolean ("closed",
154 P_("Is the stream closed"),
156 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
158 g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
159 g_param_spec_object ("input-stream",
161 P_("The GInputStream to read from"),
163 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
164 g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
165 g_param_spec_object ("output-stream",
167 P_("The GOutputStream to write to"),
168 G_TYPE_OUTPUT_STREAM,
169 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
173 * g_io_stream_is_closed:
174 * @stream: a #GIOStream
176 * Checks if a stream is closed.
178 * Returns: %TRUE if the stream is closed.
183 g_io_stream_is_closed (GIOStream *stream)
185 g_return_val_if_fail (G_IS_IO_STREAM (stream), TRUE);
187 return stream->priv->closed;
191 * g_io_stream_get_input_stream:
192 * @stream: a #GIOStream
194 * Gets the input stream for this object. This is used
197 * Returns: (transfer none): a #GInputStream, owned by the #GIOStream.
203 g_io_stream_get_input_stream (GIOStream *stream)
205 GIOStreamClass *klass;
207 klass = G_IO_STREAM_GET_CLASS (stream);
209 g_assert (klass->get_input_stream != NULL);
211 return klass->get_input_stream (stream);
215 * g_io_stream_get_output_stream:
216 * @stream: a #GIOStream
218 * Gets the output stream for this object. This is used for
221 * Returns: (transfer none): a #GOutputStream, owned by the #GIOStream.
227 g_io_stream_get_output_stream (GIOStream *stream)
229 GIOStreamClass *klass;
231 klass = G_IO_STREAM_GET_CLASS (stream);
233 g_assert (klass->get_output_stream != NULL);
234 return klass->get_output_stream (stream);
238 * g_io_stream_has_pending:
239 * @stream: a #GIOStream
241 * Checks if a stream has pending actions.
243 * Returns: %TRUE if @stream has pending actions.
248 g_io_stream_has_pending (GIOStream *stream)
250 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
252 return stream->priv->pending;
256 * g_io_stream_set_pending:
257 * @stream: a #GIOStream
258 * @error: a #GError location to store the error occurring, or %NULL to
261 * Sets @stream to have actions pending. If the pending flag is
262 * already set or @stream is closed, it will return %FALSE and set
265 * Return value: %TRUE if pending was previously unset and is now set.
270 g_io_stream_set_pending (GIOStream *stream,
273 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
275 if (stream->priv->closed)
277 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
278 _("Stream is already closed"));
282 if (stream->priv->pending)
284 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
285 /* Translators: This is an error you get if there is
286 * already an operation running against this stream when
287 * you try to start one */
288 _("Stream has outstanding operation"));
292 stream->priv->pending = TRUE;
297 * g_io_stream_clear_pending:
298 * @stream: a #GIOStream
300 * Clears the pending flag on @stream.
305 g_io_stream_clear_pending (GIOStream *stream)
307 g_return_if_fail (G_IS_IO_STREAM (stream));
309 stream->priv->pending = FALSE;
313 g_io_stream_real_close (GIOStream *stream,
314 GCancellable *cancellable,
319 res = g_output_stream_close (g_io_stream_get_output_stream (stream),
322 /* If this errored out, unset error so that we don't report
323 further errors, but still do the following ops */
324 if (error != NULL && *error != NULL)
327 res &= g_input_stream_close (g_io_stream_get_input_stream (stream),
335 * @stream: a #GIOStream
336 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
337 * @error: location to store the error occurring, or %NULL to ignore
339 * Closes the stream, releasing resources related to it. This will also
340 * closes the individual input and output streams, if they are not already
343 * Once the stream is closed, all other operations will return
344 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
347 * Closing a stream will automatically flush any outstanding buffers
350 * Streams will be automatically closed when the last reference
351 * is dropped, but you might want to call this function to make sure
352 * resources are released as early as possible.
354 * Some streams might keep the backing store of the stream (e.g. a file
355 * descriptor) open after the stream is closed. See the documentation for
356 * the individual stream for details.
358 * On failure the first error that happened will be reported, but the
359 * close operation will finish as much as possible. A stream that failed
360 * to close will still return %G_IO_ERROR_CLOSED for all operations.
361 * Still, it is important to check and report the error to the user,
362 * otherwise there might be a loss of data as all data might not be written.
364 * If @cancellable is not NULL, then the operation can be cancelled by
365 * triggering the cancellable object from another thread. If the operation
366 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
367 * Cancelling a close will still leave the stream closed, but some streams
368 * can use a faster close that doesn't block to e.g. check errors.
370 * The default implementation of this method just calls close on the
371 * individual input/output streams.
373 * Return value: %TRUE on success, %FALSE on failure
378 g_io_stream_close (GIOStream *stream,
379 GCancellable *cancellable,
382 GIOStreamClass *class;
385 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
387 class = G_IO_STREAM_GET_CLASS (stream);
389 if (stream->priv->closed)
392 if (!g_io_stream_set_pending (stream, error))
396 g_cancellable_push_current (cancellable);
400 res = class->close_fn (stream, cancellable, error);
403 g_cancellable_pop_current (cancellable);
405 stream->priv->closed = TRUE;
406 g_io_stream_clear_pending (stream);
412 async_ready_close_callback_wrapper (GObject *source_object,
416 GIOStream *stream = G_IO_STREAM (source_object);
418 stream->priv->closed = TRUE;
419 g_io_stream_clear_pending (stream);
420 if (stream->priv->outstanding_callback)
421 (*stream->priv->outstanding_callback) (source_object, res, user_data);
422 g_object_unref (stream);
426 * g_io_stream_close_async:
427 * @stream: a #GIOStream
428 * @io_priority: the io priority of the request
429 * @cancellable: (allow-none): optional cancellable object
430 * @callback: (scope async): callback to call when the request is satisfied
431 * @user_data: (closure): the data to pass to callback function
433 * Requests an asynchronous close of the stream, releasing resources
434 * related to it. When the operation is finished @callback will be
435 * called. You can then call g_io_stream_close_finish() to get
436 * the result of the operation.
438 * For behaviour details see g_io_stream_close().
440 * The asynchronous methods have a default fallback that uses threads
441 * to implement asynchronicity, so they are optional for inheriting
442 * classes. However, if you override one you must override all.
447 g_io_stream_close_async (GIOStream *stream,
449 GCancellable *cancellable,
450 GAsyncReadyCallback callback,
453 GIOStreamClass *class;
454 GError *error = NULL;
456 g_return_if_fail (G_IS_IO_STREAM (stream));
458 if (stream->priv->closed)
462 task = g_task_new (stream, cancellable, callback, user_data);
463 g_task_set_source_tag (task, g_io_stream_close_async);
464 g_task_return_boolean (task, TRUE);
465 g_object_unref (task);
469 if (!g_io_stream_set_pending (stream, &error))
471 g_task_report_error (stream, callback, user_data,
472 g_io_stream_close_async,
477 class = G_IO_STREAM_GET_CLASS (stream);
478 stream->priv->outstanding_callback = callback;
479 g_object_ref (stream);
480 class->close_async (stream, io_priority, cancellable,
481 async_ready_close_callback_wrapper, user_data);
485 * g_io_stream_close_finish:
486 * @stream: a #GIOStream
487 * @result: a #GAsyncResult
488 * @error: a #GError location to store the error occurring, or %NULL to
493 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
498 g_io_stream_close_finish (GIOStream *stream,
499 GAsyncResult *result,
502 GIOStreamClass *class;
504 g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
505 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
507 if (g_async_result_legacy_propagate_error (result, error))
509 else if (g_async_result_is_tagged (result, g_io_stream_close_async))
510 return g_task_propagate_boolean (G_TASK (result), error);
512 class = G_IO_STREAM_GET_CLASS (stream);
513 return class->close_finish (stream, result, error);
518 close_async_thread (GTask *task,
519 gpointer source_object,
521 GCancellable *cancellable)
523 GIOStream *stream = source_object;
524 GIOStreamClass *class;
525 GError *error = NULL;
528 class = G_IO_STREAM_GET_CLASS (stream);
531 result = class->close_fn (stream,
532 g_task_get_cancellable (task),
536 g_task_return_error (task, error);
541 g_task_return_boolean (task, TRUE);
545 g_io_stream_real_close_async (GIOStream *stream,
547 GCancellable *cancellable,
548 GAsyncReadyCallback callback,
553 task = g_task_new (stream, cancellable, callback, user_data);
554 g_task_set_check_cancellable (task, FALSE);
555 g_task_set_priority (task, io_priority);
557 g_task_run_in_thread (task, close_async_thread);
558 g_object_unref (task);
562 g_io_stream_real_close_finish (GIOStream *stream,
563 GAsyncResult *result,
566 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
568 return g_task_propagate_boolean (G_TASK (result), error);
575 GIOStreamSpliceFlags flags;
577 GCancellable *cancellable;
579 GCancellable *op1_cancellable;
580 GCancellable *op2_cancellable;
586 splice_context_free (SpliceContext *ctx)
588 g_object_unref (ctx->stream1);
589 g_object_unref (ctx->stream2);
590 if (ctx->cancellable != NULL)
591 g_object_unref (ctx->cancellable);
592 g_object_unref (ctx->op1_cancellable);
593 g_object_unref (ctx->op2_cancellable);
594 g_clear_error (&ctx->error);
595 g_slice_free (SpliceContext, ctx);
599 splice_complete (GTask *task,
602 if (ctx->cancelled_id != 0)
603 g_cancellable_disconnect (ctx->cancellable, ctx->cancelled_id);
604 ctx->cancelled_id = 0;
606 if (ctx->error != NULL)
608 g_task_return_error (task, ctx->error);
612 g_task_return_boolean (task, TRUE);
616 splice_close_cb (GObject *iostream,
620 GTask *task = user_data;
621 SpliceContext *ctx = g_task_get_task_data (task);
622 GError *error = NULL;
624 g_io_stream_close_finish (G_IO_STREAM (iostream), res, &error);
628 /* Keep the first error that occurred */
629 if (error != NULL && ctx->error == NULL)
632 g_clear_error (&error);
634 /* If all operations are done, complete now */
635 if (ctx->completed == 4)
636 splice_complete (task, ctx);
638 g_object_unref (task);
642 splice_cb (GObject *ostream,
646 GTask *task = user_data;
647 SpliceContext *ctx = g_task_get_task_data (task);
648 GError *error = NULL;
650 g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
654 /* ignore cancellation error if it was not requested by the user */
656 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
657 (ctx->cancellable == NULL ||
658 !g_cancellable_is_cancelled (ctx->cancellable)))
659 g_clear_error (&error);
661 /* Keep the first error that occurred */
662 if (error != NULL && ctx->error == NULL)
665 g_clear_error (&error);
667 if (ctx->completed == 1 &&
668 (ctx->flags & G_IO_STREAM_SPLICE_WAIT_FOR_BOTH) == 0)
670 /* We don't want to wait for the 2nd operation to finish, cancel it */
671 g_cancellable_cancel (ctx->op1_cancellable);
672 g_cancellable_cancel (ctx->op2_cancellable);
674 else if (ctx->completed == 2)
676 if (ctx->cancellable == NULL ||
677 !g_cancellable_is_cancelled (ctx->cancellable))
679 g_cancellable_reset (ctx->op1_cancellable);
680 g_cancellable_reset (ctx->op2_cancellable);
683 /* Close the IO streams if needed */
684 if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM1) != 0)
686 g_io_stream_close_async (ctx->stream1,
687 g_task_get_priority (task),
688 ctx->op1_cancellable,
689 splice_close_cb, g_object_ref (task));
694 if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM2) != 0)
696 g_io_stream_close_async (ctx->stream2,
697 g_task_get_priority (task),
698 ctx->op2_cancellable,
699 splice_close_cb, g_object_ref (task));
704 /* If all operations are done, complete now */
705 if (ctx->completed == 4)
706 splice_complete (task, ctx);
709 g_object_unref (task);
713 splice_cancelled_cb (GCancellable *cancellable,
718 ctx = g_task_get_task_data (task);
719 g_cancellable_cancel (ctx->op1_cancellable);
720 g_cancellable_cancel (ctx->op2_cancellable);
724 * g_io_stream_splice_async:
725 * @stream1: a #GIOStream.
726 * @stream2: a #GIOStream.
727 * @flags: a set of #GIOStreamSpliceFlags.
728 * @io_priority: the io priority of the request.
729 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
730 * @callback: (scope async): a #GAsyncReadyCallback.
731 * @user_data: (closure): user data passed to @callback.
733 * Asyncronously splice the output stream of @stream1 to the input stream of
734 * @stream2, and splice the output stream of @stream2 to the input stream of
737 * When the operation is finished @callback will be called.
738 * You can then call g_io_stream_splice_finish() to get the
739 * result of the operation.
744 g_io_stream_splice_async (GIOStream *stream1,
746 GIOStreamSpliceFlags flags,
748 GCancellable *cancellable,
749 GAsyncReadyCallback callback,
754 GInputStream *istream;
755 GOutputStream *ostream;
757 if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
759 g_task_report_new_error (NULL, callback, user_data,
760 g_io_stream_splice_async,
761 G_IO_ERROR, G_IO_ERROR_CANCELLED,
762 "Operation has been cancelled");
766 ctx = g_slice_new0 (SpliceContext);
767 ctx->stream1 = g_object_ref (stream1);
768 ctx->stream2 = g_object_ref (stream2);
770 ctx->op1_cancellable = g_cancellable_new ();
771 ctx->op2_cancellable = g_cancellable_new ();
774 task = g_task_new (NULL, cancellable, callback, user_data);
775 g_task_set_task_data (task, ctx, (GDestroyNotify) splice_context_free);
777 if (cancellable != NULL)
779 ctx->cancellable = g_object_ref (cancellable);
780 ctx->cancelled_id = g_cancellable_connect (cancellable,
781 G_CALLBACK (splice_cancelled_cb), g_object_ref (task),
785 istream = g_io_stream_get_input_stream (stream1);
786 ostream = g_io_stream_get_output_stream (stream2);
787 g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
788 io_priority, ctx->op1_cancellable, splice_cb,
789 g_object_ref (task));
791 istream = g_io_stream_get_input_stream (stream2);
792 ostream = g_io_stream_get_output_stream (stream1);
793 g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
794 io_priority, ctx->op2_cancellable, splice_cb,
795 g_object_ref (task));
797 g_object_unref (task);
801 * g_io_stream_splice_finish:
802 * @result: a #GAsyncResult.
803 * @error: a #GError location to store the error occurring, or %NULL to
806 * Finishes an asynchronous io stream splice operation.
808 * Returns: %TRUE on success, %FALSE otherwise.
813 g_io_stream_splice_finish (GAsyncResult *result,
816 g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
818 return g_task_propagate_boolean (G_TASK (result), error);