From: Samuel Cormier-Iijima Date: Fri, 29 Feb 2008 01:09:01 +0000 (-0500) Subject: Finish up making domain, type, protocol gobject properties X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fgnio.git;a=commitdiff_plain;h=b9453dd5e1e0a4c94a766bc741dc7d4a9457f686 Finish up making domain, type, protocol gobject properties --- diff --git a/gnio/gsocket.c b/gnio/gsocket.c index 0ca9091..cfeaa73 100644 --- a/gnio/gsocket.c +++ b/gnio/gsocket.c @@ -81,7 +81,91 @@ struct _GSocketPrivate static void g_socket_constructed (GObject *object) { + GSocket *sock = G_SOCKET (object); + GError *error = NULL; + static GStaticMutex getprotobyname_mutex = G_STATIC_MUTEX_INIT; + gint fd, native_domain, native_type, native_protocol; + + if (sock->priv->fd >= 0) + { + // we've been constructed from an existing file descriptor + glong arg; + gboolean blocking; + + // TODO: set the socket type with getsockopt (SO_TYPE) + // TODO: what should we do about domain and protocol? + + if ((arg = fcntl (sock->priv->fd, F_GETFL, NULL)) < 0) + g_warning ("Error getting socket status flags: %s", g_strerror (errno)); + + blocking = ((arg & O_NONBLOCK) == 0); + + return; + } + + switch (sock->priv->domain) + { + case G_SOCKET_DOMAIN_INET: + native_domain = PF_INET; + break; + + case G_SOCKET_DOMAIN_INET6: + native_domain = PF_INET6; + break; + + case G_SOCKET_DOMAIN_UNIX: + native_domain = PF_UNIX; + break; + + default: + g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket domain"); + return; + } + + switch (sock->priv->type) + { + case G_SOCKET_TYPE_STREAM: + native_type = SOCK_STREAM; + break; + + case G_SOCKET_TYPE_DATAGRAM: + native_type = SOCK_DGRAM; + break; + + case G_SOCKET_TYPE_SEQPACKET: + native_type = SOCK_SEQPACKET; + break; + default: + g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket type"); + return; + } + + if (sock->priv->protocol == NULL) + native_protocol = 0; + else + { + struct protoent *ent; + g_static_mutex_lock (&getprotobyname_mutex); + if (!(ent = getprotobyname (sock->priv->protocol))) + { + g_static_mutex_unlock (&getprotobyname_mutex); + g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket protocol"); + return; + } + native_protocol = ent->p_proto; + g_static_mutex_unlock (&getprotobyname_mutex); + } + + fd = socket (native_domain, native_type, native_protocol); + + if (fd < 0) + { + g_set_error (&error, G_IO_ERROR, g_io_error_from_errno (errno), "unable to create socket: %s", g_strerror (errno)); + return; + } + + sock->priv->fd = fd; } static void @@ -92,11 +176,11 @@ 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); + g_value_set_enum (value, socket->priv->domain); break; case PROP_TYPE: - g_value_set_int (value, socket->priv->type); + g_value_set_enum (value, socket->priv->type); break; case PROP_PROTOCOL: @@ -139,6 +223,18 @@ g_socket_set_property (GObject *object, guint prop_id, const GValue *value, GPar switch (prop_id) { + case PROP_DOMAIN: + socket->priv->domain = g_value_get_enum (value); + break; + + case PROP_TYPE: + socket->priv->type = g_value_get_enum (value); + break; + + case PROP_PROTOCOL: + socket->priv->protocol = g_value_dup_string (value); + break; + case PROP_FD: socket->priv->fd = g_value_get_int (value); break; @@ -180,6 +276,10 @@ g_socket_dispose (GObject *object) { GSocket *socket = G_SOCKET (object); + g_free (socket->priv->protocol); + + g_clear_error (&socket->priv->error); + g_socket_close (socket); if (G_OBJECT_CLASS (g_socket_parent_class)->dispose) @@ -202,31 +302,27 @@ g_socket_class_init (GSocketClass *klass) 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_param_spec_enum ("domain", + "socket domain", + "the socket's domain", + G_TYPE_SOCKET_DOMAIN, + G_SOCKET_DOMAIN_INET, + 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_param_spec_enum ("type", + "socket type", + "the socket's type", + G_TYPE_SOCKET_TYPE, + G_SOCKET_TYPE_STREAM, + 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_param_spec_string ("protocol", + "socket protocol", + "the socket's protocol", + 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_FD, g_param_spec_int ("fd", @@ -292,89 +388,13 @@ g_socket_init (GSocket *socket) GSocket * 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; - - switch (domain) - { - case G_SOCKET_DOMAIN_INET: - native_domain = PF_INET; - break; - - case G_SOCKET_DOMAIN_INET6: - native_domain = PF_INET6; - break; - - case G_SOCKET_DOMAIN_UNIX: - native_domain = PF_UNIX; - break; - - default: - g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket domain"); - return NULL; - } - - switch (type) - { - case G_SOCKET_TYPE_STREAM: - native_type = SOCK_STREAM; - break; - - case G_SOCKET_TYPE_DATAGRAM: - native_type = SOCK_DGRAM; - break; - - case G_SOCKET_TYPE_SEQPACKET: - native_type = SOCK_SEQPACKET; - break; - - default: - g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket type"); - return NULL; - } - - if (protocol == NULL) - native_protocol = 0; - else - { - struct protoent *ent; - g_static_mutex_lock (&getprotobyname_mutex); - 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"); - return NULL; - } - native_protocol = ent->p_proto; - g_static_mutex_unlock (&getprotobyname_mutex); - } - - fd = socket(native_domain, native_type, native_protocol); - - if (fd < 0) - { - g_set_error (&error, G_IO_ERROR, g_io_error_from_errno (errno), "unable to create socket: %s", g_strerror (errno)); - return NULL; - } - - g_print ("finishing socket_new\n"); - - return G_SOCKET (g_object_new (G_TYPE_SOCKET, "blocking", TRUE, "fd", fd, NULL)); + return G_SOCKET (g_object_new (G_TYPE_SOCKET, "domain", domain, "type", type, "protocol", protocol, NULL)); } GSocket * g_socket_new_from_fd (gint fd) { - glong arg; - gboolean blocking; - - if ((arg = fcntl (fd, F_GETFL, NULL)) < 0) - g_warning ("Error getting socket status flags: %s", g_strerror (errno)); - - blocking = ((arg & O_NONBLOCK) == 0); - - return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, "blocking", blocking, NULL)); + return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, NULL)); } void