From c69e0ebc3a9e13b4af030a19b117175f881f2b3c Mon Sep 17 00:00:00 2001 From: Samuel Cormier-Iijima Date: Thu, 28 Feb 2008 19:42:20 -0500 Subject: [PATCH] autotools are dumb, so I'm also adding gnioenumtypes.{c,h} --- Makefile.am | 2 + gnio/Makefile.am | 52 ++++++++++++------ gnio/gnio.h | 6 +- gnio/gnioenumtypes.c | 52 ++++++++++++++++++ gnio/gnioenumtypes.c.template | 40 ++++++++++++++ gnio/gnioenumtypes.h | 21 +++++++ gnio/gnioenumtypes.h.template | 24 ++++++++ gnio/gsocket.c | 100 +++++++++++++++++++++++++++++++--- gnio/gsocket.h | 6 +- test/test-client.c | 2 +- test/test-server.c | 2 +- 11 files changed, 278 insertions(+), 29 deletions(-) create mode 100644 gnio/gnioenumtypes.c create mode 100644 gnio/gnioenumtypes.c.template create mode 100644 gnio/gnioenumtypes.h create mode 100644 gnio/gnioenumtypes.h.template diff --git a/Makefile.am b/Makefile.am index 008f097..da4b791 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,4 +5,6 @@ SUBDIRS = \ test \ $(NULL) +BUILT_SOURCES = + DISTCHECK_CONFIGURE_FLAGS = --enable-gtk-doc diff --git a/gnio/Makefile.am b/gnio/Makefile.am index 62cc1bf..780cef5 100644 --- a/gnio/Makefile.am +++ b/gnio/Makefile.am @@ -9,6 +9,22 @@ INCLUDES = \ lib_LTLIBRARIES=libgnio.la +libgnio_la_SOURCES = \ + ginetaddress.c \ + ginet4address.c \ + ginet6address.c \ + gresolver.c \ + gsocketaddress.c \ + ginetsocketaddress.c \ + gsocket.c \ + gtcpserver.c \ + gtcpclient.c \ + gnetworkinputstream.c \ + gnetworkoutputstream.c \ + gasynchelper.c \ + gnioenumtypes.c \ + $(NULL) + gnio_headers = \ ginetaddress.h \ @@ -20,28 +36,30 @@ gnio_headers = \ gsocket.h \ gtcpserver.h \ gtcpclient.h \ + gnio.h \ + gnioenumtypes.h \ gnetworkinputstream.h \ gnetworkoutputstream.h \ $(NULL) -gioincludedir=$(includedir)/glib-2.0/gnio/ -gioinclude_HEADERS = \ - $(gnio_headers) +gnioincludedir=$(includedir)/gnio/ +gnioinclude_HEADERS = \ + $(gnio_headers) \ + gnioenumtypes.h +BUILT_SOURCES = \ + gnioenumtypes.h \ + gnioenumtypes.c \ + $(NULL) -libgnio_la_SOURCES = \ - ginetaddress.c \ - ginet4address.c \ - ginet6address.c \ - gresolver.c \ - gsocketaddress.c \ - ginetsocketaddress.c \ - gsocket.c \ - gtcpserver.c \ - gtcpclient.c \ - gnetworkinputstream.c \ - gnetworkoutputstream.c \ - gasynchelper.c \ - $(NULL) +gnioenumtypes.h: $(gnio_headers) gnioenumtypes.h.template + ( top_builddir=`cd $(top_builddir) && pwd`; \ + cd $(srcdir) && glib-mkenums --template gnioenumtypes.h.template $(gnio_headers) ) > \ + gnioenumtypes.h.tmp && mv gnioenumtypes.h.tmp gnioenumtypes.h + +gnioenumtypes.c: $(gnio_headers) gnioenumtypes.c.template + ( top_builddir=`cd $(top_builddir) && pwd`; \ + cd $(srcdir) && glib-mkenums --template gnioenumtypes.c.template $(gnio_headers) ) > \ + gnioenumtypes.c.tmp && mv gnioenumtypes.c.tmp gnioenumtypes.c diff --git a/gnio/gnio.h b/gnio/gnio.h index 64e64fa..f1d0e93 100644 --- a/gnio/gnio.h +++ b/gnio/gnio.h @@ -29,9 +29,13 @@ #include #include #include -#include #include +#include #include +#include +#include +#include +#include #undef __GNIO_GNIO_H_INSIDE__ diff --git a/gnio/gnioenumtypes.c b/gnio/gnioenumtypes.c new file mode 100644 index 0000000..5a31106 --- /dev/null +++ b/gnio/gnioenumtypes.c @@ -0,0 +1,52 @@ + +/* Generated data (by glib-mkenums) */ + +#include + +/* enumerations from "gsocket.h" */ +GType +g_socket_domain_get_type (void) +{ + static volatile gsize g_define_type_id__volatile = 0; + + if (g_once_init_enter (&g_define_type_id__volatile)) + { + static const GEnumValue values[] = { + { G_SOCKET_DOMAIN_INET, "G_SOCKET_DOMAIN_INET", "inet" }, + { G_SOCKET_DOMAIN_INET6, "G_SOCKET_DOMAIN_INET6", "inet6" }, + { G_SOCKET_DOMAIN_UNIX, "G_SOCKET_DOMAIN_UNIX", "unix" }, + { 0, NULL, NULL } + }; + GType g_define_type_id = + g_enum_register_static (g_intern_static_string ("GSocketDomain"), values); + g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); + } + + return g_define_type_id__volatile; +} + +GType +g_socket_type_get_type (void) +{ + static volatile gsize g_define_type_id__volatile = 0; + + if (g_once_init_enter (&g_define_type_id__volatile)) + { + static const GEnumValue values[] = { + { G_SOCKET_TYPE_STREAM, "G_SOCKET_TYPE_STREAM", "stream" }, + { G_SOCKET_TYPE_DATAGRAM, "G_SOCKET_TYPE_DATAGRAM", "datagram" }, + { G_SOCKET_TYPE_SEQPACKET, "G_SOCKET_TYPE_SEQPACKET", "seqpacket" }, + { 0, NULL, NULL } + }; + GType g_define_type_id = + g_enum_register_static (g_intern_static_string ("GSocketType"), values); + g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); + } + + return g_define_type_id__volatile; +} + +#define __GNIO_ENUM_TYPES_C__ + +/* Generated data ends here */ + diff --git a/gnio/gnioenumtypes.c.template b/gnio/gnioenumtypes.c.template new file mode 100644 index 0000000..30e9441 --- /dev/null +++ b/gnio/gnioenumtypes.c.template @@ -0,0 +1,40 @@ +/*** BEGIN file-header ***/ +#include + +/*** END file-header ***/ + +/*** BEGIN file-production ***/ +/* enumerations from "@filename@" */ +/*** END file-production ***/ + +/*** BEGIN value-header ***/ +GType +@enum_name@_get_type (void) +{ + static volatile gsize g_define_type_id__volatile = 0; + + if (g_once_init_enter (&g_define_type_id__volatile)) + { + static const G@Type@Value values[] = { +/*** END value-header ***/ + +/*** BEGIN value-production ***/ + { @VALUENAME@, "@VALUENAME@", "@valuenick@" }, +/*** END value-production ***/ + +/*** BEGIN value-tail ***/ + { 0, NULL, NULL } + }; + GType g_define_type_id = + g_@type@_register_static (g_intern_static_string ("@EnumName@"), values); + g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); + } + + return g_define_type_id__volatile; +} + +/*** END value-tail ***/ + +/*** BEGIN file-tail ***/ +#define __GNIO_ENUM_TYPES_C__ +/*** END file-tail ***/ diff --git a/gnio/gnioenumtypes.h b/gnio/gnioenumtypes.h new file mode 100644 index 0000000..1281c64 --- /dev/null +++ b/gnio/gnioenumtypes.h @@ -0,0 +1,21 @@ + +/* Generated data (by glib-mkenums) */ + +#ifndef __GNIO_ENUM_TYPES_H__ +#define __GNIO_ENUM_TYPES_H__ + +#include + +G_BEGIN_DECLS + +/* enumerations from "gsocket.h" */ +GType g_socket_domain_get_type (void) G_GNUC_CONST; +#define G_TYPE_SOCKET_DOMAIN (g_socket_domain_get_type ()) +GType g_socket_type_get_type (void) G_GNUC_CONST; +#define G_TYPE_SOCKET_TYPE (g_socket_type_get_type ()) +G_END_DECLS + +#endif /* __GNIO_ENUM_TYPES_H__ */ + +/* Generated data ends here */ + diff --git a/gnio/gnioenumtypes.h.template b/gnio/gnioenumtypes.h.template new file mode 100644 index 0000000..22c2f00 --- /dev/null +++ b/gnio/gnioenumtypes.h.template @@ -0,0 +1,24 @@ +/*** BEGIN file-header ***/ +#ifndef __GNIO_ENUM_TYPES_H__ +#define __GNIO_ENUM_TYPES_H__ + +#include + +G_BEGIN_DECLS +/*** END file-header ***/ + +/*** BEGIN file-production ***/ + +/* enumerations from "@filename@" */ +/*** END file-production ***/ + +/*** BEGIN value-header ***/ +GType @enum_name@_get_type (void) G_GNUC_CONST; +#define G_TYPE_@ENUMSHORT@ (@enum_name@_get_type ()) +/*** END value-header ***/ + +/*** BEGIN file-tail ***/ +G_END_DECLS + +#endif /* __GNIO_ENUM_TYPES_H__ */ +/*** END file-tail ***/ diff --git a/gnio/gsocket.c b/gnio/gsocket.c index 69a1b5c..0ca9091 100644 --- a/gnio/gsocket.c +++ b/gnio/gsocket.c @@ -24,7 +24,9 @@ #include #include #include -#include +#include +#include "gasynchelper.h" +#include "gnioenumtypes.h" #include #ifndef G_OS_WIN32 @@ -51,6 +53,9 @@ G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT); enum { PROP_0, + PROP_DOMAIN, + PROP_TYPE, + PROP_PROTOCOL, PROP_FD, PROP_BLOCKING, PROP_BACKLOG, @@ -61,14 +66,24 @@ enum struct _GSocketPrivate { + GSocketDomain domain; + GSocketType type; + gchar *protocol; gint fd; gboolean blocking; gint backlog; gboolean reuse_address; + GError *error; GSocketAddress *local_address; GSocketAddress *remote_address; }; +static void +g_socket_constructed (GObject *object) +{ + +} + static void g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { @@ -76,6 +91,18 @@ g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec switch (prop_id) { + case PROP_DOMAIN: + g_value_set_int (value, socket->priv->domain); + break; + + case PROP_TYPE: + g_value_set_int (value, socket->priv->type); + break; + + case PROP_PROTOCOL: + g_value_set_string (value, socket->priv->protocol); + break; + case PROP_FD: g_value_set_int (value, socket->priv->fd); break; @@ -170,9 +197,37 @@ g_socket_class_init (GSocketClass *klass) gobject_class->finalize = g_socket_finalize; gobject_class->dispose = g_socket_dispose; + gobject_class->constructed = g_socket_constructed; gobject_class->set_property = g_socket_set_property; gobject_class->get_property = g_socket_get_property; + g_object_class_install_property (gobject_class, PROP_DOMAIN, + g_param_spec_int ("domain", + "file descriptor", + "the socket's file descriptor", + G_MININT, + G_MAXINT, + -1, + 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_TYPE, + g_param_spec_int ("fd", + "file descriptor", + "the socket's file descriptor", + G_MININT, + G_MAXINT, + -1, + 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_PROTOCOL, + g_param_spec_int ("fd", + "file descriptor", + "the socket's file descriptor", + G_MININT, + G_MAXINT, + -1, + 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_FD, g_param_spec_int ("fd", "file descriptor", @@ -228,13 +283,16 @@ g_socket_init (GSocket *socket) socket->priv->fd = -1; socket->priv->blocking = TRUE; socket->priv->backlog = 10; + socket->priv->reuse_address = FALSE; + socket->priv->error = NULL; socket->priv->remote_address = NULL; socket->priv->local_address = NULL; } GSocket * -g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol, GError **error) +g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol) { + GError *error = NULL; static GStaticMutex getprotobyname_mutex = G_STATIC_MUTEX_INIT; gint fd, native_domain, native_type, native_protocol; @@ -253,7 +311,7 @@ g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol, GEr break; default: - g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket domain"); + g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket domain"); return NULL; } @@ -272,7 +330,7 @@ g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol, GEr break; default: - g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket type"); + g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket type"); return NULL; } @@ -285,7 +343,7 @@ g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol, GEr if (!(ent = getprotobyname (protocol))) { g_static_mutex_unlock (&getprotobyname_mutex); - g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket protocol"); + g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket protocol"); return NULL; } native_protocol = ent->p_proto; @@ -296,11 +354,13 @@ g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol, GEr if (fd < 0) { - g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "unable to create socket: %s", g_strerror (errno)); + g_set_error (&error, G_IO_ERROR, g_io_error_from_errno (errno), "unable to create socket: %s", g_strerror (errno)); return NULL; } - return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, "blocking", TRUE, NULL)); + g_print ("finishing socket_new\n"); + + return G_SOCKET (g_object_new (G_TYPE_SOCKET, "blocking", TRUE, "fd", fd, NULL)); } GSocket * @@ -408,12 +468,27 @@ g_socket_get_remote_address (GSocket *socket, return (socket->priv->remote_address = g_object_ref_sink (g_socket_address_from_native (buffer, len))); } +gboolean +g_socket_has_error (GSocket *socket, + GError **error) +{ + g_return_val_if_fail (G_IS_SOCKET (socket), FALSE); + + if (!socket->priv->error) + return FALSE; + + return TRUE; +} + gboolean g_socket_listen (GSocket *socket, GError **error) { g_return_val_if_fail (G_IS_SOCKET (socket), FALSE); + if (g_socket_has_error (socket, error)) + return FALSE; + if (listen (socket->priv->fd, socket->priv->backlog) < 0) { g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not listen: %s", g_strerror (errno)); @@ -430,6 +505,9 @@ g_socket_bind (GSocket *socket, { g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE); + if (g_socket_has_error (socket, error)) + return FALSE; + { gchar addr[256]; @@ -456,6 +534,11 @@ g_socket_accept (GSocket *socket, { gint ret; + g_return_val_if_fail (G_IS_SOCKET (socket), NULL); + + if (g_socket_has_error (socket, error)) + return NULL; + if ((ret = accept (socket->priv->fd, NULL, 0)) < 0) { g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection: %s", g_strerror (errno)); @@ -474,6 +557,9 @@ g_socket_connect (GSocket *socket, g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE); + if (g_socket_has_error (socket, error)) + return FALSE; + g_socket_address_to_native (address, buffer); if (connect (socket->priv->fd, (struct sockaddr *) buffer, g_socket_address_native_size (address)) < 0) diff --git a/gnio/gsocket.h b/gnio/gsocket.h index 5aa7040..e1eca71 100644 --- a/gnio/gsocket.h +++ b/gnio/gsocket.h @@ -73,8 +73,7 @@ GType g_socket_get_type (void) G_GNUC_CONST; GSocket * g_socket_new (GSocketDomain domain, GSocketType type, - const gchar *protocol, - GError **error); + const gchar *protocol); GSocket * g_socket_new_from_fd (gint fd); @@ -92,6 +91,9 @@ void g_socket_set_reuse_address (GSocket *socket, gboolean g_socket_get_reuse_address (GSocket *socket); +gboolean g_socket_has_error (GSocket *socket, + GError **error); + gboolean g_socket_bind (GSocket *socket, GSocketAddress *address, GError **error); diff --git a/test/test-client.c b/test/test-client.c index 838b7d0..901709a 100644 --- a/test/test-client.c +++ b/test/test-client.c @@ -34,7 +34,7 @@ int main (int argc, char *argv[]) loop = g_main_loop_new (NULL, FALSE); - socket = g_socket_new (G_SOCKET_DOMAIN_INET, G_SOCKET_TYPE_STREAM, NULL, NULL); + socket = g_socket_new (G_SOCKET_DOMAIN_INET, G_SOCKET_TYPE_STREAM, NULL); g_printf ("connecting to 127.0.0.1:31882...\n"); diff --git a/test/test-server.c b/test/test-server.c index c74d289..0c2a787 100644 --- a/test/test-server.c +++ b/test/test-server.c @@ -60,7 +60,7 @@ int main (int argc, char *argv[]) loop = g_main_loop_new (NULL, FALSE); - socket = g_socket_new (G_SOCKET_DOMAIN_INET, G_SOCKET_TYPE_STREAM, NULL, NULL); + socket = g_socket_new (G_SOCKET_DOMAIN_INET, G_SOCKET_TYPE_STREAM, NULL); g_socket_set_reuse_address (socket, TRUE); -- 2.20.1