1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
4 * Copyright © 2009 Red Hat, Inc
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General
19 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Authors: Ryan Lortie <desrt@desrt.ca>
22 * Alexander Larsson <alexl@redhat.com>
28 * A `GSocketService` is an object that represents a service that
29 * is provided to the network or over local sockets. When a new
30 * connection is made to the service the [signal@Gio.SocketService::incoming]
33 * A `GSocketService` is a subclass of [class@Gio.SocketListener] and you need
34 * to add the addresses you want to accept connections on with the
35 * [class@Gio.SocketListener] APIs.
37 * There are two options for implementing a network service based on
38 * `GSocketService`. The first is to create the service using
39 * [ctor@Gio.SocketService.new] and to connect to the
40 * [signal@Gio.SocketService::incoming] signal. The second is to subclass
41 * `GSocketService` and override the default signal handler implementation.
43 * In either case, the handler must immediately return, or else it
44 * will block additional incoming connections from being serviced.
45 * If you are interested in writing connection handlers that contain
46 * blocking code then see [class@Gio.ThreadedSocketService].
48 * The socket service runs on the main loop of the
49 * thread-default context (see
50 * [method@GLib.MainContext.push_thread_default]) of the thread it is
51 * created in, and is not threadsafe in general. However, the calls to start and
52 * stop the service are thread-safe so these can be used from threads that
53 * handle incoming clients.
59 #include "gsocketservice.h"
62 #include "gsocketlistener.h"
63 #include "gsocketconnection.h"
65 #include "gmarshal-internal.h"
67 struct _GSocketServicePrivate
69 GCancellable *cancellable;
71 guint outstanding_accept : 1;
74 static guint g_socket_service_incoming_signal;
76 G_LOCK_DEFINE_STATIC(active);
78 G_DEFINE_TYPE_WITH_PRIVATE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER)
86 static void g_socket_service_ready (GObject *object,
91 g_socket_service_real_incoming (GSocketService *service,
92 GSocketConnection *connection,
93 GObject *source_object)
99 g_socket_service_init (GSocketService *service)
101 service->priv = g_socket_service_get_instance_private (service);
102 service->priv->cancellable = g_cancellable_new ();
103 service->priv->active = TRUE;
107 g_socket_service_finalize (GObject *object)
109 GSocketService *service = G_SOCKET_SERVICE (object);
111 g_object_unref (service->priv->cancellable);
113 G_OBJECT_CLASS (g_socket_service_parent_class)
118 do_accept (GSocketService *service)
120 g_socket_listener_accept_async (G_SOCKET_LISTENER (service),
121 service->priv->cancellable,
122 g_socket_service_ready, NULL);
123 service->priv->outstanding_accept = TRUE;
127 get_active (GSocketService *service)
132 active = service->priv->active;
139 set_active (GSocketService *service, gboolean active)
141 gboolean notify = FALSE;
147 if (active != service->priv->active)
149 service->priv->active = active;
154 if (service->priv->outstanding_accept)
155 g_cancellable_cancel (service->priv->cancellable);
161 if (service->priv->outstanding_accept)
162 g_cancellable_cancel (service->priv->cancellable);
169 g_object_notify (G_OBJECT (service), "active");
173 g_socket_service_get_property (GObject *object,
178 GSocketService *service = G_SOCKET_SERVICE (object);
183 g_value_set_boolean (value, get_active (service));
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 g_socket_service_set_property (GObject *object,
197 GSocketService *service = G_SOCKET_SERVICE (object);
202 set_active (service, g_value_get_boolean (value));
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211 g_socket_service_changed (GSocketListener *listener)
213 GSocketService *service = G_SOCKET_SERVICE (listener);
217 if (service->priv->active)
219 if (service->priv->outstanding_accept)
220 g_cancellable_cancel (service->priv->cancellable);
229 * g_socket_service_is_active:
230 * @service: a #GSocketService
232 * Check whether the service is active or not. An active
233 * service will accept new clients that connect, while
234 * a non-active service will let connecting clients queue
235 * up until the service is started.
237 * Returns: %TRUE if the service is active, %FALSE otherwise
242 g_socket_service_is_active (GSocketService *service)
244 g_return_val_if_fail (G_IS_SOCKET_SERVICE (service), FALSE);
246 return get_active (service);
250 * g_socket_service_start:
251 * @service: a #GSocketService
253 * Restarts the service, i.e. start accepting connections
254 * from the added sockets when the mainloop runs. This only needs
255 * to be called after the service has been stopped from
256 * g_socket_service_stop().
258 * This call is thread-safe, so it may be called from a thread
259 * handling an incoming client request.
264 g_socket_service_start (GSocketService *service)
266 g_return_if_fail (G_IS_SOCKET_SERVICE (service));
268 set_active (service, TRUE);
272 * g_socket_service_stop:
273 * @service: a #GSocketService
275 * Stops the service, i.e. stops accepting connections
276 * from the added sockets when the mainloop runs.
278 * This call is thread-safe, so it may be called from a thread
279 * handling an incoming client request.
281 * Note that this only stops accepting new connections; it does not
282 * close the listening sockets, and you can call
283 * g_socket_service_start() again later to begin listening again. To
284 * close the listening sockets, call g_socket_listener_close(). (This
285 * will happen automatically when the #GSocketService is finalized.)
287 * This must be called before calling g_socket_listener_close() as
288 * the socket service will start accepting connections immediately
289 * when a new socket is added.
294 g_socket_service_stop (GSocketService *service)
296 g_return_if_fail (G_IS_SOCKET_SERVICE (service));
298 set_active (service, FALSE);
302 g_socket_service_incoming (GSocketService *service,
303 GSocketConnection *connection,
304 GObject *source_object)
308 g_signal_emit (service, g_socket_service_incoming_signal,
309 0, connection, source_object, &result);
314 g_socket_service_class_init (GSocketServiceClass *class)
316 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
317 GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
319 gobject_class->finalize = g_socket_service_finalize;
320 gobject_class->set_property = g_socket_service_set_property;
321 gobject_class->get_property = g_socket_service_get_property;
322 listener_class->changed = g_socket_service_changed;
323 class->incoming = g_socket_service_real_incoming;
326 * GSocketService::incoming:
327 * @service: the #GSocketService
328 * @connection: a new #GSocketConnection object
329 * @source_object: (nullable): the source_object passed to
330 * g_socket_listener_add_address()
332 * The ::incoming signal is emitted when a new incoming connection
333 * to @service needs to be handled. The handler must initiate the
334 * handling of @connection, but may not block; in essence,
335 * asynchronous operations must be used.
337 * @connection will be unreffed once the signal handler returns,
338 * so you need to ref it yourself if you are planning to use it.
340 * Returns: %TRUE to stop other handlers from being called
344 g_socket_service_incoming_signal =
345 g_signal_new (I_("incoming"), G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
346 G_STRUCT_OFFSET (GSocketServiceClass, incoming),
347 g_signal_accumulator_true_handled, NULL,
348 _g_cclosure_marshal_BOOLEAN__OBJECT_OBJECT,
350 2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
351 g_signal_set_va_marshaller (g_socket_service_incoming_signal,
352 G_TYPE_FROM_CLASS (class),
353 _g_cclosure_marshal_BOOLEAN__OBJECT_OBJECTv);
356 * GSocketService:active:
358 * Whether the service is currently accepting connections.
362 g_object_class_install_property (gobject_class, PROP_ACTIVE,
363 g_param_spec_boolean ("active", NULL, NULL,
365 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 g_socket_service_ready (GObject *object,
370 GAsyncResult *result,
373 GSocketListener *listener = G_SOCKET_LISTENER (object);
374 GSocketService *service = G_SOCKET_SERVICE (object);
375 GSocketConnection *connection;
376 GObject *source_object;
377 GError *error = NULL;
379 connection = g_socket_listener_accept_finish (listener, result, &source_object, &error);
382 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
383 g_warning ("fail: %s", error->message);
384 g_error_free (error);
388 g_socket_service_incoming (service, connection, source_object);
389 g_object_unref (connection);
394 g_cancellable_reset (service->priv->cancellable);
397 service->priv->outstanding_accept = FALSE;
398 if (service->priv->active)
405 * g_socket_service_new:
407 * Creates a new #GSocketService with no sockets to listen for.
408 * New listeners can be added with e.g. g_socket_listener_add_address()
409 * or g_socket_listener_add_inet_port().
411 * New services are created active, there is no need to call
412 * g_socket_service_start(), unless g_socket_service_stop() has been
415 * Returns: a new #GSocketService.
420 g_socket_service_new (void)
422 return g_object_new (G_TYPE_SOCKET_SERVICE, NULL);