Tidy Up the glib MSVC Project
[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: Wrapper for non-GSocketConnection-based, GSocket-based GIOStreams
27  * @see_also: #GSocketConnection.
28  *
29  * A #GTcpWrapperConnection can be used to wrap a #GIOStream that is
30  * based on a #GSocket, but which is not actually a
31  * #GSocketConnection. This is used by #GSocketClient so that it can
32  * always return a #GSocketConnection, even when the connection it has
33  * actually created is not directly a #GSocketConnection.
34  *
35  * Since: 2.28
36  */
37
38 #include "config.h"
39
40 #include "gtcpwrapperconnection.h"
41
42 #include "gtcpconnection.h"
43 #include "glibintl.h"
44
45 struct _GTcpWrapperConnectionPrivate
46 {
47   GIOStream *base_io_stream;
48 };
49
50 G_DEFINE_TYPE_WITH_PRIVATE (GTcpWrapperConnection, g_tcp_wrapper_connection, G_TYPE_TCP_CONNECTION)
51
52 enum
53 {
54   PROP_NONE,
55   PROP_BASE_IO_STREAM
56 };
57
58 static GInputStream *
59 g_tcp_wrapper_connection_get_input_stream (GIOStream *io_stream)
60 {
61   GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream);
62
63   return g_io_stream_get_input_stream (connection->priv->base_io_stream);
64 }
65
66 static GOutputStream *
67 g_tcp_wrapper_connection_get_output_stream (GIOStream *io_stream)
68 {
69   GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream);
70
71   return g_io_stream_get_output_stream (connection->priv->base_io_stream);
72 }
73
74 static void
75 g_tcp_wrapper_connection_get_property (GObject    *object,
76                                        guint       prop_id,
77                                        GValue     *value,
78                                        GParamSpec *pspec)
79 {
80   GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
81
82   switch (prop_id)
83     {
84      case PROP_BASE_IO_STREAM:
85       g_value_set_object (value, connection->priv->base_io_stream);
86       break;
87
88      default:
89       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90     }
91 }
92
93 static void
94 g_tcp_wrapper_connection_set_property (GObject      *object,
95                                         guint         prop_id,
96                                         const GValue *value,
97                                         GParamSpec   *pspec)
98 {
99   GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
100
101   switch (prop_id)
102     {
103      case PROP_BASE_IO_STREAM:
104       connection->priv->base_io_stream = g_value_dup_object (value);
105       break;
106
107      default:
108       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109     }
110 }
111
112 static void
113 g_tcp_wrapper_connection_finalize (GObject *object)
114 {
115   GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
116
117   if (connection->priv->base_io_stream)
118     g_object_unref (connection->priv->base_io_stream);
119
120   G_OBJECT_CLASS (g_tcp_wrapper_connection_parent_class)->finalize (object);
121 }
122
123 static void
124 g_tcp_wrapper_connection_class_init (GTcpWrapperConnectionClass *klass)
125 {
126   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
127   GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
128
129   gobject_class->set_property = g_tcp_wrapper_connection_set_property;
130   gobject_class->get_property = g_tcp_wrapper_connection_get_property;
131   gobject_class->finalize = g_tcp_wrapper_connection_finalize;
132
133   stream_class->get_input_stream = g_tcp_wrapper_connection_get_input_stream;
134   stream_class->get_output_stream = g_tcp_wrapper_connection_get_output_stream;
135
136   g_object_class_install_property (gobject_class,
137                                    PROP_BASE_IO_STREAM,
138                                    g_param_spec_object ("base-io-stream",
139                                                         P_("Base IO Stream"),
140                                                         P_("The wrapped GIOStream"),
141                                                         G_TYPE_IO_STREAM,
142                                                         G_PARAM_CONSTRUCT_ONLY |
143                                                         G_PARAM_READWRITE |
144                                                         G_PARAM_STATIC_STRINGS));
145 }
146
147 static void
148 g_tcp_wrapper_connection_init (GTcpWrapperConnection *connection)
149 {
150   connection->priv = g_tcp_wrapper_connection_get_instance_private (connection);
151 }
152
153 /**
154  * g_tcp_wrapper_connection_new:
155  * @base_io_stream: the #GIOStream to wrap
156  * @socket: the #GSocket associated with @base_io_stream
157  *
158  * Wraps @base_io_stream and @socket together as a #GSocketConnection.
159  *
160  * Return value: the new #GSocketConnection.
161  *
162  * Since: 2.28
163  */
164 GSocketConnection *
165 g_tcp_wrapper_connection_new (GIOStream *base_io_stream,
166                               GSocket   *socket)
167 {
168   g_return_val_if_fail (G_IS_IO_STREAM (base_io_stream), NULL);
169   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
170   g_return_val_if_fail (g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV4 ||
171                         g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6, NULL);
172   g_return_val_if_fail (g_socket_get_socket_type (socket) == G_SOCKET_TYPE_STREAM, NULL);
173
174   return g_object_new (G_TYPE_TCP_WRAPPER_CONNECTION,
175                        "base-io-stream", base_io_stream,
176                        "socket", socket,
177                        NULL);
178 }
179
180 /**
181  * g_tcp_wrapper_connection_get_base_io_stream:
182  * @conn: a #GTcpWrapperConnection
183  *
184  * Get's @conn's base #GIOStream
185  *
186  * Return value: (transfer none): @conn's base #GIOStream
187  */
188 GIOStream *
189 g_tcp_wrapper_connection_get_base_io_stream (GTcpWrapperConnection *conn)
190 {
191   g_return_val_if_fail (G_IS_TCP_WRAPPER_CONNECTION (conn), NULL);
192
193   return conn->priv->base_io_stream;
194 }