From d0e1cea0bea2d0b292f1a89b8a0aba8d7cee2619 Mon Sep 17 00:00:00 2001 From: piaip Date: Tue, 25 Aug 2009 03:28:28 +0000 Subject: * code refine: reduce potential fd flag changes in tonnectex git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4771 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- common/sys/net.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/sys/net.c b/common/sys/net.c index 8166ed1d..c8ae9e50 100644 --- a/common/sys/net.c +++ b/common/sys/net.c @@ -158,7 +158,7 @@ int toconnectex(const char *addr, int timeout) else { char buf[64], *port; struct sockaddr_in serv_name; - int oflags = 0; + int was_block = 1; if( (sock = socket(PF_INET, SOCK_STREAM, 0)) < 0 ){ perror("socket"); @@ -168,7 +168,8 @@ int toconnectex(const char *addr, int timeout) if (timeout > 0) { // set to non-block to allow timeout - oflags = fcntl(sock, F_GETFL, NULL); + int oflags = fcntl(sock, F_GETFL, NULL); + was_block = !(oflags & O_NONBLOCK); fcntl(sock, F_SETFL, oflags | O_NONBLOCK); } @@ -213,6 +214,11 @@ int toconnectex(const char *addr, int timeout) if (timeout > 0) { // restore flags + int oflags = fcntl(sock, F_GETFL, NULL); + if (was_block) + oflags &= ~O_NONBLOCK; + else + oflags |= O_NONBLOCK; fcntl(sock, F_SETFL, oflags); } } -- cgit v1.2.3