--- a/ExternalStream.st Tue Nov 22 14:52:52 2016 +0000
+++ b/ExternalStream.st Thu Nov 24 06:40:21 2016 +0100
@@ -411,16 +411,22 @@
} \
CLEAR_ERRNO; \
{ \
- int res = 0; \
- if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res), 1)) \
- || (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \
- || (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0), 1))) { \
+ int res = -1, ok = 0; \
+ SOCKET sock = 0; \
+ if ((handleType == @symbol(socketFilePointer) && ((ok = ioctlsocket(sock = (SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res) == 0), 1)) \
+ || (handleType == @symbol(socketHandle) && ((ok = ioctlsocket(sock = (SOCKET)(f), FIONREAD, &res) == 0), 1)) \
+ || (handleType == @symbol(pipeFilePointer) && ((ok = PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)), 1))) { \
+ if (!ok) { (ret) = -1; break; } \
if (res > 0) { \
if (res > ((cnt)-__offs)) \
res = (cnt)-__offs; \
READ((ret), f, (buf)+__offs, res, handleType); \
} else { \
- (ret) = 0; \
+ if (sock && send(sock, NULL, 0, 0) == SOCKET_ERROR) { \
+ (ret) = -1; __threadErrno = WSAGetLastError(); \
+ } else { \
+ (ret) = 0; \
+ } \
break; \
} \
} else { \
@@ -428,7 +434,7 @@
} \
} \
if ((ret) <= 0) { \
- if (ret < 0 && __threadErrno == EINTR) \
+ if ((ret) < 0 && __threadErrno == EINTR)\
continue; \
break; \
} \
@@ -484,7 +490,7 @@
l = (cnt)-__offs; \
if ( l > IO_BUFFER_SIZE) \
l = IO_BUFFER_SIZE; \
- READ((ret),f, __buf, l, handleType); \
+ READ((ret),f, __buf, l, handleType); \
if ((ret) <= 0) { \
if ((ret) < 0 && __threadErrno == EINTR) { \
continue; \
@@ -494,7 +500,7 @@
} \
if ((ret) > 0 ) { \
/* refetch */ \
- buf = (char *)(obj); \
+ buf = (char *)(obj); \
memcpy((buf)+__ooffs+__offs,__buf,(ret)); \
__offs += (ret); \
} else { \
@@ -522,7 +528,7 @@
if (__threadErrno == EINTR) { \
clearerr(f); \
/* refetch */ \
- buf = (char *)(obj);\
+ buf = (char *)(obj); \
continue; \
} \
} else { \
@@ -547,26 +553,31 @@
continue; \
} \
{ \
- int res = 0; \
+ int res = -1, ok = 0; \
+ SOCKET sock = 0; \
int l = (cnt)-__offs; \
CLEAR_ERRNO; \
- if (l > IO_BUFFER_SIZE) \
- l = IO_BUFFER_SIZE; \
- if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)), FIONREAD, &res), 1)) \
- || (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \
- || (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0,0,0,&res,0), 1))) { \
- if (res > 0) { \
- if (res > l) res = l; \
+ if (l > IO_BUFFER_SIZE) l = IO_BUFFER_SIZE; \
+ if ((handleType == @symbol(socketFilePointer) && ((ok = ioctlsocket(sock = (SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res) == 0), 1)) \
+ || (handleType == @symbol(socketHandle) && ((ok = ioctlsocket(sock = (SOCKET)(f), FIONREAD, &res) == 0), 1)) \
+ || (handleType == @symbol(pipeFilePointer) && ((ok = PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)), 1))) { \
+ if (!ok) { (ret) = -1; break; } \
+ if (res > 0) { \
+ if (res > l) res = l; \
READ((ret), f, __buf, res, handleType); \
- } else { \
- (ret) = 0; \
- break; \
- } \
+ } else { \
+ if (sock && send(sock, NULL, 0, 0) == SOCKET_ERROR) { \
+ (ret) = -1; __threadErrno = WSAGetLastError(); \
+ } else { \
+ (ret) = 0; \
+ } \
+ break; \
+ } \
} else { \
READ((ret), f, __buf, l, handleType); \
} \
if ((ret) <= 0) { \
- if (ret < 0 && __threadErrno == EINTR) \
+ if ((ret) < 0 && __threadErrno == EINTR) \
continue; \
break; \
} \
@@ -592,7 +603,7 @@
if ((ret) >= 0) { \
(ret) = 1; \
} else if (ferror(f)) { \
- if (__threadErrno == EINTR) { \
+ if (__threadErrno == EINTR) { \
clearerr(f); \
continue; \
} \
@@ -603,8 +614,8 @@
} else { \
for (;;) { \
CLEAR_ERRNO; \
- WRITE(ret,f, buf, 1, handleType); \
- if ((ret) >= 0 || __threadErrno != EINTR) \
+ WRITE(ret,f, buf, 1, handleType); \
+ if ((ret) >= 0 || __threadErrno != EINTR) \
break; \
} \
}
@@ -618,7 +629,7 @@
(ret) = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\
if ((ret) <= 0) { \
if (ferror(f)) { \
- if (__threadErrno == EINTR) { \
+ if (__threadErrno == EINTR) { \
clearerr(f); \
continue; \
} \
@@ -635,9 +646,9 @@
int __offs = 0; \
while (__offs < (cnt)) { \
CLEAR_ERRNO; \
- WRITE(ret,f, (buf)+__offs, (cnt)-__offs, handleType); \
- if (ret <= 0) { \
- if (ret < 0 && __threadErrno == EINTR) { \
+ WRITE((ret),f, (buf)+__offs, (cnt)-__offs, handleType); \
+ if ((ret) <= 0) { \
+ if ((ret) < 0 && __threadErrno == EINTR) { \
continue; \
} \
break; \
@@ -663,7 +674,7 @@
if (ferror(f)) { \
if (__threadErrno == EINTR) { \
/* refetch */ \
- buf = (char *)(obj); \
+ buf = (char *)(obj); \
clearerr(f); \
continue; \
} \
@@ -685,11 +696,11 @@
if ( l > IO_BUFFER_SIZE) \
l = IO_BUFFER_SIZE; \
/* refetch */ \
- buf = (char *)(obj); \
+ buf = (char *)(obj); \
memcpy(__buf,(buf)+__ooffs+__offs,l); \
- WRITE(ret,f, __buf, l, handleType); \
- if (ret <= 0) { \
- if (ret < 0 && __threadErrno == EINTR) { \
+ WRITE(ret,f, __buf, l, handleType); \
+ if ((ret) <= 0) { \
+ if ((ret) < 0 && __threadErrno == EINTR) { \
continue; \
} \
break; \