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