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 "gsocketaddressenumerator.h"
25 #include "gsimpleasyncresult.h"
28 G_DEFINE_ABSTRACT_TYPE (GSocketAddressEnumerator, g_socket_address_enumerator, G_TYPE_OBJECT);
30 static void g_socket_address_enumerator_real_next_async (GSocketAddressEnumerator *enumerator,
31 GCancellable *cancellable,
32 GAsyncReadyCallback callback,
34 static GSocketAddress *g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator *enumerator,
39 g_socket_address_enumerator_init (GSocketAddressEnumerator *enumerator)
44 g_socket_address_enumerator_class_init (GSocketAddressEnumeratorClass *enumerator_class)
46 enumerator_class->next_async = g_socket_address_enumerator_real_next_async;
47 enumerator_class->next_finish = g_socket_address_enumerator_real_next_finish;
51 * g_socket_address_enumerator_next:
52 * @enumerator: a #GSocketAddressEnumerator
53 * @cancellable: optional #GCancellable object, %NULL to ignore.
56 * Retrieves the next #GSocketAddress from @enumerator. Note that this
57 * may block for some amount of time. (Eg, a #GNetworkAddress may need
58 * to do a DNS lookup before it can return an address.) Use
59 * g_socket_address_enumerator_next_async() if you need to avoid
62 * If @enumerator is expected to yield addresses, but for some reason
63 * is unable to (eg, because of a DNS error), then the first call to
64 * g_socket_address_enumerator_next() will return an appropriate error
65 * in *@error. However, if the first call to
66 * g_socket_address_enumerator_next() succeeds, then any further
67 * internal errors (other than @cancellable being triggered) will be
70 * Return value: (transfer full): a #GSocketAddress (owned by the caller), or %NULL on
71 * error (in which case *@error will be set) or if there are no
75 g_socket_address_enumerator_next (GSocketAddressEnumerator *enumerator,
76 GCancellable *cancellable,
79 GSocketAddressEnumeratorClass *klass;
81 g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL);
83 klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
85 return (* klass->next) (enumerator, cancellable, error);
88 /* Default implementation just calls the synchronous method; this can
89 * be used if the implementation already knows all of its addresses,
90 * and so the synchronous method will never block.
93 g_socket_address_enumerator_real_next_async (GSocketAddressEnumerator *enumerator,
94 GCancellable *cancellable,
95 GAsyncReadyCallback callback,
98 GSimpleAsyncResult *result;
99 GSocketAddress *address;
100 GError *error = NULL;
102 result = g_simple_async_result_new (G_OBJECT (enumerator),
104 g_socket_address_enumerator_real_next_async);
105 address = g_socket_address_enumerator_next (enumerator, cancellable, &error);
107 g_simple_async_result_set_op_res_gpointer (result, address, NULL);
110 g_simple_async_result_set_from_error (result, error);
111 g_error_free (error);
113 g_simple_async_result_complete_in_idle (result);
114 g_object_unref (result);
118 * g_socket_address_enumerator_next_async:
119 * @enumerator: a #GSocketAddressEnumerator
120 * @cancellable: optional #GCancellable object, %NULL to ignore.
121 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
122 * @user_data: the data to pass to callback function
124 * Asynchronously retrieves the next #GSocketAddress from @enumerator
125 * and then calls @callback, which must call
126 * g_socket_address_enumerator_next_finish() to get the result.
129 g_socket_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
130 GCancellable *cancellable,
131 GAsyncReadyCallback callback,
134 GSocketAddressEnumeratorClass *klass;
136 g_return_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator));
138 klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
140 (* klass->next_async) (enumerator, cancellable, callback, user_data);
143 static GSocketAddress *
144 g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator *enumerator,
145 GAsyncResult *result,
148 GSimpleAsyncResult *simple;
150 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
151 simple = G_SIMPLE_ASYNC_RESULT (result);
152 g_return_val_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_address_enumerator_real_next_async, NULL);
154 if (g_simple_async_result_propagate_error (simple, error))
157 return g_simple_async_result_get_op_res_gpointer (simple);
161 * g_socket_address_enumerator_next_finish:
162 * @enumerator: a #GSocketAddressEnumerator
163 * @result: a #GAsyncResult
166 * Retrieves the result of a completed call to
167 * g_socket_address_enumerator_next_async(). See
168 * g_socket_address_enumerator_next() for more information about
171 * Return value: (transfer full): a #GSocketAddress (owned by the caller), or %NULL on
172 * error (in which case *@error will be set) or if there are no
176 g_socket_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
177 GAsyncResult *result,
180 GSocketAddressEnumeratorClass *klass;
182 g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL);
184 klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
186 return (* klass->next_finish) (enumerator, result, error);