summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrobertabcd <robertabcd@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2012-10-13 12:55:35 +0800
committerrobertabcd <robertabcd@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2012-10-13 12:55:35 +0800
commitefaf829296e2281530604e680e7f74be7082c4ec (patch)
tree21a931978222a78b5743d36c1bbc025dd35223c5
parenta38d56334b5e9eead3b7dc0760f102410ebc308e (diff)
downloadpttbbs-efaf829296e2281530604e680e7f74be7082c4ec.tar
pttbbs-efaf829296e2281530604e680e7f74be7082c4ec.tar.gz
pttbbs-efaf829296e2281530604e680e7f74be7082c4ec.tar.bz2
pttbbs-efaf829296e2281530604e680e7f74be7082c4ec.tar.lz
pttbbs-efaf829296e2281530604e680e7f74be7082c4ec.tar.xz
pttbbs-efaf829296e2281530604e680e7f74be7082c4ec.tar.zst
pttbbs-efaf829296e2281530604e680e7f74be7082c4ec.zip
Add microsecond timeout support to toconnectex as toconnect3
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5703 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r--pttbbs/common/sys/net.c16
-rw-r--r--pttbbs/include/cmsys.h1
2 files changed, 13 insertions, 4 deletions
diff --git a/pttbbs/common/sys/net.c b/pttbbs/common/sys/net.c
index fff804ec..143f4fd9 100644
--- a/pttbbs/common/sys/net.c
+++ b/pttbbs/common/sys/net.c
@@ -140,6 +140,11 @@ int toconnect(const char *addr)
int toconnectex(const char *addr, int timeout)
{
+ return toconnect3(addr, timeout, 0);
+}
+
+int toconnect3(const char *addr, int timeout, int microseconds)
+{
int sock;
assert(addr && *addr);
@@ -164,13 +169,14 @@ int toconnectex(const char *addr, int timeout)
char buf[64], *port;
struct sockaddr_in serv_name;
int was_block = 1;
+ int timed = timeout > 0 || (timeout == 0 && microseconds > 0);
if( (sock = socket(PF_INET, SOCK_STREAM, 0)) < 0 ){
perror("socket");
return -1;
}
- if (timeout > 0)
+ if (timed)
{
// set to non-block to allow timeout
int oflags = fcntl(sock, F_GETFL, NULL);
@@ -199,8 +205,10 @@ int toconnectex(const char *addr, int timeout)
struct timeval tv = {0};
fd_set myset;
- assert(timeout > 0);
- tv.tv_sec = timeout; // set timeout here
+ assert(timed);
+ // set timeout here
+ tv.tv_sec = timeout;
+ tv.tv_usec = microseconds;
FD_ZERO(&myset);
FD_SET(sock, &myset);
@@ -220,7 +228,7 @@ int toconnectex(const char *addr, int timeout)
return -1;
}
- if (timeout > 0)
+ if (timed)
{
// restore flags
int oflags = fcntl(sock, F_GETFL, NULL);
diff --git a/pttbbs/include/cmsys.h b/pttbbs/include/cmsys.h
index 9b929f0f..8c0bf5e8 100644
--- a/pttbbs/include/cmsys.h
+++ b/pttbbs/include/cmsys.h
@@ -82,6 +82,7 @@ int tobind (const char *addr);
int tobindex (const char *addr, int qlen, int (*setsock)(int), int do_listen);
int toconnect(const char *addr);
int toconnectex(const char *addr, int timeout);
+int toconnect3(const char *addr, int timeout, int microseconds);
int toread (int fd, void *buf, int len);
int towrite (int fd, const void *buf, int len);
int torecv (int fd, void *buf, int len, int flag);