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
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:
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;
{
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)
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",
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