Stop using glib-genmarshal at build time
[platform/upstream/glib.git] / gio / gsocketservice.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2009 Codethink Limited
4  * Copyright © 2009 Red Hat, Inc
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2 of the licence or (at
9  * your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Ryan Lortie <desrt@desrt.ca>
22  *          Alexander Larsson <alexl@redhat.com>
23  */
24
25 /**
26  * SECTION:gsocketservice
27  * @title: GSocketService
28  * @short_description: Make it easy to implement a network service
29  * @see_also: #GThreadedSocketService, #GSocketListener.
30  *
31  * A #GSocketService is an object that represents a service that
32  * is provided to the network or over local sockets.  When a new
33  * connection is made to the service the #GSocketService::incoming
34  * signal is emitted.
35  *
36  * A #GSocketService is a subclass of #GSocketListener and you need
37  * to add the addresses you want to accept connections on with the
38  * #GSocketListener APIs.
39  *
40  * There are two options for implementing a network service based on
41  * #GSocketService. The first is to create the service using
42  * g_socket_service_new() and to connect to the #GSocketService::incoming
43  * signal. The second is to subclass #GSocketService and override the
44  * default signal handler implementation.
45  *
46  * In either case, the handler must immediately return, or else it
47  * will block additional incoming connections from being serviced.
48  * If you are interested in writing connection handlers that contain
49  * blocking code then see #GThreadedSocketService.
50  *
51  * The socket service runs on the main loop in the main thread, and is
52  * not threadsafe in general. However, the calls to start and stop
53  * the service are threadsafe so these can be used from threads that
54  * handle incoming clients.
55  *
56  * Since: 2.22
57  */
58
59 #include "config.h"
60 #include "gsocketservice.h"
61
62 #include <gio/gio.h>
63 #include "gsocketlistener.h"
64 #include "gsocketconnection.h"
65
66
67 static guint g_socket_service_incoming_signal;
68
69 G_DEFINE_TYPE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER);
70
71 G_LOCK_DEFINE_STATIC(active);
72
73 struct _GSocketServicePrivate
74 {
75   GCancellable *cancellable;
76   guint active : 1;
77   guint outstanding_accept : 1;
78 };
79
80 static void g_socket_service_ready (GObject      *object,
81                                     GAsyncResult *result,
82                                     gpointer      user_data);
83
84 static gboolean
85 g_socket_service_real_incoming (GSocketService    *service,
86                                 GSocketConnection *connection,
87                                 GObject           *source_object)
88 {
89   return FALSE;
90 }
91
92 static void
93 g_socket_service_init (GSocketService *service)
94 {
95   service->priv = G_TYPE_INSTANCE_GET_PRIVATE (service,
96                                                G_TYPE_SOCKET_SERVICE,
97                                                GSocketServicePrivate);
98   service->priv->cancellable = g_cancellable_new ();
99   service->priv->active = TRUE;
100 }
101
102 static void
103 g_socket_service_finalize (GObject *object)
104 {
105   GSocketService *service = G_SOCKET_SERVICE (object);
106
107   g_object_unref (service->priv->cancellable);
108
109   G_OBJECT_CLASS (g_socket_service_parent_class)
110     ->finalize (object);
111 }
112
113 static void
114 do_accept (GSocketService  *service)
115 {
116   g_socket_listener_accept_async (G_SOCKET_LISTENER (service),
117                                   service->priv->cancellable,
118                                   g_socket_service_ready, NULL);
119   service->priv->outstanding_accept = TRUE;
120 }
121
122 static void
123 g_socket_service_changed (GSocketListener *listener)
124 {
125   GSocketService  *service = G_SOCKET_SERVICE (listener);
126
127   G_LOCK (active);
128
129   if (service->priv->active)
130     {
131       if (service->priv->outstanding_accept)
132         g_cancellable_cancel (service->priv->cancellable);
133       else
134         {
135           g_socket_listener_accept_async (listener, service->priv->cancellable,
136                                           g_socket_service_ready, NULL);
137           service->priv->outstanding_accept = TRUE;
138         }
139     }
140
141   G_UNLOCK (active);
142 }
143
144 /**
145  * g_socket_service_is_active:
146  * @service: a #GSocketService
147  *
148  * Check whether the service is active or not. An active
149  * service will accept new clients that connect, while
150  * a non-active service will let connecting clients queue
151  * up until the service is started.
152  *
153  * Returns: %TRUE if the service is active, %FALSE otherwise
154  *
155  * Since: 2.22
156  */
157 gboolean
158 g_socket_service_is_active (GSocketService *service)
159 {
160   gboolean active;
161
162   G_LOCK (active);
163   active = service->priv->active;
164   G_UNLOCK (active);
165   return active;
166 }
167
168 /**
169  * g_socket_service_start:
170  * @service: a #GSocketService
171  *
172  * Starts the service, i.e. start accepting connections
173  * from the added sockets when the mainloop runs.
174  *
175  * This call is threadsafe, so it may be called from a thread
176  * handling an incoming client request.
177  *
178  * Since: 2.22
179  */
180 void
181 g_socket_service_start (GSocketService *service)
182 {
183   G_LOCK (active);
184
185   if (!service->priv->active)
186     {
187       service->priv->active = TRUE;
188
189       if (service->priv->outstanding_accept)
190         g_cancellable_cancel (service->priv->cancellable);
191       else
192         do_accept (service);
193     }
194
195   G_UNLOCK (active);
196 }
197
198 /**
199  * g_socket_service_stop:
200  * @service: a #GSocketService
201  *
202  * Stops the service, i.e. stops accepting connections
203  * from the added sockets when the mainloop runs.
204  *
205  * This call is threadsafe, so it may be called from a thread
206  * handling an incoming client request.
207  *
208  * Since: 2.22
209  */
210 void
211 g_socket_service_stop (GSocketService *service)
212 {
213   G_LOCK (active);
214
215   if (service->priv->active)
216     {
217       service->priv->active = FALSE;
218
219       if (service->priv->outstanding_accept)
220         g_cancellable_cancel (service->priv->cancellable);
221     }
222
223   G_UNLOCK (active);
224 }
225
226
227 static gboolean
228 g_socket_service_incoming (GSocketService    *service,
229                            GSocketConnection *connection,
230                            GObject           *source_object)
231 {
232   gboolean result;
233
234   g_signal_emit (service, g_socket_service_incoming_signal,
235                  0, connection, source_object, &result);
236   return result;
237 }
238
239 static void
240 g_socket_service_class_init (GSocketServiceClass *class)
241 {
242   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
243   GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
244
245   g_type_class_add_private (class, sizeof (GSocketServicePrivate));
246
247   gobject_class->finalize = g_socket_service_finalize;
248   listener_class->changed = g_socket_service_changed;
249   class->incoming = g_socket_service_real_incoming;
250
251   /**
252    * GSocketService::incoming:
253    * @service: the #GSocketService
254    * @connection: a new #GSocketConnection object
255    * @source_object: the source_object passed to g_socket_listener_add_address()
256    *
257    * The ::incoming signal is emitted when a new incoming connection
258    * to @service needs to be handled. The handler must initiate the
259    * handling of @connection, but may not block; in essence,
260    * asynchronous operations must be used.
261    *
262    * @connection will be unreffed once the signal handler returns,
263    * so you need to ref it yourself if you are planning to use it.
264    *
265    * Returns: %TRUE to stop other handlers from being called
266    *
267    * Since: 2.22
268    */
269   g_socket_service_incoming_signal =
270     g_signal_new ("incoming", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
271                   G_STRUCT_OFFSET (GSocketServiceClass, incoming),
272                   g_signal_accumulator_true_handled, NULL,
273                   g_cclosure_marshal_generic, G_TYPE_BOOLEAN,
274                   2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
275 }
276
277 static void
278 g_socket_service_ready (GObject      *object,
279                         GAsyncResult *result,
280                         gpointer      user_data)
281 {
282   GSocketListener *listener = G_SOCKET_LISTENER (object);
283   GSocketService *service = G_SOCKET_SERVICE (object);
284   GSocketConnection *connection;
285   GObject *source_object;
286   GError *error = NULL;
287
288   connection = g_socket_listener_accept_finish (listener, result, &source_object, &error);
289   if (error)
290     {
291       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
292         g_warning ("fail: %s", error->message);
293       g_error_free (error);
294     }
295   else
296     {
297       g_socket_service_incoming (service, connection, source_object);
298       g_object_unref (connection);
299     }
300
301   G_LOCK (active);
302
303   g_cancellable_reset (service->priv->cancellable);
304
305   /* requeue */
306   service->priv->outstanding_accept = FALSE;
307   if (service->priv->active)
308     do_accept (service);
309
310   G_UNLOCK (active);
311 }
312
313
314 /**
315  * g_socket_service_new:
316  *
317  * Creates a new #GSocketService with no sockets to listen for.
318  * New listeners can be added with e.g. g_socket_listener_add_address()
319  * or g_socket_listener_add_inet_port().
320  *
321  * Returns: a new #GSocketService.
322  *
323  * Since: 2.22
324  */
325 GSocketService *
326 g_socket_service_new (void)
327 {
328   return g_object_new (G_TYPE_SOCKET_SERVICE, NULL);
329 }