1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008, 2009 Codethink Limited
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * See the included COPYING file for more information.
18 * This is the subclass of [class@Gio.SocketConnection] that is created
25 #include "gtcpconnection.h"
26 #include "gasyncresult.h"
28 #include "giostream.h"
31 struct _GTcpConnectionPrivate
33 guint graceful_disconnect : 1;
36 G_DEFINE_TYPE_WITH_CODE (GTcpConnection, g_tcp_connection,
37 G_TYPE_SOCKET_CONNECTION,
38 G_ADD_PRIVATE (GTcpConnection)
39 g_socket_connection_factory_register_type (g_define_type_id,
42 G_SOCKET_PROTOCOL_DEFAULT);
43 g_socket_connection_factory_register_type (g_define_type_id,
46 G_SOCKET_PROTOCOL_DEFAULT);
47 g_socket_connection_factory_register_type (g_define_type_id,
50 G_SOCKET_PROTOCOL_TCP);
51 g_socket_connection_factory_register_type (g_define_type_id,
54 G_SOCKET_PROTOCOL_TCP);
57 static gboolean g_tcp_connection_close (GIOStream *stream,
58 GCancellable *cancellable,
60 static void g_tcp_connection_close_async (GIOStream *stream,
62 GCancellable *cancellable,
63 GAsyncReadyCallback callback,
70 PROP_GRACEFUL_DISCONNECT
74 g_tcp_connection_init (GTcpConnection *connection)
76 connection->priv = g_tcp_connection_get_instance_private (connection);
77 connection->priv->graceful_disconnect = FALSE;
81 g_tcp_connection_get_property (GObject *object,
86 GTcpConnection *connection = G_TCP_CONNECTION (object);
90 case PROP_GRACEFUL_DISCONNECT:
91 g_value_set_boolean (value, connection->priv->graceful_disconnect);
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
100 g_tcp_connection_set_property (GObject *object,
105 GTcpConnection *connection = G_TCP_CONNECTION (object);
109 case PROP_GRACEFUL_DISCONNECT:
110 g_tcp_connection_set_graceful_disconnect (connection,
111 g_value_get_boolean (value));
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120 g_tcp_connection_class_init (GTcpConnectionClass *class)
122 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
123 GIOStreamClass *stream_class = G_IO_STREAM_CLASS (class);
125 gobject_class->set_property = g_tcp_connection_set_property;
126 gobject_class->get_property = g_tcp_connection_get_property;
128 stream_class->close_fn = g_tcp_connection_close;
129 stream_class->close_async = g_tcp_connection_close_async;
132 * GTcpConnection:graceful-disconnect:
134 * Whether [method@Gio.IOStream.close] does a graceful disconnect.
138 g_object_class_install_property (gobject_class, PROP_GRACEFUL_DISCONNECT,
139 g_param_spec_boolean ("graceful-disconnect", NULL, NULL,
141 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146 g_tcp_connection_close (GIOStream *stream,
147 GCancellable *cancellable,
150 GTcpConnection *connection = G_TCP_CONNECTION (stream);
156 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
159 if (connection->priv->graceful_disconnect &&
160 !g_cancellable_is_cancelled (cancellable) /* Cancelled -> close fast */)
162 if (!g_socket_shutdown (socket, FALSE, TRUE, error))
164 error = NULL; /* Ignore further errors */
171 ret = g_socket_receive_with_blocking (socket, buffer, sizeof (buffer),
172 TRUE, cancellable, error);
185 return G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
186 ->close_fn (stream, cancellable, error) && !had_error;
189 /* consumes @error */
191 async_close_finish (GTask *task,
194 GIOStreamClass *parent = G_IO_STREAM_CLASS (g_tcp_connection_parent_class);
195 GIOStream *stream = g_task_get_source_object (task);
196 GCancellable *cancellable = g_task_get_cancellable (task);
198 /* Close underlying stream, ignoring further errors if we already
202 parent->close_fn (stream, cancellable, NULL);
204 parent->close_fn (stream, cancellable, &error);
207 g_task_return_error (task, error);
209 g_task_return_boolean (task, TRUE);
211 g_object_unref (task);
216 close_read_ready (GSocket *socket,
217 GIOCondition condition,
220 GError *error = NULL;
224 ret = g_socket_receive_with_blocking (socket, buffer, sizeof (buffer),
225 FALSE, g_task_get_cancellable (task),
229 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
231 g_error_free (error);
236 async_close_finish (task, error);
243 async_close_finish (task, NULL);
252 g_tcp_connection_close_async (GIOStream *stream,
254 GCancellable *cancellable,
255 GAsyncReadyCallback callback,
258 GTcpConnection *connection = G_TCP_CONNECTION (stream);
264 if (connection->priv->graceful_disconnect &&
265 !g_cancellable_is_cancelled (cancellable) /* Cancelled -> close fast */)
267 task = g_task_new (stream, cancellable, callback, user_data);
268 g_task_set_source_tag (task, g_tcp_connection_close_async);
269 g_task_set_priority (task, io_priority);
271 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
274 if (!g_socket_shutdown (socket, FALSE, TRUE, &error))
276 g_task_return_error (task, error);
277 g_object_unref (task);
281 source = g_socket_create_source (socket, G_IO_IN, cancellable);
282 g_task_attach_source (task, source, (GSourceFunc) close_read_ready);
283 g_source_unref (source);
288 G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
289 ->close_async (stream, io_priority, cancellable, callback, user_data);
293 * g_tcp_connection_set_graceful_disconnect:
294 * @connection: a #GTcpConnection
295 * @graceful_disconnect: Whether to do graceful disconnects or not
297 * This enables graceful disconnects on close. A graceful disconnect
298 * means that we signal the receiving end that the connection is terminated
299 * and wait for it to close the connection before closing the connection.
301 * A graceful disconnect means that we can be sure that we successfully sent
302 * all the outstanding data to the other end, or get an error reported.
303 * However, it also means we have to wait for all the data to reach the
304 * other side and for it to acknowledge this by closing the socket, which may
305 * take a while. For this reason it is disabled by default.
310 g_tcp_connection_set_graceful_disconnect (GTcpConnection *connection,
311 gboolean graceful_disconnect)
313 graceful_disconnect = !!graceful_disconnect;
314 if (graceful_disconnect != connection->priv->graceful_disconnect)
316 connection->priv->graceful_disconnect = graceful_disconnect;
317 g_object_notify (G_OBJECT (connection), "graceful-disconnect");
322 * g_tcp_connection_get_graceful_disconnect:
323 * @connection: a #GTcpConnection
325 * Checks if graceful disconnects are used. See
326 * g_tcp_connection_set_graceful_disconnect().
328 * Returns: %TRUE if graceful disconnect is used on close, %FALSE otherwise
333 g_tcp_connection_get_graceful_disconnect (GTcpConnection *connection)
335 return connection->priv->graceful_disconnect;