Rename iochannel to hcconn.
[cascardo/rnetproxy.git] / hcconn.c
diff --git a/hcconn.c b/hcconn.c
new file mode 100644 (file)
index 0000000..5c6e60e
--- /dev/null
+++ b/hcconn.c
@@ -0,0 +1,153 @@
+/*
+ *  Copyright (C) 2009  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include "hcconn.h"
+#include <unistd.h>
+#include <fcntl.h>
+
+struct hc_server_cb
+{
+  GIOChannel *channel;
+  HCServerFunc func;
+  gpointer data;
+};
+
+static void
+hc_server_cb_destroy (gpointer cb)
+{
+  g_slice_free (struct hc_server_cb, cb);
+}
+
+static gboolean
+hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
+{
+  struct hc_server_cb *cb = data;
+  int fd = g_io_channel_unix_get_fd (channel);
+  struct sockaddr addr;
+  socklen_t saddr = sizeof (addr);
+  int client = accept (fd, &addr, &saddr);
+  if (client >= 0 && cb->func)
+    cb->func (client, &addr, saddr, cb->data);
+  return TRUE;
+}
+
+void
+hc_server_add_watch (int fd,
+                     HCServerFunc func,
+                     gpointer data)
+{
+  struct hc_server_cb *cb;
+  cb = g_slice_new (struct hc_server_cb);
+  cb->channel = g_io_channel_unix_new (fd);
+  cb->func = func;
+  cb->data = data;
+  g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
+                       hc_server_watch, cb, hc_server_cb_destroy);
+}
+
+struct _hc_conn_t
+{
+  GIOChannel *channel;
+  HCClientFunc func;
+  gpointer data;
+  ssize_t (*read) (gpointer, char *, size_t);
+  ssize_t (*write) (gpointer, char *, size_t);
+  void (*close) (gpointer);
+  gpointer layer;
+  guint watch;
+};
+
+ssize_t
+hc_conn_channel_read (gpointer data, char *buffer, size_t len)
+{
+  int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
+  return read (fd, buffer, len);
+}
+
+ssize_t
+hc_conn_channel_write (gpointer data, char *buffer, size_t len)
+{
+  int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
+  return write (fd, buffer, len);
+}
+
+void
+hc_conn_channel_close (gpointer data)
+{
+  int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
+  shutdown (fd, SHUT_RDWR);
+}
+
+gboolean
+hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
+{
+  HCConn *conn = data;
+  HCEvent event = HC_EVENT_READ;
+  if (conn->func)
+    conn->func (conn, event, conn->data);
+  return TRUE;
+}
+
+HCConn *
+hc_conn_new (int fd, HCClientFunc func, gpointer data)
+{
+  HCConn *conn;
+  conn = g_slice_new (HCConn);
+  conn->channel = g_io_channel_unix_new (fd);
+  conn->func = func;
+  conn->data = data;
+  conn->layer = conn->channel;
+  conn->read = hc_conn_channel_read;
+  conn->write = hc_conn_channel_write;
+  conn->close = hc_conn_channel_close;
+  conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn);
+  if (conn->func)
+    conn->func (conn, HC_EVENT_CONNECT, conn->data);
+  fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
+  return conn;
+}
+
+void
+hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
+{
+  conn->func = func;
+  conn->data = data;
+}
+
+ssize_t
+hc_conn_read (HCConn *conn, char *buffer, size_t len)
+{
+  return conn->read (conn->layer, buffer, len);
+}
+
+void
+hc_conn_write (HCConn *conn, char *buffer, size_t len)
+{
+  /* TODO: Do buffering or something like that */
+  conn->write (conn->layer, buffer, len);
+}
+
+void
+hc_conn_close (HCConn *conn)
+{
+  conn->close (conn->layer);
+  g_source_remove (conn->watch);
+  g_io_channel_unref (conn->channel);
+  g_slice_free (HCConn, conn);
+}