1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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, see <http://www.gnu.org/licenses/>.
22 #include "gsocketconnectable.h"
29 * Objects that describe one or more potential socket endpoints
30 * implement `GSocketConnectable`. Callers can then use
31 * [method@Gio.SocketConnectable.enumerate] to get a
32 * [class@Gio.SocketAddressEnumerator] to try out each socket address in turn
33 * until one succeeds, as shown in the sample code below.
37 * connect_to_host (const char *hostname,
39 * GCancellable *cancellable,
42 * MyConnection *conn = NULL;
43 * GSocketConnectable *addr;
44 * GSocketAddressEnumerator *enumerator;
45 * GSocketAddress *sockaddr;
46 * GError *conn_error = NULL;
48 * addr = g_network_address_new (hostname, port);
49 * enumerator = g_socket_connectable_enumerate (addr);
50 * g_object_unref (addr);
52 * // Try each sockaddr until we succeed. Record the first connection error,
53 * // but not any further ones (since they'll probably be basically the same
55 * while (!conn && (sockaddr = g_socket_address_enumerator_next (enumerator, cancellable, error))
57 * conn = connect_to_sockaddr (sockaddr, conn_error ? NULL : &conn_error);
58 * g_object_unref (sockaddr);
60 * g_object_unref (enumerator);
66 * // We couldn't connect to the first address, but we succeeded
67 * // in connecting to a later address.
68 * g_error_free (conn_error);
74 * /// Either initial lookup failed, or else the caller cancelled us.
76 * g_error_free (conn_error);
81 * g_error_propagate (error, conn_error);
89 typedef GSocketConnectableIface GSocketConnectableInterface;
90 G_DEFINE_INTERFACE (GSocketConnectable, g_socket_connectable, G_TYPE_OBJECT)
93 g_socket_connectable_default_init (GSocketConnectableInterface *iface)
98 * g_socket_connectable_enumerate:
99 * @connectable: a #GSocketConnectable
101 * Creates a #GSocketAddressEnumerator for @connectable.
103 * Returns: (transfer full): a new #GSocketAddressEnumerator.
107 GSocketAddressEnumerator *
108 g_socket_connectable_enumerate (GSocketConnectable *connectable)
110 GSocketConnectableIface *iface;
112 g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
114 iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
116 return (* iface->enumerate) (connectable);
120 * g_socket_connectable_proxy_enumerate:
121 * @connectable: a #GSocketConnectable
123 * Creates a #GSocketAddressEnumerator for @connectable that will
124 * return a #GProxyAddress for each of its addresses that you must connect
127 * If @connectable does not implement
128 * g_socket_connectable_proxy_enumerate(), this will fall back to
129 * calling g_socket_connectable_enumerate().
131 * Returns: (transfer full): a new #GSocketAddressEnumerator.
135 GSocketAddressEnumerator *
136 g_socket_connectable_proxy_enumerate (GSocketConnectable *connectable)
138 GSocketConnectableIface *iface;
140 g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
142 iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
144 if (iface->proxy_enumerate)
145 return (* iface->proxy_enumerate) (connectable);
147 return (* iface->enumerate) (connectable);
151 * g_socket_connectable_to_string:
152 * @connectable: a #GSocketConnectable
154 * Format a #GSocketConnectable as a string. This is a human-readable format for
155 * use in debugging output, and is not a stable serialization format. It is not
156 * suitable for use in user interfaces as it exposes too much information for a
159 * If the #GSocketConnectable implementation does not support string formatting,
160 * the implementation’s type name will be returned as a fallback.
162 * Returns: (transfer full): the formatted string
167 g_socket_connectable_to_string (GSocketConnectable *connectable)
169 GSocketConnectableIface *iface;
171 g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
173 iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
175 if (iface->to_string != NULL)
176 return iface->to_string (connectable);
178 return g_strdup (G_OBJECT_TYPE_NAME (connectable));