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"
39 G_DEFINE_TYPE_WITH_CODE (GTcpConnection, g_tcp_connection,
40 G_TYPE_SOCKET_CONNECTION,
41 g_socket_connection_factory_register_type (g_define_type_id,
44 G_SOCKET_PROTOCOL_DEFAULT);
45 g_socket_connection_factory_register_type (g_define_type_id,
48 G_SOCKET_PROTOCOL_DEFAULT);
49 g_socket_connection_factory_register_type (g_define_type_id,
52 G_SOCKET_PROTOCOL_TCP);
53 g_socket_connection_factory_register_type (g_define_type_id,
56 G_SOCKET_PROTOCOL_TCP);
59 static gboolean g_tcp_connection_close (GIOStream *stream,
60 GCancellable *cancellable,
62 static void g_tcp_connection_close_async (GIOStream *stream,
64 GCancellable *cancellable,
65 GAsyncReadyCallback callback,
68 struct _GTcpConnectionPrivate
70 guint graceful_disconnect : 1;
77 PROP_GRACEFUL_DISCONNECT
81 g_tcp_connection_init (GTcpConnection *connection)
83 connection->priv = G_TYPE_INSTANCE_GET_PRIVATE (connection,
84 G_TYPE_TCP_CONNECTION,
85 GTcpConnectionPrivate);
86 connection->priv->graceful_disconnect = FALSE;
90 g_tcp_connection_get_property (GObject *object,
95 GTcpConnection *connection = G_TCP_CONNECTION (object);
99 case PROP_GRACEFUL_DISCONNECT:
100 g_value_set_boolean (value, connection->priv->graceful_disconnect);
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 g_tcp_connection_set_property (GObject *object,
114 GTcpConnection *connection = G_TCP_CONNECTION (object);
118 case PROP_GRACEFUL_DISCONNECT:
119 g_tcp_connection_set_graceful_disconnect (connection,
120 g_value_get_boolean (value));
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 g_tcp_connection_class_init (GTcpConnectionClass *class)
131 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
132 GIOStreamClass *stream_class = G_IO_STREAM_CLASS (class);
134 g_type_class_add_private (class, sizeof (GTcpConnectionPrivate));
136 gobject_class->set_property = g_tcp_connection_set_property;
137 gobject_class->get_property = g_tcp_connection_get_property;
139 stream_class->close_fn = g_tcp_connection_close;
140 stream_class->close_async = g_tcp_connection_close_async;
142 g_object_class_install_property (gobject_class, PROP_GRACEFUL_DISCONNECT,
143 g_param_spec_boolean ("graceful-disconnect",
144 P_("Graceful Disconnect"),
145 P_("Whether or not close does a graceful disconnect"),
147 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 g_tcp_connection_close (GIOStream *stream,
153 GCancellable *cancellable,
156 GTcpConnection *connection = G_TCP_CONNECTION (stream);
163 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
166 if (connection->priv->graceful_disconnect &&
167 !g_cancellable_is_cancelled (cancellable) /* Cancelled -> close fast */)
169 if (!g_socket_shutdown (socket, FALSE, TRUE, error))
171 error = NULL; /* Ignore further errors */
179 ret = g_socket_receive (socket, buffer, sizeof (buffer),
180 cancellable, &my_error);
183 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
184 g_error_free (my_error);
188 g_propagate_error (error, my_error);
199 return G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
200 ->close_fn (stream, cancellable, error) && !had_error;
204 GSimpleAsyncResult *res;
205 GCancellable *cancellable;
209 close_async_data_free (CloseAsyncData *data)
211 g_object_unref (data->res);
212 if (data->cancellable)
213 g_object_unref (data->cancellable);
218 async_close_finish (CloseAsyncData *data,
220 gboolean in_mainloop)
222 GIOStreamClass *parent = G_IO_STREAM_CLASS (g_tcp_connection_parent_class);
226 stream = G_IO_STREAM (g_async_result_get_source_object (G_ASYNC_RESULT (data->res)));
228 /* Doesn't block, ignore error */
231 parent->close_fn (stream, data->cancellable, NULL);
232 g_simple_async_result_set_from_error (data->res, error);
237 parent->close_fn (stream, data->cancellable, &my_error);
240 g_simple_async_result_set_from_error (data->res, my_error);
241 g_error_free (my_error);
246 g_simple_async_result_complete (data->res);
248 g_simple_async_result_complete_in_idle (data->res);
252 close_read_ready (GSocket *socket,
253 GIOCondition condition,
254 CloseAsyncData *data)
256 GError *error = NULL;
260 ret = g_socket_receive (socket, buffer, sizeof (buffer),
261 data->cancellable, &error);
264 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
265 g_error_free (error);
268 async_close_finish (data, error, TRUE);
269 g_error_free (error);
276 async_close_finish (data, NULL, TRUE);
285 g_tcp_connection_close_async (GIOStream *stream,
287 GCancellable *cancellable,
288 GAsyncReadyCallback callback,
291 GTcpConnection *connection = G_TCP_CONNECTION (stream);
292 CloseAsyncData *data;
297 if (connection->priv->graceful_disconnect &&
298 !g_cancellable_is_cancelled (cancellable) /* Cancelled -> close fast */)
300 data = g_new (CloseAsyncData, 1);
302 g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
303 g_tcp_connection_close_async);
305 data->cancellable = g_object_ref (cancellable);
307 data->cancellable = NULL;
309 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
312 if (!g_socket_shutdown (socket, FALSE, TRUE, &error))
314 async_close_finish (data, error, FALSE);
315 g_error_free (error);
316 close_async_data_free (data);
320 source = g_socket_create_source (socket, G_IO_IN, cancellable);
321 g_source_set_callback (source,
322 (GSourceFunc) close_read_ready,
323 data, (GDestroyNotify)close_async_data_free);
324 g_source_attach (source, g_main_context_get_thread_default ());
325 g_source_unref (source);
330 G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
331 ->close_async (stream, io_priority, cancellable, callback, user_data);
335 * g_tcp_connection_set_graceful_disconnect:
336 * @connection: a #GTcpConnection
337 * @graceful_disconnect: Whether to do graceful disconnects or not
339 * This enabled graceful disconnects on close. A graceful disconnect
340 * means that we signal the recieving end that the connection is terminated
341 * and wait for it to close the connection before closing the connection.
343 * A graceful disconnect means that we can be sure that we successfully sent
344 * all the outstanding data to the other end, or get an error reported.
345 * However, it also means we have to wait for all the data to reach the
346 * other side and for it to acknowledge this by closing the socket, which may
347 * take a while. For this reason it is disabled by default.
352 g_tcp_connection_set_graceful_disconnect (GTcpConnection *connection,
353 gboolean graceful_disconnect)
355 graceful_disconnect = !!graceful_disconnect;
356 if (graceful_disconnect != connection->priv->graceful_disconnect)
358 connection->priv->graceful_disconnect = graceful_disconnect;
359 g_object_notify (G_OBJECT (connection), "graceful-disconnect");
364 * g_tcp_connection_get_graceful_disconnect:
365 * @connection: a #GTcpConnection
367 * Checks if graceful disconnects are used. See
368 * g_tcp_connection_set_graceful_disconnect().
370 * Returns: %TRUE if graceful disconnect is used on close, %FALSE otherwise
375 g_tcp_connection_get_graceful_disconnect (GTcpConnection *connection)
377 return connection->priv->graceful_disconnect;
381 #define __G_TCP_CONNECTION_C__
382 #include "gioaliasdef.c"