From 67231e25e44301a8a4509db96f34cf7f56cd5f52 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 19 Mar 2002 03:49:03 +0000 Subject: [PATCH] make feof more useful on network streams --- main/network.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/main/network.c b/main/network.c index 25284a8eb8..0d80527018 100644 --- a/main/network.c +++ b/main/network.c @@ -45,6 +45,9 @@ #ifdef HAVE_SYS_SELECT_H #include #endif +#if HAVE_SYS_POLL_H +#include +#endif #ifndef PHP_WIN32 #include @@ -677,13 +680,48 @@ static size_t php_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract; size_t ret = 0; - if (sock->is_blocked) { + if (buf == NULL && count == 0) { + /* check for EOF condition */ + + if (sock->eof) + return EOF; + + if (TOREAD(sock)) + return 0; + + /* no data in the buffer - lets examine the socket */ +#if HAVE_SYS_POLL_H + { + struct pollfd topoll; + + topoll.fd = sock->socket; + topoll.events = POLLIN; + topoll.revents = 0; + + if (poll(&topoll, 1, 0) == 1) { + return topoll.revents & POLLHUP ? EOF : 0; + } + } +#endif + + /* in the absence of other methods of checking if the + * socket is still active, try to read a chunk of data */ + sock->timeout_event = 0; + php_sock_stream_read_internal(stream, sock TSRMLS_CC); + + if (sock->timeout_event || sock->eof) + return EOF; + + return 0; + } + + if (sock->is_blocked) { sock->timeout_event = 0; while(!sock->eof && TOREAD(sock) < count && !sock->timeout_event) php_sock_stream_read_internal(stream, sock TSRMLS_CC); - } - else + } else { php_sock_stream_read(stream, sock TSRMLS_CC); + } if(count < 0) return ret; -- 2.50.1