1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008, 2009 codethink
4 * Copyright © 2009 Red Hat, Inc
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Ryan Lortie <desrt@desrt.ca>
22 * Alexander Larsson <alexl@redhat.com>
26 #include "gsocketclient.h"
31 #include <gio/gioenumtypes.h>
32 #include <gio/gsocketaddressenumerator.h>
33 #include <gio/gsocketconnectable.h>
34 #include <gio/gsocketconnection.h>
35 #include <gio/gproxyaddressenumerator.h>
36 #include <gio/gproxyaddress.h>
37 #include <gio/gproxyconnection.h>
38 #include <gio/gsimpleasyncresult.h>
39 #include <gio/gcancellable.h>
40 #include <gio/gioerror.h>
41 #include <gio/gsocket.h>
42 #include <gio/gnetworkaddress.h>
43 #include <gio/gnetworkservice.h>
44 #include <gio/gproxy.h>
45 #include <gio/gsocketaddress.h>
46 #include <gio/gtcpconnection.h>
51 * SECTION:gsocketclient
52 * @short_description: Helper for connecting to a network service
54 * @see_also: #GSocketConnection, #GSocketListener
56 * #GSocketClient is a high-level utility class for connecting to a
57 * network host using a connection oriented socket type.
59 * You create a #GSocketClient object, set any options you want, then
60 * call a sync or async connect operation, which returns a #GSocketConnection
61 * subclass on success.
63 * The type of the #GSocketConnection object returned depends on the type of
64 * the underlying socket that is in use. For instance, for a TCP/IP connection
65 * it will be a #GTcpConnection.
71 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
84 struct _GSocketClientPrivate
88 GSocketProtocol protocol;
89 GSocketAddress *local_address;
91 gboolean enable_proxy;
95 create_socket (GSocketClient *client,
96 GSocketAddress *dest_address,
102 family = client->priv->family;
103 if (family == G_SOCKET_FAMILY_INVALID &&
104 client->priv->local_address != NULL)
105 family = g_socket_address_get_family (client->priv->local_address);
106 if (family == G_SOCKET_FAMILY_INVALID)
107 family = g_socket_address_get_family (dest_address);
109 socket = g_socket_new (family,
111 client->priv->protocol,
116 if (client->priv->local_address)
118 if (!g_socket_bind (socket,
119 client->priv->local_address,
123 g_object_unref (socket);
128 if (client->priv->timeout)
129 g_socket_set_timeout (socket, client->priv->timeout);
135 can_use_proxy (GSocketClient *client)
137 GSocketClientPrivate *priv = client->priv;
139 return priv->enable_proxy
140 && priv->type == G_SOCKET_TYPE_STREAM;
144 g_socket_client_init (GSocketClient *client)
146 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
147 G_TYPE_SOCKET_CLIENT,
148 GSocketClientPrivate);
149 client->priv->type = G_SOCKET_TYPE_STREAM;
153 * g_socket_client_new:
155 * Creates a new #GSocketClient with the default options.
157 * Returns: a #GSocketClient.
158 * Free the returned object with g_object_unref().
163 g_socket_client_new (void)
165 return g_object_new (G_TYPE_SOCKET_CLIENT, NULL);
169 g_socket_client_finalize (GObject *object)
171 GSocketClient *client = G_SOCKET_CLIENT (object);
173 if (client->priv->local_address)
174 g_object_unref (client->priv->local_address);
176 if (G_OBJECT_CLASS (g_socket_client_parent_class)->finalize)
177 (*G_OBJECT_CLASS (g_socket_client_parent_class)->finalize) (object);
181 g_socket_client_get_property (GObject *object,
186 GSocketClient *client = G_SOCKET_CLIENT (object);
191 g_value_set_enum (value, client->priv->family);
195 g_value_set_enum (value, client->priv->type);
199 g_value_set_enum (value, client->priv->protocol);
202 case PROP_LOCAL_ADDRESS:
203 g_value_set_object (value, client->priv->local_address);
207 g_value_set_uint (value, client->priv->timeout);
210 case PROP_ENABLE_PROXY:
211 g_value_set_boolean (value, client->priv->enable_proxy);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 g_socket_client_set_property (GObject *object,
225 GSocketClient *client = G_SOCKET_CLIENT (object);
230 g_socket_client_set_family (client, g_value_get_enum (value));
234 g_socket_client_set_socket_type (client, g_value_get_enum (value));
238 g_socket_client_set_protocol (client, g_value_get_enum (value));
241 case PROP_LOCAL_ADDRESS:
242 g_socket_client_set_local_address (client, g_value_get_object (value));
246 g_socket_client_set_timeout (client, g_value_get_uint (value));
249 case PROP_ENABLE_PROXY:
250 g_socket_client_set_enable_proxy (client, g_value_get_boolean (value));
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 * g_socket_client_get_family:
260 * @client: a #GSocketClient.
262 * Gets the socket family of the socket client.
264 * See g_socket_client_set_family() for details.
266 * Returns: a #GSocketFamily
271 g_socket_client_get_family (GSocketClient *client)
273 return client->priv->family;
277 * g_socket_client_set_family:
278 * @client: a #GSocketClient.
279 * @family: a #GSocketFamily
281 * Sets the socket family of the socket client.
282 * If this is set to something other than %G_SOCKET_FAMILY_INVALID
283 * then the sockets created by this object will be of the specified
286 * This might be useful for instance if you want to force the local
287 * connection to be an ipv4 socket, even though the address might
288 * be an ipv6 mapped to ipv4 address.
293 g_socket_client_set_family (GSocketClient *client,
294 GSocketFamily family)
296 if (client->priv->family == family)
299 client->priv->family = family;
300 g_object_notify (G_OBJECT (client), "family");
304 * g_socket_client_get_socket_type:
305 * @client: a #GSocketClient.
307 * Gets the socket type of the socket client.
309 * See g_socket_client_set_socket_type() for details.
311 * Returns: a #GSocketFamily
316 g_socket_client_get_socket_type (GSocketClient *client)
318 return client->priv->type;
322 * g_socket_client_set_socket_type:
323 * @client: a #GSocketClient.
324 * @type: a #GSocketType
326 * Sets the socket type of the socket client.
327 * The sockets created by this object will be of the specified
330 * It doesn't make sense to specify a type of %G_SOCKET_TYPE_DATAGRAM,
331 * as GSocketClient is used for connection oriented services.
336 g_socket_client_set_socket_type (GSocketClient *client,
339 if (client->priv->type == type)
342 client->priv->type = type;
343 g_object_notify (G_OBJECT (client), "type");
347 * g_socket_client_get_protocol:
348 * @client: a #GSocketClient
350 * Gets the protocol name type of the socket client.
352 * See g_socket_client_set_protocol() for details.
354 * Returns: a #GSocketProtocol
359 g_socket_client_get_protocol (GSocketClient *client)
361 return client->priv->protocol;
365 * g_socket_client_set_protocol:
366 * @client: a #GSocketClient.
367 * @protocol: a #GSocketProtocol
369 * Sets the protocol of the socket client.
370 * The sockets created by this object will use of the specified
373 * If @protocol is %0 that means to use the default
374 * protocol for the socket family and type.
379 g_socket_client_set_protocol (GSocketClient *client,
380 GSocketProtocol protocol)
382 if (client->priv->protocol == protocol)
385 client->priv->protocol = protocol;
386 g_object_notify (G_OBJECT (client), "protocol");
390 * g_socket_client_get_local_address:
391 * @client: a #GSocketClient.
393 * Gets the local address of the socket client.
395 * See g_socket_client_set_local_address() for details.
397 * Returns: (transfer none): a #GSocketAddres or %NULL. don't free
402 g_socket_client_get_local_address (GSocketClient *client)
404 return client->priv->local_address;
408 * g_socket_client_set_local_address:
409 * @client: a #GSocketClient.
410 * @address: a #GSocketAddress, or %NULL
412 * Sets the local address of the socket client.
413 * The sockets created by this object will bound to the
414 * specified address (if not %NULL) before connecting.
416 * This is useful if you want to ensure the the local
417 * side of the connection is on a specific port, or on
418 * a specific interface.
423 g_socket_client_set_local_address (GSocketClient *client,
424 GSocketAddress *address)
427 g_object_ref (address);
429 if (client->priv->local_address)
431 g_object_unref (client->priv->local_address);
433 client->priv->local_address = address;
434 g_object_notify (G_OBJECT (client), "local-address");
438 * g_socket_client_get_timeout:
439 * @client: a #GSocketClient
441 * Gets the I/O timeout time for sockets created by @client.
443 * See g_socket_client_set_timeout() for details.
445 * Returns: the timeout in seconds
450 g_socket_client_get_timeout (GSocketClient *client)
452 return client->priv->timeout;
457 * g_socket_client_set_timeout:
458 * @client: a #GSocketClient.
459 * @timeout: the timeout
461 * Sets the I/O timeout for sockets created by @client. @timeout is a
462 * time in seconds, or 0 for no timeout (the default).
464 * The timeout value affects the initial connection attempt as well,
465 * so setting this may cause calls to g_socket_client_connect(), etc,
466 * to fail with %G_IO_ERROR_TIMED_OUT.
471 g_socket_client_set_timeout (GSocketClient *client,
474 if (client->priv->timeout == timeout)
477 client->priv->timeout = timeout;
478 g_object_notify (G_OBJECT (client), "timeout");
482 * g_socket_client_get_enable_proxy:
483 * @client: a #GSocketClient.
485 * Gets the proxy enable state; see g_socket_client_set_enable_proxy()
487 * Returns: whether proxying is enabled
492 g_socket_client_get_enable_proxy (GSocketClient *client)
494 return client->priv->enable_proxy;
498 * g_socket_client_set_enable_proxy:
499 * @client: a #GSocketClient.
500 * @enable: whether to enable proxies
502 * Sets whether or not @client attempts to make connections via a
503 * proxy server. When enabled (the default), #GSocketClient will use a
504 * #GProxyResolver to determine if a proxy protocol such as SOCKS is
505 * needed, and automatically do the necessary proxy negotiation.
510 g_socket_client_set_enable_proxy (GSocketClient *client,
514 if (client->priv->enable_proxy == enable)
517 client->priv->enable_proxy = enable;
518 g_object_notify (G_OBJECT (client), "enable-proxy");
522 g_socket_client_class_init (GSocketClientClass *class)
524 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
526 g_type_class_add_private (class, sizeof (GSocketClientPrivate));
528 gobject_class->finalize = g_socket_client_finalize;
529 gobject_class->set_property = g_socket_client_set_property;
530 gobject_class->get_property = g_socket_client_get_property;
532 g_object_class_install_property (gobject_class, PROP_FAMILY,
533 g_param_spec_enum ("family",
535 P_("The sockets address family to use for socket construction"),
536 G_TYPE_SOCKET_FAMILY,
537 G_SOCKET_FAMILY_INVALID,
540 G_PARAM_STATIC_STRINGS));
542 g_object_class_install_property (gobject_class, PROP_TYPE,
543 g_param_spec_enum ("type",
545 P_("The sockets type to use for socket construction"),
547 G_SOCKET_TYPE_STREAM,
550 G_PARAM_STATIC_STRINGS));
552 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
553 g_param_spec_enum ("protocol",
554 P_("Socket protocol"),
555 P_("The protocol to use for socket construction, or 0 for default"),
556 G_TYPE_SOCKET_PROTOCOL,
557 G_SOCKET_PROTOCOL_DEFAULT,
560 G_PARAM_STATIC_STRINGS));
562 g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
563 g_param_spec_object ("local-address",
565 P_("The local address constructed sockets will be bound to"),
566 G_TYPE_SOCKET_ADDRESS,
569 G_PARAM_STATIC_STRINGS));
571 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
572 g_param_spec_uint ("timeout",
573 P_("Socket timeout"),
574 P_("The I/O timeout for sockets, or 0 for none"),
578 G_PARAM_STATIC_STRINGS));
580 g_object_class_install_property (gobject_class, PROP_ENABLE_PROXY,
581 g_param_spec_boolean ("enable-proxy",
583 P_("Enable proxy support"),
587 G_PARAM_STATIC_STRINGS));
592 * g_socket_client_connect:
593 * @client: a #GSocketClient.
594 * @connectable: a #GSocketConnectable specifying the remote address.
595 * @cancellable: optional #GCancellable object, %NULL to ignore.
596 * @error: #GError for error reporting, or %NULL to ignore.
598 * Tries to resolve the @connectable and make a network connection to it..
600 * Upon a successful connection, a new #GSocketConnection is constructed
601 * and returned. The caller owns this new object and must drop their
602 * reference to it when finished with it.
604 * The type of the #GSocketConnection object returned depends on the type of
605 * the underlying socket that is used. For instance, for a TCP/IP connection
606 * it will be a #GTcpConnection.
608 * The socket created will be the same family as the the address that the
609 * @connectable resolves to, unless family is set with g_socket_client_set_family()
610 * or indirectly via g_socket_client_set_local_address(). The socket type
611 * defaults to %G_SOCKET_TYPE_STREAM but can be set with
612 * g_socket_client_set_socket_type().
614 * If a local address is specified with g_socket_client_set_local_address() the
615 * socket will be bound to this address before connecting.
617 * Returns: a #GSocketConnection on success, %NULL on error.
622 g_socket_client_connect (GSocketClient *client,
623 GSocketConnectable *connectable,
624 GCancellable *cancellable,
627 GSocketConnection *connection = NULL;
628 GSocketAddressEnumerator *enumerator = NULL;
629 GError *last_error, *tmp_error;
633 if (can_use_proxy (client))
634 enumerator = g_socket_connectable_proxy_enumerate (connectable);
636 enumerator = g_socket_connectable_enumerate (connectable);
638 while (connection == NULL)
640 GSocketAddress *address = NULL;
643 if (g_cancellable_is_cancelled (cancellable))
645 g_clear_error (error);
646 g_cancellable_set_error_if_cancelled (cancellable, error);
651 address = g_socket_address_enumerator_next (enumerator, cancellable,
658 g_clear_error (&last_error);
659 g_propagate_error (error, tmp_error);
663 g_propagate_error (error, last_error);
666 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
667 _("Unknown error on connect"));
671 /* clear error from previous attempt */
672 g_clear_error (&last_error);
674 socket = create_socket (client, address, &last_error);
677 if (g_socket_connect (socket, address, cancellable, &last_error))
678 connection = g_socket_connection_factory_create_connection (socket);
680 g_object_unref (socket);
684 G_IS_PROXY_ADDRESS (address) &&
685 client->priv->enable_proxy)
687 GProxyAddress *proxy_addr = G_PROXY_ADDRESS (address);
688 const gchar *protocol;
691 protocol = g_proxy_address_get_protocol (proxy_addr);
692 proxy = g_proxy_get_default_for_protocol (protocol);
694 /* The connection should not be anything else then TCP Connection,
695 * but let's put a safety guard in case
697 if (!G_IS_TCP_CONNECTION (connection))
699 g_critical ("Trying to proxy over non-TCP connection, this is "
700 "most likely a bug in GLib IO library.");
702 g_set_error_literal (&last_error,
703 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
704 _("Trying to proxy over non-TCP connection is not supported."));
706 g_object_unref (connection);
711 GIOStream *io_stream;
712 GTcpConnection *old_connection = G_TCP_CONNECTION (connection);
714 io_stream = g_proxy_connect (proxy,
715 G_IO_STREAM (old_connection),
722 if (G_IS_SOCKET_CONNECTION (io_stream))
723 connection = G_SOCKET_CONNECTION (g_object_ref (io_stream));
725 connection = _g_proxy_connection_new (old_connection,
728 g_object_unref (io_stream);
735 g_object_unref (old_connection);
736 g_object_unref (proxy);
740 g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
741 _("Proxy protocol '%s' is not supported."),
743 g_object_unref (connection);
748 g_object_unref (address);
750 g_object_unref (enumerator);
756 * g_socket_client_connect_to_host:
757 * @client: a #SocketClient
758 * @host_and_port: the name and optionally port of the host to connect to
759 * @default_port: the default port to connect to
760 * @cancellable: a #GCancellable, or %NULL
761 * @error: a pointer to a #GError, or %NULL
763 * This is a helper function for g_socket_client_connect().
765 * Attempts to create a TCP connection to the named host.
767 * @host_and_port may be in any of a number of recognised formats: an IPv6
768 * address, an IPv4 address, or a domain name (in which case a DNS
769 * lookup is performed). Quoting with [] is supported for all address
770 * types. A port override may be specified in the usual way with a
771 * colon. Ports may be given as decimal numbers or symbolic names (in
772 * which case an /etc/services lookup is performed).
774 * If no port override is given in @host_and_port then @default_port will be
775 * used as the port number to connect to.
777 * In general, @host_and_port is expected to be provided by the user (allowing
778 * them to give the hostname, and a port overide if necessary) and
779 * @default_port is expected to be provided by the application.
781 * In the case that an IP address is given, a single connection
782 * attempt is made. In the case that a name is given, multiple
783 * connection attempts may be made, in turn and according to the
784 * number of address records in DNS, until a connection succeeds.
786 * Upon a successful connection, a new #GSocketConnection is constructed
787 * and returned. The caller owns this new object and must drop their
788 * reference to it when finished with it.
790 * In the event of any failure (DNS error, service not found, no hosts
791 * connectable) %NULL is returned and @error (if non-%NULL) is set
794 Returns: a #GSocketConnection on success, %NULL on error.
799 g_socket_client_connect_to_host (GSocketClient *client,
800 const gchar *host_and_port,
801 guint16 default_port,
802 GCancellable *cancellable,
805 GSocketConnectable *connectable;
806 GSocketConnection *connection;
808 connectable = g_network_address_parse (host_and_port, default_port, error);
809 if (connectable == NULL)
812 connection = g_socket_client_connect (client, connectable,
814 g_object_unref (connectable);
820 * g_socket_client_connect_to_service:
821 * @client: a #GSocketConnection
822 * @domain: a domain name
823 * @service: the name of the service to connect to
824 * @cancellable: a #GCancellable, or %NULL
825 * @error: a pointer to a #GError, or %NULL
826 * @returns: a #GSocketConnection if successful, or %NULL on error
828 * Attempts to create a TCP connection to a service.
830 * This call looks up the SRV record for @service at @domain for the
831 * "tcp" protocol. It then attempts to connect, in turn, to each of
832 * the hosts providing the service until either a connection succeeds
833 * or there are no hosts remaining.
835 * Upon a successful connection, a new #GSocketConnection is constructed
836 * and returned. The caller owns this new object and must drop their
837 * reference to it when finished with it.
839 * In the event of any failure (DNS error, service not found, no hosts
840 * connectable) %NULL is returned and @error (if non-%NULL) is set
844 g_socket_client_connect_to_service (GSocketClient *client,
846 const gchar *service,
847 GCancellable *cancellable,
850 GSocketConnectable *connectable;
851 GSocketConnection *connection;
853 connectable = g_network_service_new (service, "tcp", domain);
854 connection = g_socket_client_connect (client, connectable,
856 g_object_unref (connectable);
863 GSimpleAsyncResult *result;
864 GCancellable *cancellable;
865 GSocketClient *client;
867 GSocketAddressEnumerator *enumerator;
868 GProxyAddress *proxy_addr;
869 GSocket *current_socket;
870 GSocketConnection *connection;
873 } GSocketClientAsyncConnectData;
876 g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
878 if (data->last_error)
880 g_simple_async_result_set_from_error (data->result, data->last_error);
881 g_error_free (data->last_error);
885 g_assert (data->connection);
887 g_simple_async_result_set_op_res_gpointer (data->result,
892 g_simple_async_result_complete (data->result);
893 g_object_unref (data->result);
894 g_object_unref (data->enumerator);
895 if (data->cancellable)
896 g_object_unref (data->cancellable);
897 if (data->current_socket)
898 g_object_unref (data->current_socket);
899 if (data->proxy_addr)
900 g_object_unref (data->proxy_addr);
901 g_slice_free (GSocketClientAsyncConnectData, data);
906 g_socket_client_enumerator_callback (GObject *object,
907 GAsyncResult *result,
911 set_last_error (GSocketClientAsyncConnectData *data,
914 g_clear_error (&data->last_error);
915 data->last_error = error;
919 enumerator_next_async (GSocketClientAsyncConnectData *data)
921 g_socket_address_enumerator_next_async (data->enumerator,
923 g_socket_client_enumerator_callback,
928 g_socket_client_proxy_connect_callback (GObject *object,
929 GAsyncResult *result,
932 GSocketClientAsyncConnectData *data = user_data;
933 GIOStream *io_stream;
934 GTcpConnection *old_connection = G_TCP_CONNECTION (data->connection);
936 io_stream = g_proxy_connect_finish (G_PROXY (object),
942 if (G_IS_SOCKET_CONNECTION (io_stream))
943 data->connection = G_SOCKET_CONNECTION (g_object_ref (io_stream));
945 data->connection = _g_proxy_connection_new (old_connection,
947 g_object_unref (io_stream);
951 data->connection = NULL;
954 g_object_unref (old_connection);
956 g_socket_client_async_connect_complete (data);
960 g_socket_client_proxy_connect (GSocketClientAsyncConnectData *data)
963 const gchar *protocol = g_proxy_address_get_protocol (data->proxy_addr);
965 proxy = g_proxy_get_default_for_protocol (protocol);
967 /* The connection should not be anything else then TCP Connection,
968 * but let's put a safety guard in case
970 if (!G_IS_TCP_CONNECTION (data->connection))
972 g_critical ("Trying to proxy over non-TCP connection, this is "
973 "most likely a bug in GLib IO library.");
975 g_set_error_literal (&data->last_error,
976 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
977 _("Trying to proxy over non-TCP connection is not supported."));
979 g_object_unref (data->connection);
980 data->connection = NULL;
982 enumerator_next_async (data);
986 g_proxy_connect_async (proxy,
987 G_IO_STREAM (data->connection),
990 g_socket_client_proxy_connect_callback,
992 g_object_unref (proxy);
996 g_clear_error (&data->last_error);
998 g_set_error (&data->last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
999 _("Proxy protocol '%s' is not supported."),
1002 g_object_unref (data->connection);
1003 data->connection = NULL;
1005 enumerator_next_async (data);
1010 g_socket_client_socket_callback (GSocket *socket,
1011 GIOCondition condition,
1012 GSocketClientAsyncConnectData *data)
1014 GError *error = NULL;
1016 if (g_cancellable_is_cancelled (data->cancellable))
1018 /* Cancelled, return done with last error being cancelled */
1019 g_clear_error (&data->last_error);
1020 g_object_unref (data->current_socket);
1021 data->current_socket = NULL;
1022 g_cancellable_set_error_if_cancelled (data->cancellable,
1027 /* socket is ready for writing means connect done, did it succeed? */
1028 if (!g_socket_check_connect_result (data->current_socket, &error))
1030 set_last_error (data, error);
1031 g_object_unref (data->current_socket);
1032 data->current_socket = NULL;
1035 enumerator_next_async (data);
1041 g_socket_set_blocking (data->current_socket, TRUE);
1044 g_socket_connection_factory_create_connection (data->current_socket);
1045 g_object_unref (data->current_socket);
1046 data->current_socket = NULL;
1048 if (data->proxy_addr)
1049 g_socket_client_proxy_connect (data);
1051 g_socket_client_async_connect_complete (data);
1057 g_socket_client_enumerator_callback (GObject *object,
1058 GAsyncResult *result,
1061 GSocketClientAsyncConnectData *data = user_data;
1062 GSocketAddress *address = NULL;
1064 GError *tmp_error = NULL;
1066 if (g_cancellable_is_cancelled (data->cancellable))
1068 g_clear_error (&data->last_error);
1069 g_cancellable_set_error_if_cancelled (data->cancellable, &data->last_error);
1070 g_socket_client_async_connect_complete (data);
1074 address = g_socket_address_enumerator_next_finish (data->enumerator,
1075 result, &tmp_error);
1077 if (address == NULL)
1080 set_last_error (data, tmp_error);
1081 else if (data->last_error == NULL)
1082 g_set_error_literal (&data->last_error, G_IO_ERROR, G_IO_ERROR_FAILED,
1083 _("Unknown error on connect"));
1085 g_socket_client_async_connect_complete (data);
1089 if (G_IS_PROXY_ADDRESS (address) &&
1090 data->client->priv->enable_proxy)
1091 data->proxy_addr = g_object_ref (G_PROXY_ADDRESS (address));
1093 g_clear_error (&data->last_error);
1095 socket = create_socket (data->client, address, &data->last_error);
1098 g_socket_set_blocking (socket, FALSE);
1099 if (g_socket_connect (socket, address, data->cancellable, &tmp_error))
1101 data->current_socket = socket;
1102 g_socket_client_async_connect_complete (data);
1104 g_object_unref (address);
1107 else if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_PENDING))
1111 data->current_socket = socket;
1112 g_error_free (tmp_error);
1114 source = g_socket_create_source (socket, G_IO_OUT,
1116 g_source_set_callback (source,
1117 (GSourceFunc) g_socket_client_socket_callback,
1119 g_source_attach (source, g_main_context_get_thread_default ());
1120 g_source_unref (source);
1122 g_object_unref (address);
1127 data->last_error = tmp_error;
1128 g_object_unref (socket);
1132 g_object_unref (address);
1133 enumerator_next_async (data);
1137 * g_socket_client_connect_async:
1138 * @client: a #GTcpClient
1139 * @connectable: a #GSocketConnectable specifying the remote address.
1140 * @cancellable: a #GCancellable, or %NULL
1141 * @callback: a #GAsyncReadyCallback
1142 * @user_data: user data for the callback
1144 * This is the asynchronous version of g_socket_client_connect().
1146 * When the operation is finished @callback will be
1147 * called. You can then call g_socket_client_connect_finish() to get
1148 * the result of the operation.
1153 g_socket_client_connect_async (GSocketClient *client,
1154 GSocketConnectable *connectable,
1155 GCancellable *cancellable,
1156 GAsyncReadyCallback callback,
1159 GSocketClientAsyncConnectData *data;
1161 g_return_if_fail (G_IS_SOCKET_CLIENT (client));
1163 data = g_slice_new0 (GSocketClientAsyncConnectData);
1165 data->result = g_simple_async_result_new (G_OBJECT (client),
1166 callback, user_data,
1167 g_socket_client_connect_async);
1168 data->client = client;
1170 data->cancellable = g_object_ref (cancellable);
1172 if (can_use_proxy (client))
1173 data->enumerator = g_socket_connectable_proxy_enumerate (connectable);
1175 data->enumerator = g_socket_connectable_enumerate (connectable);
1177 enumerator_next_async (data);
1181 * g_socket_client_connect_to_host_async:
1182 * @client: a #GTcpClient
1183 * @host_and_port: the name and optionally the port of the host to connect to
1184 * @default_port: the default port to connect to
1185 * @cancellable: a #GCancellable, or %NULL
1186 * @callback: a #GAsyncReadyCallback
1187 * @user_data: user data for the callback
1189 * This is the asynchronous version of g_socket_client_connect_to_host().
1191 * When the operation is finished @callback will be
1192 * called. You can then call g_socket_client_connect_to_host_finish() to get
1193 * the result of the operation.
1198 g_socket_client_connect_to_host_async (GSocketClient *client,
1199 const gchar *host_and_port,
1200 guint16 default_port,
1201 GCancellable *cancellable,
1202 GAsyncReadyCallback callback,
1205 GSocketConnectable *connectable;
1209 connectable = g_network_address_parse (host_and_port, default_port,
1211 if (connectable == NULL)
1213 g_simple_async_report_gerror_in_idle (G_OBJECT (client),
1214 callback, user_data, error);
1215 g_error_free (error);
1219 g_socket_client_connect_async (client,
1220 connectable, cancellable,
1221 callback, user_data);
1222 g_object_unref (connectable);
1227 * g_socket_client_connect_to_service_async:
1228 * @client: a #GSocketClient
1229 * @domain: a domain name
1230 * @service: the name of the service to connect to
1231 * @cancellable: a #GCancellable, or %NULL
1232 * @callback: a #GAsyncReadyCallback
1233 * @user_data: user data for the callback
1235 * This is the asynchronous version of
1236 * g_socket_client_connect_to_service().
1241 g_socket_client_connect_to_service_async (GSocketClient *client,
1242 const gchar *domain,
1243 const gchar *service,
1244 GCancellable *cancellable,
1245 GAsyncReadyCallback callback,
1248 GSocketConnectable *connectable;
1250 connectable = g_network_service_new (service, "tcp", domain);
1251 g_socket_client_connect_async (client,
1252 connectable, cancellable,
1253 callback, user_data);
1254 g_object_unref (connectable);
1258 * g_socket_client_connect_finish:
1259 * @client: a #GSocketClient.
1260 * @result: a #GAsyncResult.
1261 * @error: a #GError location to store the error occuring, or %NULL to
1264 * Finishes an async connect operation. See g_socket_client_connect_async()
1266 * Returns: a #GSocketConnection on success, %NULL on error.
1271 g_socket_client_connect_finish (GSocketClient *client,
1272 GAsyncResult *result,
1275 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1277 if (g_simple_async_result_propagate_error (simple, error))
1280 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
1284 * g_socket_client_connect_to_host_finish:
1285 * @client: a #GSocketClient.
1286 * @result: a #GAsyncResult.
1287 * @error: a #GError location to store the error occuring, or %NULL to
1290 * Finishes an async connect operation. See g_socket_client_connect_to_host_async()
1292 * Returns: a #GSocketConnection on success, %NULL on error.
1297 g_socket_client_connect_to_host_finish (GSocketClient *client,
1298 GAsyncResult *result,
1301 return g_socket_client_connect_finish (client, result, error);
1305 * g_socket_client_connect_to_service_finish:
1306 * @client: a #GSocketClient.
1307 * @result: a #GAsyncResult.
1308 * @error: a #GError location to store the error occuring, or %NULL to
1311 * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
1313 * Returns: a #GSocketConnection on success, %NULL on error.
1318 g_socket_client_connect_to_service_finish (GSocketClient *client,
1319 GAsyncResult *result,
1322 return g_socket_client_connect_finish (client, result, error);