(gtk) use evutils for a portability layer on making sockets nonblocking and closing sockets

This commit is contained in:
Charles Kerr 2008-02-26 20:31:33 +00:00
parent 851e6ce008
commit 402a18a84e
2 changed files with 23 additions and 28 deletions

View File

@ -26,7 +26,6 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -35,6 +34,8 @@
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <libevent/evutil.h>
#include <libtransmission/transmission.h>
#include <libtransmission/bencode.h>
#include <libtransmission/ipcparse.h>
@ -309,7 +310,7 @@ serv_bind(struct constate *con) {
errmsg(con->u.serv.wind, _("Failed to set up socket: %s"),
g_strerror(errno));
if(0 <= con->fd)
close(con->fd);
EVUTIL_CLOSESOCKET(con->fd);
con->fd = -1;
rmsock();
}
@ -346,14 +347,14 @@ client_connect(char *path, struct constate *con) {
all_io_closed, con);
if( NULL == con->source )
{
close( con->fd );
EVUTIL_CLOSESOCKET( con->fd );
return FALSE;
}
buf = ipc_mkvers( &size, "Transmission GTK+ " LONG_VERSION_STRING );
if( NULL == buf )
{
close( con->fd );
EVUTIL_CLOSESOCKET( con->fd );
return FALSE;
}
@ -378,7 +379,7 @@ srv_io_accept(GSource *source UNUSED, int fd, struct sockaddr *sa UNUSED,
if( NULL == newcon->ipc )
{
g_free( newcon );
close( fd );
EVUTIL_CLOSESOCKET( fd );
return;
}
@ -387,7 +388,7 @@ srv_io_accept(GSource *source UNUSED, int fd, struct sockaddr *sa UNUSED,
{
ipc_freecon( newcon->ipc );
g_free( newcon );
close( fd );
EVUTIL_CLOSESOCKET( fd );
return;
}
@ -396,7 +397,7 @@ srv_io_accept(GSource *source UNUSED, int fd, struct sockaddr *sa UNUSED,
{
ipc_freecon( newcon->ipc );
g_free( newcon );
close( fd );
EVUTIL_CLOSESOCKET( fd );
return;
}
@ -541,7 +542,7 @@ destroycon(struct constate *con) {
con->source = NULL;
if(0 <= con->fd)
close(con->fd);
EVUTIL_CLOSESOCKET(con->fd);
con->fd = -1;
ipc_freecon( con->ipc );

View File

@ -24,18 +24,18 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <string.h> /* memset, memmove */
#include <unistd.h> /* read, write */
#include <glib.h>
#include <libevent/evutil.h> /* evutil_make_socket_nonblocking */
#include "tr-io.h"
#include "util.h"
#define IO_BLOCKSIZE (1024)
#define IO_BLOCKSIZE (1024)
struct iosource {
GSource source;
@ -60,8 +60,6 @@ struct iooutbuf {
unsigned int id;
};
static gboolean
nonblock(int fd);
static struct iosource *
newsource(void);
static void
@ -92,12 +90,19 @@ static GSourceFuncs sourcefuncs = {
NULL
};
static int
nonblock(int fd)
{
const int err = evutil_make_socket_nonblocking( fd );
return err;
}
GSource *
io_new(int fd, ioidfunc_t sent, iodatafunc_t received,
iofunc_t closed, void *cbdata) {
struct iosource *io;
if(!nonblock(fd))
if( nonblock( fd ) )
return NULL;
io = newsource();
@ -125,7 +130,7 @@ io_new_listening(int fd, socklen_t len, ionewfunc_t accepted,
g_assert(NULL != accepted);
if(!nonblock(fd))
if( nonblock( fd ) )
return NULL;
io = newsource();
@ -144,17 +149,6 @@ io_new_listening(int fd, socklen_t len, ionewfunc_t accepted,
return (GSource*)io;
}
static gboolean
nonblock(int fd) {
int flags;
if(0 > (flags = fcntl(fd, F_GETFL)) ||
0 > fcntl(fd, F_SETFL, flags | O_NONBLOCK))
return FALSE;
return TRUE;
}
static struct iosource *
newsource(void) {
GSource *source = g_source_new(&sourcefuncs, sizeof(struct iosource));