Coverage Report

Created: 2025-07-01 07:09

/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
 * 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.
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, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Authors: Nicolas Dufresne <nicolas.dufresne@colllabora.co.uk>
19
 */
20
21
/**
22
 * SECTION:gtcpwrapperconnection
23
 * @title: GTcpWrapperConnection
24
 * @short_description: Wrapper for non-GSocketConnection-based,
25
 *     GSocket-based GIOStreams
26
 * @include: gio/gio.h
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
/**
39
 * GTcpWrapperConnection:
40
 *
41
 * #GTcpWrapperConnection is an opaque data structure and can only be accessed
42
 * using the following functions.
43
 **/
44
45
#include "config.h"
46
47
#include "gtcpwrapperconnection.h"
48
49
#include "gtcpconnection.h"
50
#include "glibintl.h"
51
52
struct _GTcpWrapperConnectionPrivate
53
{
54
  GIOStream *base_io_stream;
55
};
56
57
G_DEFINE_TYPE_WITH_PRIVATE (GTcpWrapperConnection, g_tcp_wrapper_connection, G_TYPE_TCP_CONNECTION)
58
59
enum
60
{
61
  PROP_NONE,
62
  PROP_BASE_IO_STREAM
63
};
64
65
static GInputStream *
66
g_tcp_wrapper_connection_get_input_stream (GIOStream *io_stream)
67
0
{
68
0
  GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream);
69
70
0
  return g_io_stream_get_input_stream (connection->priv->base_io_stream);
71
0
}
72
73
static GOutputStream *
74
g_tcp_wrapper_connection_get_output_stream (GIOStream *io_stream)
75
0
{
76
0
  GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream);
77
78
0
  return g_io_stream_get_output_stream (connection->priv->base_io_stream);
79
0
}
80
81
static void
82
g_tcp_wrapper_connection_get_property (GObject    *object,
83
               guint       prop_id,
84
               GValue     *value,
85
               GParamSpec *pspec)
86
0
{
87
0
  GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
88
89
0
  switch (prop_id)
90
0
    {
91
0
     case PROP_BASE_IO_STREAM:
92
0
      g_value_set_object (value, connection->priv->base_io_stream);
93
0
      break;
94
95
0
     default:
96
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97
0
    }
98
0
}
99
100
static void
101
g_tcp_wrapper_connection_set_property (GObject      *object,
102
                                        guint         prop_id,
103
                                        const GValue *value,
104
                                        GParamSpec   *pspec)
105
0
{
106
0
  GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
107
108
0
  switch (prop_id)
109
0
    {
110
0
     case PROP_BASE_IO_STREAM:
111
0
      connection->priv->base_io_stream = g_value_dup_object (value);
112
0
      break;
113
114
0
     default:
115
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116
0
    }
117
0
}
118
119
static void
120
g_tcp_wrapper_connection_finalize (GObject *object)
121
0
{
122
0
  GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
123
124
0
  if (connection->priv->base_io_stream)
125
0
    g_object_unref (connection->priv->base_io_stream);
126
127
0
  G_OBJECT_CLASS (g_tcp_wrapper_connection_parent_class)->finalize (object);
128
0
}
129
130
static void
131
g_tcp_wrapper_connection_class_init (GTcpWrapperConnectionClass *klass)
132
0
{
133
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
134
0
  GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
135
136
0
  gobject_class->set_property = g_tcp_wrapper_connection_set_property;
137
0
  gobject_class->get_property = g_tcp_wrapper_connection_get_property;
138
0
  gobject_class->finalize = g_tcp_wrapper_connection_finalize;
139
140
0
  stream_class->get_input_stream = g_tcp_wrapper_connection_get_input_stream;
141
0
  stream_class->get_output_stream = g_tcp_wrapper_connection_get_output_stream;
142
143
0
  g_object_class_install_property (gobject_class,
144
0
                                   PROP_BASE_IO_STREAM,
145
0
                                   g_param_spec_object ("base-io-stream",
146
0
                                      P_("Base IO Stream"),
147
0
                                      P_("The wrapped GIOStream"),
148
0
                                                        G_TYPE_IO_STREAM,
149
0
                                                        G_PARAM_CONSTRUCT_ONLY |
150
0
                                                        G_PARAM_READWRITE |
151
0
                                                        G_PARAM_STATIC_STRINGS));
152
0
}
153
154
static void
155
g_tcp_wrapper_connection_init (GTcpWrapperConnection *connection)
156
0
{
157
0
  connection->priv = g_tcp_wrapper_connection_get_instance_private (connection);
158
0
}
159
160
/**
161
 * g_tcp_wrapper_connection_new:
162
 * @base_io_stream: the #GIOStream to wrap
163
 * @socket: the #GSocket associated with @base_io_stream
164
 *
165
 * Wraps @base_io_stream and @socket together as a #GSocketConnection.
166
 *
167
 * Returns: the new #GSocketConnection.
168
 *
169
 * Since: 2.28
170
 */
171
GSocketConnection *
172
g_tcp_wrapper_connection_new (GIOStream *base_io_stream,
173
            GSocket   *socket)
174
0
{
175
0
  g_return_val_if_fail (G_IS_IO_STREAM (base_io_stream), NULL);
176
0
  g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
177
0
  g_return_val_if_fail (g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV4 ||
178
0
      g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6, NULL);
179
0
  g_return_val_if_fail (g_socket_get_socket_type (socket) == G_SOCKET_TYPE_STREAM, NULL);
180
181
0
  return g_object_new (G_TYPE_TCP_WRAPPER_CONNECTION,
182
0
           "base-io-stream", base_io_stream,
183
0
           "socket", socket,
184
0
           NULL);
185
0
}
186
187
/**
188
 * g_tcp_wrapper_connection_get_base_io_stream:
189
 * @conn: a #GTcpWrapperConnection
190
 *
191
 * Gets @conn's base #GIOStream
192
 *
193
 * Returns: (transfer none): @conn's base #GIOStream
194
 */
195
GIOStream *
196
g_tcp_wrapper_connection_get_base_io_stream (GTcpWrapperConnection *conn)
197
0
{
198
0
  g_return_val_if_fail (G_IS_TCP_WRAPPER_CONNECTION (conn), NULL);
199
200
0
  return conn->priv->base_io_stream;
201
0
}