#include <fcntl.h>
#include "hcconn_internal.h"
+/* The server connection watch */
+
struct hc_server_cb
{
GIOChannel *channel;
cb->channel = g_io_channel_unix_new (fd);
cb->func = func;
cb->data = data;
+ /* TODO: we should have some way to remove this watch */
g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
hc_server_watch, cb, hc_server_cb_destroy);
}
+
+/* The IOChannel (simple socket) layer */
+
struct channel_layer
{
GIOChannel *channel;
guint watch;
};
-
ssize_t
hc_conn_channel_read (gpointer data, char *buffer, size_t len)
{
hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
{
HCConn *conn = data;
+ /* TODO: What about other events, like closing? */
HCEvent event = HC_EVENT_READ;
if (conn->func)
conn->func (conn, event, conn->data);
conn->read = hc_conn_channel_read;
conn->write = hc_conn_channel_write;
conn->close = hc_conn_channel_close;
+ /* TODO: We must watch other events */
layer->watch = g_io_add_watch (layer->channel, G_IO_IN, hc_conn_watch, conn);
+ /* TODO: connection should be asynchronous so this could make sense */
if (conn->func)
conn->func (conn, HC_EVENT_CONNECT, conn->data);
fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
}
+
+/* The core connection system */
+
HCConn *
hc_conn_new (HCClientFunc func, gpointer data)
{
hc_conn_write (HCConn *conn, char *buffer, size_t len)
{
/* TODO: Do buffering or something like that */
+ /* Do we really need to? */
+ /* In case of error, we should do something */
conn->write (conn->layer, buffer, len);
}