1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright (C) 2010 Xavier Claessens <xclaesse@gmail.com>
* Copyright (C) 2010 Collabora Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "common.h"
typedef struct
{
guint ref_count;
gboolean closed;
GError *error;
GIOStream *stream1;
GIOStream *stream2;
_GIOStreamSpliceCallback callback;
gpointer user_data;
} SpliceContext;
static SpliceContext *
splice_context_ref (SpliceContext *self)
{
self->ref_count++;
return self;
}
static void
splice_context_unref (SpliceContext *self)
{
if (--self->ref_count == 0)
{
if (self->callback != NULL)
self->callback (self->stream1, self->stream2, self->error,
self->user_data);
g_clear_error (&self->error);
g_object_unref (self->stream1);
g_object_unref (self->stream2);
g_slice_free (SpliceContext, self);
}
}
static void
splice_context_close (SpliceContext *self, const GError *error)
{
if (self->closed)
return;
self->closed = TRUE;
if (error != NULL)
self->error = g_error_copy (error);
g_io_stream_close (self->stream1, NULL, NULL);
g_io_stream_close (self->stream2, NULL, NULL);
}
static void
splice_cb (GObject *ostream,
GAsyncResult *res,
gpointer user_data)
{
SpliceContext *ctx = user_data;
GError *error = NULL;
g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
splice_context_close (ctx, error);
splice_context_unref (ctx);
g_clear_error (&error);
}
void
_g_io_stream_splice (GIOStream *stream1,
GIOStream *stream2,
_GIOStreamSpliceCallback callback,
gpointer user_data)
{
SpliceContext *ctx;
GInputStream *istream;
GOutputStream *ostream;
ctx = g_slice_new0 (SpliceContext);
ctx->ref_count = 1;
ctx->stream1 = g_object_ref (stream1);
ctx->stream2 = g_object_ref (stream2);
ctx->callback = callback;
ctx->user_data = user_data;
istream = g_io_stream_get_input_stream (stream1);
ostream = g_io_stream_get_output_stream (stream2);
g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
G_PRIORITY_DEFAULT, NULL, splice_cb, splice_context_ref (ctx));
istream = g_io_stream_get_input_stream (stream2);
ostream = g_io_stream_get_output_stream (stream1);
g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
G_PRIORITY_DEFAULT, NULL, splice_cb, splice_context_ref (ctx));
splice_context_unref (ctx);
}
|