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);
73 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
75 struct channel_layer *layer = data;
76 int fd = g_io_channel_unix_get_fd (layer->channel);
77 return read (fd, buffer, len);
81 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
83 struct channel_layer *layer = data;
84 int fd = g_io_channel_unix_get_fd (layer->channel);
85 return write (fd, buffer, len);
89 hc_conn_channel_close (gpointer data)
91 struct channel_layer *layer = data;
92 int fd = g_io_channel_unix_get_fd (layer->channel);
93 g_source_remove (layer->watch);
94 shutdown (fd, SHUT_RDWR);
95 g_io_channel_unref (layer->channel);
96 g_slice_free (struct channel_layer, layer);
100 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
103 HCEvent event = HC_EVENT_READ;
105 conn->func (conn, event, conn->data);
110 hc_conn_set_driver_channel (HCConn *conn, int fd)
112 struct channel_layer *layer = g_slice_new (struct channel_layer);
113 layer->channel = g_io_channel_unix_new (fd);
115 conn->read = hc_conn_channel_read;
116 conn->write = hc_conn_channel_write;
117 conn->close = hc_conn_channel_close;
118 layer->watch = g_io_add_watch (layer->channel, G_IO_IN, hc_conn_watch, conn);
120 conn->func (conn, HC_EVENT_CONNECT, conn->data);
121 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
125 hc_conn_new (HCClientFunc func, gpointer data)
128 conn = g_slice_new (HCConn);
135 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
142 hc_conn_read (HCConn *conn, char *buffer, size_t len)
144 return conn->read (conn->layer, buffer, len);
148 hc_conn_write (HCConn *conn, char *buffer, size_t len)
150 /* TODO: Do buffering or something like that */
151 conn->write (conn->layer, buffer, len);
155 hc_conn_close (HCConn *conn)
157 conn->close (conn->layer);
158 g_slice_free (HCConn, conn);