Using fcntl() with F_SETF to set non-blocking mode, instead of
ioctl() with FIONBIO. This is the POSIX way. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2601 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
@@ -29,7 +29,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||||
# include <netinet/in_systm.h>
|
# include <netinet/in_systm.h>
|
||||||
@@ -38,6 +37,7 @@
|
|||||||
#include <netinet/ip.h>
|
#include <netinet/ip.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
|
||||||
Socket *
|
Socket *
|
||||||
@@ -136,15 +136,25 @@ Socket_recv(Socket *sock, void *buf, size_t len, int flags) {
|
|||||||
|
|
||||||
int
|
int
|
||||||
Socket_setNonBlocking(Socket *sock) {
|
Socket_setNonBlocking(Socket *sock) {
|
||||||
int flag = 1;
|
int flags;
|
||||||
|
|
||||||
if (ioctl(sock->fd, FIONBIO, &flag) == -1) {
|
flags = fcntl(sock->fd, F_GETFL);
|
||||||
|
if (flags == -1) {
|
||||||
int savedErrno = errno;
|
int savedErrno = errno;
|
||||||
log_add(log_Error, "Setting non-block mode on socket failed: %s.\n",
|
log_add(log_Error, "Getting file descriptor flags of socket failed: "
|
||||||
strerror(errno));
|
"%s.\n", strerror(errno));
|
||||||
errno = savedErrno;
|
errno = savedErrno;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fcntl(sock->fd, F_SETFL, (long) (flags | O_NONBLOCK)) == -1) {
|
||||||
|
int savedErrno = errno;
|
||||||
|
log_add(log_Error, "Setting non-blocking mode on socket failed: "
|
||||||
|
"%s.\n", strerror(errno));
|
||||||
|
errno = savedErrno;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user