GDBusMessage: fast-path decoding of fixed arrays
[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, see <http://www.gnu.org/licenses/>.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  *          Alexander Larsson <alexl@redhat.com>
21  */
22
23 /**
24  * SECTION:gsocketservice
25  * @title: GSocketService
26  * @short_description: Make it easy to implement a network service
27  * @include: gio/gio.h
28  * @see_also: #GThreadedSocketService, #GSocketListener.
29  *
30  * A #GSocketService is an object that represents a service that
31  * is provided to the network or over local sockets.  When a new
32  * connection is made to the service the #GSocketService::incoming
33  * signal is emitted.
34  *
35  * A #GSocketService is a subclass of #GSocketListener and you need
36  * to add the addresses you want to accept connections on with the
37  * #GSocketListener APIs.
38  *
39  * There are two options for implementing a network service based on
40  * #GSocketService. The first is to create the service using
41  * g_socket_service_new() and to connect to the #GSocketService::incoming
42  * signal. The second is to subclass #GSocketService and override the
43  * default signal handler implementation.
44  *
45  * In either case, the handler must immediately return, or else it
46  * will block additional incoming connections from being serviced.
47  * If you are interested in writing connection handlers that contain
48  * blocking code then see #GThreadedSocketService.
49  *
50  * The socket service runs on the main loop of the 
51  * [thread-default context][g-main-context-push-thread-default-context]
52  * of the thread it is created in, and is not
53  * threadsafe in general. However, the calls to start and stop the
54  * service are thread-safe so these can be used from threads that
55  * handle incoming clients.
56  *
57  * Since: 2.22
58  */
59
60 #include "config.h"
61 #include "gsocketservice.h"
62
63 #include <gio/gio.h>
64 #include "gsocketlistener.h"
65 #include "gsocketconnection.h"
66
67 struct _GSocketServicePrivate
68 {
69   GCancellable *cancellable;
70   guint active : 1;
71   guint outstanding_accept : 1;
72 };
73
74 static guint g_socket_service_incoming_signal;
75
76 G_LOCK_DEFINE_STATIC(active);
77
78 G_DEFINE_TYPE_WITH_PRIVATE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER)
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_socket_service_get_instance_private (service);
96   service->priv->cancellable = g_cancellable_new ();
97   service->priv->active = TRUE;
98 }
99
100 static void
101 g_socket_service_finalize (GObject *object)
102 {
103   GSocketService *service = G_SOCKET_SERVICE (object);
104
105   g_object_unref (service->priv->cancellable);
106
107   G_OBJECT_CLASS (g_socket_service_parent_class)
108     ->finalize (object);
109 }
110
111 static void
112 do_accept (GSocketService  *service)
113 {
114   g_socket_listener_accept_async (G_SOCKET_LISTENER (service),
115                                   service->priv->cancellable,
116                                   g_socket_service_ready, NULL);
117   service->priv->outstanding_accept = TRUE;
118 }
119
120 static void
121 g_socket_service_changed (GSocketListener *listener)
122 {
123   GSocketService  *service = G_SOCKET_SERVICE (listener);
124
125   G_LOCK (active);
126
127   if (service->priv->active)
128     {
129       if (service->priv->outstanding_accept)
130         g_cancellable_cancel (service->priv->cancellable);
131       else
132         {
133           g_socket_listener_accept_async (listener, service->priv->cancellable,
134                                           g_socket_service_ready, NULL);
135           service->priv->outstanding_accept = TRUE;
136         }
137     }
138
139   G_UNLOCK (active);
140 }
141
142 /**
143  * g_socket_service_is_active:
144  * @service: a #GSocketService
145  *
146  * Check whether the service is active or not. An active
147  * service will accept new clients that connect, while
148  * a non-active service will let connecting clients queue
149  * up until the service is started.
150  *
151  * Returns: %TRUE if the service is active, %FALSE otherwise
152  *
153  * Since: 2.22
154  */
155 gboolean
156 g_socket_service_is_active (GSocketService *service)
157 {
158   gboolean active;
159
160   G_LOCK (active);
161   active = service->priv->active;
162   G_UNLOCK (active);
163   return active;
164 }
165
166 /**
167  * g_socket_service_start:
168  * @service: a #GSocketService
169  *
170  * Starts the service, i.e. start accepting connections
171  * from the added sockets when the mainloop runs.
172  *
173  * This call is thread-safe, so it may be called from a thread
174  * handling an incoming client request.
175  *
176  * Since: 2.22
177  */
178 void
179 g_socket_service_start (GSocketService *service)
180 {
181   G_LOCK (active);
182
183   if (!service->priv->active)
184     {
185       service->priv->active = TRUE;
186
187       if (service->priv->outstanding_accept)
188         g_cancellable_cancel (service->priv->cancellable);
189       else
190         do_accept (service);
191     }
192
193   G_UNLOCK (active);
194 }
195
196 /**
197  * g_socket_service_stop:
198  * @service: a #GSocketService
199  *
200  * Stops the service, i.e. stops accepting connections
201  * from the added sockets when the mainloop runs.
202  *
203  * This call is thread-safe, so it may be called from a thread
204  * handling an incoming client request.
205  *
206  * Note that this only stops accepting new connections; it does not
207  * close the listening sockets, and you can call
208  * g_socket_service_start() again later to begin listening again. To
209  * close the listening sockets, call g_socket_listener_close(). (This
210  * will happen automatically when the #GSocketService is finalized.)
211  *
212  * Since: 2.22
213  */
214 void
215 g_socket_service_stop (GSocketService *service)
216 {
217   G_LOCK (active);
218
219   if (service->priv->active)
220     {
221       service->priv->active = FALSE;
222
223       if (service->priv->outstanding_accept)
224         g_cancellable_cancel (service->priv->cancellable);
225     }
226
227   G_UNLOCK (active);
228 }
229
230
231 static gboolean
232 g_socket_service_incoming (GSocketService    *service,
233                            GSocketConnection *connection,
234                            GObject           *source_object)
235 {
236   gboolean result;
237
238   g_signal_emit (service, g_socket_service_incoming_signal,
239                  0, connection, source_object, &result);
240   return result;
241 }
242
243 static void
244 g_socket_service_class_init (GSocketServiceClass *class)
245 {
246   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
247   GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
248
249   gobject_class->finalize = g_socket_service_finalize;
250   listener_class->changed = g_socket_service_changed;
251   class->incoming = g_socket_service_real_incoming;
252
253   /**
254    * GSocketService::incoming:
255    * @service: the #GSocketService
256    * @connection: a new #GSocketConnection object
257    * @source_object: (allow-none): the source_object passed to
258    *     g_socket_listener_add_address()
259    *
260    * The ::incoming signal is emitted when a new incoming connection
261    * to @service needs to be handled. The handler must initiate the
262    * handling of @connection, but may not block; in essence,
263    * asynchronous operations must be used.
264    *
265    * @connection will be unreffed once the signal handler returns,
266    * so you need to ref it yourself if you are planning to use it.
267    *
268    * Returns: %TRUE to stop other handlers from being called
269    *
270    * Since: 2.22
271    */
272   g_socket_service_incoming_signal =
273     g_signal_new ("incoming", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
274                   G_STRUCT_OFFSET (GSocketServiceClass, incoming),
275                   g_signal_accumulator_true_handled, NULL,
276                   NULL, G_TYPE_BOOLEAN,
277                   2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
278 }
279
280 static void
281 g_socket_service_ready (GObject      *object,
282                         GAsyncResult *result,
283                         gpointer      user_data)
284 {
285   GSocketListener *listener = G_SOCKET_LISTENER (object);
286   GSocketService *service = G_SOCKET_SERVICE (object);
287   GSocketConnection *connection;
288   GObject *source_object;
289   GError *error = NULL;
290
291   connection = g_socket_listener_accept_finish (listener, result, &source_object, &error);
292   if (error)
293     {
294       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
295         g_warning ("fail: %s", error->message);
296       g_error_free (error);
297     }
298   else
299     {
300       g_socket_service_incoming (service, connection, source_object);
301       g_object_unref (connection);
302     }
303
304   G_LOCK (active);
305
306   g_cancellable_reset (service->priv->cancellable);
307
308   /* requeue */
309   service->priv->outstanding_accept = FALSE;
310   if (service->priv->active)
311     do_accept (service);
312
313   G_UNLOCK (active);
314 }
315
316
317 /**
318  * g_socket_service_new:
319  *
320  * Creates a new #GSocketService with no sockets to listen for.
321  * New listeners can be added with e.g. g_socket_listener_add_address()
322  * or g_socket_listener_add_inet_port().
323  *
324  * Returns: a new #GSocketService.
325  *
326  * Since: 2.22
327  */
328 GSocketService *
329 g_socket_service_new (void)
330 {
331   return g_object_new (G_TYPE_SOCKET_SERVICE, NULL);
332 }