TcpClient seems to be working for sync connects, but connect()
[cascardo/gnio.git] / gnio / gtcpclient.c
index 4898399..1bb056f 100644 (file)
 #include <config.h>
 #include <glib.h>
 #include <gio/gio.h>
-#include <gio/gasynchelper.h>
+#include <gnio/gnio.h>
 
 #include <string.h>
 #include <errno.h>
 
-#include "ginetaddress.h"
-#include "ginet4address.h"
-#include "ginet6address.h"
-#include "gsocket.h"
-#include "gtcpclient.h"
-#include "gnioerror.h"
-#include "ginetsocketaddress.h"
-
 G_DEFINE_TYPE (GTcpClient, g_tcp_client, G_TYPE_OBJECT);
 
 enum
 {
   PROP_0,
-  PROP_ADDRESS
+  PROP_ADDRESS,
+  PROP_HOSTNAME,
+  PROP_PORT
 };
 
 struct _GTcpClientPrivate
 {
-  GSocketAddress *address;
+  GInetSocketAddress *address;
+  gchar              *hostname;
+  gushort             port;
+  GSocket            *socket;
 };
 
 static void
-g_tcp_client_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+g_tcp_client_constructed (GObject *object)
+{
+  GTcpClient *client = G_TCP_CLIENT (object);
+
+  if (client->priv->address)
+    {
+      // we've been constructed with an address, extract hostname+port
+      client->priv->hostname = g_inet_address_to_string (g_inet_socket_address_get_address (client->priv->address));
+      client->priv->port = g_inet_socket_address_get_port (client->priv->address);
+      return;
+    }
+}
+
+static void
+g_tcp_client_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
@@ -61,18 +75,44 @@ g_tcp_client_get_property (GObject *object, guint prop_id, GValue *value, GParam
         g_value_set_object (value, client->priv->address);
         break;
 
+      case PROP_HOSTNAME:
+        g_value_set_string (value, client->priv->hostname);
+        break;
+
+      case PROP_PORT:
+        g_value_set_uint (value, client->priv->port);
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
 }
 
 static void
-g_tcp_client_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+g_tcp_client_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
   switch (prop_id)
     {
+      case PROP_ADDRESS:
+        // sink the address' floating reference
+        client->priv->address = G_INET_SOCKET_ADDRESS (g_value_get_object (value));
+        if (client->priv->address)
+          g_object_ref_sink (client->priv->address);
+        break;
+
+      case PROP_HOSTNAME:
+        client->priv->hostname = g_value_dup_string (value);
+        break;
+
+      case PROP_PORT:
+        client->priv->port = g_value_get_uint (value);
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -83,6 +123,8 @@ g_tcp_client_finalize (GObject *object)
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
+  g_object_unref (client->priv->address);
+
   if (G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize)
     (*G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize) (object);
 }
@@ -92,6 +134,8 @@ g_tcp_client_dispose (GObject *object)
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
+  g_free (client->priv->hostname);
+
   if (G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose)
     (*G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose) (object);
 }
@@ -105,6 +149,7 @@ g_tcp_client_class_init (GTcpClientClass *klass)
 
   gobject_class->finalize = g_tcp_client_finalize;
   gobject_class->dispose = g_tcp_client_dispose;
+  gobject_class->constructed = g_tcp_client_constructed;
   gobject_class->set_property = g_tcp_client_set_property;
   gobject_class->get_property = g_tcp_client_get_property;
 
@@ -112,27 +157,48 @@ g_tcp_client_class_init (GTcpClientClass *klass)
                                    g_param_spec_object ("address",
                                                         "address",
                                                         "the remote address the socket will connect to",
-                                                        G_TYPE_SOCKET_ADDRESS,
-                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+                                                        G_TYPE_INET_SOCKET_ADDRESS,
+                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+
+  g_object_class_install_property (gobject_class, PROP_HOSTNAME,
+                                   g_param_spec_string ("hostname",
+                                                        "hostname",
+                                                        "the hostname of the remote address the socket will connect to",
+                                                        NULL,
+                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+
+  g_object_class_install_property (gobject_class, PROP_PORT,
+                                   g_param_spec_uint ("port",
+                                                      "port",
+                                                      "the remote port the socket will connect to",
+                                                      0,
+                                                      G_MAXUSHORT,
+                                                      0,
+                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
 }
 
 static void
 g_tcp_client_init (GTcpClient *client)
 {
   client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client, G_TYPE_TCP_CLIENT, GTcpClientPrivate);
+
+  client->priv->address = NULL;
+  client->priv->hostname = NULL;
+  client->priv->port = 0;
+  client->priv->socket = NULL;
 }
 
 GTcpClient *
 g_tcp_client_new (const gchar *hostname,
                   gushort      port)
 {
-  return NULL;
+  return G_TCP_CLIENT (g_object_new (G_TYPE_TCP_CLIENT, "hostname", hostname, "port", port, NULL));
 }
 
 GTcpClient *
-g_tcp_client_new_with_address (GInetSocketAddress *address)
+g_tcp_client_new_from_address (GInetSocketAddress *address)
 {
-  return NULL;
+  return G_TCP_CLIENT (g_object_new (G_TYPE_TCP_CLIENT, "address", address, NULL));
 }
 
 gboolean
@@ -140,7 +206,51 @@ g_tcp_client_connect (GTcpClient    *client,
                       GCancellable  *cancellable,
                       GError       **error)
 {
-  return FALSE;
+  GInetAddress *address;
+
+  g_return_val_if_fail (G_IS_TCP_CLIENT (client), FALSE);
+
+  if (!client->priv->address)
+    {
+      // we've been constructed with just hostname+port, resolve
+      GResolver *resolver = g_resolver_new ();
+
+      address = g_resolver_resolve (resolver, client->priv->hostname, cancellable, error);
+
+      if (!address)
+        return FALSE;
+
+      client->priv->address = g_inet_socket_address_new (address, client->priv->port);
+
+      g_object_unref (resolver);
+
+      g_object_ref_sink (client->priv->address);
+    }
+  else
+    {
+     address = g_inet_socket_address_get_address (client->priv->address);
+    }
+
+  if (G_IS_INET4_ADDRESS (address))
+    client->priv->socket = g_socket_new (G_SOCKET_DOMAIN_INET, G_SOCKET_TYPE_STREAM, NULL, error);
+  else if (G_IS_INET6_ADDRESS (address))
+    client->priv->socket = g_socket_new (G_SOCKET_DOMAIN_INET6, G_SOCKET_TYPE_STREAM, NULL, error);
+  else
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported address domain");
+      return FALSE;
+    }
+
+  if (!client->priv->socket)
+    return FALSE;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
+  if (!g_socket_connect (client->priv->socket, G_SOCKET_ADDRESS (client->priv->address), error))
+    return FALSE;
+
+  return TRUE;
 }
 
 typedef struct {