X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_conn.c;h=f2a26366f3c9b8a6f7fdbd8de7d75005881efb0c;hb=29cca33b9001961fa2c33bb9d9fe4a9983913fce;hp=251d62e01dbf63161c7339deedac59fe66fc90b0;hpb=f916062edf0e04cd4e0a78f6975892f59fae3b60;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_conn.c b/src/xcb_conn.c index 251d62e..f2a2636 100644 --- a/src/xcb_conn.c +++ b/src/xcb_conn.c @@ -30,7 +30,6 @@ #include #include #include -#include #include #include @@ -38,10 +37,21 @@ #include "xcbint.h" #if USE_POLL #include -#else +#elif !defined _WIN32 #include #endif +#ifdef _WIN32 +#include "xcb_windefs.h" +#else +#include +#endif /* _WIN32 */ + +/* SHUT_RDWR is fairly recent and is not available on all platforms */ +#if !defined(SHUT_RDWR) +#define SHUT_RDWR 2 +#endif + typedef struct { uint8_t status; uint8_t pad0[5]; @@ -52,6 +62,17 @@ static const int error_connection = 1; static int set_fd_flags(const int fd) { +/* Win32 doesn't have file descriptors and the fcntl function. This block sets the socket in non-blocking mode */ + +#ifdef _WIN32 + u_long iMode = 1; /* non-zero puts it in non-blocking mode, 0 in blocking mode */ + int ret = 0; + + ret = ioctlsocket(fd, FIONBIO, &iMode); + if(ret != 0) + return 0; + return 1; +#else int flags = fcntl(fd, F_GETFL, 0); if(flags == -1) return 0; @@ -61,6 +82,7 @@ static int set_fd_flags(const int fd) if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) return 0; return 1; +#endif /* _WIN32 */ } static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info) @@ -102,10 +124,7 @@ static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info) assert(count <= (int) (sizeof(parts) / sizeof(*parts))); pthread_mutex_lock(&c->iolock); - { - struct iovec *parts_ptr = parts; - ret = _xcb_out_send(c, &parts_ptr, &count); - } + ret = _xcb_out_send(c, parts, count); pthread_mutex_unlock(&c->iolock); return ret; } @@ -156,9 +175,37 @@ static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count) { int n; assert(!c->out.queue_len); + +#ifdef _WIN32 + int i = 0; + int ret = 0,err = 0; + struct iovec *vec; + n = 0; + + /* Could use the WSASend win32 function for scatter/gather i/o but setting up the WSABUF struct from + an iovec would require more work and I'm not sure of the benefit....works for now */ + vec = *vector; + while(i < *count) + { + ret = send(c->fd,vec->iov_base,vec->iov_len,0); + if(ret == SOCKET_ERROR) + { + err = WSAGetLastError(); + if(err == WSAEWOULDBLOCK) + { + return 1; + } + } + n += ret; + *vec++; + i++; + } +#else n = writev(c->fd, *vector, *count); if(n < 0 && errno == EAGAIN) return 1; +#endif /* _WIN32 */ + if(n <= 0) { _xcb_conn_shutdown(c); @@ -210,9 +257,19 @@ xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info) { xcb_connection_t* c; +#ifndef USE_POLL + if(fd >= FD_SETSIZE) /* would overflow in FD_SET */ + { + close(fd); + return (xcb_connection_t *) &error_connection; + } +#endif + c = calloc(1, sizeof(xcb_connection_t)); - if(!c) + if(!c) { + close(fd); return (xcb_connection_t *) &error_connection; + } c->fd = fd; @@ -240,6 +297,9 @@ void xcb_disconnect(xcb_connection_t *c) return; free(c->setup); + + /* disallow further sends and receives */ + shutdown(c->fd, SHUT_RDWR); close(c->fd); pthread_mutex_destroy(&c->iolock); @@ -303,15 +363,22 @@ int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vec pthread_mutex_unlock(&c->iolock); do { #if USE_POLL - ret = poll(&fd, 1, -1); + ret = poll(&fd, 1, -1); + /* If poll() returns an event we didn't expect, such as POLLNVAL, treat + * it as if it failed. */ + if(ret >= 0 && (fd.revents & ~fd.events)) + { + ret = -1; + break; + } #else - ret = select(c->fd + 1, &rfds, &wfds, 0, 0); + ret = select(c->fd + 1, &rfds, &wfds, 0, 0); #endif } while (ret == -1 && errno == EINTR); - if (ret < 0) + if(ret < 0) { _xcb_conn_shutdown(c); - ret = 0; + ret = 0; } pthread_mutex_lock(&c->iolock);