1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

glib iochannel fixes from exg.

git-svn-id: file:///var/www/svn.irssi.org/SVN/irssi/trunk@5137 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Alexander Færøy 2010-04-03 20:04:15 +00:00 committed by ahf
parent dd23f39f09
commit df1d7a7814
6 changed files with 39 additions and 34 deletions

View File

@ -35,38 +35,37 @@ typedef struct {
int tag; int tag;
} SIMPLE_THREAD_REC; } SIMPLE_THREAD_REC;
#define is_fatal_error(err) \
(err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
static int g_io_channel_write_block(GIOChannel *channel, void *data, int len) static int g_io_channel_write_block(GIOChannel *channel, void *data, int len)
{ {
gsize ret; gsize ret;
int err, sent; int sent;
GIOStatus status;
sent = 0; sent = 0;
do { do {
err = g_io_channel_write(channel, (char *) data + sent, status = g_io_channel_write_chars(channel, (char *) data + sent,
len-sent, &ret); len-sent, &ret, NULL);
sent += ret; sent += ret;
} while (sent < len && !is_fatal_error(err)); } while (sent < len && status != G_IO_STATUS_ERROR);
return err != 0 ? -1 : 0; return sent < len ? -1 : 0;
} }
static int g_io_channel_read_block(GIOChannel *channel, void *data, int len) static int g_io_channel_read_block(GIOChannel *channel, void *data, int len)
{ {
time_t maxwait; time_t maxwait;
gsize ret; gsize ret;
int err, received; int received;
GIOStatus status;
maxwait = time(NULL)+2; maxwait = time(NULL)+2;
received = 0; received = 0;
do { do {
err = g_io_channel_read(channel, (char *) data + received, status = g_io_channel_read_chars(channel, (char *) data + received,
len-received, &ret); len-received, &ret, NULL);
received += ret; received += ret;
} while (received < len && time(NULL) < maxwait && } while (received < len && time(NULL) < maxwait &&
(ret != 0 || !is_fatal_error(err))); status != G_IO_STATUS_ERROR && status != G_IO_STATUS_EOF);
return received < len ? -1 : 0; return received < len ? -1 : 0;
} }
@ -282,8 +281,8 @@ int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip,
} }
rec->func = func; rec->func = func;
rec->data = data; rec->data = data;
rec->pipes[0] = g_io_channel_unix_new(fd[0]); rec->pipes[0] = g_io_channel_new(fd[0]);
rec->pipes[1] = g_io_channel_unix_new(fd[1]); rec->pipes[1] = g_io_channel_new(fd[1]);
/* start nonblocking host name lookup */ /* start nonblocking host name lookup */
net_gethostbyname_nonblock(server, rec->pipes[1], 0); net_gethostbyname_nonblock(server, rec->pipes[1], 0);

View File

