From: Thadeu Lima de Souza Cascardo Date: Thu, 2 Jul 2009 16:08:06 +0000 (-0300) Subject: Added watch for server connections. X-Git-Tag: v0.1.3~73 X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Frnetproxy.git;a=commitdiff_plain;h=e82a48ecf7514b90b01adcb3ad4d6b05066a2027 Added watch for server connections. Register a callback to be called when a server socket receives a connection. --- diff --git a/iochannel.c b/iochannel.c new file mode 100644 index 0000000..a380a7c --- /dev/null +++ b/iochannel.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo + * + * 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 index 0000000..daf0bc1 --- /dev/null +++ b/iochannel.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo + * + * 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 +#include + +typedef void (*HCServerFunc) (int, struct sockaddr*, socklen_t, gpointer); + +void hc_server_add_watch (int, HCServerFunc, gpointer); + +#endif