Support g_main_context_push_thread_default() in gio
[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
203  * @source_object: Optional #GObject identifying this source
204  * @error: #GError for error reporting, or %NULL to ignore.
205  *
206  * Adds @socket to the set of sockets that we try to accept
207  * new clients from. The socket must be bound to a local
208  * address and listened to.
209  *
210  * @source_object will be passed out in the various calls
211  * to accept to identify this particular source, which is
212  * useful if you're listening on multiple addresses and do
213  * different things depending on what address is connected to.
214  *
215  * Returns: %TRUE on success, %FALSE on error.
216  *
217  * Since: 2.22
218  */
219 gboolean
220 g_socket_listener_add_socket (GSocketListener  *listener,
221                               GSocket          *socket,
222                               GObject          *source_object,
223                               GError          **error)
224 {
225   if (!check_listener (listener, error))
226     return FALSE;
227
228   /* TODO: Check that socket it is bound & not closed? */
229
230   if (g_socket_is_closed (socket))
231     {
232       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
233                            _("Added socket is closed"));
234       return FALSE;
235     }
236
237   g_object_ref (socket);
238   g_ptr_array_add (listener->priv->sockets, socket);
239
240   if (source_object)
241     g_object_set_qdata_full (G_OBJECT (socket), source_quark,
242                              g_object_ref (source_object), g_object_unref);
243
244
245   if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
246     G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
247
248   return TRUE;
249 }
250
251 /**
252  * g_socket_listener_add_address:
253  * @listener: a #GSocketListener
254  * @address: a #GSocketAddress
255  * @type: a #GSocketType
256  * @protocol: a #GSocketProtocol
257  * @source_object: Optional #GObject identifying this source
258  * @effective_address: location to store the address that was bound to, or %NULL.
259  * @error: #GError for error reporting, or %NULL to ignore.
260  *
261  * Creates a socket of type @type and protocol @protocol, binds
262  * it to @address and adds it to the set of sockets we're accepting
263  * sockets from.
264  *
265  * Note that adding an IPv6 address, depending on the platform,
266  * may or may not result in a listener that also accepts IPv4
267  * connections.  For more determinstic behaviour, see
268  * g_socket_listener_add_inet_port().
269  *
270  * @source_object will be passed out in the various calls
271  * to accept to identify this particular source, which is
272  * useful if you're listening on multiple addresses and do
273  * different things depending on what address is connected to.
274  *
275  * If successful and @effective_address is non-%NULL then it will
276  * be set to the address that the binding actually occured at.  This
277  * is helpful for determining the port number that was used for when
278  * requesting a binding to port 0 (ie: "any port").  This address, if
279  * requested, belongs to the caller and must be freed.
280  *
281  * Returns: %TRUE on success, %FALSE on error.
282  *
283  * Since: 2.22
284  */
285 gboolean
286 g_socket_listener_add_address (GSocketListener  *listener,
287                                GSocketAddress   *address,
288                                GSocketType       type,
289                                GSocketProtocol   protocol,
290                                GObject          *source_object,
291                                GSocketAddress  **effective_address,
292                                GError          **error)
293 {
294   GSocketAddress *local_address;
295   GSocketFamily family;
296   GSocket *socket;
297
298   if (!check_listener (listener, error))
299     return FALSE;
300
301   family = g_socket_address_get_family (address);
302   socket = g_socket_new (family, type, protocol, error);
303   if (socket == NULL)
304     return FALSE;
305
306   g_socket_set_listen_backlog (socket, listener->priv->listen_backlog);
307
308   if (!g_socket_bind (socket, address, TRUE, error) ||
309       !g_socket_listen (socket, error))
310     {
311       g_object_unref (socket);
312       return FALSE;
313     }
314
315   local_address = NULL;
316   if (effective_address)
317     {
318       local_address = g_socket_get_local_address (socket, error);
319       if (local_address == NULL)
320         {
321           g_object_unref (socket);
322           return FALSE;
323         }
324     }
325
326   if (!g_socket_listener_add_socket (listener, socket,
327                                      source_object,
328                                      error))
329     {
330       if (local_address)
331         g_object_unref (local_address);
332       g_object_unref (socket);
333       return FALSE;
334     }
335
336   if (effective_address)
337     *effective_address = local_address;
338
339   g_object_unref (socket); /* add_socket refs this */
340
341   return TRUE;
342 }
343
344 /**
345  * g_socket_listener_add_inet_port:
346  * @listener: a #GSocketListener
347  * @port: an IP port number (non-zero)
348  * @source_object: Optional #GObject identifying this source
349  * @error: #GError for error reporting, or %NULL to ignore.
350  *
351  * Helper function for g_socket_listener_add_address() that
352  * creates a TCP/IP socket listening on IPv4 and IPv6 (if
353  * supported) on the specified port on all interfaces.
354  *
355  * @source_object will be passed out in the various calls
356  * to accept to identify this particular source, which is
357  * useful if you're listening on multiple addresses and do
358  * different things depending on what address is connected to.
359  *
360  * Returns: %TRUE on success, %FALSE on error.
361  *
362  * Since: 2.22
363  */
364 gboolean
365 g_socket_listener_add_inet_port (GSocketListener  *listener,
366                                  guint16           port,
367                                  GObject          *source_object,
368                                  GError          **error)
369 {
370   gboolean need_ipv4_socket = TRUE;
371   GSocket *socket4 = NULL;
372   GSocket *socket6;
373
374   g_return_val_if_fail (listener != NULL, FALSE);
375   g_return_val_if_fail (port != 0, FALSE);
376
377   if (!check_listener (listener, error))
378     return FALSE;
379
380   /* first try to create an IPv6 socket */
381   socket6 = g_socket_new (G_SOCKET_FAMILY_IPV6,
382                           G_SOCKET_TYPE_STREAM,
383                           G_SOCKET_PROTOCOL_DEFAULT,
384                           NULL);
385
386   if (socket6 != NULL)
387     /* IPv6 is supported on this platform, so if we fail now it is
388      * a result of being unable to bind to our port.  Don't fail
389      * silently as a result of this!
390      */
391     {
392       GInetAddress *inet_address;
393       GSocketAddress *address;
394       gboolean result;
395
396       inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
397       address = g_inet_socket_address_new (inet_address, port);
398       g_object_unref (inet_address);
399
400       g_socket_set_listen_backlog (socket6, listener->priv->listen_backlog);
401
402       result = g_socket_bind (socket6, address, TRUE, error) &&
403                g_socket_listen (socket6, error);
404
405       g_object_unref (address);
406
407       if (!result)
408         {
409           g_object_unref (socket6);
410
411           return FALSE;
412         }
413
414       if (source_object)
415         g_object_set_qdata_full (G_OBJECT (socket6), source_quark,
416                                  g_object_ref (source_object),
417                                  g_object_unref);
418
419       /* If this socket already speaks IPv4 then we are done. */
420       if (g_socket_speaks_ipv4 (socket6))
421         need_ipv4_socket = FALSE;
422     }
423
424   if (need_ipv4_socket)
425     /* We are here for exactly one of the following reasons:
426      *
427      *   - our platform doesn't support IPv6
428      *   - we successfully created an IPv6 socket but it's V6ONLY
429      *
430      * In either case, we need to go ahead and create an IPv4 socket
431      * and fail the call if we can't bind to it.
432      */
433     {
434       socket4 = g_socket_new (G_SOCKET_FAMILY_IPV4,
435                               G_SOCKET_TYPE_STREAM,
436                               G_SOCKET_PROTOCOL_DEFAULT,
437                               error);
438
439       if (socket4 != NULL)
440         /* IPv4 is supported on this platform, so if we fail now it is
441          * a result of being unable to bind to our port.  Don't fail
442          * silently as a result of this!
443          */
444         {
445           GInetAddress *inet_address;
446           GSocketAddress *address;
447           gboolean result;
448
449           inet_address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
450           address = g_inet_socket_address_new (inet_address, port);
451           g_object_unref (inet_address);
452
453           g_socket_set_listen_backlog (socket4,
454                                        listener->priv->listen_backlog);
455
456           result = g_socket_bind (socket4, address, TRUE, error) &&
457                    g_socket_listen (socket4, error);
458
459           g_object_unref (address);
460
461           if (!result)
462             {
463               g_object_unref (socket4);
464
465               if (socket6 != NULL)
466                 g_object_unref (socket6);
467
468               return FALSE;
469             }
470
471           if (source_object)
472             g_object_set_qdata_full (G_OBJECT (socket4), source_quark,
473                                      g_object_ref (source_object),
474                                      g_object_unref);
475         }
476       else
477         /* Ok.  So IPv4 is not supported on this platform.  If we
478          * succeeded at creating an IPv6 socket then that's OK, but
479          * otherwise we need to tell the user we failed.
480          */
481         {
482           if (socket6 != NULL)
483             g_clear_error (error);
484           else
485             return FALSE;
486         }
487     }
488
489   g_assert (socket6 != NULL || socket4 != NULL);
490
491   if (socket6 != NULL)
492     g_ptr_array_add (listener->priv->sockets, socket6);
493
494   if (socket4 != NULL)
495     g_ptr_array_add (listener->priv->sockets, socket4);
496
497   if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
498     G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);
499
500   return TRUE;
501 }
502
503 static GList *
504 add_sources (GSocketListener   *listener,
505              GSocketSourceFunc  callback,
506              gpointer           callback_data,
507              GCancellable      *cancellable,
508              GMainContext      *context)
509 {
510   GSocket *socket;
511   GSource *source;
512   GList *sources;
513   int i;
514
515   sources = NULL;
516   for (i = 0; i < listener->priv->sockets->len; i++)
517     {
518       socket = listener->priv->sockets->pdata[i];
519
520       source = g_socket_create_source (socket, G_IO_IN, cancellable);
521       g_source_set_callback (source,
522                              (GSourceFunc) callback,
523                              callback_data, NULL);
524       g_source_attach (source, context);
525
526       sources = g_list_prepend (sources, source);
527     }
528
529   return sources;
530 }
531
532 static void
533 free_sources (GList *sources)
534 {
535   GSource *source;
536   while (sources != NULL)
537     {
538       source = sources->data;
539       sources = g_list_delete_link (sources, sources);
540       g_source_destroy (source);
541       g_source_unref (source);
542     }
543 }
544
545 struct AcceptData {
546   GMainLoop *loop;
547   GSocket *socket;
548 };
549
550 static gboolean
551 accept_callback (GSocket      *socket,
552                  GIOCondition  condition,
553                  gpointer      user_data)
554 {
555   struct AcceptData *data = user_data;
556
557   data->socket = socket;
558   g_main_loop_quit (data->loop);
559
560   return TRUE;
561 }
562
563 /**
564  * g_socket_listener_accept_socket:
565  * @listener: a #GSocketListener
566  * @source_object: location where #GObject pointer will be stored, or %NULL
567  * @cancellable: optional #GCancellable object, %NULL to ignore.
568  * @error: #GError for error reporting, or %NULL to ignore.
569  *
570  * Blocks waiting for a client to connect to any of the sockets added
571  * to the listener. Returns the #GSocket that was accepted.
572  *
573  * If you want to accept the high-level #GSocketConnection, not a #GSocket,
574  * which is often the case, then you should use g_socket_listener_accept()
575  * instead.
576  *
577  * If @source_object is not %NULL it will be filled out with the source
578  * object specified when the corresponding socket or address was added
579  * to the listener.
580  *
581  * If @cancellable is not %NULL, then the operation can be cancelled by
582  * triggering the cancellable object from another thread. If the operation
583  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
584  *
585  * Returns: a #GSocket on success, %NULL on error.
586  *
587  * Since: 2.22
588  */
589 GSocket *
590 g_socket_listener_accept_socket (GSocketListener  *listener,
591                                  GObject         **source_object,
592                                  GCancellable     *cancellable,
593                                  GError          **error)
594 {
595   GSocket *accept_socket, *socket;
596
597   g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), NULL);
598
599   if (!check_listener (listener, error))
600     return NULL;
601
602   if (listener->priv->sockets->len == 1)
603     {
604       accept_socket = listener->priv->sockets->pdata[0];
605       if (!g_socket_condition_wait (accept_socket, G_IO_IN,
606                                     cancellable, error))
607         return NULL;
608     }
609   else
610     {
611       GList *sources;
612       struct AcceptData data;
613       GMainLoop *loop;
614
615       if (listener->priv->main_context == NULL)
616         listener->priv->main_context = g_main_context_new ();
617
618       loop = g_main_loop_new (listener->priv->main_context, FALSE);
619       data.loop = loop;
620       sources = add_sources (listener,
621                              accept_callback,
622                              &data,
623                              cancellable,
624                              listener->priv->main_context);
625       g_main_loop_run (loop);
626       accept_socket = data.socket;
627       free_sources (sources);
628       g_main_loop_unref (loop);
629     }
630
631   if (!(socket = g_socket_accept (accept_socket, cancellable, error)))
632     return NULL;
633
634   if (source_object)
635     *source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
636
637   return socket;
638 }
639
640 /**
641  * g_socket_listener_accept:
642  * @listener: a #GSocketListener
643  * @source_object: location where #GObject pointer will be stored, or %NULL
644  * @cancellable: optional #GCancellable object, %NULL to ignore.
645  * @error: #GError for error reporting, or %NULL to ignore.
646  *
647  * Blocks waiting for a client to connect to any of the sockets added
648  * to the listener. Returns a #GSocketConnection for the socket that was
649  * accepted.
650  *
651  * If @source_object is not %NULL it will be filled out with the source
652  * object specified when the corresponding socket or address was added
653  * to the listener.
654  *
655  * If @cancellable is not %NULL, then the operation can be cancelled by
656  * triggering the cancellable object from another thread. If the operation
657  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
658  *
659  * Returns: a #GSocketConnection on success, %NULL on error.
660  *
661  * Since: 2.22
662  */
663 GSocketConnection *
664 g_socket_listener_accept (GSocketListener  *listener,
665                           GObject         **source_object,
666                           GCancellable     *cancellable,
667                           GError          **error)
668 {
669   GSocketConnection *connection;
670   GSocket *socket;
671
672   socket = g_socket_listener_accept_socket (listener,
673                                             source_object,
674                                             cancellable,
675                                             error);
676   if (socket == NULL)
677     return NULL;
678
679   connection = g_socket_connection_factory_create_connection (socket);
680   g_object_unref (socket);
681
682   return connection;
683 }
684
685 struct AcceptAsyncData {
686   GSimpleAsyncResult *simple;
687   GCancellable *cancellable;
688   GList *sources;
689 };
690
691 static gboolean
692 accept_ready (GSocket      *accept_socket,
693               GIOCondition  condition,
694               gpointer      _data)
695 {
696   struct AcceptAsyncData *data = _data;
697   GError *error = NULL;
698   GSocket *socket;
699   GObject *source_object;
700
701   socket = g_socket_accept (accept_socket, data->cancellable, &error);
702   if (socket)
703     {
704       g_simple_async_result_set_op_res_gpointer (data->simple, socket,
705                                                  g_object_unref);
706       source_object = g_object_get_qdata (G_OBJECT (accept_socket), source_quark);
707       if (source_object)
708         g_object_set_qdata_full (G_OBJECT (data->simple),
709                                  source_quark,
710                                  g_object_ref (source_object), g_object_unref);
711     }
712   else
713     {
714       g_simple_async_result_set_from_error (data->simple, error);
715       g_error_free (error);
716     }
717
718   g_simple_async_result_complete_in_idle (data->simple);
719   g_object_unref (data->simple);
720   free_sources (data->sources);
721   g_free (data);
722
723   return FALSE;
724 }
725
726 /**
727  * g_socket_listener_accept_socket_async:
728  * @listener: a #GSocketListener
729  * @cancellable: a #GCancellable, or %NULL
730  * @callback: a #GAsyncReadyCallback
731  * @user_data: user data for the callback
732  *
733  * This is the asynchronous version of g_socket_listener_accept_socket().
734  *
735  * When the operation is finished @callback will be
736  * called. You can then call g_socket_listener_accept_socket_finish()
737  * to get the result of the operation.
738  *
739  * Since: 2.22
740  */
741 void
742 g_socket_listener_accept_socket_async (GSocketListener     *listener,
743                                        GCancellable        *cancellable,
744                                        GAsyncReadyCallback  callback,
745                                        gpointer             user_data)
746 {
747   struct AcceptAsyncData *data;
748   GError *error = NULL;
749
750   if (!check_listener (listener, &error))
751     {
752       g_simple_async_report_gerror_in_idle (G_OBJECT (listener),
753                                             callback, user_data,
754                                             error);
755       g_error_free (error);
756       return;
757     }
758
759   data = g_new0 (struct AcceptAsyncData, 1);
760   data->simple = g_simple_async_result_new (G_OBJECT (listener),
761                                             callback, user_data,
762                                             g_socket_listener_accept_socket_async);
763   data->cancellable = cancellable;
764   data->sources = add_sources (listener,
765                                accept_ready,
766                                data,
767                                cancellable,
768                                g_main_context_get_thread_default ());
769 }
770
771 /**
772  * g_socket_listener_accept_socket_finish:
773  * @listener: a #GSocketListener
774  * @result: a #GAsyncResult.
775  * @source_object: Optional #GObject identifying this source
776  * @error: a #GError location to store the error occuring, or %NULL to
777  * ignore.
778  *
779  * Finishes an async accept operation. See g_socket_listener_accept_socket_async()
780  *
781  * Returns: a #GSocket on success, %NULL on error.
782  *
783  * Since: 2.22
784  */
785 GSocket *
786 g_socket_listener_accept_socket_finish (GSocketListener  *listener,
787                                         GAsyncResult     *result,
788                                         GObject         **source_object,
789                                         GError          **error)
790 {
791   GSocket *socket;
792   GSimpleAsyncResult *simple;
793
794   g_return_val_if_fail (G_IS_SOCKET_LISTENER (listener), FALSE);
795
796   simple = G_SIMPLE_ASYNC_RESULT (result);
797
798   if (g_simple_async_result_propagate_error (simple, error))
799     return NULL;
800
801   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_listener_accept_socket_async);
802
803   socket = g_simple_async_result_get_op_res_gpointer (simple);
804
805   if (source_object)
806     *source_object = g_object_get_qdata (G_OBJECT (result), source_quark);
807
808   return g_object_ref (socket);
809 }
810
811 /**
812  * g_socket_listener_accept_async:
813  * @listener: a #GSocketListener
814  * @cancellable: a #GCancellable, or %NULL
815  * @callback: a #GAsyncReadyCallback
816  * @user_data: user data for the callback
817  *
818  * This is the asynchronous version of g_socket_listener_accept().
819  *
820  * When the operation is finished @callback will be
821  * called. You can then call g_socket_listener_accept_socket()
822  * to get the result of the operation.
823  *
824  * Since: 2.22
825  */
826 void
827 g_socket_listener_accept_async (GSocketListener     *listener,
828                                 GCancellable        *cancellable,
829                                 GAsyncReadyCallback  callback,
830                                 gpointer             user_data)
831 {
832   g_socket_listener_accept_socket_async (listener,
833                                          cancellable,
834                                          callback,
835                                          user_data);
836 }
837
838 /**
839  * g_socket_listener_accept_finish:
840  * @listener: a #GSocketListener
841  * @result: a #GAsyncResult.
842  * @source_object: Optional #GObject identifying this source
843  * @error: a #GError location to store the error occuring, or %NULL to
844  * ignore.
845  *
846  * Finishes an async accept operation. See g_socket_listener_accept_async()
847  *
848  * Returns: a #GSocketConnection on success, %NULL on error.
849  *
850  * Since: 2.22
851  */
852 GSocketConnection *
853 g_socket_listener_accept_finish (GSocketListener  *listener,
854                                  GAsyncResult     *result,
855                                  GObject         **source_object,
856                                  GError          **error)
857 {
858   GSocket *socket;
859   GSocketConnection *connection;
860
861   socket = g_socket_listener_accept_socket_finish (listener,
862                                                    result,
863                                                    source_object,
864                                                    error);
865   if (socket == NULL)
866     return NULL;
867
868   connection = g_socket_connection_factory_create_connection (socket);
869   g_object_unref (socket);
870   return connection;
871 }
872
873 /**
874  * g_socket_listener_set_backlog:
875  * @listener: a #GSocketListener
876  * @listen_backlog: an integer
877  *
878  * Sets the listen backlog on the sockets in the listener.
879  *
880  * See g_socket_set_listen_backlog() for details
881  *
882  * Since: 2.22
883  */
884 void
885 g_socket_listener_set_backlog (GSocketListener *listener,
886                                int              listen_backlog)
887 {
888   GSocket *socket;
889   int i;
890
891   if (listener->priv->closed)
892     return;
893
894   listener->priv->listen_backlog = listen_backlog;
895
896   for (i = 0; i < listener->priv->sockets->len; i++)
897     {
898       socket = listener->priv->sockets->pdata[i];
899       g_socket_set_listen_backlog (socket, listen_backlog);
900     }
901 }
902
903 /**
904  * g_socket_listener_close:
905  * @listener: a #GSocketListener
906  *
907  * Closes all the sockets in the listener.
908  *
909  * Since: 2.22
910  */
911 void
912 g_socket_listener_close (GSocketListener *listener)
913 {
914   GSocket *socket;
915   int i;
916
917   g_return_if_fail (G_IS_SOCKET_LISTENER (listener));
918
919   if (listener->priv->closed)
920     return;
921
922   for (i = 0; i < listener->priv->sockets->len; i++)
923     {
924       socket = listener->priv->sockets->pdata[i];
925       g_socket_close (socket, NULL);
926     }
927   listener->priv->closed = TRUE;
928 }
929
930 #define __G_SOCKET_LISTENER_C__
931 #include "gioaliasdef.c"