projects
/
cascardo
/
rnetproxy.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added some comments and reminders in connection system.
[cascardo/rnetproxy.git]
/
hcconn.c
diff --git
a/hcconn.c
b/hcconn.c
index
5c6e60e
..
3b2a890
100644
(file)
--- a/
hcconn.c
+++ b/
hcconn.c
@@
-20,6
+20,9
@@
#include "hcconn.h"
#include <unistd.h>
#include <fcntl.h>
#include "hcconn.h"
#include <unistd.h>
#include <fcntl.h>
+#include "hcconn_internal.h"
+
+/* The server connection watch */
struct hc_server_cb
{
struct hc_server_cb
{
@@
-57,69
+60,85
@@
hc_server_add_watch (int fd,
cb->channel = g_io_channel_unix_new (fd);
cb->func = func;
cb->data = data;
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);
}
g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
hc_server_watch, cb, hc_server_cb_destroy);
}
-struct _hc_conn_t
+
+/* The IOChannel (simple socket) layer */
+
+struct channel_layer
{
GIOChannel *channel;
{
GIOChannel *channel;
- HCClientFunc func;
- gpointer data;
- ssize_t (*read) (gpointer, char *, size_t);
- ssize_t (*write) (gpointer, char *, size_t);
- void (*close) (gpointer);
- gpointer layer;
guint watch;
};
ssize_t
hc_conn_channel_read (gpointer data, char *buffer, size_t len)
{
guint watch;
};
ssize_t
hc_conn_channel_read (gpointer data, char *buffer, size_t len)
{
- int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
+ struct channel_layer *layer = data;
+ int fd = g_io_channel_unix_get_fd (layer->channel);
return read (fd, buffer, len);
}
ssize_t
hc_conn_channel_write (gpointer data, char *buffer, size_t len)
{
return read (fd, buffer, len);
}
ssize_t
hc_conn_channel_write (gpointer data, char *buffer, size_t len)
{
- int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
+ struct channel_layer *layer = data;
+ int fd = g_io_channel_unix_get_fd (layer->channel);
return write (fd, buffer, len);
}
void
hc_conn_channel_close (gpointer data)
{
return write (fd, buffer, len);
}
void
hc_conn_channel_close (gpointer data)
{
- int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
+ struct channel_layer *layer = data;
+ int fd = g_io_channel_unix_get_fd (layer->channel);
+ g_source_remove (layer->watch);
shutdown (fd, SHUT_RDWR);
shutdown (fd, SHUT_RDWR);
+ g_io_channel_unref (layer->channel);
+ g_slice_free (struct channel_layer, layer);
}
gboolean
hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
{
HCConn *conn = data;
}
gboolean
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);
return TRUE;
}
HCEvent event = HC_EVENT_READ;
if (conn->func)
conn->func (conn, event, conn->data);
return TRUE;
}
-HCConn *
-hc_conn_
new (int fd, HCClientFunc func, gpointer data
)
+void
+hc_conn_
set_driver_channel (HCConn *conn, int fd
)
{
{
- HCConn *conn;
- conn = g_slice_new (HCConn);
- conn->channel = g_io_channel_unix_new (fd);
- conn->func = func;
- conn->data = data;
- conn->layer = conn->channel;
+ struct channel_layer *layer = g_slice_new (struct channel_layer);
+ layer->channel = g_io_channel_unix_new (fd);
+ conn->layer = layer;
conn->read = hc_conn_channel_read;
conn->write = hc_conn_channel_write;
conn->close = hc_conn_channel_close;
conn->read = hc_conn_channel_read;
conn->write = hc_conn_channel_write;
conn->close = hc_conn_channel_close;
- conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn);
+ /* 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);
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)
+{
+ HCConn *conn;
+ conn = g_slice_new (HCConn);
+ conn->func = func;
+ conn->data = data;
return conn;
}
return conn;
}
@@
-140,6
+159,8
@@
void
hc_conn_write (HCConn *conn, char *buffer, size_t len)
{
/* TODO: Do buffering or something like that */
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);
}
conn->write (conn->layer, buffer, len);
}
@@
-147,7
+168,5
@@
void
hc_conn_close (HCConn *conn)
{
conn->close (conn->layer);
hc_conn_close (HCConn *conn)
{
conn->close (conn->layer);
- g_source_remove (conn->watch);
- g_io_channel_unref (conn->channel);
g_slice_free (HCConn, conn);
}
g_slice_free (HCConn, conn);
}