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.
This commit is contained in:
Dan Winship 2010-08-14 15:04:24 -04:00
parent 28a4fff7ec
commit 16bafb4799
3 changed files with 76 additions and 1 deletions

View File

@ -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));
}
/**

View File

@ -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,

View File

@ -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)
{