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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Ryan Lortie <desrt@desrt.ca>
22 * Alexander Larsson <alexl@redhat.com>
26 * SECTION:gsocketservice
27 * @title: GSocketService
28 * @short_description: Make it easy to implement a network service
30 * @see_also: #GThreadedSocketService, #GSocketListener.
32 * A #GSocketService is an object that represents a service that
33 * is provided to the network or over local sockets. When a new
34 * connection is made to the service the #GSocketService::incoming
37 * A #GSocketService is a subclass of #GSocketListener and you need
38 * to add the addresses you want to accept connections on with the
39 * #GSocketListener APIs.
41 * There are two options for implementing a network service based on
42 * #GSocketService. The first is to create the service using
43 * g_socket_service_new() and to connect to the #GSocketService::incoming
44 * signal. The second is to subclass #GSocketService and override the
45 * default signal handler implementation.
47 * In either case, the handler must immediately return, or else it
48 * will block additional incoming connections from being serviced.
49 * If you are interested in writing connection handlers that contain
50 * blocking code then see #GThreadedSocketService.
52 * The socket service runs on the main loop of the <link
53 * linkend="g-main-context-push-thread-default-context">thread-default
54 * context</link> of the thread it is created in, and is not
55 * threadsafe in general. However, the calls to start and stop the
56 * service are thread-safe so these can be used from threads that
57 * handle incoming clients.
63 #include "gsocketservice.h"
66 #include "gsocketlistener.h"
67 #include "gsocketconnection.h"
69 struct _GSocketServicePrivate
71 GCancellable *cancellable;
73 guint outstanding_accept : 1;
76 static guint g_socket_service_incoming_signal;
78 G_LOCK_DEFINE_STATIC(active);
80 G_DEFINE_TYPE_WITH_PRIVATE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER)
82 static void g_socket_service_ready (GObject *object,
87 g_socket_service_real_incoming (GSocketService *service,
88 GSocketConnection *connection,
89 GObject *source_object)
95 g_socket_service_init (GSocketService *service)
97 service->priv = g_socket_service_get_instance_private (service);
98 service->priv->cancellable = g_cancellable_new ();
99 service->priv->active = TRUE;
103 g_socket_service_finalize (GObject *object)
105 GSocketService *service = G_SOCKET_SERVICE (object);
107 g_object_unref (service->priv->cancellable);
109 G_OBJECT_CLASS (g_socket_service_parent_class)
114 do_accept (GSocketService *service)
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;
123 g_socket_service_changed (GSocketListener *listener)
125 GSocketService *service = G_SOCKET_SERVICE (listener);
129 if (service->priv->active)
131 if (service->priv->outstanding_accept)
132 g_cancellable_cancel (service->priv->cancellable);
135 g_socket_listener_accept_async (listener, service->priv->cancellable,
136 g_socket_service_ready, NULL);
137 service->priv->outstanding_accept = TRUE;
145 * g_socket_service_is_active:
146 * @service: a #GSocketService
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.
153 * Returns: %TRUE if the service is active, %FALSE otherwise
158 g_socket_service_is_active (GSocketService *service)
163 active = service->priv->active;
169 * g_socket_service_start:
170 * @service: a #GSocketService
172 * Starts the service, i.e. start accepting connections
173 * from the added sockets when the mainloop runs.
175 * This call is thread-safe, so it may be called from a thread
176 * handling an incoming client request.
181 g_socket_service_start (GSocketService *service)
185 if (!service->priv->active)
187 service->priv->active = TRUE;
189 if (service->priv->outstanding_accept)
190 g_cancellable_cancel (service->priv->cancellable);
199 * g_socket_service_stop:
200 * @service: a #GSocketService
202 * Stops the service, i.e. stops accepting connections
203 * from the added sockets when the mainloop runs.
205 * This call is thread-safe, so it may be called from a thread
206 * handling an incoming client request.
211 g_socket_service_stop (GSocketService *service)
215 if (service->priv->active)
217 service->priv->active = FALSE;
219 if (service->priv->outstanding_accept)
220 g_cancellable_cancel (service->priv->cancellable);
228 g_socket_service_incoming (GSocketService *service,
229 GSocketConnection *connection,
230 GObject *source_object)
234 g_signal_emit (service, g_socket_service_incoming_signal,
235 0, connection, source_object, &result);
240 g_socket_service_class_init (GSocketServiceClass *class)
242 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
243 GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
245 gobject_class->finalize = g_socket_service_finalize;
246 listener_class->changed = g_socket_service_changed;
247 class->incoming = g_socket_service_real_incoming;
250 * GSocketService::incoming:
251 * @service: the #GSocketService
252 * @connection: a new #GSocketConnection object
253 * @source_object: (allow-none): the source_object passed to
254 * g_socket_listener_add_address()
256 * The ::incoming signal is emitted when a new incoming connection
257 * to @service needs to be handled. The handler must initiate the
258 * handling of @connection, but may not block; in essence,
259 * asynchronous operations must be used.
261 * @connection will be unreffed once the signal handler returns,
262 * so you need to ref it yourself if you are planning to use it.
264 * Returns: %TRUE to stop other handlers from being called
268 g_socket_service_incoming_signal =
269 g_signal_new ("incoming", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
270 G_STRUCT_OFFSET (GSocketServiceClass, incoming),
271 g_signal_accumulator_true_handled, NULL,
272 NULL, G_TYPE_BOOLEAN,
273 2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
277 g_socket_service_ready (GObject *object,
278 GAsyncResult *result,
281 GSocketListener *listener = G_SOCKET_LISTENER (object);
282 GSocketService *service = G_SOCKET_SERVICE (object);
283 GSocketConnection *connection;
284 GObject *source_object;
285 GError *error = NULL;
287 connection = g_socket_listener_accept_finish (listener, result, &source_object, &error);
290 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
291 g_warning ("fail: %s", error->message);
292 g_error_free (error);
296 g_socket_service_incoming (service, connection, source_object);
297 g_object_unref (connection);
302 g_cancellable_reset (service->priv->cancellable);
305 service->priv->outstanding_accept = FALSE;
306 if (service->priv->active)
314 * g_socket_service_new:
316 * Creates a new #GSocketService with no sockets to listen for.
317 * New listeners can be added with e.g. g_socket_listener_add_address()
318 * or g_socket_listener_add_inet_port().
320 * Returns: a new #GSocketService.
325 g_socket_service_new (void)
327 return g_object_new (G_TYPE_SOCKET_SERVICE, NULL);