[gnio/connection-factory] Add connect to host helper functions



commit 8dad450f19b63d18c09ffa9db2f6ef6f2552e353
Author: Alexander Larsson <alexl redhat com>
Date:   Fri May 8 21:24:44 2009 +0200

    Add connect to host helper functions
    
    Connecting to an hostname/ip with a port is the only really often
    used helper, so push it to g_socket_client.
---
 gio/gsocketclient.c |   79 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gio/gsocketclient.h |   11 +++++++
 2 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/gio/gsocketclient.c b/gio/gsocketclient.c
index 4cef2d0..9601ab4 100644
--- a/gio/gsocketclient.c
+++ b/gio/gsocketclient.c
@@ -29,6 +29,7 @@
 #include <gio/gsimpleasyncresult.h>
 #include <gio/gcancellable.h>
 #include <gio/gioerror.h>
+#include <gio/gnetworkaddress.h>
 #include "gsocket.h"
 
 G_DEFINE_TYPE (GSocketClient, g_socket_client, G_TYPE_OBJECT);
@@ -274,6 +275,54 @@ g_socket_client_connect (GSocketClient       *client,
   return connection;
 }
 
+/**
+ * g_socket_client_connect_to_host:
+ * @client: a #GTcpClient
+ * @hostname: the name of the host to connect to
+ * @port: the port to connect to
+ * @cancellable: a #GCancellable, or %NULL
+ * @error: a pointer to a #GError, or %NULL
+ * @returns: a #GSocketConnection if successful, or %NULL on error
+ *
+ * This is a helper function for g_socket_client_connect().
+ *
+ * Attempts to create a TCP connection to the named host.
+ *
+ * @host may be in any of a number of recognised formats: an IPv6
+ * address, an IPv4 address, or a domain name (in which case a DNS
+ * lookup is performed).
+ *
+ * In the case that an IP address is given, a single connection
+ * attempt is made.  In the case that a name is given, multiple
+ * connection attempts may be made, in turn and according to the
+ * number of address records in DNS, until a connection succeeds.
+ *
+ * Upon a successful connection, a new #GSocketConnection is constructed
+ * and returned.  The caller owns this new object and must drop their
+ * reference to it when finished with it.
+ *
+ * In the event of any failure (DNS error, service not found, no hosts
+ * connectable) %NULL is returned and @error (if non-%NULL) is set
+ * accordingly.
+ **/
+GSocketConnection *
+g_socket_client_connect_to_host (GSocketClient        *client,
+				 const char           *hostname,
+				 int                   port,
+				 GCancellable         *cancellable,
+				 GError              **error)
+{
+  GSocketConnectable *connectable;
+  GSocketConnection *connection;
+
+  connectable = g_network_address_new (hostname, port);
+  connection = g_socket_client_connect (client, connectable,
+					cancellable, error);
+  g_object_unref (connectable);
+
+  return connection;
+}
+
 typedef struct
 {
   GSimpleAsyncResult *result;
@@ -465,6 +514,8 @@ g_socket_client_connect_async (GSocketClient       *client,
 {
   GSocketClientAsyncConnectData *data;
 
+  g_return_if_fail (G_IS_SOCKET_CLIENT (client));
+
   data = g_slice_new (GSocketClientAsyncConnectData);
 
   data->result = g_simple_async_result_new (G_OBJECT (client),
@@ -483,6 +534,34 @@ g_socket_client_connect_async (GSocketClient       *client,
 					  data);
 }
 
+/**
+ * g_socket_client_connect_to_host_async:
+ * @client: a #GTcpClient
+ * @hostname: the name of the host to connect to
+ * @port: the port to connect to
+ * @cancellable: a #GCancellable, or %NULL
+ * @callback: a #GAsyncReadyCallback
+ * @user_data: user data for the callback
+ *
+ * This is the asynchronous version of g_socket_client_connect_to_host().
+ **/
+void
+g_socket_client_connect_to_host_async (GSocketClient        *client,
+				       const char           *hostname,
+				       int                   port,
+				       GCancellable         *cancellable,
+				       GAsyncReadyCallback   callback,
+				       gpointer              user_data)
+{
+  GSocketConnectable *connectable;
+
+  connectable = g_network_address_new (hostname, port);
+  g_socket_client_connect_async (client,
+				 connectable, cancellable,
+				 callback, user_data);
+  g_object_unref (connectable);
+}
+
 GSocketConnection *
 g_socket_client_connect_finish (GSocketClient  *client,
                                 GAsyncResult   *result,
diff --git a/gio/gsocketclient.h b/gio/gsocketclient.h
index d2c6b70..5549db4 100644
--- a/gio/gsocketclient.h
+++ b/gio/gsocketclient.h
@@ -60,11 +60,22 @@ GSocketConnection *     g_socket_client_connect                         (GSocket
                                                                          GSocketConnectable   *connectable,
                                                                          GCancellable         *cancellable,
                                                                          GError              **error);
+GSocketConnection *     g_socket_client_connect_to_host                 (GSocketClient        *client,
+									 const char           *hostname,
+									 int                   port,
+                                                                         GCancellable         *cancellable,
+                                                                         GError              **error);
 void                    g_socket_client_connect_async                   (GSocketClient        *client,
                                                                          GSocketConnectable   *connectable,
                                                                          GCancellable         *cancellable,
                                                                          GAsyncReadyCallback   callback,
                                                                          gpointer              user_data);
+void                    g_socket_client_connect_to_host_async           (GSocketClient        *client,
+									 const char           *hostname,
+									 int                   port,
+                                                                         GCancellable         *cancellable,
+                                                                         GAsyncReadyCallback   callback,
+                                                                         gpointer              user_data);
 GSocketConnection *     g_socket_client_connect_finish                  (GSocketClient        *client,
                                                                          GAsyncResult         *result,
                                                                          GError              **error);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]