clarify documentation
[platform/upstream/glib.git] / gio / gsocketlistener.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
4  * Copyright © 2009 codethink
5  * Copyright © 2009 Red Hat, Inc
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 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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Authors: Christian Kellner <gicmo@gnome.org>
23  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
24  *          Ryan Lortie <desrt@desrt.ca>
25  *          Alexander Larsson <alexl@redhat.com>
26  */
27
28 #include "config.h"
29 #include "gsocketlistener.h"
30
31 #include <gio/gsimpleasyncresult.h>
32 #include <gio/gcancellable.h>
33 #include <gio/gsocketaddress.h>
34 #include <gio/ginetaddress.h>
35 #include <gio/gioerror.h>
36 #include <gio/gsocket.h>
37 #include <gio/gsocketconnection.h>
38 #include <gio/ginetsocketaddress.h>
39 #include "glibintl.h"
40
41 #include "gioalias.h"
42
43 /**
44  * SECTION: gsocketlistener
45  * @title: GSocketListener
46  * @short_description: Helper for accepting network client connections
47  * @see_also: #GThreadedSocketService, #GSocketService.
48  *
49  * A #GSocketListener is an object that keeps track of a set
50  * of server sockets and helps you accept sockets from any of the
51  * socket, either sync or async.
52  *
53  * If you want to implement a network server, also look at #GSocketService
54  * and #GThreadedSocketService which are subclass of #GSocketListener
55  * that makes this even easier.
56  *
57  * Since: 2.22
58  */
59
60 G_DEFINE_TYPE (GSocketListener, g_socket_listener, G_TYPE_OBJECT);
61
62 enum
63 {
64   PROP_0,
65   PROP_LISTEN_BACKLOG
66 };
67
68
69 static GQuark source_quark = 0;
70
71 struct _GSocketListenerPrivate
72 {
73   GPtrArray           *sockets;
74   GMainContext        *main_context;
75   int                 listen_backlog;
76   guint               closed : 1;
77 };
78
79 static void
80 g_socket_listener_finalize (GObject *object)
81 {
82   GSocketListener *listener = G_SOCKET_LISTENER (object);
83
84   if (listener->priv->main_context)
85     g_main_context_unref (listener->priv->main_context);
86
87   if (!listener->priv->closed)
88     g_socket_listener_close (listener);
89
90   g_ptr_array_free (listener->priv->sockets, TRUE);
91
92   G_OBJECT_CLASS (g_socket_listener_parent_class)
93     ->finalize (object);
94 }
95
96 static void
97 g_socket_listener_get_property (GObject    *object,
98                                 guint       prop_id,
99                                 GValue     *value,
100                                 GParamSpec *pspec)
101 {
102   GSocketListener *listener = G_SOCKET_LISTENER (object);
103
104   switch (prop_id)
105     {
106       case PROP_LISTEN_BACKLOG:
107         g_value_set_int (value, listener->priv->listen_backlog);
108         break;
109
110       default:
111         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112     }
113 }
114
115 static void
116 g_socket_listener_set_property (GObject      *object,
117                                 guint         prop_id,
118                                 const GValue *value,
119                                 GParamSpec   *pspec)
120 {
121   GSocketListener *listener = G_SOCKET_LISTENER (object);
122
123   switch (prop_id)
124     {
125       case PROP_LISTEN_BACKLOG:
126         g_socket_listener_set_backlog (listener, g_value_get_int (value));
127         break;
128
129       default:
130         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131     }
132 }
133
134
135 static void
136 g_socket_listener_class_init (GSocketListenerClass *klass)
137 {
138   GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
139
140   g_type_class_add_private (klass, sizeof (GSocketListenerPrivate));
141
142   gobject_class->finalize = g_socket_listener_finalize;
143   gobject_class->set_property = g_socket_listener_set_property;
144   gobject_class->get_property = g_socket_listener_get_property;
145   g_object_class_install_property (gobject_class, PROP_LISTEN_BACKLOG,
146                                    g_param_spec_int ("listen-backlog",
147                                                      P_("Listen backlog"),
148                                                      P_("outstanding connections in the listen queue"),
149                                                      0,
150                                                      2000,
151                                                      10,
152                                                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153
154   source_quark = g_quark_from_static_string ("g-socket-listener-source");
155 }
156
157 static void
158 g_socket_listener_init (GSocketListener *listener)
159 {
160   listener->priv = G_TYPE_INSTANCE_GET_PRIVATE (listener,
161                                                 G_TYPE_SOCKET_LISTENER,
162                                                 GSocketListenerPrivate);
163   listener->priv->sockets =
164     g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
165   listener->priv->listen_backlog = 10;
166 }
167
168 /**
169  * g_socket_listener_new:
170  *
171  * Creates a new #GSocketListener with no sockets to listen for.
172  * New listeners can be added with e.g. g_socket_listener_add_address()
173  * or g_socket_listener_add_inet_port().
174  *
175  * Returns: a new #GSocketListener.
176  *
177  * Since: 2.22
178  */
179 GSocketListener *
180 g_socket_listener_new (void)
181 {
182   return g_object_new (G_TYPE_SOCKET_LISTENER, NULL);
183 }
184
185 static gboolean
186 check_listener (GSocketListener *listener,
187                 GError **error)
188 {
189   if (listener->priv->closed)
190     {
191       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
192                            _("Listener is already closed"));
193       return FALSE;
194     }
195
196   return TRUE;
197 }
198
199 /**
200  * g_socket_listener_add_socket:
201  * @listener: a #GSocketListener
202  * @socket: a listening #GSocket, this function takes ownership
203  *          of the reference
204  * @source_object: Optional #GObject identifying this source
205  * @error: #GError for error reporting, or %NULL to ignore.
206  *
207  * Adds @socket to the set of sockets that we try to accept
208  * new clients from. The socket must be bound to a local
209  * address and listened to.
210  *
211  * @source_object will be passed out in the various calls
212  * to accept to identify this particular source, which is
213  * useful if you're listening on multiple addresses and do
214  * different things depending on what address is connected to.
215  *
216  * Returns: %TRUE on success, %FALSE on error.
217  *
218  * Since: 2.22
219  */
220 gboolean
221 g_socket_listener_add_socket (GSocketListener  *listener,
222                               GSocket          *socket,
223                               GObject          *source_object,
224                               GError          **error)
225 {
226   if (!check_listener (listener, error))
227     return FALSE;
228
229   /* TODO: Check that socket it is bound & not closed? */
230
231   if (g_socket_is_closed (socket))
232     {
233       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
234                            _("Added socket is closed"));
235       return FALSE;
236     }
237
238   g_object_ref (socket);
239   g_ptr_array_add (listener->priv->sockets, socket);
240
241   if (source_object)
242     g_object_set_qdata_full (G_OBJECT (socket), source_quark,
243                              g_object_ref (source_object), g_object_unref);
244
245
246   if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
247     G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
248
249   return TRUE;
250 }
251
252 /**
253  * g_socket_listener_add_address:
254  * @listener: a #GSocketListener
255  * @address: a #GSocketAddress
256  * @type: a #GSocketType
257  * @protocol: a #GSocketProtocol
258  * @source_object: Optional #GObject identifying this source
259  * @effective_address: location to store the address that was bound to, or %NULL.
260  * @error: #GError for error reporting, or %NULL to ignore.
261  *
262  * Creates a socket of type @type and protocol @protocol, binds
263  * it to @address and adds it to the set of sockets we're accepting
264  * sockets from.
265  *
266  * Note that adding an IPv6 address, depending on the platform,
267  * may or may not result in a listener that also accepts IPv4
268  * connections.  For more determinstic behaviour, see
269  * g_socket_listener_add_inet_port().
270  *
271  * @source_object will be passed out in the various calls
272  * to accept to identify this particular source, which is
273  * useful if you're listening on multiple addresses and do
274  * different things depending on what address is connected to.
275  *
276  * If successful and @effective_address is non-%NULL then it will
277  * be set to the address that the binding actually occured at.  This
278  * is helpful for determining the port number that was used for when
279  * requesting a binding to port 0 (ie: "any port").  This address, if
280  * requested, belongs to the caller and must be freed.
281  *
282  * Returns: %TRUE on success, %FALSE on error.
283  *
284  * Since: 2.22
285  */
286 gboolean
287 g_socket_listener_add_address (GSocketListener  *listener,
288                                GSocketAddress   *address,
289                                GSocketType       type,
290                                GSocketProtocol   protocol,
291                                GObject          *source_object,
292                                GSocketAddress  **effective_address,
293                                GError          **error)
294 {
295   GSocketAddress *local_address;
296   GSocketFamily family;
297   GSocket *socket;
298
299   if (!check_listener (listener, error))
300     return FALSE;
301
302   family = g_socket_address_get_family (address);
303   socket = g_socket_new (family, type, protocol, error);
304   if (socket == NULL)
305     return FALSE;
306
307   g_socket_set_listen_backlog (socket, listener->priv->listen_backlog);
308
309   if (!g_socket_bind (socket, address, TRUE, error) ||
310       !g_socket_listen (socket, error))
311     {
312       g_object_unref (socket);
313       return FALSE;
314     }
315
316   local_address = NULL;
317   if (effective_address)
318     {
319       local_address = g_socket_get_local_address (socket, error);
320       if (local_address == NULL)
321         {
322           g_object_unref (socket);
323           return FALSE;
324         }
325     }
326
327   if (!g_socket_listener_add_socket (listener, socket,
328                                      source_object,
329                                      error))
330     {
331       if (local_address)
332         g_object_unref (local_address);
333       g_object_unref (socket);
334       return FALSE;
335     }
336
337   if (effective_address)
338     *effective_address = local_address;
339
340   g_object_unref (socket); /* add_socket refs this */
341
342   return TRUE;
343 }
344
345 /**
346  * g_socket_listener_add_inet_port:
347  * @listener: a #GSocketListener
348  * @port: an IP port number (non-zero)
349  * @source_object: Optional #GObject identifying this source
350  * @error: #GError for error reporting, or %NULL to ignore.
351  *
352  * Helper function for g_socket_listener_add_address() that
353  * creates a TCP/IP socket listening on IPv4 and IPv6 (if
354  * supported) on the specified port on all interfaces.
355  *
356  * @source_object will be passed out in the various calls
357  * to accept to identify this particular source, which is
358  * useful if you're listening on multiple addresses and do
359  * different things depending on what address is connected to.
360  *
361  * Returns: %TRUE on success, %FALSE on error.
362  *
363  * Since: 2.22
364  */
365 gboolean
366 g_socket_listener_add_inet_port (GSocketListener  *listener,
367                                  guint16           port,
368                                  GObject          *source_object,
369                                  GError          **error)
370 {
371   gboolean need_ipv4_socket = TRUE;
372   GSocket *socket4 = NULL;
373   GSocket *socket6;
374
375   g_return_val_if_fail (listener != NULL, FALSE);
376   g_return_val_if_fail (port != 0, FALSE);
377
378   if (!check_listener (listener, error))
379     return FALSE;
380
381   /* first try to create an IPv6 socket */
382   socket6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
383                           G_SOCKET_TYPE_STREAM,
384                           G_SOCKET_PROTOCOL_DEFAULT,
385                           NULL);
386
387   if (socket6 != NULL)
388     /* IPv6 is supported on this platform, so if we fail now it is
389      * a result of being unable to bind to our port.  Don't fail
390      * silently as a result of this!
391      */
392     {
393       GInetAddress *inet_address;
394       GSocketAddress *address;
395       gboolean result;
396
397       inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
398       address = g_inet_socket_address_new (inet_address, port);
399       g_object_unref (inet_address);
400
401       g_socket_set_listen_backlog (socket6, listener->priv->listen_backlog);
402
403       result = g_socket_bind (socket6, address, TRUE, error) &&
404                g_socket_listen (socket6, error);
405
406       g_object_unref (address);
407
408       if (!result)
409         {
410           g_object_unref (socket6);
411
412           return FALSE;
413         }
414
415       if (source_object)
416         g_object_set_qdata_full (G_OBJECT (socket6), source_quark,
417                                  g_object_ref (source_object),
418                                  g_object_unref);
419
420       /* If this socket already speaks IPv4 then we are done. */
421       if (g_socket_speaks_ipv4 (socket6))
422         need_ipv4_socket = FALSE;
423     }
424
425   if (need_ipv4_socket)
426     /* We are here for exactly one of the following reasons:
427      *
428      *   - our platform doesn't support IPv6
429      *   - we successfully created an IPv6 socket but it's V6ONLY
430      *
431      * In either case, we need to go ahead and create an IPv4 socket
432      * and fail the call if we can't bind to it.
433      */
434     {
435       socket4 = g_socket_new (G_SOCKET_FAMILY_IPV4,
436                               G_SOCKET_TYPE_STREAM,
437                               G_SOCKET_PROTOCOL_DEFAULT,
438                               error);
439
440       if (socket4 != NULL)
441         /* IPv4 is supported on this platform, so if we fail now it is
442          * a result of being unable to bind to our port.  Don't fail
443          * silently as a result of this!
444          */
445         {
446           GInetAddress *inet_address;
447           GSocketAddress *address;
448           gboolean result;
449
450           inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
451           address = g_inet_socket_address_new (inet_address, port);
452           g_object_unref (inet_address);
453
454           g_socket_set_listen_backlog (socket4,
455                                        listener->priv->listen_backlog);
456
457           result = g_socket_bind (socket4, address, TRUE, error) &&
458                    g_socket_listen (socket4, error);
459
460           g_object_unref (address);
461
462           if (!result)
463             {
464               g_object_unref (socket4);
465
466               if (socket6 != NULL)
467                 g_object_unref (socket6);
468
469               return FALSE;
470             }
471
472           if (source_object)
473             g_object_set_qdata_full (G_OBJECT (socket4), source_quark,
474                                      g_object_ref (source_object),
475                                      g_object_unref);
476         }
477       else
478         /* Ok.  So IPv4 is not supported on this platform.  If we
479          * succeeded at creating an IPv6 socket then that's OK, but
480          * otherwise we need to tell the user we failed.
481          */
482         {
483           if (socket6 != NULL)
484             g_clear_error (error);
485           else
486             return FALSE;
487         }
488     }
489
490   g_assert (socket6 != NULL || socket4 != NULL);
491
492   if (socket6 != NULL)
493     g_ptr_array_add (listener->priv->sockets, socket6);
494
495   if (socket4 != NULL)
496     g_ptr_array_add (listener->priv->sockets, socket4);
497
498   if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
499     G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
500
501   return TRUE;
502 }
503
504 static GList *
505 add_sources (GSocketListener   *listener,
506              GSocketSourceFunc  callback,
507              gpointer           callback_data,
508              GCancellable      *cancellable,
509              GMainContext      *context)
510 {
511   GSocket *socket;
512   GSource *source;
513   GList *sources;
514   int i;
515
516   sources = NULL;
517   for (i = 0; i < listener->priv->sockets->len; i++)
518     {
519       socket = listener->priv->sockets->pdata[i];
520
521       source = g_socket_create_source (socket, G_IO_IN, cancellable);
522       g_source_set_callback (source,
523                              (GSourceFunc) callback,
524                              callback_data, NULL);
525       g_source_attach (source, context);
526
527       sources = g_list_prepend (sources, source);
528     }
529
530   return sources;
531 }
532
533 static void
534 free_sources (GList *sources)
535 {
536   GSource *source;
537   while (sources != NULL)
538     {
539       source = sources->data;
540       sources = g_list_delete_link (sources, sources);
541       g_source_destroy (source);
542       g_source_unref (source);
543     }
544 }
545
546 struct AcceptData {
547   GMainLoop *loop;
548   GSocket *socket;
549 };
550
551 static gboolean
552 accept_callback (GSocket      *socket,
553                  GIOCondition  condition,
554                  gpointer      user_data)
555 {
556   struct AcceptData *data = user_data;
557
558   data->socket = socket;
559   g_main_loop_quit (data->loop);
560
561   return TRUE;
562 }
563
564 /**
565  * g_socket_listener_accept_socket:
566  * @listener: a #GSocketListener
567  * @source_object: location where #GObject pointer will be stored, or %NULL
568  * @cancellable: optional #GCancellable object, %NULL to ignore.
569  * @error: #GError for error reporting, or %NULL to ignore.
570  *
571  * Blocks waiting for a client to connect to any of the sockets added
572  * to the listener. Returns the #GSocket that was accepted.
573  *
574  * If you want to accept the high-level #GSocketConnection, not a #GSocket,
575  * which is often the case, then you should use g_socket_listener_accept()
576  * instead.
577  *
578  * If @source_object is not %NULL it will be filled out with the source
579  * object specified when the corresponding socket or address was added
580  * to the listener.
581  *
582  * If @cancellable is not %NULL, then the operation can be cancelled by
583  * triggering the cancellable object from another thread. If the operation
584  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
585  *
586  * Returns: a #GSocket on success, %NULL on error.
587  *
588  * Since: 2.22
589  */
590 GSocket *
591 g_socket_listener_accept_socket (GSocketListener  *listener,
592                                  GObject         **source_object,
593                                  GCancellable     *cancellable,
594                                  GError          **error)
595 {
596   GSocket *accept_socket, *socket;
597
598   g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
599
600   if (!check_listener (listener, error))
601     return NULL;
602
603   if (listener->priv->sockets->len == 1)
604     {
605       accept_socket = listener->priv->sockets->pdata[0];
606       if (!g_socket_condition_wait (accept_socket, G_IO_IN,
607                                     cancellable, error))
608         return NULL;
609     }
610   else
611     {
612       GList *sources;
613       struct AcceptData data;
614       GMainLoop *loop;
615
616       if (listener->priv->main_context == NULL)
617         listener->priv->main_context = g_main_context_new ();
618
619       loop = g_main_loop_new (listener->priv->main_context, FALSE);
620       data.loop = loop;
621       sources = add_sources (listener,
622                              accept_callback,
623                              &data,
624                              cancellable,
625                              listener->priv->main_context);
626       g_main_loop_run (loop);
627       accept_socket = data.socket;
628       free_sources (sources);
629       g_main_loop_unref (loop);
630     }
631
632   if (!(socket = g_socket_accept (accept_socket, error)))
633     return NULL;
634
635   if (source_object)
636     *source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
637
638   return socket;
639 }
640
641 /**
642  * g_socket_listener_accept:
643  * @listener: a #GSocketListener
644  * @source_object: location where #GObject pointer will be stored, or %NULL
645  * @cancellable: optional #GCancellable object, %NULL to ignore.
646  * @error: #GError for error reporting, or %NULL to ignore.
647  *
648  * Blocks waiting for a client to connect to any of the sockets added
649  * to the listener. Returns a #GSocketConnection for the socket that was
650  * accepted.
651  *
652  * If @source_object is not %NULL it will be filled out with the source
653  * object specified when the corresponding socket or address was added
654  * to the listener.
655  *
656  * If @cancellable is not %NULL, then the operation can be cancelled by
657  * triggering the cancellable object from another thread. If the operation
658  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
659  *
660  * Returns: a #GSocketConnection on success, %NULL on error.
661  *
662  * Since: 2.22
663  */
664 GSocketConnection *
665 g_socket_listener_accept (GSocketListener  *listener,
666                           GObject         **source_object,
667                           GCancellable     *cancellable,
668                           GError          **error)
669 {
670   GSocketConnection *connection;
671   GSocket *socket;
672
673   socket = g_socket_listener_accept_socket (listener,
674                                             source_object,
675                                             cancellable,
676                                             error);
677   if (socket == NULL)
678     return NULL;
679
680   connection = g_socket_connection_factory_create_connection (socket);
681   g_object_unref (socket);
682
683   return connection;
684 }
685
686 struct AcceptAsyncData {
687   GSimpleAsyncResult *simple;
688   GCancellable *cancellable;
689   GList *sources;
690 };
691
692 static gboolean
693 accept_ready (GSocket      *accept_socket,
694               GIOCondition  condition,
695               gpointer      _data)
696 {
697   struct AcceptAsyncData *data = _data;
698   GError *error = NULL;
699
700   if (!g_cancellable_set_error_if_cancelled (data->cancellable,
701                                              &error))
702     {
703       GSocket *socket;
704       GObject *source_object;
705
706       socket = g_socket_accept (accept_socket, &error);
707       if (socket)
708         {
709           g_simple_async_result_set_op_res_gpointer (data->simple, socket,
710                                                      g_object_unref);
711           source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
712           if (source_object)
713             g_object_set_qdata_full (G_OBJECT (data->simple),
714                                      source_quark,
715                                      g_object_ref (source_object), g_object_unref);
716         }
717     }
718
719   if (error)
720     {
721       g_simple_async_result_set_from_error (data->simple, error);
722       g_error_free (error);
723     }
724
725   g_simple_async_result_complete_in_idle (data->simple);
726   g_object_unref (data->simple);
727   free_sources (data->sources);
728   g_free (data);
729
730   return FALSE;
731 }
732
733 /**
734  * g_socket_listener_accept_socket_async:
735  * @listener: a #GSocketListener
736  * @cancellable: a #GCancellable, or %NULL
737  * @callback: a #GAsyncReadyCallback
738  * @user_data: user data for the callback
739  *
740  * This is the asynchronous version of g_socket_listener_accept_socket().
741  *
742  * When the operation is finished @callback will be
743  * called. You can then call g_socket_listener_accept_socket_finish()
744  * to get the result of the operation.
745  *
746  * Since: 2.22
747  */
748 void
749 g_socket_listener_accept_socket_async (GSocketListener     *listener,
750                                        GCancellable        *cancellable,
751                                        GAsyncReadyCallback  callback,
752                                        gpointer             user_data)
753 {
754   struct AcceptAsyncData *data;
755   GError *error = NULL;
756
757   if (!check_listener (listener, &error))
758     {
759       g_simple_async_report_gerror_in_idle (G_OBJECT (listener),
760                                             callback, user_data,
761                                             error);
762       g_error_free (error);
763       return;
764     }
765
766   data = g_new0 (struct AcceptAsyncData, 1);
767   data->simple = g_simple_async_result_new (G_OBJECT (listener),
768                                             callback, user_data,
769                                             g_socket_listener_accept_socket_async);
770   data->cancellable = cancellable;
771   data->sources = add_sources (listener,
772                                accept_ready,
773                                data,
774                                cancellable,
775                                NULL);
776 }
777
778 /**
779  * g_socket_listener_accept_socket_finish:
780  * @listener: a #GSocketListener
781  * @result: a #GAsyncResult.
782  * @source_object: Optional #GObject identifying this source
783  * @error: a #GError location to store the error occuring, or %NULL to
784  * ignore.
785  *
786  * Finishes an async accept operation. See g_socket_listener_accept_socket_async()
787  *
788  * Returns: a #GSocket on success, %NULL on error.
789  *
790  * Since: 2.22
791  */
792 GSocket *
793 g_socket_listener_accept_socket_finish (GSocketListener  *listener,
794                                         GAsyncResult     *result,
795                                         GObject         **source_object,
796                                         GError          **error)
797 {
798   GSocket *socket;
799   GSimpleAsyncResult *simple;
800
801   g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), FALSE);
802
803   simple = G_SIMPLE_ASYNC_RESULT (result);
804
805   if (g_simple_async_result_propagate_error (simple, error))
806     return NULL;
807
808   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_listener_accept_socket_async);
809
810   socket = g_simple_async_result_get_op_res_gpointer (simple);
811
812   if (source_object)
813     *source_object = g_object_get_qdata (G_OBJECT (result), source_quark);
814
815   return g_object_ref (socket);
816 }
817
818 /**
819  * g_socket_listener_accept_async:
820  * @listener: a #GSocketListener
821  * @cancellable: a #GCancellable, or %NULL
822  * @callback: a #GAsyncReadyCallback
823  * @user_data: user data for the callback
824  *
825  * This is the asynchronous version of g_socket_listener_accept().
826  *
827  * When the operation is finished @callback will be
828  * called. You can then call g_socket_listener_accept_socket()
829  * to get the result of the operation.
830  *
831  * Since: 2.22
832  */
833 void
834 g_socket_listener_accept_async (GSocketListener     *listener,
835                                 GCancellable        *cancellable,
836                                 GAsyncReadyCallback  callback,
837                                 gpointer             user_data)
838 {
839   g_socket_listener_accept_socket_async (listener,
840                                          cancellable,
841                                          callback,
842                                          user_data);
843 }
844
845 /**
846  * g_socket_listener_accept_finish:
847  * @listener: a #GSocketListener
848  * @result: a #GAsyncResult.
849  * @source_object: Optional #GObject identifying this source
850  * @error: a #GError location to store the error occuring, or %NULL to
851  * ignore.
852  *
853  * Finishes an async accept operation. See g_socket_listener_accept_async()
854  *
855  * Returns: a #GSocketConnection on success, %NULL on error.
856  *
857  * Since: 2.22
858  */
859 GSocketConnection *
860 g_socket_listener_accept_finish (GSocketListener  *listener,
861                                  GAsyncResult     *result,
862                                  GObject         **source_object,
863                                  GError          **error)
864 {
865   GSocket *socket;
866   GSocketConnection *connection;
867
868   socket = g_socket_listener_accept_socket_finish (listener,
869                                                    result,
870                                                    source_object,
871                                                    error);
872   if (socket == NULL)
873     return NULL;
874
875   connection = g_socket_connection_factory_create_connection (socket);
876   g_object_unref (socket);
877   return connection;
878 }
879
880 /**
881  * g_socket_listener_set_backlog:
882  * @listener: a #GSocketListener
883  * @listen_backlog: an integer
884  *
885  * Sets the listen backlog on the sockets in the listener.
886  *
887  * See g_socket_set_listen_backlog() for details
888  *
889  * Since: 2.22
890  */
891 void
892 g_socket_listener_set_backlog (GSocketListener *listener,
893                                int              listen_backlog)
894 {
895   GSocket *socket;
896   int i;
897
898   if (listener->priv->closed)
899     return;
900
901   listener->priv->listen_backlog = listen_backlog;
902
903   for (i = 0; i < listener->priv->sockets->len; i++)
904     {
905       socket = listener->priv->sockets->pdata[i];
906       g_socket_set_listen_backlog (socket, listen_backlog);
907     }
908 }
909
910 /**
911  * g_socket_listener_close:
912  * @listener: a #GSocketListener
913  *
914  * Closes all the sockets in the listener.
915  *
916  * Since: 2.22
917  */
918 void
919 g_socket_listener_close (GSocketListener *listener)
920 {
921   GSocket *socket;
922   int i;
923
924   g_return_if_fail (G_IS_SOCKET_LISTENER (listener));
925
926   if (listener->priv->closed)
927     return;
928
929   for (i = 0; i < listener->priv->sockets->len; i++)
930     {
931       socket = listener->priv->sockets->pdata[i];
932       g_socket_close (socket, NULL);
933     }
934   listener->priv->closed = TRUE;
935 }
936
937 #define __G_SOCKET_LISTENER_C__
938 #include "gioaliasdef.c"