Update German translation
[glib.git] / gio / gsocketservice.c
blob57113a00f408cf691282de0796ba698cf8ca0f18
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
4 * Copyright © 2009 Red Hat, Inc
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.
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.
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/>.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
20 * Alexander Larsson <alexl@redhat.com>
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.
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.
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.
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.
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.
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.
57 * Since: 2.22
60 #include "config.h"
61 #include "gsocketservice.h"
63 #include <gio/gio.h>
64 #include "gsocketlistener.h"
65 #include "gsocketconnection.h"
66 #include "glibintl.h"
68 struct _GSocketServicePrivate
70 GCancellable *cancellable;
71 guint active : 1;
72 guint outstanding_accept : 1;
75 static guint g_socket_service_incoming_signal;
77 G_LOCK_DEFINE_STATIC(active);
79 G_DEFINE_TYPE_WITH_PRIVATE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER)
81 enum
83 PROP_0,
84 PROP_ACTIVE
87 static void g_socket_service_ready (GObject *object,
88 GAsyncResult *result,
89 gpointer user_data);
91 static gboolean
92 g_socket_service_real_incoming (GSocketService *service,
93 GSocketConnection *connection,
94 GObject *source_object)
96 return FALSE;
99 static void
100 g_socket_service_init (GSocketService *service)
102 service->priv = g_socket_service_get_instance_private (service);
103 service->priv->cancellable = g_cancellable_new ();
104 service->priv->active = TRUE;
107 static void
108 g_socket_service_finalize (GObject *object)
110 GSocketService *service = G_SOCKET_SERVICE (object);
112 g_object_unref (service->priv->cancellable);
114 G_OBJECT_CLASS (g_socket_service_parent_class)
115 ->finalize (object);
118 static void
119 do_accept (GSocketService *service)
121 g_socket_listener_accept_async (G_SOCKET_LISTENER (service),
122 service->priv->cancellable,
123 g_socket_service_ready, NULL);
124 service->priv->outstanding_accept = TRUE;
127 static gboolean
128 get_active (GSocketService *service)
130 gboolean active;
132 G_LOCK (active);
133 active = service->priv->active;
134 G_UNLOCK (active);
136 return active;
139 static void
140 set_active (GSocketService *service, gboolean active)
142 gboolean notify = FALSE;
144 active = !!active;
146 G_LOCK (active);
148 if (active != service->priv->active)
150 service->priv->active = active;
151 notify = TRUE;
153 if (active)
155 if (service->priv->outstanding_accept)
156 g_cancellable_cancel (service->priv->cancellable);
157 else
158 do_accept (service);
160 else
162 if (service->priv->outstanding_accept)
163 g_cancellable_cancel (service->priv->cancellable);
167 G_UNLOCK (active);
169 if (notify)
170 g_object_notify (G_OBJECT (service), "active");
173 static void
174 g_socket_service_get_property (GObject *object,
175 guint prop_id,
176 GValue *value,
177 GParamSpec *pspec)
179 GSocketService *service = G_SOCKET_SERVICE (object);
181 switch (prop_id)
183 case PROP_ACTIVE:
184 g_value_set_boolean (value, get_active (service));
185 break;
186 default:
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 break;
192 static void
193 g_socket_service_set_property (GObject *object,
194 guint prop_id,
195 const GValue *value,
196 GParamSpec *pspec)
198 GSocketService *service = G_SOCKET_SERVICE (object);
200 switch (prop_id)
202 case PROP_ACTIVE:
203 set_active (service, g_value_get_boolean (value));
204 break;
205 default:
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 break;
211 static void
212 g_socket_service_changed (GSocketListener *listener)
214 GSocketService *service = G_SOCKET_SERVICE (listener);
216 G_LOCK (active);
218 if (service->priv->active)
220 if (service->priv->outstanding_accept)
221 g_cancellable_cancel (service->priv->cancellable);
222 else
223 do_accept (service);
226 G_UNLOCK (active);
230 * g_socket_service_is_active:
231 * @service: a #GSocketService
233 * Check whether the service is active or not. An active
234 * service will accept new clients that connect, while
235 * a non-active service will let connecting clients queue
236 * up until the service is started.
238 * Returns: %TRUE if the service is active, %FALSE otherwise
240 * Since: 2.22
242 gboolean
243 g_socket_service_is_active (GSocketService *service)
245 g_return_val_if_fail (G_IS_SOCKET_SERVICE (service), FALSE);
247 return get_active (service);
251 * g_socket_service_start:
252 * @service: a #GSocketService
254 * Restarts the service, i.e. start accepting connections
255 * from the added sockets when the mainloop runs. This only needs
256 * to be called after the service has been stopped from
257 * g_socket_service_stop().
259 * This call is thread-safe, so it may be called from a thread
260 * handling an incoming client request.
262 * Since: 2.22
264 void
265 g_socket_service_start (GSocketService *service)
267 g_return_if_fail (G_IS_SOCKET_SERVICE (service));
269 set_active (service, TRUE);
273 * g_socket_service_stop:
274 * @service: a #GSocketService
276 * Stops the service, i.e. stops accepting connections
277 * from the added sockets when the mainloop runs.
279 * This call is thread-safe, so it may be called from a thread
280 * handling an incoming client request.
282 * Note that this only stops accepting new connections; it does not
283 * close the listening sockets, and you can call
284 * g_socket_service_start() again later to begin listening again. To
285 * close the listening sockets, call g_socket_listener_close(). (This
286 * will happen automatically when the #GSocketService is finalized.)
288 * This must be called before calling g_socket_listener_close() as
289 * the socket service will start accepting connections immediately
290 * when a new socket is added.
292 * Since: 2.22
294 void
295 g_socket_service_stop (GSocketService *service)
297 g_return_if_fail (G_IS_SOCKET_SERVICE (service));
299 set_active (service, FALSE);
302 static gboolean
303 g_socket_service_incoming (GSocketService *service,
304 GSocketConnection *connection,
305 GObject *source_object)
307 gboolean result;
309 g_signal_emit (service, g_socket_service_incoming_signal,
310 0, connection, source_object, &result);
311 return result;
314 static void
315 g_socket_service_class_init (GSocketServiceClass *class)
317 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
318 GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
320 gobject_class->finalize = g_socket_service_finalize;
321 gobject_class->set_property = g_socket_service_set_property;
322 gobject_class->get_property = g_socket_service_get_property;
323 listener_class->changed = g_socket_service_changed;
324 class->incoming = g_socket_service_real_incoming;
327 * GSocketService::incoming:
328 * @service: the #GSocketService
329 * @connection: a new #GSocketConnection object
330 * @source_object: (nullable): the source_object passed to
331 * g_socket_listener_add_address()
333 * The ::incoming signal is emitted when a new incoming connection
334 * to @service needs to be handled. The handler must initiate the
335 * handling of @connection, but may not block; in essence,
336 * asynchronous operations must be used.
338 * @connection will be unreffed once the signal handler returns,
339 * so you need to ref it yourself if you are planning to use it.
341 * Returns: %TRUE to stop other handlers from being called
343 * Since: 2.22
345 g_socket_service_incoming_signal =
346 g_signal_new (I_("incoming"), G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
347 G_STRUCT_OFFSET (GSocketServiceClass, incoming),
348 g_signal_accumulator_true_handled, NULL,
349 NULL, G_TYPE_BOOLEAN,
350 2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
353 * GSocketService:active:
355 * Whether the service is currently accepting connections.
357 * Since: 2.46
359 g_object_class_install_property (gobject_class, PROP_ACTIVE,
360 g_param_spec_boolean ("active",
361 P_("Active"),
362 P_("Whether the service is currently accepting connections"),
363 TRUE,
364 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 static void
368 g_socket_service_ready (GObject *object,
369 GAsyncResult *result,
370 gpointer user_data)
372 GSocketListener *listener = G_SOCKET_LISTENER (object);
373 GSocketService *service = G_SOCKET_SERVICE (object);
374 GSocketConnection *connection;
375 GObject *source_object;
376 GError *error = NULL;
378 connection = g_socket_listener_accept_finish (listener, result, &source_object, &error);
379 if (error)
381 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
382 g_warning ("fail: %s", error->message);
383 g_error_free (error);
385 else
387 g_socket_service_incoming (service, connection, source_object);
388 g_object_unref (connection);
391 G_LOCK (active);
393 g_cancellable_reset (service->priv->cancellable);
395 /* requeue */
396 service->priv->outstanding_accept = FALSE;
397 if (service->priv->active)
398 do_accept (service);
400 G_UNLOCK (active);
404 * g_socket_service_new:
406 * Creates a new #GSocketService with no sockets to listen for.
407 * New listeners can be added with e.g. g_socket_listener_add_address()
408 * or g_socket_listener_add_inet_port().
410 * New services are created active, there is no need to call
411 * g_socket_service_start(), unless g_socket_service_stop() has been
412 * called before.
414 * Returns: a new #GSocketService.
416 * Since: 2.22
418 GSocketService *
419 g_socket_service_new (void)
421 return g_object_new (G_TYPE_SOCKET_SERVICE, NULL);