Separate iochannel implementation from HCConn interface.
[cascardo/rnetproxy.git] / hcconn.c
1 /*
2  *  Copyright (C) 2009  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19
20 #include "hcconn.h"
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include "hcconn_internal.h"
24
25 struct hc_server_cb
26 {
27   GIOChannel *channel;
28   HCServerFunc func;
29   gpointer data;
30 };
31
32 static void
33 hc_server_cb_destroy (gpointer cb)
34 {
35   g_slice_free (struct hc_server_cb, cb);
36 }
37
38 static gboolean
39 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
40 {
41   struct hc_server_cb *cb = data;
42   int fd = g_io_channel_unix_get_fd (channel);
43   struct sockaddr addr;
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);
48   return TRUE;
49 }
50
51 void
52 hc_server_add_watch (int fd,
53                      HCServerFunc func,
54                      gpointer data)
55 {
56   struct hc_server_cb *cb;
57   cb = g_slice_new (struct hc_server_cb);
58   cb->channel = g_io_channel_unix_new (fd);
59   cb->func = func;
60   cb->data = data;
61   g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
62                        hc_server_watch, cb, hc_server_cb_destroy);
63 }
64
65 struct channel_layer
66 {
67   GIOChannel *channel;
68   guint watch;
69 };
70
71
72 ssize_t
73 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
74 {
75   struct channel_layer *layer = data;
76   int fd = g_io_channel_unix_get_fd (layer->channel);
77   return read (fd, buffer, len);
78 }
79
80 ssize_t
81 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
82 {
83   struct channel_layer *layer = data;
84   int fd = g_io_channel_unix_get_fd (layer->channel);
85   return write (fd, buffer, len);
86 }
87
88 void
89 hc_conn_channel_close (gpointer data)
90 {
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 }
97
98 gboolean
99 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
100 {
101   HCConn *conn = data;
102   HCEvent event = HC_EVENT_READ;
103   if (conn->func)
104     conn->func (conn, event, conn->data);
105   return TRUE;
106 }
107
108 void
109 hc_conn_set_driver_channel (HCConn *conn, int fd)
110 {
111   struct channel_layer *layer = g_slice_new (struct channel_layer);
112   layer->channel = g_io_channel_unix_new (fd);
113   conn->layer = layer;
114   conn->read = hc_conn_channel_read;
115   conn->write = hc_conn_channel_write;
116   conn->close = hc_conn_channel_close;
117   layer->watch = g_io_add_watch (layer->channel, G_IO_IN, hc_conn_watch, conn);
118   if (conn->func)
119     conn->func (conn, HC_EVENT_CONNECT, conn->data);
120   fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
121 }
122
123 HCConn *
124 hc_conn_new (HCClientFunc func, gpointer data)
125 {
126   HCConn *conn;
127   conn = g_slice_new (HCConn);
128   conn->func = func;
129   conn->data = data;
130   return conn;
131 }
132
133 void
134 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
135 {
136   conn->func = func;
137   conn->data = data;
138 }
139
140 ssize_t
141 hc_conn_read (HCConn *conn, char *buffer, size_t len)
142 {
143   return conn->read (conn->layer, buffer, len);
144 }
145
146 void
147 hc_conn_write (HCConn *conn, char *buffer, size_t len)
148 {
149   /* TODO: Do buffering or something like that */
150   conn->write (conn->layer, buffer, len);
151 }
152
153 void
154 hc_conn_close (HCConn *conn)
155 {
156   conn->close (conn->layer);
157   g_slice_free (HCConn, conn);
158 }