2 net-sendbuffer.c : Buffered send()
4 Copyright (C) 1998-2000 Timo Sirainen
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.
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.
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.
24 #include "net-sendbuffer.h"
25 #include "line-split.h"
27 /* Create new buffer - if `bufsize' is zero or less, DEFAULT_BUFFER_SIZE
29 NET_SENDBUF_REC *net_sendbuffer_create(GIOChannel *handle, int bufsize)
33 g_return_val_if_fail(handle != NULL, NULL);
35 rec = g_new0(NET_SENDBUF_REC, 1);
38 rec->bufsize = bufsize > 0 ? bufsize : DEFAULT_BUFFER_SIZE;
39 rec->def_bufsize = rec->bufsize;
44 /* Destroy the buffer. `close' specifies if socket handle should be closed. */
45 void net_sendbuffer_destroy(NET_SENDBUF_REC *rec, int close)
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);
54 /* Transmit all data from buffer - return TRUE if the whole buffer was sent */
55 static int buffer_send(NET_SENDBUF_REC *rec)
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);
70 g_memmove(rec->buffer, rec->buffer+ret, rec->bufpos);
75 static void sig_sendbuffer(NET_SENDBUF_REC *rec)
77 if (rec->buffer != NULL) {
78 if (!buffer_send(rec))
82 g_source_remove(rec->send_tag);
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)
89 if (rec->buffer == NULL) {
90 rec->buffer = g_malloc(rec->bufsize);
94 while (rec->bufpos+size > rec->bufsize) {
95 if (rec->bufsize >= MAX_BUFFER_SIZE) {
97 g_warning("Dropping some data on an outgoing connection");
102 rec->buffer = g_realloc(rec->buffer, rec->bufsize);
105 memcpy(rec->buffer+rec->bufpos, data, size);
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
113 int net_sendbuffer_send(NET_SENDBUF_REC *rec, const void *data, int size)
117 g_return_val_if_fail(rec != NULL, -1);
118 g_return_val_if_fail(data != NULL, -1);
119 if (size <= 0) return 0;
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;
126 data = ((const char *) data) + ret;
132 /* everything couldn't be sent. */
133 if (rec->send_tag == -1) {
135 g_input_add(rec->handle, G_INPUT_WRITE,
136 (GInputFunction) sig_sendbuffer, rec);
139 return buffer_add(rec, data, size) ? 0 : -1;
142 int net_sendbuffer_receive_line(NET_SENDBUF_REC *rec, char **str, int read_socket)
148 recvlen = net_receive(rec->handle, tmpbuf, sizeof(tmpbuf));
150 return line_split(tmpbuf, recvlen, str, &rec->readbuffer);
153 /* Flush the buffer, blocks until finished. */
154 void net_sendbuffer_flush(NET_SENDBUF_REC *rec)
158 if (rec->buffer == NULL)
161 /* set the socket blocking while doing this */
162 handle = g_io_channel_unix_get_fd(rec->handle);
164 fcntl(handle, F_SETFL, 0);
166 while (!buffer_send(rec)) ;
168 fcntl(handle, F_SETFL, O_NONBLOCK);
172 /* Returns the socket handle */
173 GIOChannel *net_sendbuffer_handle(NET_SENDBUF_REC *rec)
175 g_return_val_if_fail(rec != NULL, NULL);