1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "gsocketconnectable.h"
27 * SECTION:gsocketconnectable
28 * @short_description: Interface for potential socket endpoints
30 * Objects that describe one or more potential socket endpoints
31 * implement #GSocketConnectable. Callers can then use
32 * g_socket_connectable_enumerate() to get a #GSocketAddressEnumerator
33 * to try out each socket address in turn until one succeeds, as shown
34 * in the sample code below.
38 * connect_to_host (const char *hostname,
40 * GCancellable *cancellable,
43 * MyConnection *conn = NULL;
44 * GSocketConnectable *addr;
45 * GSocketAddressEnumerator *enumerator;
46 * GSocketAddress *sockaddr;
47 * GError *conn_error = NULL;
49 * addr = g_network_address_new (hostname, port);
50 * enumerator = g_socket_connectable_enumerate (addr);
51 * g_object_unref (addr);
53 * /<!-- -->* Try each sockaddr until we succeed. Record the first
54 * * connection error, but not any further ones (since they'll probably
55 * * be basically the same as the first).
57 * while (!conn && (sockaddr = g_socket_address_enumerator_next (enumerator, cancellable, error))
59 * conn = connect_to_sockaddr (sockaddr, conn_error ? NULL : &conn_error);
60 * g_object_unref (sockaddr);
62 * g_object_unref (enumerator);
68 * /<!-- -->* We couldn't connect to the first address, but we succeeded
69 * * in connecting to a later address.
71 * g_error_free (conn_error);
77 * /<!-- -->* Either the initial lookup failed, or else the caller
81 * g_error_free (conn_error);
86 * g_error_propagate (error, conn_error);
94 typedef GSocketConnectableIface GSocketConnectableInterface;
95 G_DEFINE_INTERFACE (GSocketConnectable, g_socket_connectable, G_TYPE_OBJECT)
98 g_socket_connectable_default_init (GSocketConnectableInterface *iface)
103 * g_socket_connectable_enumerate:
104 * @connectable: a #GSocketConnectable
106 * Creates a #GSocketAddressEnumerator for @connectable.
108 * Return value: (transfer full): a new #GSocketAddressEnumerator.
112 GSocketAddressEnumerator *
113 g_socket_connectable_enumerate (GSocketConnectable *connectable)
115 GSocketConnectableIface *iface;
117 g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
119 iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
121 return (* iface->enumerate) (connectable);
125 * g_socket_connectable_proxy_enumerate:
126 * @connectable: a #GSocketConnectable
128 * Creates a #GSocketAddressEnumerator for @connectable that will
129 * return #GProxyAddress<!-- -->es for addresses that you must connect
132 * If @connectable does not implement
133 * g_socket_connectable_proxy_enumerate(), this will fall back to
134 * calling g_socket_connectable_enumerate().
136 * Return value: (transfer full): a new #GSocketAddressEnumerator.
140 GSocketAddressEnumerator *
141 g_socket_connectable_proxy_enumerate (GSocketConnectable *connectable)
143 GSocketConnectableIface *iface;
145 g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
147 iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
149 if (iface->proxy_enumerate)
150 return (* iface->proxy_enumerate) (connectable);
152 return (* iface->enumerate) (connectable);