Rename iochannel to hcconn.
[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
24 struct hc_server_cb
25 {
26   GIOChannel *channel;
27   HCServerFunc func;
28   gpointer data;
29 };
30
31 static void
32 hc_server_cb_destroy (gpointer cb)
33 {
34   g_slice_free (struct hc_server_cb, cb);
35 }
36
37 static gboolean
38 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
39 {
40   struct hc_server_cb *cb = data;
41   int fd = g_io_channel_unix_get_fd (channel);
42   struct sockaddr addr;
43   socklen_t saddr = sizeof (addr);
44   int client = accept (fd, &addr, &saddr);
45   if (client >= 0 && cb->func)
46     cb->func (client, &addr, saddr, cb->data);
47   return TRUE;
48 }
49
50 void
51 hc_server_add_watch (int fd,
52                      HCServerFunc func,
53                      gpointer data)
54 {
55   struct hc_server_cb *cb;
56   cb = g_slice_new (struct hc_server_cb);
57   cb->channel = g_io_channel_unix_new (fd);
58   cb->func = func;
59   cb->data = data;
60   g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
61                        hc_server_watch, cb, hc_server_cb_destroy);
62 }
63
64 struct _hc_conn_t
65 {
66   GIOChannel *channel;
67   HCClientFunc func;
68   gpointer data;
69   ssize_t (*read) (gpointer, char *, size_t);
70   ssize_t (*write) (gpointer, char *, size_t);
71   void (*close) (gpointer);
72   gpointer layer;
73   guint watch;
74 };
75
76 ssize_t
77 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
78 {
79   int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
80   return read (fd, buffer, len);
81 }
82
83 ssize_t
84 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
85 {
86   int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
87   return write (fd, buffer, len);
88 }
89
90 void
91 hc_conn_channel_close (gpointer data)
92 {
93   int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
94   shutdown (fd, SHUT_RDWR);
95 }
96
97 gboolean
98 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
99 {
100   HCConn *conn = data;
101   HCEvent event = HC_EVENT_READ;
102   if (conn->func)
103     conn->func (conn, event, conn->data);
104   return TRUE;
105 }
106
107 HCConn *
108 hc_conn_new (int fd, HCClientFunc func, gpointer data)
109 {
110   HCConn *conn;
111   conn = g_slice_new (HCConn);
112   conn->channel = g_io_channel_unix_new (fd);
113   conn->func = func;
114   conn->data = data;
115   conn->layer = conn->channel;
116   conn->read = hc_conn_channel_read;
117   conn->write = hc_conn_channel_write;
118   conn->close = hc_conn_channel_close;
119   conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn);
120   if (conn->func)
121     conn->func (conn, HC_EVENT_CONNECT, conn->data);
122   fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
123   return conn;
124 }
125
126 void
127 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
128 {
129   conn->func = func;
130   conn->data = data;
131 }
132
133 ssize_t
134 hc_conn_read (HCConn *conn, char *buffer, size_t len)
135 {
136   return conn->read (conn->layer, buffer, len);
137 }
138
139 void
140 hc_conn_write (HCConn *conn, char *buffer, size_t len)
141 {
142   /* TODO: Do buffering or something like that */
143   conn->write (conn->layer, buffer, len);
144 }
145
146 void
147 hc_conn_close (HCConn *conn)
148 {
149   conn->close (conn->layer);
150   g_source_remove (conn->watch);
151   g_io_channel_unref (conn->channel);
152   g_slice_free (HCConn, conn);
153 }