Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / net-sendbuffer.c
1 /*
2  net-sendbuffer.c : Buffered send()
3
4     Copyright (C) 1998-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22
23 #include "network.h"
24 #include "net-sendbuffer.h"
25
26 static GSList *buffers;
27
28 /* Create new buffer - if `bufsize' is zero or less, DEFAULT_BUFFER_SIZE
29    is used */
30 NET_SENDBUF_REC *net_sendbuffer_create(GIOChannel *handle, int bufsize)
31 {
32         NET_SENDBUF_REC *rec;
33
34         g_return_val_if_fail(handle != NULL, NULL);
35
36         rec = g_new0(NET_SENDBUF_REC, 1);
37         rec->send_tag = -1;
38         rec->handle = handle;
39         rec->bufsize = bufsize > 0 ? bufsize : DEFAULT_BUFFER_SIZE;
40
41         buffers = g_slist_append(buffers, rec);
42         return rec;
43 }
44
45 /* Destroy the buffer. `close' specifies if socket handle should be closed. */
46 void net_sendbuffer_destroy(NET_SENDBUF_REC *rec, int close)
47 {
48         buffers = g_slist_remove(buffers, rec);
49
50         if (rec->send_tag != -1) g_source_remove(rec->send_tag);
51         if (close) net_disconnect(rec->handle);
52         g_free_not_null(rec->buffer);
53         g_free(rec);
54 }
55
56 /* Transmit all data from buffer - return TRUE if the whole buffer was sent */
57 static int buffer_send(NET_SENDBUF_REC *rec)
58 {
59         int ret;
60
61         ret = net_transmit(rec->handle, rec->buffer, rec->bufpos);
62         if (ret < 0 || rec->bufpos == ret) {
63                 /* error/all sent - don't try to send it anymore */
64                 g_free_and_null(rec->buffer);
65                 return TRUE;
66         }
67
68         if (ret > 0) {
69                 rec->bufpos -= ret;
70                 g_memmove(rec->buffer, rec->buffer+ret, rec->bufpos);
71         }
72         return FALSE;
73 }
74
75 static void sig_sendbuffer(NET_SENDBUF_REC *rec)
76 {
77         if (rec->buffer != NULL) {
78                 if (!buffer_send(rec))
79                         return;
80         }
81
82         g_source_remove(rec->send_tag);
83         rec->send_tag = -1;
84 }
85
86 /* Add `data' to transmit buffer - return FALSE if buffer is full */
87 static int buffer_add(NET_SENDBUF_REC *rec, const void *data, int size)
88 {
89         if (rec->buffer == NULL) {
90                 rec->buffer = g_malloc(rec->bufsize);
91                 rec->bufpos = 0;
92         }
93
94         if (rec->bufpos+size > rec->bufsize)
95                 return FALSE;
96
97         memcpy(rec->buffer+rec->bufpos, data, size);
98         rec->bufpos += size;
99         return TRUE;
100 }
101
102 /* Send data, if all of it couldn't be sent immediately, it will be resent
103    automatically after a while. Returns -1 if some unrecoverable error
104    occured. */
105 int net_sendbuffer_send(NET_SENDBUF_REC *rec, const void *data, int size)
106 {
107         int ret;
108
109         g_return_val_if_fail(rec != NULL, -1);
110         g_return_val_if_fail(data != NULL, -1);
111         if (size <= 0) return 0;
112
113         if (rec->buffer == NULL) {
114                 /* nothing in buffer - transmit immediately */
115                 ret = net_transmit(rec->handle, data, size);
116                 if (ret < 0) return -1;
117                 size -= ret;
118                 data = ((const char *) data) + ret;
119         }
120
121         if (size <= 0)
122                 return 0;
123
124         /* everything couldn't be sent. */
125         if (rec->send_tag == -1) {
126                 rec->send_tag =
127                         g_input_add(rec->handle, G_INPUT_WRITE,
128                                     (GInputFunction) sig_sendbuffer, rec);
129         }
130
131         return buffer_add(rec, data, size) ? 0 : -1;
132 }
133
134 /* Flush the buffer, blocks until finished. */
135 void net_sendbuffer_flush(NET_SENDBUF_REC *rec)
136 {
137         int handle;
138
139         if (rec->buffer == NULL)
140                 return;
141
142         /* set the socket blocking while doing this */
143         handle = g_io_channel_unix_get_fd(rec->handle);
144 #ifndef WIN32
145         fcntl(handle, F_SETFL, 0);
146 #endif
147         while (!buffer_send(rec)) ;
148 #ifndef WIN32
149         fcntl(handle, F_SETFL, O_NONBLOCK);
150 #endif
151 }
152
153 /* Returns the socket handle */
154 GIOChannel *net_sendbuffer_handle(NET_SENDBUF_REC *rec)
155 {
156         g_return_val_if_fail(rec != NULL, NULL);
157
158         return rec->handle;
159 }
160
161 void net_sendbuffer_init(void)
162 {
163         buffers = NULL;
164 }
165
166 void net_sendbuffer_deinit(void)
167 {
168 }