/src/glib/gio/gsocketaddressenumerator.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright (C) 2008 Red Hat, Inc. |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
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. |
11 | | * |
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. |
16 | | * |
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/>. |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | #include "gsocketaddressenumerator.h" |
23 | | #include "glibintl.h" |
24 | | |
25 | | #include "gtask.h" |
26 | | |
27 | | /** |
28 | | * SECTION:gsocketaddressenumerator |
29 | | * @short_description: Enumerator for socket addresses |
30 | | * @include: gio/gio.h |
31 | | * |
32 | | * #GSocketAddressEnumerator is an enumerator type for #GSocketAddress |
33 | | * instances. It is returned by enumeration functions such as |
34 | | * g_socket_connectable_enumerate(), which returns a #GSocketAddressEnumerator |
35 | | * to list each #GSocketAddress which could be used to connect to that |
36 | | * #GSocketConnectable. |
37 | | * |
38 | | * Enumeration is typically a blocking operation, so the asynchronous methods |
39 | | * g_socket_address_enumerator_next_async() and |
40 | | * g_socket_address_enumerator_next_finish() should be used where possible. |
41 | | * |
42 | | * Each #GSocketAddressEnumerator can only be enumerated once. Once |
43 | | * g_socket_address_enumerator_next() has returned %NULL, further |
44 | | * enumeration with that #GSocketAddressEnumerator is not possible, and it can |
45 | | * be unreffed. |
46 | | */ |
47 | | |
48 | | G_DEFINE_ABSTRACT_TYPE (GSocketAddressEnumerator, g_socket_address_enumerator, G_TYPE_OBJECT) |
49 | | |
50 | | static void g_socket_address_enumerator_real_next_async (GSocketAddressEnumerator *enumerator, |
51 | | GCancellable *cancellable, |
52 | | GAsyncReadyCallback callback, |
53 | | gpointer user_data); |
54 | | static GSocketAddress *g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator *enumerator, |
55 | | GAsyncResult *result, |
56 | | GError **error); |
57 | | |
58 | | static void |
59 | | g_socket_address_enumerator_init (GSocketAddressEnumerator *enumerator) |
60 | 0 | { |
61 | 0 | } |
62 | | |
63 | | static void |
64 | | g_socket_address_enumerator_class_init (GSocketAddressEnumeratorClass *enumerator_class) |
65 | 0 | { |
66 | 0 | enumerator_class->next_async = g_socket_address_enumerator_real_next_async; |
67 | 0 | enumerator_class->next_finish = g_socket_address_enumerator_real_next_finish; |
68 | 0 | } |
69 | | |
70 | | /** |
71 | | * g_socket_address_enumerator_next: |
72 | | * @enumerator: a #GSocketAddressEnumerator |
73 | | * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. |
74 | | * @error: a #GError. |
75 | | * |
76 | | * Retrieves the next #GSocketAddress from @enumerator. Note that this |
77 | | * may block for some amount of time. (Eg, a #GNetworkAddress may need |
78 | | * to do a DNS lookup before it can return an address.) Use |
79 | | * g_socket_address_enumerator_next_async() if you need to avoid |
80 | | * blocking. |
81 | | * |
82 | | * If @enumerator is expected to yield addresses, but for some reason |
83 | | * is unable to (eg, because of a DNS error), then the first call to |
84 | | * g_socket_address_enumerator_next() will return an appropriate error |
85 | | * in *@error. However, if the first call to |
86 | | * g_socket_address_enumerator_next() succeeds, then any further |
87 | | * internal errors (other than @cancellable being triggered) will be |
88 | | * ignored. |
89 | | * |
90 | | * Returns: (transfer full) (nullable): a #GSocketAddress (owned by the caller), or %NULL on |
91 | | * error (in which case *@error will be set) or if there are no |
92 | | * more addresses. |
93 | | */ |
94 | | GSocketAddress * |
95 | | g_socket_address_enumerator_next (GSocketAddressEnumerator *enumerator, |
96 | | GCancellable *cancellable, |
97 | | GError **error) |
98 | 0 | { |
99 | 0 | GSocketAddressEnumeratorClass *klass; |
100 | |
|
101 | 0 | g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL); |
102 | | |
103 | 0 | klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator); |
104 | |
|
105 | 0 | return (* klass->next) (enumerator, cancellable, error); |
106 | 0 | } |
107 | | |
108 | | /* Default implementation just calls the synchronous method; this can |
109 | | * be used if the implementation already knows all of its addresses, |
110 | | * and so the synchronous method will never block. |
111 | | */ |
112 | | static void |
113 | | g_socket_address_enumerator_real_next_async (GSocketAddressEnumerator *enumerator, |
114 | | GCancellable *cancellable, |
115 | | GAsyncReadyCallback callback, |
116 | | gpointer user_data) |
117 | 0 | { |
118 | 0 | GTask *task; |
119 | 0 | GSocketAddress *address; |
120 | 0 | GError *error = NULL; |
121 | |
|
122 | 0 | task = g_task_new (enumerator, NULL, callback, user_data); |
123 | 0 | g_task_set_source_tag (task, g_socket_address_enumerator_real_next_async); |
124 | |
|
125 | 0 | address = g_socket_address_enumerator_next (enumerator, cancellable, &error); |
126 | 0 | if (error) |
127 | 0 | g_task_return_error (task, error); |
128 | 0 | else |
129 | 0 | g_task_return_pointer (task, address, g_object_unref); |
130 | |
|
131 | 0 | g_object_unref (task); |
132 | 0 | } |
133 | | |
134 | | /** |
135 | | * g_socket_address_enumerator_next_async: |
136 | | * @enumerator: a #GSocketAddressEnumerator |
137 | | * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. |
138 | | * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call |
139 | | * when the request is satisfied |
140 | | * @user_data: the data to pass to callback function |
141 | | * |
142 | | * Asynchronously retrieves the next #GSocketAddress from @enumerator |
143 | | * and then calls @callback, which must call |
144 | | * g_socket_address_enumerator_next_finish() to get the result. |
145 | | * |
146 | | * It is an error to call this multiple times before the previous callback has finished. |
147 | | */ |
148 | | void |
149 | | g_socket_address_enumerator_next_async (GSocketAddressEnumerator *enumerator, |
150 | | GCancellable *cancellable, |
151 | | GAsyncReadyCallback callback, |
152 | | gpointer user_data) |
153 | 0 | { |
154 | 0 | GSocketAddressEnumeratorClass *klass; |
155 | |
|
156 | 0 | g_return_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator)); |
157 | | |
158 | 0 | klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator); |
159 | |
|
160 | 0 | (* klass->next_async) (enumerator, cancellable, callback, user_data); |
161 | 0 | } |
162 | | |
163 | | static GSocketAddress * |
164 | | g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator *enumerator, |
165 | | GAsyncResult *result, |
166 | | GError **error) |
167 | 0 | { |
168 | 0 | g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL); |
169 | | |
170 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
171 | 0 | } |
172 | | |
173 | | /** |
174 | | * g_socket_address_enumerator_next_finish: |
175 | | * @enumerator: a #GSocketAddressEnumerator |
176 | | * @result: a #GAsyncResult |
177 | | * @error: a #GError |
178 | | * |
179 | | * Retrieves the result of a completed call to |
180 | | * g_socket_address_enumerator_next_async(). See |
181 | | * g_socket_address_enumerator_next() for more information about |
182 | | * error handling. |
183 | | * |
184 | | * Returns: (transfer full) (nullable): a #GSocketAddress (owned by the caller), or %NULL on |
185 | | * error (in which case *@error will be set) or if there are no |
186 | | * more addresses. |
187 | | */ |
188 | | GSocketAddress * |
189 | | g_socket_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator, |
190 | | GAsyncResult *result, |
191 | | GError **error) |
192 | 0 | { |
193 | 0 | GSocketAddressEnumeratorClass *klass; |
194 | |
|
195 | 0 | g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL); |
196 | | |
197 | 0 | klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator); |
198 | |
|
199 | 0 | return (* klass->next_finish) (enumerator, result, error); |
200 | 0 | } |