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.1 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, see <http://www.gnu.org/licenses/>.
20 #include "gsocketaddressenumerator.h"
26 * SECTION:gsocketaddressenumerator
27 * @short_description: Enumerator for socket addresses
30 * #GSocketAddressEnumerator is an enumerator type for #GSocketAddress
31 * instances. It is returned by enumeration functions such as
32 * g_socket_connectable_enumerate(), which returns a #GSocketAddressEnumerator
33 * to list each #GSocketAddress which could be used to connect to that
34 * #GSocketConnectable.
36 * Enumeration is typically a blocking operation, so the asynchronous methods
37 * g_socket_address_enumerator_next_async() and
38 * g_socket_address_enumerator_next_finish() should be used where possible.
40 * Each #GSocketAddressEnumerator can only be enumerated once. Once
41 * g_socket_address_enumerator_next() has returned %NULL, further
42 * enumeration with that #GSocketAddressEnumerator is not possible, and it can
46 G_DEFINE_ABSTRACT_TYPE (GSocketAddressEnumerator, g_socket_address_enumerator, G_TYPE_OBJECT)
48 static void g_socket_address_enumerator_real_next_async (GSocketAddressEnumerator *enumerator,
49 GCancellable *cancellable,
50 GAsyncReadyCallback callback,
52 static GSocketAddress *g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator *enumerator,
57 g_socket_address_enumerator_init (GSocketAddressEnumerator *enumerator)
62 g_socket_address_enumerator_class_init (GSocketAddressEnumeratorClass *enumerator_class)
64 enumerator_class->next_async = g_socket_address_enumerator_real_next_async;
65 enumerator_class->next_finish = g_socket_address_enumerator_real_next_finish;
69 * g_socket_address_enumerator_next:
70 * @enumerator: a #GSocketAddressEnumerator
71 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
74 * Retrieves the next #GSocketAddress from @enumerator. Note that this
75 * may block for some amount of time. (Eg, a #GNetworkAddress may need
76 * to do a DNS lookup before it can return an address.) Use
77 * g_socket_address_enumerator_next_async() if you need to avoid
80 * If @enumerator is expected to yield addresses, but for some reason
81 * is unable to (eg, because of a DNS error), then the first call to
82 * g_socket_address_enumerator_next() will return an appropriate error
83 * in *@error. However, if the first call to
84 * g_socket_address_enumerator_next() succeeds, then any further
85 * internal errors (other than @cancellable being triggered) will be
88 * Returns: (transfer full): a #GSocketAddress (owned by the caller), or %NULL on
89 * error (in which case *@error will be set) or if there are no
93 g_socket_address_enumerator_next (GSocketAddressEnumerator *enumerator,
94 GCancellable *cancellable,
97 GSocketAddressEnumeratorClass *klass;
99 g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL);
101 klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
103 return (* klass->next) (enumerator, cancellable, error);
106 /* Default implementation just calls the synchronous method; this can
107 * be used if the implementation already knows all of its addresses,
108 * and so the synchronous method will never block.
111 g_socket_address_enumerator_real_next_async (GSocketAddressEnumerator *enumerator,
112 GCancellable *cancellable,
113 GAsyncReadyCallback callback,
117 GSocketAddress *address;
118 GError *error = NULL;
120 task = g_task_new (enumerator, NULL, callback, user_data);
121 g_task_set_source_tag (task, g_socket_address_enumerator_real_next_async);
123 address = g_socket_address_enumerator_next (enumerator, cancellable, &error);
125 g_task_return_error (task, error);
127 g_task_return_pointer (task, address, g_object_unref);
129 g_object_unref (task);
133 * g_socket_address_enumerator_next_async:
134 * @enumerator: a #GSocketAddressEnumerator
135 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
136 * @callback: (scope async): a #GAsyncReadyCallback to call when the request
138 * @user_data: (closure): the data to pass to callback function
140 * Asynchronously retrieves the next #GSocketAddress from @enumerator
141 * and then calls @callback, which must call
142 * g_socket_address_enumerator_next_finish() to get the result.
144 * It is an error to call this multiple times before the previous callback has finished.
147 g_socket_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
148 GCancellable *cancellable,
149 GAsyncReadyCallback callback,
152 GSocketAddressEnumeratorClass *klass;
154 g_return_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator));
156 klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
158 (* klass->next_async) (enumerator, cancellable, callback, user_data);
161 static GSocketAddress *
162 g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator *enumerator,
163 GAsyncResult *result,
166 g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL);
168 return g_task_propagate_pointer (G_TASK (result), error);
172 * g_socket_address_enumerator_next_finish:
173 * @enumerator: a #GSocketAddressEnumerator
174 * @result: a #GAsyncResult
177 * Retrieves the result of a completed call to
178 * g_socket_address_enumerator_next_async(). See
179 * g_socket_address_enumerator_next() for more information about
182 * Returns: (transfer full): a #GSocketAddress (owned by the caller), or %NULL on
183 * error (in which case *@error will be set) or if there are no
187 g_socket_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
188 GAsyncResult *result,
191 GSocketAddressEnumeratorClass *klass;
193 g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL);
195 klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
197 return (* klass->next_finish) (enumerator, result, error);