From f88a5d4f15e0bbed0746dab6778631ce44b856d0 Mon Sep 17 00:00:00 2001 From: Samuel Cormier-Iijima Date: Sat, 1 Mar 2008 11:22:53 -0500 Subject: [PATCH] Some more TcpClient stuff --- gnio/ginetaddress.h | 1 - gnio/ginetsocketaddress.h | 4 +- gnio/gnio.h | 4 ++ gnio/gresolver.c | 2 +- gnio/gresolver.h | 2 +- gnio/gsocket.h | 3 +- gnio/gtcpclient.c | 94 ++++++++++++++++++++++++++++++++------- gnio/gtcpclient.h | 5 +-- gnio/gtcpserver.c | 7 --- gnio/gtcpserver.h | 8 ++-- 10 files changed, 92 insertions(+), 38 deletions(-) diff --git a/gnio/ginetaddress.h b/gnio/ginetaddress.h index 19bb313..31d247d 100644 --- a/gnio/ginetaddress.h +++ b/gnio/ginetaddress.h @@ -25,7 +25,6 @@ #define G_INET_ADDRESS_H #include -#include G_BEGIN_DECLS diff --git a/gnio/ginetsocketaddress.h b/gnio/ginetsocketaddress.h index fc44062..27f80ee 100644 --- a/gnio/ginetsocketaddress.h +++ b/gnio/ginetsocketaddress.h @@ -25,8 +25,8 @@ #define G_INET_SOCKET_ADDRESS_H #include -#include "gsocketaddress.h" -#include "ginetaddress.h" +#include +#include G_BEGIN_DECLS diff --git a/gnio/gnio.h b/gnio/gnio.h index 64e64fa..816459c 100644 --- a/gnio/gnio.h +++ b/gnio/gnio.h @@ -32,6 +32,10 @@ #include #include #include +#include +#include +#include +#include #undef __GNIO_GNIO_H_INSIDE__ diff --git a/gnio/gresolver.c b/gnio/gresolver.c index 5a74b7c..dac4a93 100644 --- a/gnio/gresolver.c +++ b/gnio/gresolver.c @@ -39,10 +39,10 @@ #endif #include +#include "gresolver.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); diff --git a/gnio/gresolver.h b/gnio/gresolver.h index e91ced8..fbc1ffb 100644 --- a/gnio/gresolver.h +++ b/gnio/gresolver.h @@ -27,7 +27,7 @@ #include #include -#include "ginetaddress.h" +#include G_BEGIN_DECLS diff --git a/gnio/gsocket.h b/gnio/gsocket.h index 5aa7040..d28244c 100644 --- a/gnio/gsocket.h +++ b/gnio/gsocket.h @@ -27,8 +27,7 @@ #include #include -#include "ginetaddress.h" -#include "gsocketaddress.h" +#include G_BEGIN_DECLS diff --git a/gnio/gtcpclient.c b/gnio/gtcpclient.c index 4898399..fc03627 100644 --- a/gnio/gtcpclient.c +++ b/gnio/gtcpclient.c @@ -24,34 +24,49 @@ #include #include #include -#include #include #include -#include "ginetaddress.h" -#include "ginet4address.h" -#include "ginet6address.h" -#include "gsocket.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, - PROP_ADDRESS + PROP_ADDRESS, + PROP_HOSTNAME, + PROP_PORT }; struct _GTcpClientPrivate { - GSocketAddress *address; + GInetSocketAddress *address; + gchar *hostname; + gushort port; }; 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 +76,42 @@ 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_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); } @@ -83,6 +122,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 +133,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 +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->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 +156,47 @@ 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_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_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); + + client->priv->address = NULL; + client->priv->hostname = NULL; + client->priv->port = 0; } 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 diff --git a/gnio/gtcpclient.h b/gnio/gtcpclient.h index e94df73..f3a9cce 100644 --- a/gnio/gtcpclient.h +++ b/gnio/gtcpclient.h @@ -27,9 +27,7 @@ #include #include -#include "ginetsocketaddress.h" -#include "gnetworkinputstream.h" -#include "gnetworkoutputstream.h" +#include G_BEGIN_DECLS @@ -81,4 +79,3 @@ void g_tcp_client_close (GTcpClient *client); G_END_DECLS #endif /* G_TCP_CLIENT_H */ - diff --git a/gnio/gtcpserver.c b/gnio/gtcpserver.c index 6b7349b..6697a5f 100644 --- a/gnio/gtcpserver.c +++ b/gnio/gtcpserver.c @@ -24,18 +24,11 @@ #include #include #include -#include #include #include -#include "ginetaddress.h" -#include "ginet4address.h" -#include "ginet6address.h" -#include "gsocket.h" #include "gtcpserver.h" -#include "gnioerror.h" -#include "ginetsocketaddress.h" G_DEFINE_TYPE (GTcpServer, g_tcp_server, G_TYPE_OBJECT); diff --git a/gnio/gtcpserver.h b/gnio/gtcpserver.h index 5cb8d63..42048cc 100644 --- a/gnio/gtcpserver.h +++ b/gnio/gtcpserver.h @@ -27,11 +27,6 @@ #include #include -#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 ()) @@ -45,6 +40,9 @@ typedef struct _GTcpServer GTcpServer; typedef struct _GTcpServerClass GTcpServerClass; typedef struct _GTcpServerPrivate GTcpServerPrivate; +#include +#include + struct _GTcpServer { GObject parent; -- 2.20.1