mirror of
https://github.com/irssi/irssi.git
synced 2026-08-10 20:33:46 +02:00
Implemented network transmit buffer. If all data couldn't be sent
immediately, it's sent after a small timeout. This cleans up some code with IRC command sending. git-svn-id: http://svn.irssi.org/repos/irssi/trunk@478 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
parent
c6f949ef6f
commit
ee226421f1
8 changed files with 238 additions and 49 deletions
|
|
@ -24,6 +24,7 @@
|
|||
#include "pidwait.h"
|
||||
|
||||
#include "net-disconnect.h"
|
||||
#include "net-sendbuffer.h"
|
||||
#include "signals.h"
|
||||
#include "settings.h"
|
||||
|
||||
|
|
@ -41,6 +42,7 @@ void core_init(void)
|
|||
pidwait_init();
|
||||
|
||||
net_disconnect_init();
|
||||
net_sendbuffer_init();
|
||||
signals_init();
|
||||
settings_init();
|
||||
commands_init();
|
||||
|
|
@ -61,6 +63,7 @@ void core_deinit(void)
|
|||
commands_deinit();
|
||||
settings_deinit();
|
||||
signals_deinit();
|
||||
net_sendbuffer_deinit();
|
||||
net_disconnect_deinit();
|
||||
|
||||
pidwait_deinit();
|
||||
|
|
|
|||
165
src/core/net-sendbuffer.c
Normal file
165
src/core/net-sendbuffer.c
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
net-sendbuffer.c : Buffered send()
|
||||
|
||||
Copyright (C) 1998-2000 Timo Sirainen
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "module.h"
|
||||
|
||||
#include "network.h"
|
||||
#include "net-sendbuffer.h"
|
||||
|
||||
struct _NET_SENDBUF_REC {
|
||||
int handle;
|
||||
|
||||
int bufsize;
|
||||
int bufpos;
|
||||
char *buffer; /* Buffer is NULL until it's actually needed. */
|
||||
};
|
||||
|
||||
static GSList *buffers;
|
||||
static int timeout_tag;
|
||||
|
||||
/* Create new buffer - if `bufsize' is zero or less, DEFAULT_BUFFER_SIZE
|
||||
is used */
|
||||
NET_SENDBUF_REC *net_sendbuffer_create(int handle, int bufsize)
|
||||
{
|
||||
NET_SENDBUF_REC *rec;
|
||||
|
||||
rec = g_new0(NET_SENDBUF_REC, 1);
|
||||
rec->handle = handle;
|
||||
rec->bufsize = bufsize > 0 ? bufsize : DEFAULT_BUFFER_SIZE;
|
||||
|
||||
buffers = g_slist_append(buffers, rec);
|
||||
return rec;
|
||||
}
|
||||
|
||||
/* Destroy the buffer. `close' specifies if socket handle should be closed. */
|
||||
void net_sendbuffer_destroy(NET_SENDBUF_REC *rec, int close)
|
||||
{
|
||||
buffers = g_slist_remove(buffers, rec);
|
||||
|
||||
if (close) net_disconnect(rec->handle);
|
||||
g_free_not_null(rec->buffer);
|
||||
g_free(rec);
|
||||
}
|
||||
|
||||
/* Transmit all data from buffer - return TRUE if successful */
|
||||
static int buffer_send(NET_SENDBUF_REC *rec)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = net_transmit(rec->handle, rec->buffer, rec->bufpos);
|
||||
if (ret < 0 || rec->bufpos == ret) {
|
||||
/* error/all sent - don't try to send it anymore */
|
||||
g_free_and_null(rec->buffer);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (ret > 0) {
|
||||
rec->bufpos -= ret;
|
||||
memmove(rec->buffer, rec->buffer+ret, rec->bufpos);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int sig_sendbuffer(void)
|
||||
{
|
||||
GSList *tmp;
|
||||
int stop;
|
||||
|
||||
stop = TRUE;
|
||||
for (tmp = buffers; tmp != NULL; tmp = tmp->next) {
|
||||
NET_SENDBUF_REC *rec = tmp->data;
|
||||
|
||||
if (rec->buffer != NULL) {
|
||||
if (!buffer_send(rec))
|
||||
stop = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if (stop) timeout_tag = -1;
|
||||
return !stop;
|
||||
}
|
||||
|
||||
/* Add `data' to transmit buffer - return FALSE if buffer is full */
|
||||
static int buffer_add(NET_SENDBUF_REC *rec, void *data, int size)
|
||||
{
|
||||
if (rec->buffer == NULL) {
|
||||
rec->buffer = g_malloc(rec->bufsize);
|
||||
rec->bufpos = 0;
|
||||
}
|
||||
|
||||
if (rec->bufpos+size > rec->bufsize)
|
||||
return FALSE;
|
||||
|
||||
memcpy(rec->buffer+rec->bufpos, data, size);
|
||||
rec->bufpos += size;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* Send data, if all of it couldn't be sent immediately, it will be resent
|
||||
automatically after a while. Returns -1 if some unrecoverable error
|
||||
occured. */
|
||||
int net_sendbuffer_send(NET_SENDBUF_REC *rec, void *data, int size)
|
||||
{
|
||||
int ret;
|
||||
|
||||
g_return_val_if_fail(rec != NULL, -1);
|
||||
g_return_val_if_fail(data != NULL, -1);
|
||||
if (size <= 0) return 0;
|
||||
|
||||
if (rec->buffer == NULL) {
|
||||
/* nothing in buffer - transmit immediately */
|
||||
ret = net_transmit(rec->handle, data, size);
|
||||
if (ret < 0) return -1;
|
||||
size -= ret;
|
||||
data = ((char *) data) + ret;
|
||||
}
|
||||
|
||||
if (size > 0) {
|
||||
/* everything couldn't be sent. */
|
||||
if (timeout_tag == -1) {
|
||||
timeout_tag = g_timeout_add(100, (GSourceFunc)
|
||||
sig_sendbuffer, NULL);
|
||||
}
|
||||
|
||||
if (!buffer_add(rec, data, size))
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Returns the socket handle */
|
||||
int net_sendbuffer_handle(NET_SENDBUF_REC *rec)
|
||||
{
|
||||
g_return_val_if_fail(rec != NULL, -1);
|
||||
|
||||
return rec->handle;
|
||||
}
|
||||
|
||||
void net_sendbuffer_init(void)
|
||||
{
|
||||
timeout_tag = -1;
|
||||
buffers = NULL;
|
||||
}
|
||||
|
||||
void net_sendbuffer_deinit(void)
|
||||
{
|
||||
if (timeout_tag != -1) g_source_remove(timeout_tag);
|
||||
}
|
||||
25
src/core/net-sendbuffer.h
Normal file
25
src/core/net-sendbuffer.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef __NET_SENDBUFFER_H
|
||||
#define __NET_SENDBUFFER_H
|
||||
|
||||
#define DEFAULT_BUFFER_SIZE 8192
|
||||
|
||||
typedef struct _NET_SENDBUF_REC NET_SENDBUF_REC;
|
||||
|
||||
/* Create new buffer - if `bufsize' is zero or less, DEFAULT_BUFFER_SIZE
|
||||
is used */
|
||||
NET_SENDBUF_REC *net_sendbuffer_create(int handle, int bufsize);
|
||||
/* Destroy the buffer. `close' specifies if socket handle should be closed. */
|
||||
void net_sendbuffer_destroy(NET_SENDBUF_REC *rec, int close);
|
||||
|
||||
/* Send data, if all of it couldn't be sent immediately, it will be resent
|
||||
automatically after a while. Returns -1 if some unrecoverable error
|
||||
occured. */
|
||||
int net_sendbuffer_send(NET_SENDBUF_REC *rec, void *data, int size);
|
||||
|
||||
/* Returns the socket handle */
|
||||
int net_sendbuffer_handle(NET_SENDBUF_REC *rec);
|
||||
|
||||
void net_sendbuffer_init(void);
|
||||
void net_sendbuffer_deinit(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
#include "signals.h"
|
||||
#include "line-split.h"
|
||||
#include "net-nonblock.h"
|
||||
#include "net-sendbuffer.h"
|
||||
#include "rawlog.h"
|
||||
#include "misc.h"
|
||||
#include "server.h"
|
||||
|
|
@ -124,15 +125,16 @@ static void server_connect_callback_init(SERVER_REC *server, int handle)
|
|||
signal_emit("server connected", 1, server);
|
||||
}
|
||||
|
||||
static void server_connect_callback_readpipe(SERVER_REC *server, int handle)
|
||||
static void server_connect_callback_readpipe(SERVER_REC *server)
|
||||
{
|
||||
SERVER_CONNECT_REC *conn;
|
||||
RESOLVED_IP_REC iprec;
|
||||
int handle;
|
||||
|
||||
g_source_remove(server->connect_tag);
|
||||
server->connect_tag = -1;
|
||||
|
||||
net_gethostbyname_return(handle, &iprec);
|
||||
net_gethostbyname_return(server->connect_pipe[0], &iprec);
|
||||
|
||||
close(server->connect_pipe[0]);
|
||||
close(server->connect_pipe[1]);
|
||||
|
|
@ -141,11 +143,11 @@ static void server_connect_callback_readpipe(SERVER_REC *server, int handle)
|
|||
server->connect_pipe[1] = -1;
|
||||
|
||||
conn = server->connrec;
|
||||
server->handle = iprec.error != 0 ? -1 :
|
||||
handle = iprec.error != 0 ? -1 :
|
||||
net_connect_ip(&iprec.ip, conn->proxy != NULL ?
|
||||
conn->proxy_port : conn->port,
|
||||
conn->own_ip != NULL ? conn->own_ip : NULL);
|
||||
if (server->handle == -1) {
|
||||
if (handle == -1) {
|
||||
/* failed */
|
||||
if (iprec.error == 0 || !net_hosterror_notfound(iprec.error)) {
|
||||
/* reconnect back only if either
|
||||
|
|
@ -162,7 +164,8 @@ static void server_connect_callback_readpipe(SERVER_REC *server, int handle)
|
|||
return;
|
||||
}
|
||||
|
||||
server->connect_tag = g_input_add(server->handle, G_INPUT_WRITE|G_INPUT_READ|G_INPUT_EXCEPTION,
|
||||
server->handle = net_sendbuffer_create(handle, 0);
|
||||
server->connect_tag = g_input_add(handle, G_INPUT_WRITE|G_INPUT_READ|G_INPUT_EXCEPTION,
|
||||
(GInputFunction) server_connect_callback_init, server);
|
||||
signal_emit("server connecting", 2, server, &iprec.ip);
|
||||
}
|
||||
|
|
@ -179,13 +182,10 @@ int server_connect(SERVER_REC *server)
|
|||
}
|
||||
|
||||
server->tag = server_create_tag(server->connrec);
|
||||
server->handle = -1;
|
||||
|
||||
server->connect_pid =
|
||||
net_gethostbyname_nonblock(server->connrec->proxy != NULL ?
|
||||
server->connrec->proxy : server->connrec->address,
|
||||
server->connect_pipe[1]);
|
||||
|
||||
server->connect_tag =
|
||||
g_input_add(server->connect_pipe[0], G_INPUT_READ,
|
||||
(GInputFunction) server_connect_callback_readpipe, server);
|
||||
|
|
@ -212,8 +212,8 @@ void server_disconnect(SERVER_REC *server)
|
|||
|
||||
signal_emit("server disconnected", 1, server);
|
||||
|
||||
if (server->handle != -1)
|
||||
net_disconnect(server->handle);
|
||||
if (server->handle != NULL)
|
||||
net_sendbuffer_destroy(server->handle, TRUE);
|
||||
|
||||
MODULE_DATA_DEINIT(server);
|
||||
rawlog_destroy(server->rawlog);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue