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 */
178 if (!g_socket_condition_wait (socket,
179 G_IO_IN, cancellable, error))
187 ret = g_socket_receive (socket, buffer, sizeof (buffer),
191 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
192 g_error_free (my_error);
196 g_propagate_error (error, my_error);
207 return G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
208 ->close_fn (stream, cancellable, error) && !had_error;
212 GSimpleAsyncResult *res;
213 GCancellable *cancellable;
217 close_async_data_free (CloseAsyncData *data)
219 g_object_unref (data->res);
220 if (data->cancellable)
221 g_object_unref (data->cancellable);
226 async_close_finish (CloseAsyncData *data,
228 gboolean in_mainloop)
230 GIOStreamClass *parent = G_IO_STREAM_CLASS (g_tcp_connection_parent_class);
234 stream = G_IO_STREAM (g_async_result_get_source_object (G_ASYNC_RESULT (data->res)));
236 /* Doesn't block, ignore error */
239 parent->close_fn (stream, data->cancellable, NULL);
240 g_simple_async_result_set_from_error (data->res, error);
245 parent->close_fn (stream, data->cancellable, &my_error);
248 g_simple_async_result_set_from_error (data->res, my_error);
249 g_error_free (my_error);
254 g_simple_async_result_complete (data->res);
256 g_simple_async_result_complete_in_idle (data->res);
260 close_read_ready (GSocket *socket,
261 GIOCondition condition,
262 CloseAsyncData *data)
264 GError *error = NULL;
268 if (g_cancellable_set_error_if_cancelled (data->cancellable,
271 async_close_finish (data, error, TRUE);
272 g_error_free (error);
276 ret = g_socket_receive (socket, buffer, sizeof (buffer), &error);
279 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
280 g_error_free (error);
283 async_close_finish (data, error, TRUE);
284 g_error_free (error);
291 async_close_finish (data, NULL, TRUE);
300 g_tcp_connection_close_async (GIOStream *stream,
302 GCancellable *cancellable,
303 GAsyncReadyCallback callback,
306 GTcpConnection *connection = G_TCP_CONNECTION (stream);
307 CloseAsyncData *data;
312 if (connection->priv->graceful_disconnect &&
313 !g_cancellable_is_cancelled (cancellable) /* Cancelled -> close fast */)
315 data = g_new (CloseAsyncData, 1);
317 g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
318 g_tcp_connection_close_async);
320 data->cancellable = g_object_ref (cancellable);
322 data->cancellable = NULL;
324 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
327 if (!g_socket_shutdown (socket, FALSE, TRUE, &error))
329 async_close_finish (data, error, FALSE);
330 g_error_free (error);
331 close_async_data_free (data);
335 source = g_socket_create_source (socket, G_IO_IN, cancellable);
336 g_source_set_callback (source,
337 (GSourceFunc) close_read_ready,
338 data, (GDestroyNotify)close_async_data_free);
339 g_source_attach (source, NULL);
340 g_source_unref (source);
346 return G_IO_STREAM_CLASS (g_tcp_connection_parent_class)
347 ->close_async (stream, io_priority, cancellable, callback, user_data);
353 * g_tcp_connection_set_graceful_disconnect:
354 * @connection: a #GTcpConnection
355 * @graceful_disconnect: Whether to do graceful disconnects or not
357 * This enabled graceful disconnects on close. A graceful disconnect
358 * means that we signal the recieving end that the connection is terminated
359 * and wait for it to close the connection before closing the connection.
361 * A graceful disconnect means that we can be sure that we successfully sent
362 * all the outstanding data to the other end, or get an error reported.
363 * However, it also means we have to wait for all the data to reach the
364 * other side and for it to acknowledge this by closing the socket, which may
365 * take a while. For this reason it is disabled by default.
370 g_tcp_connection_set_graceful_disconnect (GTcpConnection *connection,
371 gboolean graceful_disconnect)
373 graceful_disconnect = !!graceful_disconnect;
374 if (graceful_disconnect != connection->priv->graceful_disconnect)
376 connection->priv->graceful_disconnect = graceful_disconnect;
377 g_object_notify (G_OBJECT (connection), "graceful-disconnect");
382 * g_tcp_connection_get_graceful_disconnect:
383 * @connection: a #GTcpConnection
385 * Checks if graceful disconnects are used. See
386 * g_tcp_connection_set_graceful_disconnect().
388 * Returns: %TRUE if graceful disconnect is used on close, %FALSE otherwise
393 g_tcp_connection_get_graceful_disconnect (GTcpConnection *connection)
395 return connection->priv->graceful_disconnect;
399 #define __G_TCP_CONNECTION_C__
400 #include "gioaliasdef.c"