From 11ee7b45fdb2583ccd2e30305e71175c20a0fdb1 Mon Sep 17 00:00:00 2001 From: Samuel CORMIER-IIJIMA Date: Mon, 11 Feb 2008 11:12:56 -0500 Subject: [PATCH] Beginnings of GSocket API, small fixes for InetAddress --- gnio/Makefile.am | 4 +++ gnio/ginet4address.c | 3 +- gnio/ginet6address.c | 3 +- gnio/ginetsocketaddress.c | 3 +- gnio/gnetworkinputstream.c | 60 ++++++++++++++++++++++++++++++++++++ gnio/gnetworkinputstream.h | 61 +++++++++++++++++++++++++++++++++++++ gnio/gnetworkoutputstream.c | 60 ++++++++++++++++++++++++++++++++++++ gnio/gnetworkoutputstream.h | 61 +++++++++++++++++++++++++++++++++++++ gnio/gresolver.h | 2 +- gnio/gsocket.c | 23 +++++++++++++- gnio/gsocket.h | 16 ++++++---- 11 files changed, 285 insertions(+), 11 deletions(-) create mode 100644 gnio/gnetworkinputstream.c create mode 100644 gnio/gnetworkinputstream.h create mode 100644 gnio/gnetworkoutputstream.c create mode 100644 gnio/gnetworkoutputstream.h diff --git a/gnio/Makefile.am b/gnio/Makefile.am index dfb4d13..17f5a64 100644 --- a/gnio/Makefile.am +++ b/gnio/Makefile.am @@ -18,6 +18,8 @@ gnio_headers = \ gsocketaddress.h \ ginetsocketaddress.h \ gsocket.h \ + gnetworkinputstream.h \ + gnetworkoutputstream.h \ $(NULL) gioincludedir=$(includedir)/glib-2.0/gnio/ @@ -33,6 +35,8 @@ libgnio_la_SOURCES = \ gsocketaddress.c \ ginetsocketaddress.c \ gsocket.c \ + gnetworkinputstream.c \ + gnetworkoutputstream.c \ $(NULL) diff --git a/gnio/ginet4address.c b/gnio/ginet4address.c index 12919b1..61f1226 100644 --- a/gnio/ginet4address.c +++ b/gnio/ginet4address.c @@ -36,7 +36,8 @@ G_DEFINE_TYPE (GInet4Address, g_inet4_address, G_TYPE_INET_ADDRESS); -struct _GInet4AddressPrivate { +struct _GInet4AddressPrivate +{ union { guint8 u4_addr8[4]; guint32 u4_addr32; diff --git a/gnio/ginet6address.c b/gnio/ginet6address.c index c8c1a39..6598572 100644 --- a/gnio/ginet6address.c +++ b/gnio/ginet6address.c @@ -32,7 +32,8 @@ G_DEFINE_TYPE (GInet6Address, g_inet6_address, G_TYPE_INET_ADDRESS); -struct _GInet6AddressPrivate { +struct _GInet6AddressPrivate +{ union { guint8 u6_addr8[16]; guint16 u6_addr16[8]; diff --git a/gnio/ginetsocketaddress.c b/gnio/ginetsocketaddress.c index 1771240..6b2d7b1 100644 --- a/gnio/ginetsocketaddress.c +++ b/gnio/ginetsocketaddress.c @@ -34,7 +34,8 @@ enum { PROP_PORT }; -struct _GInetSocketAddressPrivate { +struct _GInetSocketAddressPrivate +{ GInetAddress *address; guint16 port; }; diff --git a/gnio/gnetworkinputstream.c b/gnio/gnetworkinputstream.c new file mode 100644 index 0000000..50eb1a6 --- /dev/null +++ b/gnio/gnetworkinputstream.c @@ -0,0 +1,60 @@ +/* GNIO - GLib Network Layer of GIO + * + * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: Christian Kellner + * Samuel Cormier-Iijima + */ + +#include +#include +#include + +#include +#ifndef G_OS_WIN32 +# include +# include +# include +#else +# include +# include +# include +# undef HAVE_GETADDRINFO +# define HAVE_GETHOSTBYNAME_THREADSAFE 1 +#endif +#include + +#include "ginetaddress.h" +#include "ginet4address.h" +#include "ginet6address.h" +#include "gnetworkinputstream.h" +#include "gnioerror.h" + +G_DEFINE_TYPE (GNetworkInputStream, g_network_input_stream, G_TYPE_INPUT_STREAM); + +static void +g_network_input_stream_class_init (GNetworkInputStreamClass *klass) +{ + GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass); +} + +static void +g_network_input_stream_init (GNetworkInputStream *address) +{ + +} diff --git a/gnio/gnetworkinputstream.h b/gnio/gnetworkinputstream.h new file mode 100644 index 0000000..06df919 --- /dev/null +++ b/gnio/gnetworkinputstream.h @@ -0,0 +1,61 @@ +/* GNIO - GLib Network Layer of GIO + * + * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: Christian Kellner + * Samuel Cormier-Iijima + */ + +#ifndef G_NETWORK_INPUT_STREAM_H +#define G_NETWORK_INPUT_STREAM_H + +#include +#include + +#include "gsocket.h" + +G_BEGIN_DECLS + +#define G_TYPE_NETWORK_INPUT_STREAM (g_socket_get_type ()) +#define G_NETWORK_INPUT_STREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_NETWORK_INPUT_STREAM, GNetworkInputStream)) +#define G_NETWORK_INPUT_STREAM_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_NETWORK_INPUT_STREAM, GNetworkInputStreamClass)) +#define G_IS_NETWORK_INPUT_STREAM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_NETWORK_INPUT_STREAM)) +#define G_IS_NETWORK_INPUT_STREAM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_NETWORK_INPUT_STREAM)) +#define G_NETWORK_INPUT_STREAM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_NETWORK_INPUT_STREAM, GNetworkInputStream)) + +typedef struct _GNetworkInputStream GNetworkInputStream; +typedef struct _GNetworkInputStreamClass GNetworkInputStreamClass; + +struct _GNetworkInputStream +{ + GInputStream parent; +}; + +struct _GNetworkInputStreamClass +{ + GInputStreamClass parent_class; +}; + +GType g_network_input_stream_get_type (void) G_GNUC_CONST; + +GNetworkInputStream * g_network_input_stream_new (GSocket *socket); + +G_END_DECLS + +#endif /* G_NETWORK_INPUT_STREAM_H */ + diff --git a/gnio/gnetworkoutputstream.c b/gnio/gnetworkoutputstream.c new file mode 100644 index 0000000..eecd2db --- /dev/null +++ b/gnio/gnetworkoutputstream.c @@ -0,0 +1,60 @@ +/* GNIO - GLib Network Layer of GIO + * + * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: Christian Kellner + * Samuel Cormier-Iijima + */ + +#include +#include +#include + +#include +#ifndef G_OS_WIN32 +# include +# include +# include +#else +# include +# include +# include +# undef HAVE_GETADDRINFO +# define HAVE_GETHOSTBYNAME_THREADSAFE 1 +#endif +#include + +#include "ginetaddress.h" +#include "ginet4address.h" +#include "ginet6address.h" +#include "gnetworkoutputstream.h" +#include "gnioerror.h" + +G_DEFINE_TYPE (GNetworkOutputStream, g_network_output_stream, G_TYPE_OUTPUT_STREAM); + +static void +g_network_output_stream_class_init (GNetworkOutputStreamClass *klass) +{ + GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass); +} + +static void +g_network_output_stream_init (GNetworkOutputStream *address) +{ + +} diff --git a/gnio/gnetworkoutputstream.h b/gnio/gnetworkoutputstream.h new file mode 100644 index 0000000..b3dfee0 --- /dev/null +++ b/gnio/gnetworkoutputstream.h @@ -0,0 +1,61 @@ +/* GNIO - GLib Network Layer of GIO + * + * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: Christian Kellner + * Samuel Cormier-Iijima + */ + +#ifndef G_NETWORK_OUTPUT_STREAM_H +#define G_NETWORK_OUTPUT_STREAM_H + +#include +#include + +#include "gsocket.h" + +G_BEGIN_DECLS + +#define G_TYPE_NETWORK_OUTPUT_STREAM (g_socket_get_type ()) +#define G_NETWORK_OUTPUT_STREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_NETWORK_OUTPUT_STREAM, GNetworkOutputStream)) +#define G_NETWORK_OUTPUT_STREAM_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_NETWORK_OUTPUT_STREAM, GNetworkOutputStreamClass)) +#define G_IS_NETWORK_OUTPUT_STREAM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_NETWORK_OUTPUT_STREAM)) +#define G_IS_NETWORK_OUTPUT_STREAM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_NETWORK_OUTPUT_STREAM)) +#define G_NETWORK_OUTPUT_STREAM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_NETWORK_OUTPUT_STREAM, GNetworkOutputStream)) + +typedef struct _GNetworkOutputStream GNetworkOutputStream; +typedef struct _GNetworkOutputStreamClass GNetworkOutputStreamClass; + +struct _GNetworkOutputStream +{ + GOutputStream parent; +}; + +struct _GNetworkOutputStreamClass +{ + GOutputStreamClass parent_class; +}; + +GType g_network_output_stream_get_type (void) G_GNUC_CONST; + +GNetworkOutputStream * g_network_output_stream_new (GSocket *socket); + +G_END_DECLS + +#endif /* G_NETWORK_OUTPUT_STREAM_H */ + diff --git a/gnio/gresolver.h b/gnio/gresolver.h index cfc5892..e91ced8 100644 --- a/gnio/gresolver.h +++ b/gnio/gresolver.h @@ -51,7 +51,7 @@ struct _GResolverClass GObjectClass parent_class; }; -GType g_resolver_get_type (void) G_GNUC_CONST; +GType g_resolver_get_type (void) G_GNUC_CONST; GInetAddress * g_resolver_resolve (GResolver *resolver, const char *host, diff --git a/gnio/gsocket.c b/gnio/gsocket.c index c6d1ce9..c0a8a1a 100644 --- a/gnio/gsocket.c +++ b/gnio/gsocket.c @@ -44,18 +44,39 @@ #include "ginet6address.h" #include "gsocket.h" #include "gnioerror.h" -#include "gsocket.h" +#include "ginetsocketaddress.h" G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT); +struct _GSocketPrivate +{ + int fd; +}; + static void g_socket_class_init (GSocketClass *klass) { GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (GSocketPrivate)); } static void g_socket_init (GSocket *address) { + address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address, G_TYPE_SOCKET, GSocketPrivate); +} + +GSocket * +g_socket_new () +{ + return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL)); +} + +void +g_socket_listen (GSocket *socket, gint backlog) +{ + g_return_if_fail (G_IS_SOCKET (socket)); + listen (socket->priv->fd, backlog); } diff --git a/gnio/gsocket.h b/gnio/gsocket.h index b7af2ee..4143d3e 100644 --- a/gnio/gsocket.h +++ b/gnio/gsocket.h @@ -39,12 +39,15 @@ G_BEGIN_DECLS #define G_IS_SOCKET_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_SOCKET)) #define G_SOCKET_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_SOCKET, GSocket)) -typedef struct _GSocket GSocket; -typedef struct _GSocketClass GSocketClass; +typedef struct _GSocket GSocket; +typedef struct _GSocketClass GSocketClass; +typedef struct _GSocketPrivate GSocketPrivate; struct _GSocket { GObject parent; + + GSocketPrivate *priv; }; struct _GSocketClass @@ -52,12 +55,13 @@ struct _GSocketClass GObjectClass parent_class; }; -GType g_socket_get_type (void) G_GNUC_CONST; +GType g_socket_get_type (void) G_GNUC_CONST; -GSocket * g_socket_new (); +GSocket * g_socket_new (void); -void g_socket_bind (GSocket *socket, - GSocketAddress *address); +gboolean g_socket_bind (GSocket *socket, + GSocketAddress *address, + GError **error); void g_socket_connect (GSocket *socket, GSocketAddress *address, -- 2.20.1