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"
25 /* The server connection watch */
35 hc_server_cb_destroy (gpointer cb)
37 g_slice_free (struct hc_server_cb, cb);
41 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
43 struct hc_server_cb *cb = data;
44 int fd = g_io_channel_unix_get_fd (channel);
46 socklen_t saddr = sizeof (addr);
47 int client = accept (fd, &addr, &saddr);
48 if (client >= 0 && cb->func)
49 cb->func (client, &addr, saddr, cb->data);
54 hc_server_add_watch (int fd,
58 struct hc_server_cb *cb;
59 cb = g_slice_new (struct hc_server_cb);
60 cb->channel = g_io_channel_unix_new (fd);
63 /* TODO: we should have some way to remove this watch */
64 g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
65 hc_server_watch, cb, hc_server_cb_destroy);
69 /* The IOChannel (simple socket) layer */
78 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
80 struct channel_layer *layer = data;
81 int fd = g_io_channel_unix_get_fd (layer->channel);
82 return read (fd, buffer, len);
86 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
88 struct channel_layer *layer = data;
89 int fd = g_io_channel_unix_get_fd (layer->channel);
90 return write (fd, buffer, len);
94 hc_conn_channel_close (gpointer data)
96 struct channel_layer *layer = data;
97 int fd = g_io_channel_unix_get_fd (layer->channel);
98 g_source_remove (layer->watch);
99 shutdown (fd, SHUT_RDWR);
100 g_io_channel_unref (layer->channel);
101 g_slice_free (struct channel_layer, layer);
105 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
108 /* TODO: What about other events, like closing? */
109 HCEvent event = HC_EVENT_READ;
111 conn->func (conn, event, conn->data);
116 hc_conn_set_driver_channel (HCConn *conn, int fd)
118 struct channel_layer *layer = g_slice_new (struct channel_layer);
119 layer->channel = g_io_channel_unix_new (fd);
121 conn->read = hc_conn_channel_read;
122 conn->write = hc_conn_channel_write;
123 conn->close = hc_conn_channel_close;
124 /* TODO: We must watch other events */
125 layer->watch = g_io_add_watch (layer->channel, G_IO_IN, hc_conn_watch, conn);
126 /* TODO: connection should be asynchronous so this could make sense */
128 conn->func (conn, HC_EVENT_CONNECT, conn->data);
129 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
133 /* The core connection system */
136 hc_conn_new (HCClientFunc func, gpointer data)
139 conn = g_slice_new (HCConn);
146 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
153 hc_conn_read (HCConn *conn, char *buffer, size_t len)
155 return conn->read (conn->layer, buffer, len);
159 hc_conn_write (HCConn *conn, char *buffer, size_t len)
161 /* TODO: Do buffering or something like that */
162 /* Do we really need to? */
163 /* In case of error, we should do something */
164 conn->write (conn->layer, buffer, len);
168 hc_conn_close (HCConn *conn)
170 conn->close (conn->layer);
171 g_slice_free (HCConn, conn);