Make GSettingsSchemaKey public
[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 of the <link
52  * linkend="g-main-context-push-thread-default-context">thread-default
53  * context</link> of the thread it is created in, and is not
54  * threadsafe in general. However, the calls to start and stop the
55  * service are thread-safe so these can be used from threads that
56  * handle incoming clients.
57  *
58  * Since: 2.22
59  */
60
61 #include "config.h"
62 #include "gsocketservice.h"
63
64 #include <gio/gio.h>
65 #include "gsocketlistener.h"
66 #include "gsocketconnection.h"
67
68 struct _GSocketServicePrivate
69 {
70   GCancellable *cancellable;
71   guint active : 1;
72   guint outstanding_accept : 1;
73 };
74
75 static guint g_socket_service_incoming_signal;
76
77 G_LOCK_DEFINE_STATIC(active);
78
79 G_DEFINE_TYPE_WITH_PRIVATE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER)
80
81 static void g_socket_service_ready (GObject      *object,
82                                     GAsyncResult *result,
83                                     gpointer      user_data);
84
85 static gboolean
86 g_socket_service_real_incoming (GSocketService    *service,
87                                 GSocketConnection *connection,
88                                 GObject           *source_object)
89 {
90   return FALSE;
91 }
92
93 static void
94 g_socket_service_init (GSocketService *service)
95 {
96   service->priv = g_socket_service_get_instance_private (service);
97   service->priv->cancellable = g_cancellable_new ();
98   service->priv->active = TRUE;
99 }
100
101 static void
102 g_socket_service_finalize (GObject *object)
103 {
104   GSocketService *service = G_SOCKET_SERVICE (object);
105
106   g_object_unref (service->priv->cancellable);
107
108   G_OBJECT_CLASS (g_socket_service_parent_class)
109     ->finalize (object);
110 }
111
112 static void
113 do_accept (GSocketService  *service)
114 {
115   g_socket_listener_accept_async (G_SOCKET_LISTENER (service),
116                                   service->priv->cancellable,
117                                   g_socket_service_ready, NULL);
118   service->priv->outstanding_accept = TRUE;
119 }
120
121 static void
122 g_socket_service_changed (GSocketListener *listener)
123 {
124   GSocketService  *service = G_SOCKET_SERVICE (listener);
125
126   G_LOCK (active);
127
128   if (service->priv->active)
129     {
130       if (service->priv->outstanding_accept)
131         g_cancellable_cancel (service->priv->cancellable);
132       else
133         {
134           g_socket_listener_accept_async (listener, service->priv->cancellable,
135                                           g_socket_service_ready, NULL);
136           service->priv->outstanding_accept = TRUE;
137         }
138     }
139
140   G_UNLOCK (active);
141 }
142
143 /**
144  * g_socket_service_is_active:
145  * @service: a #GSocketService
146  *
147  * Check whether the service is active or not. An active
148  * service will accept new clients that connect, while
149  * a non-active service will let connecting clients queue
150  * up until the service is started.
151  *
152  * Returns: %TRUE if the service is active, %FALSE otherwise
153  *
154  * Since: 2.22
155  */
156 gboolean
157 g_socket_service_is_active (GSocketService *service)
158 {
159   gboolean active;
160
161   G_LOCK (active);
162   active = service->priv->active;
163   G_UNLOCK (active);
164   return active;
165 }
166
167 /**
168  * g_socket_service_start:
169  * @service: a #GSocketService
170  *
171  * Starts the service, i.e. start accepting connections
172  * from the added sockets when the mainloop runs.
173  *
174  * This call is thread-safe, so it may be called from a thread
175  * handling an incoming client request.
176  *
177  * Since: 2.22
178  */
179 void
180 g_socket_service_start (GSocketService *service)
181 {
182   G_LOCK (active);
183
184   if (!service->priv->active)
185     {
186       service->priv->active = TRUE;
187
188       if (service->priv->outstanding_accept)
189         g_cancellable_cancel (service->priv->cancellable);
190       else
191         do_accept (service);
192     }
193
194   G_UNLOCK (active);
195 }
196
197 /**
198  * g_socket_service_stop:
199  * @service: a #GSocketService
200  *
201  * Stops the service, i.e. stops accepting connections
202  * from the added sockets when the mainloop runs.
203  *
204  * This call is thread-safe, so it may be called from a thread
205  * handling an incoming client request.
206  *
207  * Since: 2.22
208  */
209 void
210 g_socket_service_stop (GSocketService *service)
211 {
212   G_LOCK (active);
213
214   if (service->priv->active)
215     {
216       service->priv->active = FALSE;
217
218       if (service->priv->outstanding_accept)
219         g_cancellable_cancel (service->priv->cancellable);
220     }
221
222   G_UNLOCK (active);
223 }
224
225
226 static gboolean
227 g_socket_service_incoming (GSocketService    *service,
228                            GSocketConnection *connection,
229                            GObject           *source_object)
230 {
231   gboolean result;
232
233   g_signal_emit (service, g_socket_service_incoming_signal,
234                  0, connection, source_object, &result);
235   return result;
236 }
237
238 static void
239 g_socket_service_class_init (GSocketServiceClass *class)
240 {
241   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
242   GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
243
244   gobject_class->finalize = g_socket_service_finalize;
245   listener_class->changed = g_socket_service_changed;
246   class->incoming = g_socket_service_real_incoming;
247
248   /**
249    * GSocketService::incoming:
250    * @service: the #GSocketService
251    * @connection: a new #GSocketConnection object
252    * @source_object: (allow-none): the source_object passed to
253    *     g_socket_listener_add_address()
254    *
255    * The ::incoming signal is emitted when a new incoming connection
256    * to @service needs to be handled. The handler must initiate the
257    * handling of @connection, but may not block; in essence,
258    * asynchronous operations must be used.
259    *
260    * @connection will be unreffed once the signal handler returns,
261    * so you need to ref it yourself if you are planning to use it.
262    *
263    * Returns: %TRUE to stop other handlers from being called
264    *
265    * Since: 2.22
266    */
267   g_socket_service_incoming_signal =
268     g_signal_new ("incoming", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
269                   G_STRUCT_OFFSET (GSocketServiceClass, incoming),
270                   g_signal_accumulator_true_handled, NULL,
271                   NULL, G_TYPE_BOOLEAN,
272                   2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
273 }
274
275 static void
276 g_socket_service_ready (GObject      *object,
277                         GAsyncResult *result,
278                         gpointer      user_data)
279 {
280   GSocketListener *listener = G_SOCKET_LISTENER (object);
281   GSocketService *service = G_SOCKET_SERVICE (object);
282   GSocketConnection *connection;
283   GObject *source_object;
284   GError *error = NULL;
285
286   connection = g_socket_listener_accept_finish (listener, result, &source_object, &error);
287   if (error)
288     {
289       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
290         g_warning ("fail: %s", error->message);
291       g_error_free (error);
292     }
293   else
294     {
295       g_socket_service_incoming (service, connection, source_object);
296       g_object_unref (connection);
297     }
298
299   G_LOCK (active);
300
301   g_cancellable_reset (service->priv->cancellable);
302
303   /* requeue */
304   service->priv->outstanding_accept = FALSE;
305   if (service->priv->active)
306     do_accept (service);
307
308   G_UNLOCK (active);
309 }
310
311
312 /**
313  * g_socket_service_new:
314  *
315  * Creates a new #GSocketService with no sockets to listen for.
316  * New listeners can be added with e.g. g_socket_listener_add_address()
317  * or g_socket_listener_add_inet_port().
318  *
319  * Returns: a new #GSocketService.
320  *
321  * Since: 2.22
322  */
323 GSocketService *
324 g_socket_service_new (void)
325 {
326   return g_object_new (G_TYPE_SOCKET_SERVICE, NULL);
327 }