• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *
g_tcp_wrapper_connection_get_input_stream(GIOStream * io_stream)66  g_tcp_wrapper_connection_get_input_stream (GIOStream *io_stream)
67  {
68    GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream);
69  
70    return g_io_stream_get_input_stream (connection->priv->base_io_stream);
71  }
72  
73  static GOutputStream *
g_tcp_wrapper_connection_get_output_stream(GIOStream * io_stream)74  g_tcp_wrapper_connection_get_output_stream (GIOStream *io_stream)
75  {
76    GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (io_stream);
77  
78    return g_io_stream_get_output_stream (connection->priv->base_io_stream);
79  }
80  
81  static void
g_tcp_wrapper_connection_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)82  g_tcp_wrapper_connection_get_property (GObject    *object,
83  				       guint       prop_id,
84  				       GValue     *value,
85  				       GParamSpec *pspec)
86  {
87    GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
88  
89    switch (prop_id)
90      {
91       case PROP_BASE_IO_STREAM:
92        g_value_set_object (value, connection->priv->base_io_stream);
93        break;
94  
95       default:
96        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97      }
98  }
99  
100  static void
g_tcp_wrapper_connection_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)101  g_tcp_wrapper_connection_set_property (GObject      *object,
102                                          guint         prop_id,
103                                          const GValue *value,
104                                          GParamSpec   *pspec)
105  {
106    GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
107  
108    switch (prop_id)
109      {
110       case PROP_BASE_IO_STREAM:
111        connection->priv->base_io_stream = g_value_dup_object (value);
112        break;
113  
114       default:
115        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116      }
117  }
118  
119  static void
g_tcp_wrapper_connection_finalize(GObject * object)120  g_tcp_wrapper_connection_finalize (GObject *object)
121  {
122    GTcpWrapperConnection *connection = G_TCP_WRAPPER_CONNECTION (object);
123  
124    if (connection->priv->base_io_stream)
125      g_object_unref (connection->priv->base_io_stream);
126  
127    G_OBJECT_CLASS (g_tcp_wrapper_connection_parent_class)->finalize (object);
128  }
129  
130  static void
g_tcp_wrapper_connection_class_init(GTcpWrapperConnectionClass * klass)131  g_tcp_wrapper_connection_class_init (GTcpWrapperConnectionClass *klass)
132  {
133    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
134    GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
135  
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;
139  
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;
142  
143    g_object_class_install_property (gobject_class,
144                                     PROP_BASE_IO_STREAM,
145                                     g_param_spec_object ("base-io-stream",
146  			                                P_("Base IO Stream"),
147  			                                P_("The wrapped GIOStream"),
148                                                          G_TYPE_IO_STREAM,
149                                                          G_PARAM_CONSTRUCT_ONLY |
150                                                          G_PARAM_READWRITE |
151                                                          G_PARAM_STATIC_STRINGS));
152  }
153  
154  static void
g_tcp_wrapper_connection_init(GTcpWrapperConnection * connection)155  g_tcp_wrapper_connection_init (GTcpWrapperConnection *connection)
156  {
157    connection->priv = g_tcp_wrapper_connection_get_instance_private (connection);
158  }
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 *
g_tcp_wrapper_connection_new(GIOStream * base_io_stream,GSocket * socket)172  g_tcp_wrapper_connection_new (GIOStream *base_io_stream,
173  			      GSocket   *socket)
174  {
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);
180  
181    return g_object_new (G_TYPE_TCP_WRAPPER_CONNECTION,
182  		       "base-io-stream", base_io_stream,
183  		       "socket", socket,
184  		       NULL);
185  }
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 *
g_tcp_wrapper_connection_get_base_io_stream(GTcpWrapperConnection * conn)196  g_tcp_wrapper_connection_get_base_io_stream (GTcpWrapperConnection *conn)
197  {
198    g_return_val_if_fail (G_IS_TCP_WRAPPER_CONNECTION (conn), NULL);
199  
200    return conn->priv->base_io_stream;
201  }
202