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: A socket connection
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
53 * depends on the type of the underlying socket that is in use. For
54 * instance, for a TCP/IP connection it will be a #GTcpConnection.
56 * Chosing what type of object to construct is done with the socket
57 * connection factory, and it is possible for 3rd parties to register
58 * custom socket connection types for specific combination of socket
59 * family/type/protocol using 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;
82 static gboolean g_socket_connection_close (GIOStream *stream,
83 GCancellable *cancellable,
85 static void g_socket_connection_close_async (GIOStream *stream,
87 GCancellable *cancellable,
88 GAsyncReadyCallback callback,
90 static gboolean g_socket_connection_close_finish (GIOStream *stream,
95 g_socket_connection_get_input_stream (GIOStream *io_stream)
97 GSocketConnection *connection = G_SOCKET_CONNECTION (io_stream);
99 if (connection->priv->input_stream == NULL)
100 connection->priv->input_stream = (GInputStream *)
101 _g_socket_input_stream_new (connection->priv->socket);
103 return connection->priv->input_stream;
106 static GOutputStream *
107 g_socket_connection_get_output_stream (GIOStream *io_stream)
109 GSocketConnection *connection = G_SOCKET_CONNECTION (io_stream);
111 if (connection->priv->output_stream == NULL)
112 connection->priv->output_stream = (GOutputStream *)
113 _g_socket_output_stream_new (connection->priv->socket);
115 return connection->priv->output_stream;
119 * g_socket_connection_get_socket:
120 * @connection: a #GSocketConnection
122 * Gets the underlying #GSocket object of the connection.
123 * This can be useful if you want to do something unusual on it
124 * not supported by the #GSocketConnection APIs.
126 * Returns: a #GSocketAddress or %NULL on error.
131 g_socket_connection_get_socket (GSocketConnection *connection)
133 g_return_val_if_fail (G_IS_SOCKET_CONNECTION (connection), NULL);
135 return connection->priv->socket;
139 * g_socket_connection_get_local_address:
140 * @connection: a #GSocketConnection
141 * @error: #GError for error reporting, or %NULL to ignore.
143 * Try to get the local address of a socket connection.
145 * Returns: a #GSocketAddress or %NULL on error.
146 * Free the returned object with g_object_unref().
151 g_socket_connection_get_local_address (GSocketConnection *connection,
154 return g_socket_get_local_address (connection->priv->socket, error);
158 * g_socket_connection_get_remote_address:
159 * @connection: a #GSocketConnection
160 * @error: #GError for error reporting, or %NULL to ignore.
162 * Try to get the remote address of a socket connection.
164 * Returns: a #GSocketAddress or %NULL on error.
165 * Free the returned object with g_object_unref().
170 g_socket_connection_get_remote_address (GSocketConnection *connection,
173 return g_socket_get_remote_address (connection->priv->socket, error);
177 g_socket_connection_get_property (GObject *object,
182 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
187 g_value_set_object (value, connection->priv->socket);
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 g_socket_connection_set_property (GObject *object,
201 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
206 connection->priv->socket = G_SOCKET (g_value_dup_object (value));
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 g_socket_connection_constructed (GObject *object)
217 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
219 g_assert (connection->priv->socket != NULL);
223 g_socket_connection_dispose (GObject *object)
225 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
227 connection->priv->in_dispose = TRUE;
229 G_OBJECT_CLASS (g_socket_connection_parent_class)
232 connection->priv->in_dispose = FALSE;
236 g_socket_connection_finalize (GObject *object)
238 GSocketConnection *connection = G_SOCKET_CONNECTION (object);
240 if (connection->priv->input_stream)
241 g_object_unref (connection->priv->input_stream);
243 if (connection->priv->output_stream)
244 g_object_unref (connection->priv->output_stream);
246 g_object_unref (connection->priv->socket);
248 G_OBJECT_CLASS (g_socket_connection_parent_class)
253 g_socket_connection_class_init (GSocketConnectionClass *klass)
255 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
256 GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
258 g_type_class_add_private (klass, sizeof (GSocketConnectionPrivate));
260 gobject_class->set_property = g_socket_connection_set_property;
261 gobject_class->get_property = g_socket_connection_get_property;
262 gobject_class->constructed = g_socket_connection_constructed;
263 gobject_class->finalize = g_socket_connection_finalize;
264 gobject_class->dispose = g_socket_connection_dispose;
266 stream_class->get_input_stream = g_socket_connection_get_input_stream;
267 stream_class->get_output_stream = g_socket_connection_get_output_stream;
268 stream_class->close_fn = g_socket_connection_close;
269 stream_class->close_async = g_socket_connection_close_async;
270 stream_class->close_finish = g_socket_connection_close_finish;
272 g_object_class_install_property (gobject_class,
274 g_param_spec_object ("socket",
276 P_("The underlying GSocket"),
278 G_PARAM_CONSTRUCT_ONLY |
280 G_PARAM_STATIC_STRINGS));
284 g_socket_connection_init (GSocketConnection *connection)
286 connection->priv = G_TYPE_INSTANCE_GET_PRIVATE (connection,
287 G_TYPE_SOCKET_CONNECTION,
288 GSocketConnectionPrivate);
292 g_socket_connection_close (GIOStream *stream,
293 GCancellable *cancellable,
296 GSocketConnection *connection = G_SOCKET_CONNECTION (stream);
298 if (connection->priv->output_stream)
299 g_output_stream_close (connection->priv->output_stream,
301 if (connection->priv->input_stream)
302 g_input_stream_close (connection->priv->input_stream,
305 /* Don't close the underlying socket if this is being called
306 * as part of dispose(); when destroying the GSocketConnection,
307 * we only want to close the socket if we're holding the last
308 * reference on it, and in that case it will close itself when
309 * we unref it in finalize().
311 if (connection->priv->in_dispose)
314 return g_socket_close (connection->priv->socket, error);
319 g_socket_connection_close_async (GIOStream *stream,
321 GCancellable *cancellable,
322 GAsyncReadyCallback callback,
325 GSimpleAsyncResult *res;
326 GIOStreamClass *class;
329 class = G_IO_STREAM_GET_CLASS (stream);
331 /* socket close is not blocked, just do it! */
333 if (class->close_fn &&
334 !class->close_fn (stream, cancellable, &error))
336 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
339 g_error_free (error);
343 res = g_simple_async_result_new (G_OBJECT (stream),
346 g_socket_connection_close_async);
347 g_simple_async_result_complete_in_idle (res);
348 g_object_unref (res);
352 g_socket_connection_close_finish (GIOStream *stream,
353 GAsyncResult *result,
360 GSocketFamily socket_family;
361 GSocketType socket_type;
363 GType implementation;
367 connection_factory_hash (gconstpointer key)
369 const ConnectionFactory *factory = key;
372 h = factory->socket_family ^ (factory->socket_type << 4) ^ (factory->protocol << 8);
373 /* This is likely to be small, so spread over whole
374 hash space to get some distribution */
375 h = h ^ (h << 8) ^ (h << 16) ^ (h << 24);
381 connection_factory_equal (gconstpointer _a,
384 const ConnectionFactory *a = _a;
385 const ConnectionFactory *b = _b;
387 if (a->socket_family != b->socket_family)
390 if (a->socket_type != b->socket_type)
393 if (a->protocol != b->protocol)
399 static GHashTable *connection_factories = NULL;
400 G_LOCK_DEFINE_STATIC(connection_factories);
403 * g_socket_connection_factory_register_type:
404 * @g_type: a #GType, inheriting from %G_TYPE_SOCKET_CONNECTION
405 * @family: a #GSocketFamily
406 * @type: a #GSocketType
407 * @protocol: a protocol id
409 * Looks up the #GType to be used when creating socket connections on
410 * sockets with the specified @family,@type and @protocol.
412 * If no type is registered, the #GSocketConnection base type is returned.
417 g_socket_connection_factory_register_type (GType g_type,
418 GSocketFamily family,
422 ConnectionFactory *factory;
424 g_return_if_fail (g_type_is_a (g_type, G_TYPE_SOCKET_CONNECTION));
426 G_LOCK (connection_factories);
428 if (connection_factories == NULL)
429 connection_factories = g_hash_table_new_full (connection_factory_hash,
430 connection_factory_equal,
431 (GDestroyNotify)g_free,
434 factory = g_new0 (ConnectionFactory, 1);
435 factory->socket_family = family;
436 factory->socket_type = type;
437 factory->protocol = protocol;
438 factory->implementation = g_type;
440 g_hash_table_insert (connection_factories,
443 G_UNLOCK (connection_factories);
447 init_builtin_types (void)
449 volatile GType a_type;
451 a_type = g_unix_connection_get_type ();
453 a_type = g_tcp_connection_get_type ();
457 * g_socket_connection_factory_lookup_type:
458 * @family: a #GSocketFamily
459 * @type: a #GSocketType
460 * @protocol_id: a protocol id
462 * Looks up the #GType to be used when creating socket connections on
463 * sockets with the specified @family,@type and @protocol_id.
465 * If no type is registered, the #GSocketConnection base type is returned.
472 g_socket_connection_factory_lookup_type (GSocketFamily family,
476 ConnectionFactory *factory, key;
479 init_builtin_types ();
481 G_LOCK (connection_factories);
483 g_type = G_TYPE_SOCKET_CONNECTION;
485 if (connection_factories)
487 key.socket_family = family;
488 key.socket_type = type;
489 key.protocol = protocol_id;
491 factory = g_hash_table_lookup (connection_factories, &key);
493 g_type = factory->implementation;
496 G_UNLOCK (connection_factories);
502 * g_socket_connection_factory_create_connection:
503 * @socket: a #GSocket
505 * Creates a #GSocketConnection subclass of the right type for
508 * Returns: a #GSocketConnection
513 g_socket_connection_factory_create_connection (GSocket *socket)
517 type = g_socket_connection_factory_lookup_type (g_socket_get_family (socket),
518 g_socket_get_socket_type (socket),
519 g_socket_get_protocol (socket));
520 return g_object_new (type, "socket", socket, NULL);
523 #define __G_SOCKET_CONNECTION_C__
524 #include "gioaliasdef.c"