@ -42,11 +42,18 @@ union sockaddr_union {
# define SIZEOF_SOCKADDR(so) (sizeof(so.sin)) # define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
#endif #endif
GIOChannel *g_io_channel_new(int handle)
{
GIOChannel *chan;
#ifdef WIN32 #ifdef WIN32
# define g_io_channel_new(handle) g_io_channel_win32_new_stream_socket(handle) chan = g_io_channel_win32_new_socket(handle);
#else #else
# define g_io_channel_new(handle) g_io_channel_unix_new(handle) chan = g_io_channel_unix_new(handle);
#endif #endif
g_io_channel_set_encoding(chan, NULL, NULL);
g_io_channel_set_buffered(chan, FALSE);
return chan;
}
/* Cygwin need this, don't know others.. */ /* Cygwin need this, don't know others.. */
/*#define BLOCKING_SOCKETS 1*/ /*#define BLOCKING_SOCKETS 1*/
@ -341,36 +348,32 @@ GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
int net_receive(GIOChannel *handle, char *buf, int len) int net_receive(GIOChannel *handle, char *buf, int len)
{ {
gsize ret; gsize ret;
int err; GIOStatus status;
g_return_val_if_fail(handle != NULL, -1); g_return_val_if_fail(handle != NULL, -1);
g_return_val_if_fail(buf != NULL, -1); g_return_val_if_fail(buf != NULL, -1);
err = g_io_channel_read(handle, buf, len, &ret); status = g_io_channel_read_chars(handle, buf, len, &ret, NULL);
if (err == 0 && ret == 0) if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF)
return -1; /* disconnected */ return -1; /* disconnected */
if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR)) return ret;
return 0; /* no bytes received */
return err == 0 ? (int)ret : -1;
} }
/* Transmit data, return number of bytes sent, -1 = error */ /* Transmit data, return number of bytes sent, -1 = error */
int net_transmit(GIOChannel *handle, const char *data, int len) int net_transmit(GIOChannel *handle, const char *data, int len)
{ {
gsize ret; gsize ret;
int err; GIOStatus status;
g_return_val_if_fail(handle != NULL, -1); g_return_val_if_fail(handle != NULL, -1);
g_return_val_if_fail(data != NULL, -1); g_return_val_if_fail(data != NULL, -1);
err = g_io_channel_write(handle, (char *) data, len, &ret); status = g_io_channel_write_chars(handle, (char *) data, len, &ret, NULL);
if (err == G_IO_ERROR_AGAIN || if (status == G_IO_STATUS_ERROR)
(err != 0 && (errno == EINTR || errno == EPIPE))) return -1;
return 0;
return err == 0 ? (int)ret : -1; return ret;
} }
/* Get socket address/port */ /* Get socket address/port */

View File

@ -41,6 +41,8 @@ struct _IPADDR {
extern IPADDR ip4_any; extern IPADDR ip4_any;
GIOChannel *g_io_channel_new(int handle);
/* returns 1 if IPADDRs are the same */ /* returns 1 if IPADDRs are the same */
int net_ip_compare(IPADDR *ip1, IPADDR *ip2); int net_ip_compare(IPADDR *ip1, IPADDR *ip2);

View File

@ -419,8 +419,8 @@ int server_start_connect(SERVER_REC *server)
return FALSE; return FALSE;
} }
server->connect_pipe[0] = g_io_channel_unix_new(fd[0]); server->connect_pipe[0] = g_io_channel_new(fd[0]);
server->connect_pipe[1] = g_io_channel_unix_new(fd[1]); server->connect_pipe[1] = g_io_channel_new(fd[1]);
connect_address = server->connrec->proxy != NULL ? connect_address = server->connrec->proxy != NULL ?
server->connrec->proxy : server->connrec->address; server->connrec->proxy : server->connrec->address;

View File

@ -22,6 +22,7 @@
#include "signals.h" #include "signals.h"
#include "commands.h" #include "commands.h"
#include "args.h" #include "args.h"
#include "network.h"
#include "net-sendbuffer.h" #include "net-sendbuffer.h"
#include "pidwait.h" #include "pidwait.h"
#include "lib-config/iconfig.h" #include "lib-config/iconfig.h"
@ -259,7 +260,7 @@ static void session_restore_server(CONFIG_NODE *node)
chatnet, password, nick); chatnet, password, nick);
if (conn != NULL) { if (conn != NULL) {
conn->reconnection = TRUE; conn->reconnection = TRUE;
conn->connect_handle = g_io_channel_unix_new(handle); conn->connect_handle = g_io_channel_new(handle);
server = proto->server_init_connect(conn); server = proto->server_init_connect(conn);
server->version = g_strdup(config_node_get_str(node, "version", NULL)); server->version = g_strdup(config_node_get_str(node, "version", NULL));

View File

@ -304,9 +304,9 @@ static void process_exec(PROCESS_REC *rec, const char *cmd)
if (rec->pid != 0) { if (rec->pid != 0) {
/* parent process */ /* parent process */
GIOChannel *outio = g_io_channel_unix_new(in[1]); GIOChannel *outio = g_io_channel_new(in[1]);
rec->in = g_io_channel_unix_new(out[0]); rec->in = g_io_channel_new(out[0]);
rec->out = net_sendbuffer_create(outio, 0); rec->out = net_sendbuffer_create(outio, 0);
close(out[1]); close(out[1]);