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
22 * It is currently empty; it offers no additional functionality
23 * over its base class.
25 * Eventually, some TCP-specific socket stuff will be added.
31 #include "gtcpconnection.h"
32 #include "gasyncresult.h"
33 #include "gsimpleasyncresult.h"
34 #include "giostream.h"
38 G_DEFINE_TYPE_WITH_CODE (GTcpConnection, g_tcp_connection,
39 G_TYPE_SOCKET_CONNECTION,
40 g_socket_connection_factory_register_type (g_define_type_id,
43 G_SOCKET_PROTOCOL_DEFAULT);
44 g_socket_connection_factory_register_type (g_define_type_id,
47 G_SOCKET_PROTOCOL_DEFAULT);
48 g_socket_connection_factory_register_type (g_define_type_id,
51 G_SOCKET_PROTOCOL_TCP);
52 g_socket_connection_factory_register_type (g_define_type_id,
55 G_SOCKET_PROTOCOL_TCP);
58 static gboolean g_tcp_connection_close (GIOStream *stream,
59 GCancellable *cancellable,
61 static void g_tcp_connection_close_async (GIOStream *stream,
63 GCancellable *cancellable,
64 GAsyncReadyCallback callback,
67 struct _GTcpConnectionPrivate
69 guint graceful_disconnect : 1;
76 PROP_GRACEFUL_DISCONNECT
80 g_tcp_connection_init (GTcpConnection *connection)
82 connection->priv = G_TYPE_INSTANCE_GET_PRIVATE (connection,
83 G_TYPE_TCP_CONNECTION,
84 GTcpConnectionPrivate);
85 connection->priv->graceful_disconnect = FALSE;
89 g_tcp_connection_get_property (GObject *object,
94 GTcpConnection *connection = G_TCP_CONNECTION (object);
98 case PROP_GRACEFUL_DISCONNECT:
99 g_value_set_boolean (value, connection->priv->graceful_disconnect);
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 g_tcp_connection_set_property (GObject *object,
113 GTcpConnection *connection = G_TCP_CONNECTION (object);
117 case PROP_GRACEFUL_DISCONNECT:
118 g_tcp_connection_set_graceful_disconnect (connection,
119 g_value_get_boolean (value));
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 g_tcp_connection_class_init (GTcpConnectionClass *class)
130 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
131 GIOStreamClass *stream_class = G_IO_STREAM_CLASS (class);
133 g_type_class_add_private (class, sizeof (GTcpConnectionPrivate));
135 gobject_class->set_property = g_tcp_connection_set_property;
136 gobject_class->get_property = g_tcp_connection_get_property;
138 stream_class->close_fn = g_tcp_connection_close;
139 stream_class->close_async = g_tcp_connection_close_async;
141 g_object_class_install_property (gobject_class, PROP_GRACEFUL_DISCONNECT,
142 g_param_spec_boolean ("graceful-disconnect",
143 P_("Graceful Disconnect"),
144 P_("Whether or not close does a graceful disconnect"),
146 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 g_tcp_connection_close (GIOStream *stream,
152 GCancellable *cancellable,
155 GTcpConnection *connection = G_TCP_CONNECTION (stream);
162 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
165 if (connection->priv->graceful_disconnect &&
166 !g_cancellable_is_cancelled (cancellable) /* Cancelled -> close fast */)
168 if (!g_socket_shutdown (socket, FALSE, TRUE, error))
170 error = NULL; /* Ignore further errors */
178 ret = g_socket_receive (socket, buffer, sizeof (buffer),
179 cancellable, &my_error);
182 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
183 g_error_free (my_error);
187 g_propagate_error (error, my_error);
198 return G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
199 ->close_fn (stream, cancellable, error) && !had_error;
203 GSimpleAsyncResult *res;
204 GCancellable *cancellable;
208 close_async_data_free (CloseAsyncData *data)
210 g_object_unref (data->res);
211 if (data->cancellable)
212 g_object_unref (data->cancellable);
217 async_close_finish (CloseAsyncData *data,
219 gboolean in_mainloop)
221 GIOStreamClass *parent = G_IO_STREAM_CLASS (g_tcp_connection_parent_class);
225 stream = G_IO_STREAM (g_async_result_get_source_object (G_ASYNC_RESULT (data->res)));
227 /* Doesn't block, ignore error */
230 parent->close_fn (stream, data->cancellable, NULL);
231 g_simple_async_result_set_from_error (data->res, error);
236 parent->close_fn (stream, data->cancellable, &my_error);
239 g_simple_async_result_set_from_error (data->res, my_error);
240 g_error_free (my_error);
245 g_simple_async_result_complete (data->res);
247 g_simple_async_result_complete_in_idle (data->res);
251 close_read_ready (GSocket *socket,
252 GIOCondition condition,
253 CloseAsyncData *data)
255 GError *error = NULL;
259 ret = g_socket_receive (socket, buffer, sizeof (buffer),
260 data->cancellable, &error);
263 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
264 g_error_free (error);
267 async_close_finish (data, error, TRUE);
268 g_error_free (error);
275 async_close_finish (data, NULL, TRUE);
284 g_tcp_connection_close_async (GIOStream *stream,
286 GCancellable *cancellable,
287 GAsyncReadyCallback callback,
290 GTcpConnection *connection = G_TCP_CONNECTION (stream);
291 CloseAsyncData *data;
296 if (connection->priv->graceful_disconnect &&
297 !g_cancellable_is_cancelled (cancellable) /* Cancelled -> close fast */)
299 data = g_new (CloseAsyncData, 1);
301 g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
302 g_tcp_connection_close_async);
304 data->cancellable = g_object_ref (cancellable);
306 data->cancellable = NULL;
308 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
311 if (!g_socket_shutdown (socket, FALSE, TRUE, &error))
313 async_close_finish (data, error, FALSE);
314 g_error_free (error);
315 close_async_data_free (data);
319 source = g_socket_create_source (socket, G_IO_IN, cancellable);
320 g_source_set_callback (source,
321 (GSourceFunc) close_read_ready,
322 data, (GDestroyNotify)close_async_data_free);
323 g_source_attach (source, g_main_context_get_thread_default ());
324 g_source_unref (source);
329 G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
330 ->close_async (stream, io_priority, cancellable, callback, user_data);
334 * g_tcp_connection_set_graceful_disconnect:
335 * @connection: a #GTcpConnection
336 * @graceful_disconnect: Whether to do graceful disconnects or not
338 * This enabled graceful disconnects on close. A graceful disconnect
339 * means that we signal the recieving end that the connection is terminated
340 * and wait for it to close the connection before closing the connection.
342 * A graceful disconnect means that we can be sure that we successfully sent
343 * all the outstanding data to the other end, or get an error reported.
344 * However, it also means we have to wait for all the data to reach the
345 * other side and for it to acknowledge this by closing the socket, which may
346 * take a while. For this reason it is disabled by default.
351 g_tcp_connection_set_graceful_disconnect (GTcpConnection *connection,
352 gboolean graceful_disconnect)
354 graceful_disconnect = !!graceful_disconnect;
355 if (graceful_disconnect != connection->priv->graceful_disconnect)
357 connection->priv->graceful_disconnect = graceful_disconnect;
358 g_object_notify (G_OBJECT (connection), "graceful-disconnect");
363 * g_tcp_connection_get_graceful_disconnect:
364 * @connection: a #GTcpConnection
366 * Checks if graceful disconnects are used. See
367 * g_tcp_connection_set_graceful_disconnect().
369 * Returns: %TRUE if graceful disconnect is used on close, %FALSE otherwise
374 g_tcp_connection_get_graceful_disconnect (GTcpConnection *connection)
376 return connection->priv->graceful_disconnect;