2 * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "iochannel.h"
31 hc_server_cb_destroy (gpointer cb)
33 g_slice_free (struct hc_server_cb, cb);
37 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
39 struct hc_server_cb *cb = data;
40 int fd = g_io_channel_unix_get_fd (channel);
42 socklen_t saddr = sizeof (addr);
43 int client = accept (fd, &addr, &saddr);
44 if (client >= 0 && cb->func)
45 cb->func (client, &addr, saddr, cb->data);
50 hc_server_add_watch (int fd,
54 struct hc_server_cb *cb;
55 cb = g_slice_new (struct hc_server_cb);
56 cb->channel = g_io_channel_unix_new (fd);
59 g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
60 hc_server_watch, cb, hc_server_cb_destroy);
68 ssize_t (*read) (gpointer, char *, size_t);
69 ssize_t (*write) (gpointer, char *, size_t);
70 void (*close) (gpointer);
76 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
78 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
79 return read (fd, buffer, len);
83 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
85 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
86 return write (fd, buffer, len);
90 hc_conn_channel_close (gpointer data)
92 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
93 shutdown (fd, SHUT_RDWR);
97 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
100 HCEvent event = HC_EVENT_READ;
102 conn->func (conn, event, conn->data);
107 hc_conn_new (int fd, HCClientFunc func, gpointer data)
110 conn = g_slice_new (HCConn);
111 conn->channel = g_io_channel_unix_new (fd);
114 conn->layer = conn->channel;
115 conn->read = hc_conn_channel_read;
116 conn->write = hc_conn_channel_write;
117 conn->close = hc_conn_channel_close;
118 conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn);
123 hc_conn_read (HCConn *conn, char *buffer, size_t len)
125 return conn->read (conn->layer, buffer, len);
129 hc_conn_write (HCConn *conn, char *buffer, size_t len)
131 /* TODO: Do buffering or something like that */
132 conn->write (conn->layer, buffer, len);
136 hc_conn_close (HCConn *conn)
138 conn->close (conn->layer);
139 g_source_remove (conn->watch);
140 g_io_channel_unref (conn->channel);
141 g_slice_free (HCConn, conn);