1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008, 2009 Codethink Limited
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
14 * SECTION:gtcpconnection
15 * @title: GTcpConnection
16 * @short_description: A TCP GSocketConnection
17 * @see_also: #GSocketConnection.
19 * This is the subclass of #GSocketConnection that is created
26 #include "gtcpconnection.h"
27 #include "gasyncresult.h"
29 #include "giostream.h"
33 G_DEFINE_TYPE_WITH_CODE (GTcpConnection, g_tcp_connection,
34 G_TYPE_SOCKET_CONNECTION,
35 g_socket_connection_factory_register_type (g_define_type_id,
38 G_SOCKET_PROTOCOL_DEFAULT);
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_TCP);
47 g_socket_connection_factory_register_type (g_define_type_id,
50 G_SOCKET_PROTOCOL_TCP);
53 static gboolean g_tcp_connection_close (GIOStream *stream,
54 GCancellable *cancellable,
56 static void g_tcp_connection_close_async (GIOStream *stream,
58 GCancellable *cancellable,
59 GAsyncReadyCallback callback,
62 struct _GTcpConnectionPrivate
64 guint graceful_disconnect : 1;
71 PROP_GRACEFUL_DISCONNECT
75 g_tcp_connection_init (GTcpConnection *connection)
77 connection->priv = G_TYPE_INSTANCE_GET_PRIVATE (connection,
78 G_TYPE_TCP_CONNECTION,
79 GTcpConnectionPrivate);
80 connection->priv->graceful_disconnect = FALSE;
84 g_tcp_connection_get_property (GObject *object,
89 GTcpConnection *connection = G_TCP_CONNECTION (object);
93 case PROP_GRACEFUL_DISCONNECT:
94 g_value_set_boolean (value, connection->priv->graceful_disconnect);
98 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103 g_tcp_connection_set_property (GObject *object,
108 GTcpConnection *connection = G_TCP_CONNECTION (object);
112 case PROP_GRACEFUL_DISCONNECT:
113 g_tcp_connection_set_graceful_disconnect (connection,
114 g_value_get_boolean (value));
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123 g_tcp_connection_class_init (GTcpConnectionClass *class)
125 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
126 GIOStreamClass *stream_class = G_IO_STREAM_CLASS (class);
128 g_type_class_add_private (class, sizeof (GTcpConnectionPrivate));
130 gobject_class->set_property = g_tcp_connection_set_property;
131 gobject_class->get_property = g_tcp_connection_get_property;
133 stream_class->close_fn = g_tcp_connection_close;
134 stream_class->close_async = g_tcp_connection_close_async;
136 g_object_class_install_property (gobject_class, PROP_GRACEFUL_DISCONNECT,
137 g_param_spec_boolean ("graceful-disconnect",
138 P_("Graceful Disconnect"),
139 P_("Whether or not close does a graceful disconnect"),
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);
214 close_read_ready (GSocket *socket,
215 GIOCondition condition,
218 GError *error = NULL;
222 ret = g_socket_receive_with_blocking (socket, buffer, sizeof (buffer),
223 FALSE, g_task_get_cancellable (task),
227 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
229 g_error_free (error);
234 async_close_finish (task, error);
235 g_object_unref (task);
242 async_close_finish (task, NULL);
251 g_tcp_connection_close_async (GIOStream *stream,
253 GCancellable *cancellable,
254 GAsyncReadyCallback callback,
257 GTcpConnection *connection = G_TCP_CONNECTION (stream);
263 if (connection->priv->graceful_disconnect &&
264 !g_cancellable_is_cancelled (cancellable) /* Cancelled -> close fast */)
266 task = g_task_new (stream, cancellable, callback, user_data);
267 g_task_set_priority (task, io_priority);
269 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
272 if (!g_socket_shutdown (socket, FALSE, TRUE, &error))
274 g_task_return_error (task, error);
275 g_object_unref (task);
279 source = g_socket_create_source (socket, G_IO_IN, cancellable);
280 g_task_attach_source (task, source, (GSourceFunc) close_read_ready);
281 g_source_unref (source);
286 G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
287 ->close_async (stream, io_priority, cancellable, callback, user_data);
291 * g_tcp_connection_set_graceful_disconnect:
292 * @connection: a #GTcpConnection
293 * @graceful_disconnect: Whether to do graceful disconnects or not
295 * This enabled graceful disconnects on close. A graceful disconnect
296 * means that we signal the receiving end that the connection is terminated
297 * and wait for it to close the connection before closing the connection.
299 * A graceful disconnect means that we can be sure that we successfully sent
300 * all the outstanding data to the other end, or get an error reported.
301 * However, it also means we have to wait for all the data to reach the
302 * other side and for it to acknowledge this by closing the socket, which may
303 * take a while. For this reason it is disabled by default.
308 g_tcp_connection_set_graceful_disconnect (GTcpConnection *connection,
309 gboolean graceful_disconnect)
311 graceful_disconnect = !!graceful_disconnect;
312 if (graceful_disconnect != connection->priv->graceful_disconnect)
314 connection->priv->graceful_disconnect = graceful_disconnect;
315 g_object_notify (G_OBJECT (connection), "graceful-disconnect");
320 * g_tcp_connection_get_graceful_disconnect:
321 * @connection: a #GTcpConnection
323 * Checks if graceful disconnects are used. See
324 * g_tcp_connection_set_graceful_disconnect().
326 * Returns: %TRUE if graceful disconnect is used on close, %FALSE otherwise
331 g_tcp_connection_get_graceful_disconnect (GTcpConnection *connection)
333 return connection->priv->graceful_disconnect;