Add helper functions for connecting to service (#583061)
authorSjoerd Simons <sjoerd@luon.net>
Wed, 20 May 2009 10:41:50 +0000 (12:41 +0200)
committerAlexander Larsson <alexl@redhat.com>
Wed, 20 May 2009 10:41:50 +0000 (12:41 +0200)
docs/reference/gio/gio-sections.txt
gio/gio.symbols
gio/gsocketclient.c
gio/gsocketclient.h

index d882c01a7330a1030c5a99c6a98f8a5abfe8e572..9d9fe195456e0b8ca98303104b2e9e024e67eec0 100644 (file)
@@ -1691,6 +1691,9 @@ g_socket_client_connect_finish
 g_socket_client_connect_to_host
 g_socket_client_connect_to_host_async
 g_socket_client_connect_to_host_finish
+g_socket_client_connect_to_service
+g_socket_client_connect_to_service_async
+g_socket_client_connect_to_service_finish
 g_socket_client_set_family
 g_socket_client_set_local_address
 g_socket_client_set_protocol
index ad15e409a6a86642628e96d45228ce69fcd4c383..dcdcb7f2b5b9f9a5a49f5363f16c3d1749f42602 100644 (file)
@@ -1124,6 +1124,9 @@ g_socket_client_connect_finish
 g_socket_client_connect_to_host
 g_socket_client_connect_to_host_async
 g_socket_client_connect_to_host_finish
+g_socket_client_connect_to_service
+g_socket_client_connect_to_service_async
+g_socket_client_connect_to_service_finish
 g_socket_client_get_family
 g_socket_client_get_local_address
 g_socket_client_get_protocol
index 944b73cb24b78bd921b19ec9e10c3bd1b7d118aa..bdb918da5ed411dac7b1d3bbd0bb9c4c2c6623d8 100644 (file)
@@ -36,6 +36,7 @@
 #include <gio/gioerror.h>
 #include <gio/gsocket.h>
 #include <gio/gnetworkaddress.h>
+#include <gio/gnetworkservice.h>
 #include <gio/gsocketaddress.h>
 #include "glibintl.h"
 
@@ -596,6 +597,48 @@ g_socket_client_connect_to_host (GSocketClient        *client,
   return connection;
 }
 
+/**
+ * g_socket_client_connect_to_service:
+ * @client: a #GSocketConnection
+ * @domain: a domain name
+ * @service: the name of the service 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
+ *
+ * Attempts to create a TCP connection to a service.
+ *
+ * This call looks up the SRV record for @service at @domain for the
+ * "tcp" protocol.  It then attempts to connect, in turn, to each of
+ * the hosts providing the service until either a connection succeeds
+ * or there are no hosts remaining.
+ *
+ * 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_service (GSocketClient  *client,
+                                   const char     *domain,
+                                   const char     *service,
+                                   GCancellable   *cancellable,
+                                   GError        **error)
+{
+  GSocketConnectable *connectable;
+  GSocketConnection *connection;
+
+  connectable = g_network_service_new (service, "tcp", domain);
+  connection = g_socket_client_connect (client, connectable,
+                                       cancellable, error);
+  g_object_unref (connectable);
+
+  return connection;
+}
+
 typedef struct
 {
   GSimpleAsyncResult *result;
@@ -856,6 +899,35 @@ g_socket_client_connect_to_host_async (GSocketClient        *client,
     }
 }
 
+/**
+ * g_socket_client_connect_to_service_async:
+ * @client: a #GSocketClient
+ * @domain: a domain name
+ * @service: the name of the service 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_service().
+ **/
+void
+g_socket_client_connect_to_service_async (GSocketClient       *client,
+                                         const char          *domain,
+                                         const char          *service,
+                                         GCancellable        *cancellable,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             user_data)
+{
+  GSocketConnectable *connectable;
+
+  connectable = g_network_service_new (service, "tcp", domain);
+  g_socket_client_connect_async (client,
+                                connectable, cancellable,
+                                callback, user_data);
+  g_object_unref (connectable);
+}
+
 /**
  * g_socket_client_connect_finish:
  * @client: a #GSocketClient.
@@ -903,5 +975,26 @@ g_socket_client_connect_to_host_finish (GSocketClient        *client,
   return g_socket_client_connect_finish (client, result, error);
 }
 
+/**
+ * g_socket_client_connect_to_service_finish:
+ * @client: a #GSocketClient.
+ * @result: a #GAsyncResult.
+ * @error: a #GError location to store the error occuring, or %NULL to
+ * ignore.
+ *
+ * Finishes an async connect operation. See g_socket_client_connect_to_service_async()
+ *
+ * Returns: a #GSocketConnection on success, %NULL on error.
+ *
+ * Since: 2.22
+ **/
+GSocketConnection *
+g_socket_client_connect_to_service_finish (GSocketClient        *client,
+                                          GAsyncResult         *result,
+                                          GError              **error)
+{
+  return g_socket_client_connect_finish (client, result, error);
+}
+
 #define __G_SOCKET_CLIENT_C__
 #include "gioaliasdef.c"
index b04f57be5054a70f5a79f28d0ed8353d024c3c9b..990d23bf8462c841e9686af7e0897d4588ade06f 100644 (file)
@@ -92,6 +92,11 @@ GSocketConnection *     g_socket_client_connect_to_host                 (GSocket
                                                                         int                   default_port,
                                                                          GCancellable         *cancellable,
                                                                          GError              **error);
+GSocketConnection *     g_socket_client_connect_to_service              (GSocketClient        *client,
+                                                                        const char           *domain,
+                                                                        const char           *service,
+                                                                         GCancellable         *cancellable,
+                                                                         GError              **error);
 void                    g_socket_client_connect_async                   (GSocketClient        *client,
                                                                          GSocketConnectable   *connectable,
                                                                          GCancellable         *cancellable,
@@ -110,6 +115,16 @@ GSocketConnection *     g_socket_client_connect_to_host_finish          (GSocket
                                                                          GAsyncResult         *result,
                                                                          GError              **error);
 
+void                    g_socket_client_connect_to_service_async        (GSocketClient        *client,
+                                                                        const char           *domain,
+                                                                        const char           *service,
+                                                                         GCancellable         *cancellable,
+                                                                         GAsyncReadyCallback   callback,
+                                                                         gpointer              user_data);
+GSocketConnection *     g_socket_client_connect_to_service_finish       (GSocketClient        *client,
+                                                                         GAsyncResult         *result,
+                                                                         GError              **error);
+
 G_END_DECLS
 
 #endif /* __G_SOCKET_CLIENT_H___ */