1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2010 Collabora Ltd.
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.1 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Authors: Nicolas Dufresne <nicolas.dufresne@colllabora.co.uk>
22 * SECTION:gtcpwrapperconnection
23 * @title: GTcpWrapperConnection
24 * @short_description: Wrapper for non-GSocketConnection-based,
25 * GSocket-based GIOStreams
27 * @see_also: #GSocketConnection.
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.
39 * GTcpWrapperConnection:
41 * #GTcpWrapperConnection is an opaque data structure and can only be accessed
42 * using the following functions.
47 #include "gtcpwrapperconnection.h"
49 #include "gtcpconnection.h"
52 struct _GTcpWrapperConnectionPrivate
54 GIOStream
*base_io_stream
;
57 G_DEFINE_TYPE_WITH_PRIVATE (GTcpWrapperConnection
, g_tcp_wrapper_connection
, G_TYPE_TCP_CONNECTION
)
66 g_tcp_wrapper_connection_get_input_stream (GIOStream
*io_stream
)
68 GTcpWrapperConnection
*connection
= G_TCP_WRAPPER_CONNECTION (io_stream
);
70 return g_io_stream_get_input_stream (connection
->priv
->base_io_stream
);
73 static GOutputStream
*
74 g_tcp_wrapper_connection_get_output_stream (GIOStream
*io_stream
)
76 GTcpWrapperConnection
*connection
= G_TCP_WRAPPER_CONNECTION (io_stream
);
78 return g_io_stream_get_output_stream (connection
->priv
->base_io_stream
);
82 g_tcp_wrapper_connection_get_property (GObject
*object
,
87 GTcpWrapperConnection
*connection
= G_TCP_WRAPPER_CONNECTION (object
);
91 case PROP_BASE_IO_STREAM
:
92 g_value_set_object (value
, connection
->priv
->base_io_stream
);
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
101 g_tcp_wrapper_connection_set_property (GObject
*object
,
106 GTcpWrapperConnection
*connection
= G_TCP_WRAPPER_CONNECTION (object
);
110 case PROP_BASE_IO_STREAM
:
111 connection
->priv
->base_io_stream
= g_value_dup_object (value
);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
120 g_tcp_wrapper_connection_finalize (GObject
*object
)
122 GTcpWrapperConnection
*connection
= G_TCP_WRAPPER_CONNECTION (object
);
124 if (connection
->priv
->base_io_stream
)
125 g_object_unref (connection
->priv
->base_io_stream
);
127 G_OBJECT_CLASS (g_tcp_wrapper_connection_parent_class
)->finalize (object
);
131 g_tcp_wrapper_connection_class_init (GTcpWrapperConnectionClass
*klass
)
133 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
134 GIOStreamClass
*stream_class
= G_IO_STREAM_CLASS (klass
);
136 gobject_class
->set_property
= g_tcp_wrapper_connection_set_property
;
137 gobject_class
->get_property
= g_tcp_wrapper_connection_get_property
;
138 gobject_class
->finalize
= g_tcp_wrapper_connection_finalize
;
140 stream_class
->get_input_stream
= g_tcp_wrapper_connection_get_input_stream
;
141 stream_class
->get_output_stream
= g_tcp_wrapper_connection_get_output_stream
;
143 g_object_class_install_property (gobject_class
,
145 g_param_spec_object ("base-io-stream",
146 P_("Base IO Stream"),
147 P_("The wrapped GIOStream"),
149 G_PARAM_CONSTRUCT_ONLY
|
151 G_PARAM_STATIC_STRINGS
));
155 g_tcp_wrapper_connection_init (GTcpWrapperConnection
*connection
)
157 connection
->priv
= g_tcp_wrapper_connection_get_instance_private (connection
);
161 * g_tcp_wrapper_connection_new:
162 * @base_io_stream: the #GIOStream to wrap
163 * @socket: the #GSocket associated with @base_io_stream
165 * Wraps @base_io_stream and @socket together as a #GSocketConnection.
167 * Returns: the new #GSocketConnection.
172 g_tcp_wrapper_connection_new (GIOStream
*base_io_stream
,
175 g_return_val_if_fail (G_IS_IO_STREAM (base_io_stream
), NULL
);
176 g_return_val_if_fail (G_IS_SOCKET (socket
), NULL
);
177 g_return_val_if_fail (g_socket_get_family (socket
) == G_SOCKET_FAMILY_IPV4
||
178 g_socket_get_family (socket
) == G_SOCKET_FAMILY_IPV6
, NULL
);
179 g_return_val_if_fail (g_socket_get_socket_type (socket
) == G_SOCKET_TYPE_STREAM
, NULL
);
181 return g_object_new (G_TYPE_TCP_WRAPPER_CONNECTION
,
182 "base-io-stream", base_io_stream
,
188 * g_tcp_wrapper_connection_get_base_io_stream:
189 * @conn: a #GTcpWrapperConnection
191 * Get's @conn's base #GIOStream
193 * Returns: (transfer none): @conn's base #GIOStream
196 g_tcp_wrapper_connection_get_base_io_stream (GTcpWrapperConnection
*conn
)
198 g_return_val_if_fail (G_IS_TCP_WRAPPER_CONNECTION (conn
), NULL
);
200 return conn
->priv
->base_io_stream
;