Converting InetAddresses to native struct in*_addr and SocketAddresses to struct...
[cascardo/gnio.git] / gnio / gsocket.c
index c0a8a1a..42b6394 100644 (file)
 # include <netinet/in.h>
 # include <arpa/inet.h>
 # include <netdb.h>
+# include <fcntl.h>
 #else
-# include <winsock2.h>
-# include <winerror.h>
-# include <ws2tcpip.h>
-# undef HAVE_GETADDRINFO
-# define HAVE_GETHOSTBYNAME_THREADSAFE 1
+
 #endif
 #include <errno.h>
 
@@ -53,12 +50,33 @@ struct _GSocketPrivate
   int fd;
 };
 
+static void
+g_socket_finalize (GObject *object)
+{
+  GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);
+
+  if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
+    (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
+}
+
+static void
+g_socket_dispose (GObject *object)
+{
+  GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);;
+
+  if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
+    (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
+}
+
 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));
+
+  gobject_class->finalize = g_socket_finalize;
+  gobject_class->dispose = g_socket_dispose;
 }
 
 static void
@@ -73,6 +91,28 @@ g_socket_new ()
   return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL));
 }
 
+GSocket *
+g_socket_new_from_fd (gint fd)
+{
+  return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL));
+}
+
+void
+g_socket_set_blocking (GSocket *socket, gboolean blocking)
+{
+  glong arg;
+
+  g_return_if_fail (G_IS_SOCKET (socket));
+
+  if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
+    g_warning ("Error getting socket status flags: %s", g_strerror (errno));
+
+  arg = blocking ? arg | O_NONBLOCK : arg & ~O_NONBLOCK;
+
+  if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
+    g_warning ("Error setting socket status flags: %s", g_strerror (errno));
+}
+
 void
 g_socket_listen (GSocket *socket, gint backlog)
 {
@@ -80,3 +120,41 @@ g_socket_listen (GSocket *socket, gint backlog)
 
   listen (socket->priv->fd, backlog);
 }
+
+gboolean
+g_socket_connect (GSocket         *socket,
+                  GSocketAddress  *address,
+                  GCancellable    *cancellable,
+                  GError         **error)
+{
+  g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
+/*
+  if (connect () < 0)
+    {
+      if (errno == EINPROGRESS)
+        g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
+      else
+        g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
+      return FALSE;
+    }
+*/
+  return TRUE;
+}
+
+void
+g_socket_connect_async (GSocket             *socket,
+                        GSocketAddress      *address,
+                        GCancellable        *cancellable,
+                        GAsyncReadyCallback *callback,
+                        gpointer             user_data)
+{
+
+}
+
+gboolean
+g_socket_connect_finish (GSocket       *socket,
+                         GAsyncResult  *result,
+                         GError       **error)
+{
+
+}