/src/glib/gio/gtcpwrapperconnection.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright © 2010 Collabora Ltd. |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | * |
20 | | * Authors: Nicolas Dufresne <nicolas.dufresne@colllabora.co.uk> |
21 | | */ |
22 | | |
23 | | /** |
24 | | * SECTION:gtcpwrapperconnection |
25 | | * @title: GTcpWrapperConnection |
26 | | * @short_description: Wrapper for non-GSocketConnection-based, |
27 | | * GSocket-based GIOStreams |
28 | | * @include: gio/gio.h |
29 | | * @see_also: #GSocketConnection. |
30 | | * |
31 | | * A #GTcpWrapperConnection can be used to wrap a #GIOStream that is |
32 | | * based on a #GSocket, but which is not actually a |
33 | | * #GSocketConnection. This is used by #GSocketClient so that it can |
34 | | * always return a #GSocketConnection, even when the connection it has |
35 | | * actually created is not directly a #GSocketConnection. |
36 | | * |
37 | | * Since: 2.28 |
38 | | */ |
39 | | |
40 | | /** |
41 | | * GTcpWrapperConnection: |
42 | | * |
43 | | * #GTcpWrapperConnection is an opaque data structure and can only be accessed |
44 | | * using the following functions. |
45 | | **/ |
46 | | |
47 | | #include "config.h" |
48 | | |
49 | | #include "gtcpwrapperconnection.h" |
50 | | |
51 | | #include "gtcpconnection.h" |
52 | | #include "glibintl.h" |
53 | | |
54 | | struct _GTcpWrapperConnectionPrivate |
55 | | { |
56 | | GIOStream *base_io_stream; |
57 | | }; |
58 | | |
59 | | G_DEFINE_TYPE_WITH_PRIVATE (GTcpWrapperConnection, g_tcp_wrapper_connection, G_TYPE_TCP_CONNECTION) |
60 | | |
61 | | enum |
62 | | { |
63 | | PROP_NONE, |
64 | | PROP_BASE_IO_STREAM |
65 | | }; |
66 | | |
67 | | static GInputStream * |
68 | | g_tcp_wrapper_connection_get_input_stream (GIOStream *io_stream) |
69 | 0 | { |
70 | 0 | GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream); |
71 | |
|
72 | 0 | return g_io_stream_get_input_stream (connection->priv->base_io_stream); |
73 | 0 | } |
74 | | |
75 | | static GOutputStream * |
76 | | g_tcp_wrapper_connection_get_output_stream (GIOStream *io_stream) |
77 | 0 | { |
78 | 0 | GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream); |
79 | |
|
80 | 0 | return g_io_stream_get_output_stream (connection->priv->base_io_stream); |
81 | 0 | } |
82 | | |
83 | | static void |
84 | | g_tcp_wrapper_connection_get_property (GObject *object, |
85 | | guint prop_id, |
86 | | GValue *value, |
87 | | GParamSpec *pspec) |
88 | 0 | { |
89 | 0 | GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object); |
90 | |
|
91 | 0 | switch (prop_id) |
92 | 0 | { |
93 | 0 | case PROP_BASE_IO_STREAM: |
94 | 0 | g_value_set_object (value, connection->priv->base_io_stream); |
95 | 0 | break; |
96 | | |
97 | 0 | default: |
98 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | static void |
103 | | g_tcp_wrapper_connection_set_property (GObject *object, |
104 | | guint prop_id, |
105 | | const GValue *value, |
106 | | GParamSpec *pspec) |
107 | 0 | { |
108 | 0 | GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object); |
109 | |
|
110 | 0 | switch (prop_id) |
111 | 0 | { |
112 | 0 | case PROP_BASE_IO_STREAM: |
113 | 0 | connection->priv->base_io_stream = g_value_dup_object (value); |
114 | 0 | break; |
115 | | |
116 | 0 | default: |
117 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | static void |
122 | | g_tcp_wrapper_connection_finalize (GObject *object) |
123 | 0 | { |
124 | 0 | GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object); |
125 | |
|
126 | 0 | if (connection->priv->base_io_stream) |
127 | 0 | g_object_unref (connection->priv->base_io_stream); |
128 | |
|
129 | 0 | G_OBJECT_CLASS (g_tcp_wrapper_connection_parent_class)->finalize (object); |
130 | 0 | } |
131 | | |
132 | | static void |
133 | | g_tcp_wrapper_connection_class_init (GTcpWrapperConnectionClass *klass) |
134 | 0 | { |
135 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
136 | 0 | GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass); |
137 | |
|
138 | 0 | gobject_class->set_property = g_tcp_wrapper_connection_set_property; |
139 | 0 | gobject_class->get_property = g_tcp_wrapper_connection_get_property; |
140 | 0 | gobject_class->finalize = g_tcp_wrapper_connection_finalize; |
141 | |
|
142 | 0 | stream_class->get_input_stream = g_tcp_wrapper_connection_get_input_stream; |
143 | 0 | stream_class->get_output_stream = g_tcp_wrapper_connection_get_output_stream; |
144 | |
|
145 | 0 | g_object_class_install_property (gobject_class, |
146 | 0 | PROP_BASE_IO_STREAM, |
147 | 0 | g_param_spec_object ("base-io-stream", |
148 | 0 | P_("Base IO Stream"), |
149 | 0 | P_("The wrapped GIOStream"), |
150 | 0 | G_TYPE_IO_STREAM, |
151 | 0 | G_PARAM_CONSTRUCT_ONLY | |
152 | 0 | G_PARAM_READWRITE | |
153 | 0 | G_PARAM_STATIC_STRINGS)); |
154 | 0 | } |
155 | | |
156 | | static void |
157 | | g_tcp_wrapper_connection_init (GTcpWrapperConnection *connection) |
158 | 0 | { |
159 | 0 | connection->priv = g_tcp_wrapper_connection_get_instance_private (connection); |
160 | 0 | } |
161 | | |
162 | | /** |
163 | | * g_tcp_wrapper_connection_new: |
164 | | * @base_io_stream: the #GIOStream to wrap |
165 | | * @socket: the #GSocket associated with @base_io_stream |
166 | | * |
167 | | * Wraps @base_io_stream and @socket together as a #GSocketConnection. |
168 | | * |
169 | | * Returns: the new #GSocketConnection. |
170 | | * |
171 | | * Since: 2.28 |
172 | | */ |
173 | | GSocketConnection * |
174 | | g_tcp_wrapper_connection_new (GIOStream *base_io_stream, |
175 | | GSocket *socket) |
176 | 0 | { |
177 | 0 | g_return_val_if_fail (G_IS_IO_STREAM (base_io_stream), NULL); |
178 | 0 | g_return_val_if_fail (G_IS_SOCKET (socket), NULL); |
179 | 0 | g_return_val_if_fail (g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV4 || |
180 | 0 | g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6, NULL); |
181 | 0 | g_return_val_if_fail (g_socket_get_socket_type (socket) == G_SOCKET_TYPE_STREAM, NULL); |
182 | | |
183 | 0 | return g_object_new (G_TYPE_TCP_WRAPPER_CONNECTION, |
184 | 0 | "base-io-stream", base_io_stream, |
185 | 0 | "socket", socket, |
186 | 0 | NULL); |
187 | 0 | } |
188 | | |
189 | | /** |
190 | | * g_tcp_wrapper_connection_get_base_io_stream: |
191 | | * @conn: a #GTcpWrapperConnection |
192 | | * |
193 | | * Gets @conn's base #GIOStream |
194 | | * |
195 | | * Returns: (transfer none): @conn's base #GIOStream |
196 | | */ |
197 | | GIOStream * |
198 | | g_tcp_wrapper_connection_get_base_io_stream (GTcpWrapperConnection *conn) |
199 | 0 | { |
200 | 0 | g_return_val_if_fail (G_IS_TCP_WRAPPER_CONNECTION (conn), NULL); |
201 | | |
202 | 0 | return conn->priv->base_io_stream; |
203 | 0 | } |