gio: Add g_async_result_legacy_propagate_error()
[platform/upstream/glib.git] / gio / giostream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2008 codethink
4  * Copyright © 2009 Red Hat, Inc.
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Authors: Ryan Lortie <desrt@desrt.ca>
22  *          Alexander Larsson <alexl@redhat.com>
23  */
24
25 #include "config.h"
26 #include <glib.h>
27 #include "glibintl.h"
28
29 #include "giostream.h"
30 #include <gio/gsimpleasyncresult.h>
31 #include <gio/gasyncresult.h>
32
33
34 G_DEFINE_ABSTRACT_TYPE (GIOStream, g_io_stream, G_TYPE_OBJECT);
35
36 /**
37  * SECTION:giostream
38  * @short_description: Base class for implementing read/write streams
39  * @include: gio/gio.h
40  * @see_also: #GInputStream, #GOutputStream
41  *
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.
46  *
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.
50  *
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().
53  *
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.
59  *
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.
66  *
67  * Since: 2.22
68  */
69
70 enum
71 {
72   PROP_0,
73   PROP_INPUT_STREAM,
74   PROP_OUTPUT_STREAM,
75   PROP_CLOSED
76 };
77
78 struct _GIOStreamPrivate {
79   guint closed : 1;
80   guint pending : 1;
81   GAsyncReadyCallback outstanding_callback;
82 };
83
84 static gboolean g_io_stream_real_close        (GIOStream            *stream,
85                                                GCancellable         *cancellable,
86                                                GError              **error);
87 static void     g_io_stream_real_close_async  (GIOStream            *stream,
88                                                int                   io_priority,
89                                                GCancellable         *cancellable,
90                                                GAsyncReadyCallback   callback,
91                                                gpointer              user_data);
92 static gboolean g_io_stream_real_close_finish (GIOStream            *stream,
93                                                GAsyncResult         *result,
94                                                GError              **error);
95
96 static void
97 g_io_stream_finalize (GObject *object)
98 {
99   G_OBJECT_CLASS (g_io_stream_parent_class)->finalize (object);
100 }
101
102 static void
103 g_io_stream_dispose (GObject *object)
104 {
105   GIOStream *stream;
106
107   stream = G_IO_STREAM (object);
108
109   if (!stream->priv->closed)
110     g_io_stream_close (stream, NULL, NULL);
111
112   G_OBJECT_CLASS (g_io_stream_parent_class)->dispose (object);
113 }
114
115 static void
116 g_io_stream_init (GIOStream *stream)
117 {
118   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
119                                               G_TYPE_IO_STREAM,
120                                               GIOStreamPrivate);
121 }
122
123 static void
124 g_io_stream_get_property (GObject    *object,
125                           guint       prop_id,
126                           GValue     *value,
127                           GParamSpec *pspec)
128 {
129   GIOStream *stream = G_IO_STREAM (object);
130
131   switch (prop_id)
132     {
133       case PROP_CLOSED:
134         g_value_set_boolean (value, stream->priv->closed);
135         break;
136
137       case PROP_INPUT_STREAM:
138         g_value_set_object (value, g_io_stream_get_input_stream (stream));
139         break;
140
141       case PROP_OUTPUT_STREAM:
142         g_value_set_object (value, g_io_stream_get_output_stream (stream));
143         break;
144
145       default:
146         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147     }
148 }
149
150 static void
151 g_io_stream_class_init (GIOStreamClass *klass)
152 {
153   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
154
155   g_type_class_add_private (klass, sizeof (GIOStreamPrivate));
156
157   gobject_class->finalize = g_io_stream_finalize;
158   gobject_class->dispose = g_io_stream_dispose;
159   gobject_class->get_property = g_io_stream_get_property;
160
161   klass->close_fn = g_io_stream_real_close;
162   klass->close_async = g_io_stream_real_close_async;
163   klass->close_finish = g_io_stream_real_close_finish;
164
165   g_object_class_install_property (gobject_class, PROP_CLOSED,
166                                    g_param_spec_boolean ("closed",
167                                                          P_("Closed"),
168                                                          P_("Is the stream closed"),
169                                                          FALSE,
170                                                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
171
172   g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
173                                    g_param_spec_object ("input-stream",
174                                                         P_("Input stream"),
175                                                         P_("The GInputStream to read from"),
176                                                         G_TYPE_INPUT_STREAM,
177                                                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
178   g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
179                                    g_param_spec_object ("output-stream",
180                                                         P_("Output stream"),
181                                                         P_("The GOutputStream to write to"),
182                                                         G_TYPE_OUTPUT_STREAM,
183                                                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
184 }
185
186 /**
187  * g_io_stream_is_closed:
188  * @stream: a #GIOStream
189  *
190  * Checks if a stream is closed.
191  *
192  * Returns: %TRUE if the stream is closed.
193  *
194  * Since: 2.22
195  */
196 gboolean
197 g_io_stream_is_closed (GIOStream *stream)
198 {
199   g_return_val_if_fail (G_IS_IO_STREAM (stream), TRUE);
200
201   return stream->priv->closed;
202 }
203
204 /**
205  * g_io_stream_get_input_stream:
206  * @stream: a #GIOStream
207  *
208  * Gets the input stream for this object. This is used
209  * for reading.
210  *
211  * Returns: (transfer none): a #GInputStream, owned by the #GIOStream.
212  * Do not free.
213  *
214  * Since: 2.22
215  */
216 GInputStream *
217 g_io_stream_get_input_stream (GIOStream *stream)
218 {
219   GIOStreamClass *klass;
220
221   klass = G_IO_STREAM_GET_CLASS (stream);
222
223   g_assert (klass->get_input_stream != NULL);
224
225   return klass->get_input_stream (stream);
226 }
227
228 /**
229  * g_io_stream_get_output_stream:
230  * @stream: a #GIOStream
231  *
232  * Gets the output stream for this object. This is used for
233  * writing.
234  *
235  * Returns: (transfer none): a #GOutputStream, owned by the #GIOStream.
236  * Do not free.
237  *
238  * Since: 2.22
239  */
240 GOutputStream *
241 g_io_stream_get_output_stream (GIOStream *stream)
242 {
243   GIOStreamClass *klass;
244
245   klass = G_IO_STREAM_GET_CLASS (stream);
246
247   g_assert (klass->get_output_stream != NULL);
248   return klass->get_output_stream (stream);
249 }
250
251 /**
252  * g_io_stream_has_pending:
253  * @stream: a #GIOStream
254  *
255  * Checks if a stream has pending actions.
256  *
257  * Returns: %TRUE if @stream has pending actions.
258  *
259  * Since: 2.22
260  **/
261 gboolean
262 g_io_stream_has_pending (GIOStream *stream)
263 {
264   g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
265
266   return stream->priv->pending;
267 }
268
269 /**
270  * g_io_stream_set_pending:
271  * @stream: a #GIOStream
272  * @error: a #GError location to store the error occurring, or %NULL to
273  *     ignore
274  *
275  * Sets @stream to have actions pending. If the pending flag is
276  * already set or @stream is closed, it will return %FALSE and set
277  * @error.
278  *
279  * Return value: %TRUE if pending was previously unset and is now set.
280  *
281  * Since: 2.22
282  */
283 gboolean
284 g_io_stream_set_pending (GIOStream  *stream,
285                          GError    **error)
286 {
287   g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
288
289   if (stream->priv->closed)
290     {
291       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
292                            _("Stream is already closed"));
293       return FALSE;
294     }
295
296   if (stream->priv->pending)
297     {
298       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
299                            /* Translators: This is an error you get if there is
300                             * already an operation running against this stream when
301                             * you try to start one */
302                            _("Stream has outstanding operation"));
303       return FALSE;
304     }
305
306   stream->priv->pending = TRUE;
307   return TRUE;
308 }
309
310 /**
311  * g_io_stream_clear_pending:
312  * @stream: a #GIOStream
313  *
314  * Clears the pending flag on @stream.
315  *
316  * Since: 2.22
317  */
318 void
319 g_io_stream_clear_pending (GIOStream *stream)
320 {
321   g_return_if_fail (G_IS_IO_STREAM (stream));
322
323   stream->priv->pending = FALSE;
324 }
325
326 static gboolean
327 g_io_stream_real_close (GIOStream     *stream,
328                         GCancellable  *cancellable,
329                         GError       **error)
330 {
331   gboolean res;
332
333   res = g_output_stream_close (g_io_stream_get_output_stream (stream),
334                                cancellable, error);
335
336   /* If this errored out, unset error so that we don't report
337      further errors, but still do the following ops */
338   if (error != NULL && *error != NULL)
339     error = NULL;
340
341   res &= g_input_stream_close (g_io_stream_get_input_stream (stream),
342                                cancellable, error);
343
344   return res;
345 }
346
347 /**
348  * g_io_stream_close:
349  * @stream: a #GIOStream
350  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
351  * @error: location to store the error occurring, or %NULL to ignore
352  *
353  * Closes the stream, releasing resources related to it. This will also
354  * closes the individual input and output streams, if they are not already
355  * closed.
356  *
357  * Once the stream is closed, all other operations will return
358  * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
359  * return an error.
360  *
361  * Closing a stream will automatically flush any outstanding buffers
362  * in the stream.
363  *
364  * Streams will be automatically closed when the last reference
365  * is dropped, but you might want to call this function to make sure
366  * resources are released as early as possible.
367  *
368  * Some streams might keep the backing store of the stream (e.g. a file
369  * descriptor) open after the stream is closed. See the documentation for
370  * the individual stream for details.
371  *
372  * On failure the first error that happened will be reported, but the
373  * close operation will finish as much as possible. A stream that failed
374  * to close will still return %G_IO_ERROR_CLOSED for all operations.
375  * Still, it is important to check and report the error to the user,
376  * otherwise there might be a loss of data as all data might not be written.
377  *
378  * If @cancellable is not NULL, then the operation can be cancelled by
379  * triggering the cancellable object from another thread. If the operation
380  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
381  * Cancelling a close will still leave the stream closed, but some streams
382  * can use a faster close that doesn't block to e.g. check errors.
383  *
384  * The default implementation of this method just calls close on the
385  * individual input/output streams.
386  *
387  * Return value: %TRUE on success, %FALSE on failure
388  *
389  * Since: 2.22
390  */
391 gboolean
392 g_io_stream_close (GIOStream     *stream,
393                    GCancellable  *cancellable,
394                    GError       **error)
395 {
396   GIOStreamClass *class;
397   gboolean res;
398
399   g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
400
401   class = G_IO_STREAM_GET_CLASS (stream);
402
403   if (stream->priv->closed)
404     return TRUE;
405
406   if (!g_io_stream_set_pending (stream, error))
407     return FALSE;
408
409   if (cancellable)
410     g_cancellable_push_current (cancellable);
411
412   res = TRUE;
413   if (class->close_fn)
414     res = class->close_fn (stream, cancellable, error);
415
416   if (cancellable)
417     g_cancellable_pop_current (cancellable);
418
419   stream->priv->closed = TRUE;
420   g_io_stream_clear_pending (stream);
421
422   return res;
423 }
424
425 static void
426 async_ready_close_callback_wrapper (GObject      *source_object,
427                                     GAsyncResult *res,
428                                     gpointer      user_data)
429 {
430   GIOStream *stream = G_IO_STREAM (source_object);
431
432   stream->priv->closed = TRUE;
433   g_io_stream_clear_pending (stream);
434   if (stream->priv->outstanding_callback)
435     (*stream->priv->outstanding_callback) (source_object, res, user_data);
436   g_object_unref (stream);
437 }
438
439 /**
440  * g_io_stream_close_async:
441  * @stream: a #GIOStream
442  * @io_priority: the io priority of the request
443  * @cancellable: (allow-none): optional cancellable object
444  * @callback: (scope async): callback to call when the request is satisfied
445  * @user_data: (closure): the data to pass to callback function
446  *
447  * Requests an asynchronous close of the stream, releasing resources
448  * related to it. When the operation is finished @callback will be
449  * called. You can then call g_io_stream_close_finish() to get
450  * the result of the operation.
451  *
452  * For behaviour details see g_io_stream_close().
453  *
454  * The asynchronous methods have a default fallback that uses threads
455  * to implement asynchronicity, so they are optional for inheriting
456  * classes. However, if you override one you must override all.
457  *
458  * Since: 2.22
459  */
460 void
461 g_io_stream_close_async (GIOStream           *stream,
462                          int                  io_priority,
463                          GCancellable        *cancellable,
464                          GAsyncReadyCallback  callback,
465                          gpointer             user_data)
466 {
467   GIOStreamClass *class;
468   GSimpleAsyncResult *simple;
469   GError *error = NULL;
470
471   g_return_if_fail (G_IS_IO_STREAM (stream));
472
473   if (stream->priv->closed)
474     {
475       simple = g_simple_async_result_new (G_OBJECT (stream),
476                                           callback,
477                                           user_data,
478                                           g_io_stream_close_async);
479       g_simple_async_result_complete_in_idle (simple);
480       g_object_unref (simple);
481       return;
482     }
483
484   if (!g_io_stream_set_pending (stream, &error))
485     {
486       g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
487                                             callback,
488                                             user_data,
489                                             error);
490       return;
491     }
492
493   class = G_IO_STREAM_GET_CLASS (stream);
494   stream->priv->outstanding_callback = callback;
495   g_object_ref (stream);
496   class->close_async (stream, io_priority, cancellable,
497                       async_ready_close_callback_wrapper, user_data);
498 }
499
500 /**
501  * g_io_stream_close_finish:
502  * @stream: a #GIOStream
503  * @result: a #GAsyncResult
504  * @error: a #GError location to store the error occurring, or %NULL to
505  *    ignore
506  *
507  * Closes a stream.
508  *
509  * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
510  *
511  * Since: 2.22
512  */
513 gboolean
514 g_io_stream_close_finish (GIOStream     *stream,
515                           GAsyncResult  *result,
516                           GError       **error)
517 {
518   GSimpleAsyncResult *simple;
519   GIOStreamClass *class;
520
521   g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
522   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
523
524   if (g_async_result_legacy_propagate_error (result, error))
525     return FALSE;
526
527   if (G_IS_SIMPLE_ASYNC_RESULT (result))
528     {
529       simple = G_SIMPLE_ASYNC_RESULT (result);
530
531       /* Special case already closed */
532       if (g_simple_async_result_get_source_tag (simple) == g_io_stream_close_async)
533         return TRUE;
534     }
535
536   class = G_IO_STREAM_GET_CLASS (stream);
537   return class->close_finish (stream, result, error);
538 }
539
540
541 static void
542 close_async_thread (GSimpleAsyncResult *res,
543                     GObject            *object,
544                     GCancellable       *cancellable)
545 {
546   GIOStreamClass *class;
547   GError *error = NULL;
548   gboolean result;
549
550   /* Auto handling of cancelation disabled, and ignore cancellation,
551    * since we want to close things anyway, although possibly in a
552    * quick-n-dirty way. At least we never want to leak open handles
553    */
554   class = G_IO_STREAM_GET_CLASS (object);
555   if (class->close_fn)
556     {
557       result = class->close_fn (G_IO_STREAM (object), cancellable, &error);
558       if (!result)
559         g_simple_async_result_take_error (res, error);
560     }
561 }
562
563 static void
564 g_io_stream_real_close_async (GIOStream           *stream,
565                               int                  io_priority,
566                               GCancellable        *cancellable,
567                               GAsyncReadyCallback  callback,
568                               gpointer             user_data)
569 {
570   GSimpleAsyncResult *res;
571
572   res = g_simple_async_result_new (G_OBJECT (stream),
573                                    callback,
574                                    user_data,
575                                    g_io_stream_real_close_async);
576
577   g_simple_async_result_set_handle_cancellation (res, FALSE);
578
579   g_simple_async_result_run_in_thread (res,
580                                        close_async_thread,
581                                        io_priority,
582                                        cancellable);
583   g_object_unref (res);
584 }
585
586 static gboolean
587 g_io_stream_real_close_finish (GIOStream     *stream,
588                                GAsyncResult  *result,
589                                GError       **error)
590 {
591   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
592
593   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
594                   g_io_stream_real_close_async);
595
596   if (g_simple_async_result_propagate_error (simple, error))
597     return FALSE;
598
599   return TRUE;
600 }
601
602 typedef struct
603 {
604   GIOStream *stream1;
605   GIOStream *stream2;
606   GIOStreamSpliceFlags flags;
607   gint io_priority;
608   GCancellable *cancellable;
609   gulong cancelled_id;
610   GCancellable *op1_cancellable;
611   GCancellable *op2_cancellable;
612   guint completed;
613   GError *error;
614 } SpliceContext;
615
616 static void
617 splice_context_free (SpliceContext *ctx)
618 {
619   g_object_unref (ctx->stream1);
620   g_object_unref (ctx->stream2);
621   if (ctx->cancellable != NULL)
622     g_object_unref (ctx->cancellable);
623   g_object_unref (ctx->op1_cancellable);
624   g_object_unref (ctx->op2_cancellable);
625   g_clear_error (&ctx->error);
626   g_slice_free (SpliceContext, ctx);
627 }
628
629 static void
630 splice_complete (GSimpleAsyncResult *simple,
631                  SpliceContext      *ctx)
632 {
633   if (ctx->cancelled_id != 0)
634     g_cancellable_disconnect (ctx->cancellable, ctx->cancelled_id);
635   ctx->cancelled_id = 0;
636
637   if (ctx->error != NULL)
638     g_simple_async_result_set_from_error (simple, ctx->error);
639   g_simple_async_result_complete (simple);
640 }
641
642 static void
643 splice_close_cb (GObject      *iostream,
644                  GAsyncResult *res,
645                  gpointer      user_data)
646 {
647   GSimpleAsyncResult *simple = user_data;
648   SpliceContext *ctx;
649   GError *error = NULL;
650
651   g_io_stream_close_finish (G_IO_STREAM (iostream), res, &error);
652
653   ctx = g_simple_async_result_get_op_res_gpointer (simple);
654   ctx->completed++;
655
656   /* Keep the first error that occurred */
657   if (error != NULL && ctx->error == NULL)
658     ctx->error = error;
659   else
660     g_clear_error (&error);
661
662   /* If all operations are done, complete now */
663   if (ctx->completed == 4)
664     splice_complete (simple, ctx);
665
666   g_object_unref (simple);
667 }
668
669 static void
670 splice_cb (GObject      *ostream,
671            GAsyncResult *res,
672            gpointer      user_data)
673 {
674   GSimpleAsyncResult *simple = user_data;
675   SpliceContext *ctx;
676   GError *error = NULL;
677
678   g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
679
680   ctx = g_simple_async_result_get_op_res_gpointer (simple);
681   ctx->completed++;
682
683   /* ignore cancellation error if it was not requested by the user */
684   if (error != NULL &&
685       g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
686       (ctx->cancellable == NULL ||
687        !g_cancellable_is_cancelled (ctx->cancellable)))
688     g_clear_error (&error);
689
690   /* Keep the first error that occurred */
691   if (error != NULL && ctx->error == NULL)
692     ctx->error = error;
693   else
694     g_clear_error (&error);
695
696    if (ctx->completed == 1 &&
697        (ctx->flags & G_IO_STREAM_SPLICE_WAIT_FOR_BOTH) == 0)
698     {
699       /* We don't want to wait for the 2nd operation to finish, cancel it */
700       g_cancellable_cancel (ctx->op1_cancellable);
701       g_cancellable_cancel (ctx->op2_cancellable);
702     }
703   else if (ctx->completed == 2)
704     {
705       if (ctx->cancellable == NULL ||
706           !g_cancellable_is_cancelled (ctx->cancellable))
707         {
708           g_cancellable_reset (ctx->op1_cancellable);
709           g_cancellable_reset (ctx->op2_cancellable);
710         }
711
712       /* Close the IO streams if needed */
713       if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM1) != 0)
714         g_io_stream_close_async (ctx->stream1, ctx->io_priority,
715             ctx->op1_cancellable, splice_close_cb, g_object_ref (simple));
716       else
717         ctx->completed++;
718
719       if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM2) != 0)
720         g_io_stream_close_async (ctx->stream2, ctx->io_priority,
721             ctx->op2_cancellable, splice_close_cb, g_object_ref (simple));
722       else
723         ctx->completed++;
724
725       /* If all operations are done, complete now */
726       if (ctx->completed == 4)
727         splice_complete (simple, ctx);
728     }
729
730   g_object_unref (simple);
731 }
732
733 static void
734 splice_cancelled_cb (GCancellable       *cancellable,
735                      GSimpleAsyncResult *simple)
736 {
737   SpliceContext *ctx;
738
739   ctx = g_simple_async_result_get_op_res_gpointer (simple);
740   g_cancellable_cancel (ctx->op1_cancellable);
741   g_cancellable_cancel (ctx->op2_cancellable);
742 }
743
744 /**
745  * g_io_stream_splice_async:
746  * @stream1: a #GIOStream.
747  * @stream2: a #GIOStream.
748  * @flags: a set of #GIOStreamSpliceFlags.
749  * @io_priority: the io priority of the request.
750  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
751  * @callback: (scope async): a #GAsyncReadyCallback.
752  * @user_data: (closure): user data passed to @callback.
753  *
754  * Asyncronously splice the output stream of @stream1 to the input stream of
755  * @stream2, and splice the output stream of @stream2 to the input stream of
756  * @stream1.
757  *
758  * When the operation is finished @callback will be called.
759  * You can then call g_io_stream_splice_finish() to get the
760  * result of the operation.
761  *
762  * Since: 2.28
763  **/
764 void
765 g_io_stream_splice_async (GIOStream            *stream1,
766                           GIOStream            *stream2,
767                           GIOStreamSpliceFlags  flags,
768                           gint                  io_priority,
769                           GCancellable         *cancellable,
770                           GAsyncReadyCallback   callback,
771                           gpointer              user_data)
772 {
773   GSimpleAsyncResult *simple;
774   SpliceContext *ctx;
775   GInputStream *istream;
776   GOutputStream *ostream;
777
778   if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
779     {
780       g_simple_async_report_error_in_idle (NULL, callback,
781           user_data, G_IO_ERROR, G_IO_ERROR_CANCELLED,
782           "Operation has been cancelled");
783       return;
784     }
785
786   ctx = g_slice_new0 (SpliceContext);
787   ctx->stream1 = g_object_ref (stream1);
788   ctx->stream2 = g_object_ref (stream2);
789   ctx->flags = flags;
790   ctx->io_priority = io_priority;
791   ctx->op1_cancellable = g_cancellable_new ();
792   ctx->op2_cancellable = g_cancellable_new ();
793   ctx->completed = 0;
794
795   simple = g_simple_async_result_new (NULL, callback, user_data,
796       g_io_stream_splice_finish);
797   g_simple_async_result_set_op_res_gpointer (simple, ctx,
798       (GDestroyNotify) splice_context_free);
799
800   if (cancellable != NULL)
801     {
802       ctx->cancellable = g_object_ref (cancellable);
803       ctx->cancelled_id = g_cancellable_connect (cancellable,
804           G_CALLBACK (splice_cancelled_cb), g_object_ref (simple),
805           g_object_unref);
806     }
807
808   istream = g_io_stream_get_input_stream (stream1);
809   ostream = g_io_stream_get_output_stream (stream2);
810   g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
811       io_priority, ctx->op1_cancellable, splice_cb,
812       g_object_ref (simple));
813
814   istream = g_io_stream_get_input_stream (stream2);
815   ostream = g_io_stream_get_output_stream (stream1);
816   g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
817       io_priority, ctx->op2_cancellable, splice_cb,
818       g_object_ref (simple));
819
820   g_object_unref (simple);
821 }
822
823 /**
824  * g_io_stream_splice_finish:
825  * @result: a #GAsyncResult.
826  * @error: a #GError location to store the error occurring, or %NULL to
827  * ignore.
828  *
829  * Finishes an asynchronous io stream splice operation.
830  *
831  * Returns: %TRUE on success, %FALSE otherwise.
832  *
833  * Since: 2.28
834  **/
835 gboolean
836 g_io_stream_splice_finish (GAsyncResult  *result,
837                            GError       **error)
838 {
839   GSimpleAsyncResult *simple;
840
841   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
842
843   simple = G_SIMPLE_ASYNC_RESULT (result);
844
845   if (g_simple_async_result_propagate_error (simple, error))
846     return FALSE;
847
848   g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
849       g_io_stream_splice_finish), FALSE);
850
851   return TRUE;
852 }