]> granicus.if.org Git - php/commitdiff
make feof more useful on network streams
authorWez Furlong <wez@php.net>
Tue, 19 Mar 2002 03:49:03 +0000 (03:49 +0000)
committerWez Furlong <wez@php.net>
Tue, 19 Mar 2002 03:49:03 +0000 (03:49 +0000)
main/network.c

index 25284a8eb8ffb80a6bdb934bc9f2b8d6274792de..0d805270183a0041eb8c2606ec75d5ed2ec7843e 100644 (file)
@@ -45,6 +45,9 @@
 #ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
 #endif
+#if HAVE_SYS_POLL_H
+#include <sys/poll.h>
+#endif
 
 #ifndef PHP_WIN32
 #include <netinet/in.h>
@@ -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;