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 <sys/socket.h>
24 #include "hcconn_internal.h"
26 /* The server connection watch */
36 hc_server_cb_destroy (gpointer cb)
38 g_slice_free (struct hc_server_cb, cb);
42 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
44 struct hc_server_cb *cb = data;
45 int fd = g_io_channel_unix_get_fd (channel);
47 socklen_t saddr = sizeof (addr);
48 int client = accept (fd, &addr, &saddr);
49 if (client >= 0 && cb->func)
50 cb->func (client, &addr, saddr, cb->data);
55 hc_server_add_watch (int fd,
59 struct hc_server_cb *cb;
60 cb = g_slice_new (struct hc_server_cb);
61 cb->channel = g_io_channel_unix_new (fd);
64 /* TODO: we should have some way to remove this watch */
65 g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
66 hc_server_watch, cb, hc_server_cb_destroy);
70 /* The IOChannel (simple socket) layer */
79 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
81 struct channel_layer *layer = data;
82 int fd = g_io_channel_unix_get_fd (layer->channel);
83 return read (fd, buffer, len);
87 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
89 struct channel_layer *layer = data;
90 int fd = g_io_channel_unix_get_fd (layer->channel);
91 return write (fd, buffer, len);
95 hc_conn_channel_close (gpointer data)
97 struct channel_layer *layer = data;
98 int fd = g_io_channel_unix_get_fd (layer->channel);
99 g_source_remove (layer->watch);
101 g_io_channel_unref (layer->channel);
102 g_slice_free (struct channel_layer, layer);
106 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
109 /* TODO: What about other events, like closing? */
111 int fd = g_io_channel_unix_get_fd (channel);
116 event = HC_EVENT_READ;
117 r = recv (fd, &buffer, 1, MSG_PEEK);
120 event = HC_EVENT_CLOSE;
124 /* FIXME: create HC_EVENT_ERROR */
125 event = HC_EVENT_CLOSE;
128 else if (cond & G_IO_HUP)
130 event = HC_EVENT_CLOSE;
132 else if (cond & G_IO_ERR)
134 /* FIXME: create HC_EVENT_ERROR */
135 event = HC_EVENT_CLOSE;
139 /* TODO: handle other conditions and create error event */
140 g_warning ("Received an unexpected IO condition.");
143 conn->func (conn, event, conn->data);
148 hc_conn_set_driver_channel (HCConn *conn, int fd)
150 struct channel_layer *layer = g_slice_new (struct channel_layer);
151 layer->channel = g_io_channel_unix_new (fd);
153 conn->read = hc_conn_channel_read;
154 conn->write = hc_conn_channel_write;
155 conn->close = hc_conn_channel_close;
156 /* TODO: We must watch other events */
157 layer->watch = g_io_add_watch (layer->channel,
158 G_IO_IN | G_IO_HUP | G_IO_ERR,
159 hc_conn_watch, conn);
160 /* TODO: connection should be asynchronous so this could make sense */
162 conn->func (conn, HC_EVENT_CONNECT, conn->data);
163 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
168 /* The core connection system */
171 hc_conn_new (HCClientFunc func, gpointer data)
174 conn = g_slice_new (HCConn);
181 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
188 hc_conn_read (HCConn *conn, char *buffer, size_t len)
191 return conn->read (conn->layer, buffer, len);
196 hc_conn_write (HCConn *conn, char *buffer, size_t len)
198 /* TODO: Do buffering or something like that */
199 /* Do we really need to? */
200 /* In case of error, we should do something */
202 conn->write (conn->layer, buffer, len);
206 hc_conn_close (HCConn *conn)
209 conn->close (conn->layer);
214 g_slice_free (HCConn, conn);