summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2010-05-02 23:27:00 +0800
committerpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2010-05-02 23:27:00 +0800
commit8fd2d039d1dd93b6d93515c1f7c0bbcb1b6ef043 (patch)
treee20533dd6d1a007597e1fbb30e13bd290af82c2d
parent1b0a85c4cc637c37f5b70030ca050baa19bddc96 (diff)
downloadpttbbs-8fd2d039d1dd93b6d93515c1f7c0bbcb1b6ef043.tar
pttbbs-8fd2d039d1dd93b6d93515c1f7c0bbcb1b6ef043.tar.gz
pttbbs-8fd2d039d1dd93b6d93515c1f7c0bbcb1b6ef043.tar.bz2
pttbbs-8fd2d039d1dd93b6d93515c1f7c0bbcb1b6ef043.tar.lz
pttbbs-8fd2d039d1dd93b6d93515c1f7c0bbcb1b6ef043.tar.xz
pttbbs-8fd2d039d1dd93b6d93515c1f7c0bbcb1b6ef043.tar.zst
pttbbs-8fd2d039d1dd93b6d93515c1f7c0bbcb1b6ef043.zip
* add some usleep() when i/o gets EAGAIN (note: not sure why read() on blocking fd gets EAGAIN so this may a valid workaround, or not)
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5041 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r--pttbbs/common/sys/net.c32
1 files changed, 28 insertions, 4 deletions
diff --git a/pttbbs/common/sys/net.c b/pttbbs/common/sys/net.c
index fe6c216d..9e264b91 100644
--- a/pttbbs/common/sys/net.c
+++ b/pttbbs/common/sys/net.c
@@ -14,7 +14,12 @@
#include "cmsys.h"
+#ifndef DEFAULT_TCP_QLEN
#define DEFAULT_TCP_QLEN (10)
+#endif
+#ifndef DEFAULT_IOWRITE_EAGAIN_WAIT_MICROSECOND
+#define DEFAULT_IOWRITE_EAGAIN_WAIT_MICROSECOND (10)
+#endif
uint32_t
ipstr2int(const char *ip)
@@ -226,6 +231,25 @@ int toconnectex(const char *addr, int timeout)
return sock;
}
+int is_to_readwrite_again(int s)
+{
+ if (s >= 0)
+ return 0;
+ if (errno == EINTR)
+ return 1;
+ if (errno == EAGAIN)
+ {
+#ifdef DEFAULT_IOWRITE_EAGAIN_WAIT_MICROSECOND
+ // you can usleep here, to reduce system overhead
+ usleep(DEFAULT_IOWRITE_EAGAIN_WAIT_MICROSECOND);
+#endif
+ return 1;
+ }
+
+ // all other case, failure.
+ return 0;
+}
+
/**
* same as read(2), but read until exactly size len
*/
@@ -234,7 +258,7 @@ int toread(int fd, void *buf, int len)
int s;
for( s = 0 ; len > 0 ; )
if( (s = read(fd, buf, len)) <= 0 ) {
- if (s < 0 && (errno == EINTR || errno == EAGAIN))
+ if (is_to_readwrite_again(s))
continue;
// XXX we define toread/towrite as '-1 for EOF and error'.
return -1; // s;
@@ -253,7 +277,7 @@ int towrite(int fd, const void *buf, int len)
int s;
for( s = 0 ; len > 0 ; )
if( (s = write(fd, buf, len)) <= 0){
- if (s < 0 && (errno == EINTR || errno == EAGAIN))
+ if (is_to_readwrite_again(s))
continue;
// XXX we define toread/towrite as '-1 for EOF and error'.
return -1; // s;
@@ -272,7 +296,7 @@ int torecv(int fd, void *buf, int len, int flag)
int s;
for( s = 0 ; len > 0 ; )
if( (s = recv(fd, buf, len, flag)) <= 0 ) {
- if (s < 0 && (errno == EINTR || errno == EAGAIN))
+ if (is_to_readwrite_again(s))
continue;
// XXX we define toread/towrite as '-1 for EOF and error'.
return -1; // s;
@@ -291,7 +315,7 @@ int tosend(int fd, const void *buf, int len, int flag)
int s;
for( s = 0 ; len > 0 ; )
if( (s = send(fd, buf, len, flag)) <= 0){
- if (s < 0 && (errno == EINTR || errno == EAGAIN))
+ if (is_to_readwrite_again(s))
continue;
// XXX we define toread/towrite as '-1 for EOF and error'.
return -1; // s;