From 16bafb479995e69417b86b5b26682ec7c2ceb5a0 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Sat, 14 Aug 2010 15:04:24 -0400 Subject: [PATCH] GSocketClient: add a timeout property GSocket has a timeout flag now, but when using GSocketClient there was no way to set the timeout until after connecting (or failing). Fix that by adding a timeout property to GSocketClient. --- gio/gsocketclient.c | 69 ++++++++++++++++++++++++++++++++++++++++++- gio/gsocketclient.h | 3 ++ gio/tests/send-data.c | 5 ++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/gio/gsocketclient.c b/gio/gsocketclient.c index d0266ff09..37b0b59af 100644 --- a/gio/gsocketclient.c +++ b/gio/gsocketclient.c @@ -70,7 +70,8 @@ enum PROP_FAMILY, PROP_TYPE, PROP_PROTOCOL, - PROP_LOCAL_ADDRESS + PROP_LOCAL_ADDRESS, + PROP_TIMEOUT }; struct _GSocketClientPrivate @@ -79,6 +80,7 @@ struct _GSocketClientPrivate GSocketType type; GSocketProtocol protocol; GSocketAddress *local_address; + guint timeout; }; static GSocket * @@ -115,6 +117,9 @@ create_socket (GSocketClient *client, } } + if (client->priv->timeout) + g_socket_set_timeout (socket, client->priv->timeout); + return socket; } @@ -181,6 +186,10 @@ g_socket_client_get_property (GObject *object, g_value_set_object (value, client->priv->local_address); break; + case PROP_TIMEOUT: + g_value_set_uint (value, client->priv->timeout); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); } @@ -212,6 +221,10 @@ g_socket_client_set_property (GObject *object, g_socket_client_set_local_address (client, g_value_get_object (value)); break; + case PROP_TIMEOUT: + g_socket_client_set_timeout (client, g_value_get_uint (value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); } @@ -396,6 +409,50 @@ g_socket_client_set_local_address (GSocketClient *client, g_object_notify (G_OBJECT (client), "local-address"); } +/** + * g_socket_client_get_timeout: + * @client: a #GSocketClient + * + * Gets the I/O timeout time for sockets created by @client. + * + * See g_socket_client_set_timeout() for details. + * + * Returns: the timeout in seconds + * + * Since: 2.26 + */ +guint +g_socket_client_get_timeout (GSocketClient *client) +{ + return client->priv->timeout; +} + + +/** + * g_socket_client_set_timeout: + * @client: a #GSocketClient. + * @timeout: the timeout + * + * Sets the I/O timeout for sockets created by @client. @timeout is a + * time in seconds, or 0 for no timeout (the default). + * + * The timeout value affects the initial connection attempt as well, + * so setting this may cause calls to g_socket_client_connect(), etc, + * to fail with %G_IO_ERROR_TIMED_OUT. + * + * Since: 2.26 + */ +void +g_socket_client_set_timeout (GSocketClient *client, + guint timeout) +{ + if (client->priv->timeout == timeout) + return; + + client->priv->timeout = timeout; + g_object_notify (G_OBJECT (client), "timeout"); +} + static void g_socket_client_class_init (GSocketClientClass *class) { @@ -445,6 +502,16 @@ g_socket_client_class_init (GSocketClientClass *class) G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, PROP_TIMEOUT, + g_param_spec_uint ("timeout", + P_("Socket timeout"), + P_("The I/O timeout for sockets, or 0 for none"), + 0, G_MAXUINT, 0, + G_PARAM_CONSTRUCT | + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + } /** diff --git a/gio/gsocketclient.h b/gio/gsocketclient.h index 05766148e..df9f5a2d2 100644 --- a/gio/gsocketclient.h +++ b/gio/gsocketclient.h @@ -82,6 +82,9 @@ void g_socket_client_set_protocol (GSocket GSocketAddress *g_socket_client_get_local_address (GSocketClient *client); void g_socket_client_set_local_address (GSocketClient *client, GSocketAddress *address); +guint g_socket_client_get_timeout (GSocketClient *client); +void g_socket_client_set_timeout (GSocketClient *client, + guint timeout); GSocketConnection * g_socket_client_connect (GSocketClient *client, GSocketConnectable *connectable, diff --git a/gio/tests/send-data.c b/gio/tests/send-data.c index a2592ceef..189aed27d 100644 --- a/gio/tests/send-data.c +++ b/gio/tests/send-data.c @@ -5,6 +5,7 @@ GMainLoop *loop; int cancel_timeout = 0; +int io_timeout = 0; gboolean async = FALSE; gboolean graceful = FALSE; static GOptionEntry cmd_entries[] = { @@ -14,6 +15,8 @@ static GOptionEntry cmd_entries[] = { "Use async ops", NULL}, {"graceful-disconnect", 'g', 0, G_OPTION_ARG_NONE, &graceful, "Use graceful disconnect", NULL}, + {"timeout", 't', 0, G_OPTION_ARG_INT, &io_timeout, + "Time out socket I/O after the specified number of seconds", NULL}, {NULL} }; @@ -97,6 +100,8 @@ main (int argc, char *argv[]) } client = g_socket_client_new (); + if (io_timeout) + g_socket_client_set_timeout (client, io_timeout); if (async) {