Added watch for server connections.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Thu, 2 Jul 2009 16:08:06 +0000 (13:08 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Thu, 2 Jul 2009 16:08:06 +0000 (13:08 -0300)
Register a callback to be called when a server socket receives a
connection.

iochannel.c [new file with mode: 0644]
iochannel.h [new file with mode: 0644]

diff --git a/iochannel.c b/iochannel.c
new file mode 100644 (file)
index 0000000..a380a7c
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ *  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 "iochannel.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);
+}
diff --git a/iochannel.h b/iochannel.h
new file mode 100644 (file)
index 0000000..daf0bc1
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ *  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.
+ */
+
+
+#ifndef HC_IOCHANNEL_H
+#define HC_IOCHANNEL_H
+
+#include <glib.h>
+#include <sys/socket.h>
+
+typedef void (*HCServerFunc) (int, struct sockaddr*, socklen_t, gpointer);
+
+void hc_server_add_watch (int, HCServerFunc, gpointer);
+
+#endif