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 struct _NET_SENDBUF_REC {
32 char *buffer; /* Buffer is NULL until it's actually needed. */
35 static GSList *buffers;
37 /* Create new buffer - if `bufsize' is zero or less, DEFAULT_BUFFER_SIZE
39 NET_SENDBUF_REC *net_sendbuffer_create(GIOChannel *handle, int bufsize)
43 g_return_val_if_fail(handle != NULL, NULL);
45 rec = g_new0(NET_SENDBUF_REC, 1);
48 rec->bufsize = bufsize > 0 ? bufsize : DEFAULT_BUFFER_SIZE;
50 buffers = g_slist_append(buffers, rec);
54 /* Destroy the buffer. `close' specifies if socket handle should be closed. */
55 void net_sendbuffer_destroy(NET_SENDBUF_REC *rec, int close)
57 buffers = g_slist_remove(buffers, rec);
59 if (rec->send_tag != -1) g_source_remove(rec->send_tag);
60 if (close) net_disconnect(rec->handle);
61 g_free_not_null(rec->buffer);
65 /* Transmit all data from buffer - return TRUE if the whole buffer was sent */
66 static int buffer_send(NET_SENDBUF_REC *rec)
70 ret = net_transmit(rec->handle, rec->buffer, rec->bufpos);
71 if (ret < 0 || rec->bufpos == ret) {
72 /* error/all sent - don't try to send it anymore */
73 g_free_and_null(rec->buffer);
79 g_memmove(rec->buffer, rec->buffer+ret, rec->bufpos);
84 static void sig_sendbuffer(NET_SENDBUF_REC *rec)
86 if (rec->buffer != NULL) {
87 if (!buffer_send(rec))
91 g_source_remove(rec->send_tag);
95 /* Add `data' to transmit buffer - return FALSE if buffer is full */
96 static int buffer_add(NET_SENDBUF_REC *rec, const void *data, int size)
98 if (rec->buffer == NULL) {
99 rec->buffer = g_malloc(rec->bufsize);
103 if (rec->bufpos+size > rec->bufsize)
106 memcpy(rec->buffer+rec->bufpos, data, size);
111 /* Send data, if all of it couldn't be sent immediately, it will be resent
112 automatically after a while. Returns -1 if some unrecoverable error
114 int net_sendbuffer_send(NET_SENDBUF_REC *rec, const void *data, int size)
118 g_return_val_if_fail(rec != NULL, -1);
119 g_return_val_if_fail(data != NULL, -1);
120 if (size <= 0) return 0;
122 if (rec->buffer == NULL) {
123 /* nothing in buffer - transmit immediately */
124 ret = net_transmit(rec->handle, data, size);
125 if (ret < 0) return -1;
127 data = ((const char *) data) + ret;
133 /* everything couldn't be sent. */
134 if (rec->send_tag == -1) {
136 g_input_add(rec->handle, G_INPUT_WRITE,
137 (GInputFunction) sig_sendbuffer, rec);
140 return buffer_add(rec, data, size) ? 0 : -1;
143 /* Flush the buffer, blocks until finished. */
144 void net_sendbuffer_flush(NET_SENDBUF_REC *rec)
148 if (rec->buffer == NULL)
151 /* set the socket blocking while doing this */
152 handle = g_io_channel_unix_get_fd(rec->handle);
154 fcntl(handle, F_SETFL, 0);
156 while (!buffer_send(rec)) ;
158 fcntl(handle, F_SETFL, O_NONBLOCK);
162 /* Returns the socket handle */
163 GIOChannel *net_sendbuffer_handle(NET_SENDBUF_REC *rec)
165 g_return_val_if_fail(rec != NULL, NULL);
170 void net_sendbuffer_init(void)
175 void net_sendbuffer_deinit(void)