1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
5 * Copyright © 2009 Red Hat, Inc
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 of the License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Authors: Christian Kellner <gicmo@gnome.org>
23 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
24 * Ryan Lortie <desrt@desrt.ca>
25 * Alexander Larsson <alexl@redhat.com>
30 #include "gsocketconnection.h"
32 #include "gsocketoutputstream.h"
33 #include "gsocketinputstream.h"
34 #include <gio/giostream.h>
35 #include <gio/gsimpleasyncresult.h>
36 #include "gunixconnection.h"
37 #include "gtcpconnection.h"
43 * SECTION:gsocketconnection
44 * @short_description: High-level socket connection stream
46 * @see_also: #GIOStream, #GSocketClient, #GSocketListener
48 * #GSocketConnection is a #GIOStream for a connected socket. They
49 * can be created either by #GSocketClient when connecting to a host,
50 * or by #GSocketListener when accepting a new client.
52 * The type of the #GSocketConnection object returned from these calls depends
53 * on the type of the underlying socket that is in use. For instance, for a
54 * TCP/IP connection it will be a #GTcpConnection.
56 * Chosing what type of object to construct is done with the socket connection
57 * factory, and it is possible for 3rd parties to register custom socket connection
58 * types for specific combination of socket family/type/protocol using
59 * g_socket_connection_factory_register_type().
64 G_DEFINE_TYPE (GSocketConnection,
65 g_socket_connection, G_TYPE_IO_STREAM);
73 struct _GSocketConnectionPrivate
76 GInputStream *input_stream;
77 GOutputStream *output_stream;
80 static gboolean g_socket_connection_close (GIOStream *stream,
81 GCancellable *cancellable,
83 static void g_socket_connection_close_async (GIOStream *stream,
85 GCancellable *cancellable,
86 GAsyncReadyCallback callback,
88 static gboolean g_socket_connection_close_finish (GIOStream *stream,
93 g_socket_connection_get_input_stream (GIOStream *io_stream)
95 GSocketConnection *connection = G_SOCKET_CONNECTION (io_stream);
97 if (connection->priv->input_stream == NULL)
98 connection->priv->input_stream = (GInputStream *)
99 _g_socket_input_stream_new (connection->priv->socket);
101 return connection->priv->input_stream;
104 static GOutputStream *
105 g_socket_connection_get_output_stream (GIOStream *io_stream)
107 GSocketConnection *connection = G_SOCKET_CONNECTION (io_stream);
109 if (connection->priv->output_stream == NULL)
110 connection->priv->output_stream = (GOutputStream *)
111 _g_socket_output_stream_new (connection->priv->socket);
113 return connection->priv->output_stream;
117 g_socket_connection_get_socket (GSocketConnection *connection)
119 g_return_val_if_fail (G_IS_SOCKET_CONNECTION (connection), NULL);
121 return connection->priv->socket;
125 * g_socket_connection_get_local_address:
126 * @connection: a #GSocketConnection.
127 * @error: #GError for error reporting, or %NULL to ignore.
129 * Try to get the local address of a socket connection.
131 * Returns: a #GSocketAddress or %NULL on error.
136 g_socket_connection_get_local_address (GSocketConnection *connection,
139 return g_socket_get_local_address (connection->priv->socket, error);
143 * g_socket_connection_get_remote_address:
144 * @connection: a #GSocketConnection.
145 * @error: #GError for error reporting, or %NULL to ignore.
147 * Try to get the remove address of a socket connection.
149 * Returns: a #GSocketAddress or %NULL on error.
154 g_socket_connection_get_remote_address (GSocketConnection *connection,
157 return g_socket_get_remote_address (connection->priv->socket, error);
161 g_socket_connection_get_property (GObject *object, guint prop_id,
162 GValue *value, GParamSpec *pspec)
164 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
169 g_value_set_object (value, connection->priv->socket);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 g_socket_connection_set_property (GObject *object, guint prop_id,
179 const GValue *value, GParamSpec *pspec)
181 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
186 connection->priv->socket = G_SOCKET (g_value_dup_object (value));
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195 g_socket_connection_constructed (GObject *object)
197 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
199 g_assert (connection->priv->socket != NULL);
203 g_socket_connection_finalize (GObject *object)
205 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
207 if (connection->priv->input_stream)
208 g_object_unref (connection->priv->input_stream);
210 if (connection->priv->output_stream)
211 g_object_unref (connection->priv->output_stream);
213 g_object_unref (connection->priv->socket);
215 G_OBJECT_CLASS (g_socket_connection_parent_class)
220 g_socket_connection_class_init (GSocketConnectionClass *klass)
222 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
223 GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
225 g_type_class_add_private (klass, sizeof (GSocketConnectionPrivate));
227 gobject_class->set_property = g_socket_connection_set_property;
228 gobject_class->get_property = g_socket_connection_get_property;
229 gobject_class->constructed = g_socket_connection_constructed;
230 gobject_class->finalize = g_socket_connection_finalize;
232 stream_class->get_input_stream = g_socket_connection_get_input_stream;
233 stream_class->get_output_stream = g_socket_connection_get_output_stream;
234 stream_class->close_fn = g_socket_connection_close;
235 stream_class->close_async = g_socket_connection_close_async;
236 stream_class->close_finish = g_socket_connection_close_finish;
238 g_object_class_install_property (gobject_class, PROP_SOCKET,
239 g_param_spec_object ("socket",
241 P_("The underlying GSocket"),
242 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 g_socket_connection_init (GSocketConnection *connection)
249 connection->priv = G_TYPE_INSTANCE_GET_PRIVATE (connection,
250 G_TYPE_SOCKET_CONNECTION,
251 GSocketConnectionPrivate);
255 g_socket_connection_close (GIOStream *stream,
256 GCancellable *cancellable,
259 GSocketConnection *connection = G_SOCKET_CONNECTION (stream);
261 if (connection->priv->output_stream)
262 g_output_stream_close (connection->priv->output_stream,
264 if (connection->priv->input_stream)
265 g_input_stream_close (connection->priv->input_stream,
268 return g_socket_close (connection->priv->socket, error);
273 g_socket_connection_close_async (GIOStream *stream,
275 GCancellable *cancellable,
276 GAsyncReadyCallback callback,
279 GSimpleAsyncResult *res;
282 /* socket close is not blocked, just do it! */
284 if (!g_io_stream_close (stream, cancellable, &error))
286 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
289 g_error_free (error);
293 res = g_simple_async_result_new (G_OBJECT (stream),
296 g_socket_connection_close_async);
297 g_simple_async_result_complete_in_idle (res);
298 g_object_unref (res);
302 g_socket_connection_close_finish (GIOStream *stream,
303 GAsyncResult *result,
310 GSocketFamily socket_family;
311 GSocketType socket_type;
313 GType implementation;
317 connection_factory_hash (gconstpointer key)
319 const ConnectionFactory *factory = key;
322 h = factory->socket_family ^ (factory->socket_type << 4) ^ (factory->protocol << 8);
323 /* This is likely to be small, so spread over whole
324 hash space to get some distribution */
325 h = h ^ (h << 8) ^ (h << 16) ^ (h << 24);
331 connection_factory_equal (gconstpointer _a,
334 const ConnectionFactory *a = _a;
335 const ConnectionFactory *b = _b;
337 if (a->socket_family != b->socket_family)
340 if (a->socket_type != b->socket_type)
343 if (a->protocol != b->protocol)
349 static GHashTable *connection_factories = NULL;
350 G_LOCK_DEFINE_STATIC(connection_factories);
353 * g_socket_connection_factory_register_type:
354 * @g_type: a #GType, inheriting from G_SOCKET_CONNECTION
355 * @family: a #GSocketFamily.
356 * @type: a #GSocketType
357 * @protocol: a protocol id
359 * Looks up the #GType to be used when creating socket connections on
360 * sockets with the specified @family,@type and @protocol_id.
362 * If no type is registered, the #GSocketConnection base type is returned.
368 g_socket_connection_factory_register_type (GType g_type,
369 GSocketFamily family,
373 ConnectionFactory *factory;
375 g_return_if_fail (g_type_is_a (g_type, G_TYPE_SOCKET_CONNECTION));
377 G_LOCK (connection_factories);
379 if (connection_factories == NULL)
380 connection_factories = g_hash_table_new_full (connection_factory_hash,
381 connection_factory_equal,
382 (GDestroyNotify)g_free,
385 factory = g_new0 (ConnectionFactory, 1);
386 factory->socket_family = family;
387 factory->socket_type = type;
388 factory->protocol = protocol;
389 factory->implementation = g_type;
391 g_hash_table_insert (connection_factories,
394 G_UNLOCK (connection_factories);
398 init_builtin_types (void)
400 volatile GType a_type;
402 a_type = g_unix_connection_get_type ();
404 a_type = g_tcp_connection_get_type ();
408 * g_socket_connection_factory_lookup_type:
409 * @family: a #GSocketFamily.
410 * @type: a #GSocketType
411 * @protocol_id: a protocol id
413 * Looks up the #GType to be used when creating socket connections on
414 * sockets with the specified @family,@type and @protocol_id.
416 * If no type is registered, the #GSocketConnection base type is returned.
422 g_socket_connection_factory_lookup_type (GSocketFamily family,
426 ConnectionFactory *factory, key;
429 init_builtin_types ();
431 G_LOCK (connection_factories);
433 g_type = G_TYPE_SOCKET_CONNECTION;
435 if (connection_factories)
437 key.socket_family = family;
438 key.socket_type = type;
439 key.protocol = protocol_id;
441 factory = g_hash_table_lookup (connection_factories, &key);
443 g_type = factory->implementation;
446 G_UNLOCK (connection_factories);
452 * g_socket_connection_factory_create_connection:
453 * @socket: a #GSocket.
455 * Creates a #GSocketConnection subclass of the right type for
458 * Returns: a #GSocketConnection
463 g_socket_connection_factory_create_connection (GSocket *socket)
467 type = g_socket_connection_factory_lookup_type (g_socket_get_family (socket),
468 g_socket_get_socket_type (socket),
469 g_socket_get_protocol_id (socket));
470 return g_object_new (type, "socket", socket, NULL);
473 #define __G_SOCKET_CONNECTION_C__
474 #include "gioaliasdef.c"