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
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
24 #include "net-sendbuffer.h"
26 static GSList *buffers;
28 /* Create new buffer - if `bufsize' is zero or less, DEFAULT_BUFFER_SIZE
30 NET_SENDBUF_REC *net_sendbuffer_create(GIOChannel *handle, int bufsize)
34 g_return_val_if_fail(handle != NULL, NULL);
36 rec = g_new0(NET_SENDBUF_REC, 1);
39 rec->bufsize = bufsize > 0 ? bufsize : DEFAULT_BUFFER_SIZE;
41 buffers = g_slist_append(buffers, rec);
45 /* Destroy the buffer. `close' specifies if socket handle should be closed. */
46 void net_sendbuffer_destroy(NET_SENDBUF_REC *rec, int close)
48 buffers = g_slist_remove(buffers, rec);
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);
56 /* Transmit all data from buffer - return TRUE if the whole buffer was sent */
57 static int buffer_send(NET_SENDBUF_REC *rec)
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);
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 if (rec->bufpos+size > rec->bufsize)
97 memcpy(rec->buffer+rec->bufpos, data, size);
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
105 int net_sendbuffer_send(NET_SENDBUF_REC *rec, const void *data, int size)
109 g_return_val_if_fail(rec != NULL, -1);
110 g_return_val_if_fail(data != NULL, -1);
111 if (size <= 0) return 0;
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;
118 data = ((const char *) data) + ret;
124 /* everything couldn't be sent. */
125 if (rec->send_tag == -1) {
127 g_input_add(rec->handle, G_INPUT_WRITE,
128 (GInputFunction) sig_sendbuffer, rec);
131 return buffer_add(rec, data, size) ? 0 : -1;
134 /* Flush the buffer, blocks until finished. */
135 void net_sendbuffer_flush(NET_SENDBUF_REC *rec)
139 if (rec->buffer == NULL)
142 /* set the socket blocking while doing this */
143 handle = g_io_channel_unix_get_fd(rec->handle);
145 fcntl(handle, F_SETFL, 0);
147 while (!buffer_send(rec)) ;
149 fcntl(handle, F_SETFL, O_NONBLOCK);
153 /* Returns the socket handle */
154 GIOChannel *net_sendbuffer_handle(NET_SENDBUF_REC *rec)
156 g_return_val_if_fail(rec != NULL, NULL);
161 void net_sendbuffer_init(void)
166 void net_sendbuffer_deinit(void)