Merge Irssi 0.8.16-rc1
[silc.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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "module.h"
22
23 #include "network.h"
24 #include "net-sendbuffer.h"
25 #include "line-split.h"
26
27 /* Create new buffer - if `bufsize' is zero or less, DEFAULT_BUFFER_SIZE
28    is used */
29 NET_SENDBUF_REC *net_sendbuffer_create(GIOChannel *handle, int bufsize)
30 {
31         NET_SENDBUF_REC *rec;
32
33         g_return_val_if_fail(handle != NULL, NULL);
34
35         rec = g_new0(NET_SENDBUF_REC, 1);
36         rec->send_tag = -1;
37         rec->handle = handle;
38         rec->bufsize = bufsize > 0 ? bufsize : DEFAULT_BUFFER_SIZE;
39         rec->def_bufsize = rec->bufsize;
40
41         return rec;
42 }
43
44 /* Destroy the buffer. `close' specifies if socket handle should be closed. */
45 void net_sendbuffer_destroy(NET_SENDBUF_REC *rec, int close)
46 {
47         if (rec->send_tag != -1) g_source_remove(rec->send_tag);
48         if (close) net_disconnect(rec->handle);
49         if (rec->readbuffer != NULL) line_split_free(rec->readbuffer);
50         g_free_not_null(rec->buffer);
51         g_free(rec);
52 }
53
54 /* Transmit all data from buffer - return TRUE if the whole buffer was sent */
55 static int buffer_send(NET_SENDBUF_REC *rec)
56 {
57         int ret;
58
59         ret = net_transmit(rec->handle, rec->buffer, rec->bufpos);
60         if (ret < 0 || rec->bufpos == ret) {
61                 /* error/all sent - don't try to send it anymore */
62                 rec->bufsize = rec->def_bufsize;
63                 rec->buffer = g_realloc(rec->buffer, rec->bufsize);
64                 rec->bufpos = 0;
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         while (rec->bufpos+size > rec->bufsize) {
95                 if (rec->bufsize >= MAX_BUFFER_SIZE) {
96                         if (!rec->dead)
97                                 g_warning("Dropping some data on an outgoing connection");
98                         rec->dead = 1;
99                         return FALSE;
100                 }
101                 rec->bufsize *= 2;
102                 rec->buffer = g_realloc(rec->buffer, rec->bufsize);
103         }
104
105         memcpy(rec->buffer+rec->bufpos, data, size);
106         rec->bufpos += size;
107         return TRUE;
108 }
109
110 /* Send data, if all of it couldn't be sent immediately, it will be resent
111    automatically after a while. Returns -1 if some unrecoverable error
112    occured. */
113 int net_sendbuffer_send(NET_SENDBUF_REC *rec, const void *data, int size)
114 {
115         int ret;
116
117         g_return_val_if_fail(rec != NULL, -1);
118         g_return_val_if_fail(data != NULL, -1);
119         if (size <= 0) return 0;
120
121         if (rec->buffer == NULL || rec->bufpos == 0) {
122                 /* nothing in buffer - transmit immediately */
123                 ret = net_transmit(rec->handle, data, size);
124                 if (ret < 0) return -1;
125                 size -= ret;
126                 data = ((const char *) data) + ret;
127         }
128
129         if (size <= 0)
130                 return 0;
131
132         /* everything couldn't be sent. */
133         if (rec->send_tag == -1) {
134                 rec->send_tag =
135                         g_input_add(rec->handle, G_INPUT_WRITE,
136                                     (GInputFunction) sig_sendbuffer, rec);
137         }
138
139         return buffer_add(rec, data, size) ? 0 : -1;
140 }
141
142 int net_sendbuffer_receive_line(NET_SENDBUF_REC *rec, char **str, int read_socket)
143 {
144         char tmpbuf[2048];
145         int recvlen = 0;
146
147         if (read_socket)
148                 recvlen = net_receive(rec->handle, tmpbuf, sizeof(tmpbuf));
149
150         return line_split(tmpbuf, recvlen, str, &rec->readbuffer);
151 }
152
153 /* Flush the buffer, blocks until finished. */
154 void net_sendbuffer_flush(NET_SENDBUF_REC *rec)
155 {
156         int handle;
157
158         if (rec->buffer == NULL)
159                 return;
160
161         /* set the socket blocking while doing this */
162         handle = g_io_channel_unix_get_fd(rec->handle);
163 #ifndef WIN32
164         fcntl(handle, F_SETFL, 0);
165 #endif
166         while (!buffer_send(rec)) ;
167 #ifndef WIN32
168         fcntl(handle, F_SETFL, O_NONBLOCK);
169 #endif
170 }
171
172 /* Returns the socket handle */
173 GIOChannel *net_sendbuffer_handle(NET_SENDBUF_REC *rec)
174 {
175         g_return_val_if_fail(rec != NULL, NULL);
176
177         return rec->handle;
178 }