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.
23 #include "hcconn_internal.h"
33 hc_server_cb_destroy (gpointer cb)
35 g_slice_free (struct hc_server_cb, cb);
39 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
41 struct hc_server_cb *cb = data;
42 int fd = g_io_channel_unix_get_fd (channel);
44 socklen_t saddr = sizeof (addr);
45 int client = accept (fd, &addr, &saddr);
46 if (client >= 0 && cb->func)
47 cb->func (client, &addr, saddr, cb->data);
52 hc_server_add_watch (int fd,
56 struct hc_server_cb *cb;
57 cb = g_slice_new (struct hc_server_cb);
58 cb->channel = g_io_channel_unix_new (fd);
61 g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
62 hc_server_watch, cb, hc_server_cb_destroy);
66 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
68 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
69 return read (fd, buffer, len);
73 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
75 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
76 return write (fd, buffer, len);
80 hc_conn_channel_close (gpointer data)
82 int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
83 shutdown (fd, SHUT_RDWR);
87 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
90 HCEvent event = HC_EVENT_READ;
92 conn->func (conn, event, conn->data);
97 hc_conn_new (int fd, HCClientFunc func, gpointer data)
100 conn = g_slice_new (HCConn);
101 conn->channel = g_io_channel_unix_new (fd);
104 conn->layer = conn->channel;
105 conn->read = hc_conn_channel_read;
106 conn->write = hc_conn_channel_write;
107 conn->close = hc_conn_channel_close;
108 conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn);
110 conn->func (conn, HC_EVENT_CONNECT, conn->data);
111 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
116 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
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);