Some more TcpClient stuff
authorSamuel Cormier-Iijima <sciyoshi@gmail.com>
Sat, 1 Mar 2008 16:22:53 +0000 (11:22 -0500)
committerSamuel Cormier-Iijima <sciyoshi@gmail.com>
Sat, 1 Mar 2008 16:22:53 +0000 (11:22 -0500)
gnio/ginetaddress.h
gnio/ginetsocketaddress.h
gnio/gnio.h
gnio/gresolver.c
gnio/gresolver.h
gnio/gsocket.h
gnio/gtcpclient.c
gnio/gtcpclient.h
gnio/gtcpserver.c
gnio/gtcpserver.h

index 19bb313..31d247d 100644 (file)
@@ -25,7 +25,6 @@
 #define G_INET_ADDRESS_H
 
 #include <glib-object.h>
 #define G_INET_ADDRESS_H
 
 #include <glib-object.h>
-#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
 
 G_BEGIN_DECLS
 
index fc44062..27f80ee 100644 (file)
@@ -25,8 +25,8 @@
 #define G_INET_SOCKET_ADDRESS_H
 
 #include <glib-object.h>
 #define G_INET_SOCKET_ADDRESS_H
 
 #include <glib-object.h>
-#include "gsocketaddress.h"
-#include "ginetaddress.h"
+#include <gnio/gsocketaddress.h>
+#include <gnio/ginetaddress.h>
 
 G_BEGIN_DECLS
 
 
 G_BEGIN_DECLS
 
index 64e64fa..816459c 100644 (file)
 #include <gnio/ginetsocketaddress.h>
 #include <gnio/gsocketaddress.h>
 #include <gnio/gresolver.h>
 #include <gnio/ginetsocketaddress.h>
 #include <gnio/gsocketaddress.h>
 #include <gnio/gresolver.h>
+#include <gnio/gsocket.h>
+#include <gnio/gnioerror.h>
+#include <gnio/gtcpclient.h>
+#include <gnio/gtcpserver.h>
 
 #undef __GNIO_GNIO_H_INSIDE__
 
 
 #undef __GNIO_GNIO_H_INSIDE__
 
index 5a74b7c..dac4a93 100644 (file)
 #endif
 #include <errno.h>
 
 #endif
 #include <errno.h>
 
+#include "gresolver.h"
 #include "ginetaddress.h"
 #include "ginet4address.h"
 #include "ginet6address.h"
 #include "ginetaddress.h"
 #include "ginet4address.h"
 #include "ginet6address.h"
-#include "gresolver.h"
 #include "gnioerror.h"
 
 G_DEFINE_TYPE (GResolver, g_resolver, G_TYPE_OBJECT);
 #include "gnioerror.h"
 
 G_DEFINE_TYPE (GResolver, g_resolver, G_TYPE_OBJECT);
index e91ced8..fbc1ffb 100644 (file)
@@ -27,7 +27,7 @@
 #include <glib-object.h>
 #include <gio/gio.h>
 
 #include <glib-object.h>
 #include <gio/gio.h>
 
-#include "ginetaddress.h"
+#include <gnio/ginetaddress.h>
 
 G_BEGIN_DECLS
 
 
 G_BEGIN_DECLS
 
index 5aa7040..d28244c 100644 (file)
@@ -27,8 +27,7 @@
 #include <glib-object.h>
 #include <gio/gio.h>
 
 #include <glib-object.h>
 #include <gio/gio.h>
 
-#include "ginetaddress.h"
-#include "gsocketaddress.h"
+#include <gnio/gsocketaddress.h>
 
 G_BEGIN_DECLS
 
 
 G_BEGIN_DECLS
 
index 4898399..fc03627 100644 (file)
 #include <config.h>
 #include <glib.h>
 #include <gio/gio.h>
 #include <config.h>
 #include <glib.h>
 #include <gio/gio.h>
-#include <gio/gasynchelper.h>
 
 #include <string.h>
 #include <errno.h>
 
 
 #include <string.h>
 #include <errno.h>
 
-#include "ginetaddress.h"
-#include "ginet4address.h"
-#include "ginet6address.h"
-#include "gsocket.h"
 #include "gtcpclient.h"
 #include "gtcpclient.h"
