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.
32 hc_server_cb_destroy (gpointer cb)
34 g_slice_free (struct hc_server_cb, cb);
38 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
40 struct hc_server_cb *cb = data;
41 int fd = g_io_channel_unix_get_fd (channel);
43 socklen_t saddr = sizeof (addr);
44 int client = accept (fd, &addr, &saddr);
45 if (client >= 0 && cb->func)
46 cb->func (client, &addr, saddr, cb->data);
51 hc_server_add_watch (int fd,
55 struct hc_server_cb *cb;
56 cb = g_slice_new (struct hc_server_cb);
57 cb->channel = g_io_channel_unix_new (fd);
60 g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
61 hc_server_watch, cb, hc_server_cb_destroy);
69 ssize_t (*read) (gpointer, char *, size_t);
70 ssize_t (*write) (gpointer, char *, size_t);
71 void (*close) (gpointer);
77 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
79 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
80 return read (fd, buffer, len);
84 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
86 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
87 return write (fd, buffer, len);
91 hc_conn_channel_close (gpointer data)
93 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
94 shutdown (fd, SHUT_RDWR);
98 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
101 HCEvent event = HC_EVENT_READ;
103 conn->func (conn, event, conn->data);
108 hc_conn_new (int fd, HCClientFunc func, gpointer data)
111 conn = g_slice_new (HCConn);
112 conn->channel = g_io_channel_unix_new (fd);
115 conn->layer = conn->channel;
116 conn->read = hc_conn_channel_read;
117 conn->write = hc_conn_channel_write;
118 conn->close = hc_conn_channel_close;
119 conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn);
121 conn->func (conn, HC_EVENT_CONNECT, conn->data);
122 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
127 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
134 hc_conn_read (HCConn *conn, char *buffer, size_t len)
136 return conn->read (conn->layer, buffer, len);
140 hc_conn_write (HCConn *conn, char *buffer, size_t len)
142 /* TODO: Do buffering or something like that */
143 conn->write (conn->layer, buffer, len);
147 hc_conn_close (HCConn *conn)
149 conn->close (conn->layer);
150 g_source_remove (conn->watch);
151 g_io_channel_unref (conn->channel);
152 g_slice_free (HCConn, conn);