Some more stuff for GSockets
[cascardo/gnio.git] / gnio / gsocket.c
index 3cf7534..fe93ca8 100644 (file)
@@ -31,6 +31,7 @@
 # include <arpa/inet.h>
 # include <netdb.h>
 # include <fcntl.h>
+# include <unistd.h>
 #else
 
 #endif
@@ -48,7 +49,8 @@ G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
 enum
 {
   PROP_0,
-  PROP_FD
+  PROP_FD,
+  PROP_BLOCKING
 };
 
 struct _GSocketPrivate
@@ -68,6 +70,10 @@ g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec
         g_value_set_int (value, socket->priv->fd);
         break;
 
+      case PROP_BLOCKING:
+        g_value_set_boolean (value, socket->priv->blocking);
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -84,6 +90,10 @@ g_socket_set_property (GObject *object, guint prop_id, const GValue *value, GPar
         socket->priv->fd = g_value_get_int (value);
         break;
 
+      case PROP_BLOCKING:
+        g_socket_set_blocking (socket, g_value_get_boolean (value));
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -118,6 +128,22 @@ g_socket_class_init (GSocketClass *klass)
   gobject_class->dispose = g_socket_dispose;
   gobject_class->set_property = g_socket_set_property;
   gobject_class->get_property = g_socket_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_FD,
+                                   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_BLOCKING,
+                                   g_param_spec_boolean ("blocking",
+                                                         "blocking",
+                                                         "whether or not this socket is blocking",
+                                                         FALSE,
+                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
 }
 
 static void
@@ -145,7 +171,15 @@ g_socket_new (gint domain, gint type, gint protocol)
 GSocket *
 g_socket_new_from_fd (gint fd)
 {
-  return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, NULL));
+  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, "blocking", blocking, "fd", fd, NULL));
 }
 
 void
@@ -165,6 +199,22 @@ g_socket_set_blocking (GSocket  *socket,
     g_warning ("Error setting socket status flags: %s", g_strerror (errno));
 }
 
+GSocketAddress *
+g_socket_get_peer_address (GSocket  *socket,
+                           GError  **error)
+{
+  gchar buffer[128];
+  gsize len;
+
+  if (getpeername (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
+    {
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get peer address");
+      return NULL;
+    }
+
+  return g_socket_address_from_native (buffer, len);
+}
+
 void
 g_socket_listen (GSocket *socket,
                  gint     backlog)
@@ -199,6 +249,48 @@ g_socket_bind (GSocket         *socket,
   }
 }
 
+GSocket *
+g_socket_accept (GSocket       *socket,
+                 GCancellable  *cancellable,
+                 GError       **error)
+{
+  gint ret;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, 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");
+      return NULL;
+    }
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    {
+      close (ret);
+      return NULL;
+    }
+
+  return g_socket_new_from_fd (ret);
+}
+
+void
+g_socket_accept_async (GSocket             *socket,
+                       GCancellable        *cancellable,
+                       GAsyncReadyCallback *callback,
+                       gpointer             user_data)
+{
+  
+}
+
+GSocket *
+g_socket_accept_finish (GSocket       *socket,
+                        GAsyncResult  *result,
+                        GError       **error)
+{
+  return NULL;
+}
+
 gboolean
 g_socket_connect (GSocket         *socket,
                   GSocketAddress  *address,