-#include "gnioerror.h"
-#include "ginetsocketaddress.h"
+#include "gasynchelper.h"
 
 G_DEFINE_TYPE (GTcpClient, g_tcp_client, G_TYPE_OBJECT);
 
 enum
 {
   PROP_0,
 
 G_DEFINE_TYPE (GTcpClient, g_tcp_client, G_TYPE_OBJECT);
 
 enum
 {
   PROP_0,
-  PROP_ADDRESS
+  PROP_ADDRESS,
+  PROP_HOSTNAME,
+  PROP_PORT
 };
 
 struct _GTcpClientPrivate
 {
 };
 
 struct _GTcpClientPrivate
 {
-  GSocketAddress *address;
+  GInetSocketAddress *address;
+  gchar              *hostname;
+  gushort             port;
 };
 
 static void
 };
 
 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);
 
 {
   GTcpClient *client = G_TCP_CLIENT (object);
 
@@ -61,18 +76,42 @@ g_tcp_client_get_property (GObject *object, guint prop_id, GValue *value, GParam
         g_value_set_object (value, client->priv->address);
         break;
 
         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
       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)
     {
 {
   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_object_ref_sink (g_value_get_object (value)));
+        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);
     }
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -83,6 +122,8 @@ g_tcp_client_finalize (GObject *object)
 {
   GTcpClient *client = G_TCP_CLIENT (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);
 }
   if (G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize)
     (*G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize) (object);
 }
@@ -92,6 +133,8 @@ g_tcp_client_dispose (GObject *object)
 {
   GTcpClient *client = G_TCP_CLIENT (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);
 }
   if (G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose)
     (*G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose) (object);
 }
@@ -105,6 +148,7 @@ g_tcp_client_class_init (GTcpClientClass *klass)
 
   gobject_class->finalize = g_tcp_client_finalize;
   gobject_class->dispose = g_tcp_client_dispose;
 
   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;
 
   gobject_class->set_property = g_tcp_client_set_property;
   gobject_class->get_property = g_tcp_client_get_property;
 
@@ -112,27 +156,47 @@ g_tcp_client_class_init (GTcpClientClass *klass)
                                    g_param_spec_object ("address",
                                                         "address",
                                                         "the remote address the socket will connect to",
                                    g_param_spec_object ("address",
                                                         "address",
                                                         "the remote address the socket will connect to",
-                                                        G_TYPE_SOCKET_ADDRESS,
+                                                        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_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | 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_READABLE | 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);
 }
 
 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;
 }
 
 GTcpClient *
 g_tcp_client_new (const gchar *hostname,
                   gushort      port)
 {
 }
 
 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 *
 }
 
 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
 }
 
 gboolean
index e94df73..f3a9cce 100644 (file)
@@ -27,9 +27,7 @@
 #include <glib-object.h>
 #include <gio/gio.h>
 
 #include <glib-object.h>
 #include <gio/gio.h>
 
-#include "ginetsocketaddress.h"
-#include "gnetworkinputstream.h"
-#include "gnetworkoutputstream.h"
+#include <gnio/ginetsocketaddress.h>
 
 G_BEGIN_DECLS
 
 
 G_BEGIN_DECLS
 
@@ -81,4 +79,3 @@ void             g_tcp_client_close            (GTcpClient    *client);
 G_END_DECLS
 
 #endif /* G_TCP_CLIENT_H */
 G_END_DECLS
 
 #endif /* G_TCP_CLIENT_H */
-
index 6b7349b..6697a5f 100644 (file)
 #include <config.h>
 #include <glib.h>
 #include <gio/gio.h>
 #include <config.h>
 #include <glib.h>
 #include <gio/gio.h>
-#include <gio/gasynchelper.h>
 
 #include <string.h>
 #include <errno.h>
 
 
 #include <string.h>
 #include <errno.h>
 
-#include "ginetaddress.h"
-#include "ginet4address.h"
-#include "ginet6address.h"
-#include "gsocket.h"
 #include "gtcpserver.h"
 #include "gtcpserver.h"
-#include "gnioerror.h"
-#include "ginetsocketaddress.h"
 
 G_DEFINE_TYPE (GTcpServer, g_tcp_server, G_TYPE_OBJECT);
 
 
 G_DEFINE_TYPE (GTcpServer, g_tcp_server, G_TYPE_OBJECT);
 
index 5cb8d63..42048cc 100644 (file)
 #include <glib-object.h>
 #include <gio/gio.h>
 
 #include <glib-object.h>
 #include <gio/gio.h>
 
-#include "ginetsocketaddress.h"
-#include "gtcpclient.h"
-#include "gnetworkinputstream.h"
-#include "gnetworkoutputstream.h"
-
 G_BEGIN_DECLS
 
 #define G_TYPE_TCP_SERVER         (g_tcp_server_get_type ())
 G_BEGIN_DECLS
 
 #define G_TYPE_TCP_SERVER         (g_tcp_server_get_type ())
@@ -45,6 +40,9 @@ typedef struct _GTcpServer          GTcpServer;
 typedef struct _GTcpServerClass     GTcpServerClass;
 typedef struct _GTcpServerPrivate   GTcpServerPrivate;
 
 typedef struct _GTcpServerClass     GTcpServerClass;
 typedef struct _GTcpServerPrivate   GTcpServerPrivate;
 
+#include <gnio/gnio.h>
+#include <gnio/gtcpclient.h>
+
 struct _GTcpServer
 {
   GObject parent;
 struct _GTcpServer
 {
   GObject parent;