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