loudmouth/lm-socket.c
changeset 149 54e67338c784
parent 148 255733936c01
child 150 18a91b086713
--- a/loudmouth/lm-socket.c	Wed May 31 21:05:11 2006 +0000
+++ b/loudmouth/lm-socket.c	Fri Jun 02 23:02:20 2006 +0000
@@ -20,63 +20,148 @@
 
 #include "lm-socket.h"
 
-struct _LmSock {
-	gint fd;
+#ifndef G_OS_WIN32
+typedef int LmSock;
+#else  /* G_OS_WIN32 */
+typedef SOCKET LmSock;
+#endif /* G_OS_WIN32 */
+
+/* FIXME: Integrate with the SSL stuff */
+
+struct _LmSocket {
+	LmSock sock;
 	/* FIXME: Add the rest */
 	
-	LmSockFuncs funcs;
+	LmSocketFuncs funcs;
 	
-	LmSockState state;
+	LmSocketState state;
+
+	gboolean is_blocking;
 	
 	gint ref;
 };
 
-static void sock_free (LmSock *sock);
+static LmSocket *  socket_create (void);
+static void        socket_free   (LmSocket *socket);
+
+static LmSocket *
+socket_create (void)
+{
+	LmSocket *socket;
+
+	socket = g_new0 (LmSocket, 1);
+	socket->ref_count = 1;
+	socket->is_blocking = FALSE;
+	
+	return socket;
+}
 
 static void
-sock_free (LmSock *sock)
+socket_free (LmSocket *socket)
 {
 	/* FIXME: Free up the rest of the memory */
-	g_free (sock);
+	g_free (socket->host);
+
+	g_free (socket);
 }
 
+LmSocket *
+lm_socket_new (LmSocketFuncs funcs, const gchar *host, guint port)
+{
+	LmSocket *socket;
 
-LmSock *   lm_sock_new               (LmSockFuncs  funcs,
-				      const gchar *host,
-				      guint        port);
-void       lm_sock_open              (LmSock      *sock);									
+	socket = socket_create ();
+
+	socket->funcs = funcs;
+	socket->host = g_strdup (host);
+	socket->port = port;
+
+	return socket;
+}
 
-int        lm_sock_get_fd            (LmSock   *sock);
-gboolean   lm_sock_get_is_blocking   (LmSock   *sock);
-void       lm_sock_set_is_blocking   (LmSock   *sock,
-				      gboolean  is_block);
-int        lm_sock_write             (LmSock   *sock,
-				      gsize     size,
-				      gchar    *buf,
-				      GError  **error);
-int        lm_sock_read              (LmSock   *sock,
-				      gsize     size,
-				      gchar    *buf,
-				      GError  **error);
-gboolean   lm_sock_close             (LmSock   *sock,
-				      GError  **error);
-LmSock *
-lm_sock_ref (LmSock *sock)
+void
+lm_socket_open (LmSocket *socket)
+{
+}
+
+int
+lm_socket_get_fd (LmSocket *socket)
 {
-	g_return_val_if_fail (sock != NULL, NULL);
-	sock->ref++;
-	
-	return sock;
+	g_return_val_if_fail (socket != NULL, -1);
+
+	return socket->fd;
+}
+
+gboolean
+lm_socket_get_is_blocking (LmSocket *socket)
+{
+	return socket->is_blocking;
 }
 
 void
-lm_sock_unref (LmSock *sock)
+lm_socket_set_is_blocking (LmSocket *socket, gboolean is_block)
 {
-	g_return_if_fail (sock != NULL);
+	int res;
+
+	g_return_if_fail (socket != NULL);
+
+	/* FIXME: Don't unset all flags */
+
+#ifndef G_OS_WIN32
+	res = fcntl (socket->sock, F_SETFL, is_block ? 0 : O_NONBLOCK);
+#else  /* G_OS_WIN32 */
+	u_long mode = (is_block ? 0 : 1);
+	res = ioctlsocket (socket->sock, FIONBIO, &mode);
+#endif /* G_OS_WIN32 */
+
+	if (res != 0) {
+		g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
+		       "Could not set socket to be %s\n",
+		       block ? "blocking" : "non-blocking");
+	}
+
+	socket->is_blocking = is_block;
+}
+
+int
+lm_socket_write (LmSocket   *socket,
+		 gsize       size,
+		 gchar      *buf,
+		 GError    **error)
+{
+}
+
+int
+lm_socket_read (LmSocket   *socket,
+		gsize       size,
+		gchar      *buf,
+		GError    **error)
+{
+}
+
+gboolean
+lm_socket_close (LmSocket *socket, GError **error)
+{
+}
+
+LmSocket *
+lm_socket_ref (LmSocket *socket)
+{
+	g_return_val_if_fail (socket != NULL, NULL);
+	socket->ref++;
 	
-	sock->ref--;
+	return socket;
+}
+
+void
+lm_socket_unref (LmSocket *socket)
+{
+	g_return_if_fail (socket != NULL);
 	
-	if (sock->ref <= 0) {
-		sock_free (sock);
+	socket->ref--;
+	
+	if (socket->ref <= 0) {
+		socket_free (socket);
 	}
 }
+