cvs: php4 / configure.in /ext/standard file.c /main network.c

From: Date: Fri, 09 Aug 2002 21:02:36 +0000
Subject: cvs: php4 / configure.in /ext/standard file.c /main network.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-13238@lists.php.net to get a copy of this message
wez Fri Aug 9 17:02:36 2002 EDT Modified files: /php4/ext/standard file.c /php4/main network.c /php4 configure.in Log: Centralize some blocking socket related code. # It's been a while since I wrote this! Index: php4/ext/standard/file.c diff -u php4/ext/standard/file.c:1.240 php4/ext/standard/file.c:1.241 --- php4/ext/standard/file.c:1.240 Thu Aug 8 18:59:41 2002 +++ php4/ext/standard/file.c Fri Aug 9 17:02:34 2002 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.240 2002/08/08 22:59:41 yohgaki Exp $ */ +/* $Id: file.c,v 1.241 2002/08/09 21:02:34 wez Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -956,44 +956,12 @@ } /* }}} */ -/* TODO: move to main/network.c */ -PHPAPI int php_set_sock_blocking(int socketd, int block TSRMLS_DC) -{ - int ret = SUCCESS; - int flags; - int myflag = 0; - -#ifdef PHP_WIN32 - /* with ioctlsocket, a non-zero sets nonblocking, a zero sets blocking */ - flags = !block; - if (ioctlsocket(socketd, FIONBIO, &flags)==SOCKET_ERROR){ - php_error(E_WARNING, "%s(): %s", get_active_function_name(TSRMLS_C), WSAGetLastError()); - ret = FALSE; - } -#else - flags = fcntl(socketd, F_GETFL); -#ifdef O_NONBLOCK - myflag = O_NONBLOCK; /* POSIX version */ -#elif defined(O_NDELAY) - myflag = O_NDELAY; /* old non-POSIX version */ -#endif - if (!block) { - flags |= myflag; - } else { - flags &= ~myflag; - } - fcntl(socketd, F_SETFL, flags); -#endif - return ret; -} - /* {{{ proto bool socket_set_blocking(resource socket, int mode) Set blocking/non-blocking mode on a socket */ PHP_FUNCTION(socket_set_blocking) { zval **arg1, **arg2; int block, type; - int socketd = 0; void *what; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { @@ -1007,15 +975,6 @@ block = Z_LVAL_PP(arg2); if (php_stream_is((php_stream*)what, PHP_STREAM_IS_SOCKET)) { - /* TODO: check if the blocking mode is changed elsewhere, and see if we - * can integrate these calls into php_stream_sock_set_blocking */ - if (FAILURE == php_stream_cast((php_stream *) what, PHP_STREAM_AS_SOCKETD, (void *) &socketd, REPORT_ERRORS)) { - RETURN_FALSE; - } - - if (php_set_sock_blocking(socketd, block TSRMLS_CC) == FAILURE) - RETURN_FALSE; - php_stream_sock_set_blocking((php_stream*)what, block == 0 ? 0 : 1 TSRMLS_CC); RETURN_TRUE; } Index: php4/main/network.c diff -u php4/main/network.c:1.54 php4/main/network.c:1.55 --- php4/main/network.c:1.54 Mon Jul 22 14:46:26 2002 +++ php4/main/network.c Fri Aug 9 17:02:35 2002 @@ -15,7 +15,7 @@ | Author: Stig Venaas <venaas@uninett.no> | +----------------------------------------------------------------------+ */ -/* $Id: network.c,v 1.54 2002/07/22 18:46:26 jason Exp $ */ +/* $Id: network.c,v 1.55 2002/08/09 21:02:35 wez Exp $ */ /*#define DEBUG_MAIN_NETWORK 1*/ #define MAX_CHUNKS_PER_READ 10 @@ -627,6 +627,36 @@ sock->timeout_event = 0; } +PHPAPI int php_set_sock_blocking(int socketd, int block) +{ + int ret = SUCCESS; + int flags; + int myflag = 0; + +#ifdef PHP_WIN32 + /* with ioctlsocket, a non-zero sets nonblocking, a zero sets blocking */ + flags = !block; + if (ioctlsocket(socketd, FIONBIO, &flags)==SOCKET_ERROR){ + php_error(E_WARNING, "%s", WSAGetLastError()); + ret = FALSE; + } +#else + flags = fcntl(socketd, F_GETFL); +#ifdef O_NONBLOCK + myflag = O_NONBLOCK; /* POSIX version */ +#elif defined(O_NDELAY) + myflag = O_NDELAY; /* old non-POSIX version */ +#endif + if (!block) { + flags |= myflag; + } else { + flags &= ~myflag; + } + fcntl(socketd, F_SETFL, flags); +#endif + return ret; +} + PHPAPI int php_stream_sock_set_blocking(php_stream *stream, int mode TSRMLS_DC) { int oldmode; @@ -636,9 +666,17 @@ return 0; oldmode = sock->is_blocked; - sock->is_blocked = mode; + + /* no need to change anything */ + if (mode == oldmode) + return oldmode; + + if (SUCCESS == php_set_sock_blocking(sock->socket, mode)) { + sock->is_blocked = mode; + return oldmode; + } - return oldmode; + return -1; } PHPAPI size_t php_stream_sock_set_chunk_size(php_stream *stream, size_t size TSRMLS_DC) @@ -809,6 +847,8 @@ if (buf == NULL && count == 0) { /* check for EOF condition */ + int save_blocked; + DUMP_SOCK_STATE("check for EOF", sock); if (sock->eof) @@ -818,7 +858,7 @@ return 0; /* no data in the buffer - lets examine the socket */ -#if HAVE_SYS_POLL_H +#if HAVE_SYS_POLL_H && HAVE_POLL { struct pollfd topoll; @@ -831,13 +871,16 @@ } } #endif - + /* in the absence of other methods of checking if the - * socket is still active, try to read a chunk of data */ + * socket is still active, try to read a chunk of data, + * but lets not block. */ sock->timeout_event = 0; + save_blocked = php_stream_sock_set_blocking(stream, 1 TSRMLS_CC); php_sock_stream_read_internal(stream, sock TSRMLS_CC); + php_stream_sock_set_blocking(stream, save_blocked TSRMLS_CC); - if (sock->timeout_event || sock->eof) + if (sock->eof) return EOF; return 0; Index: php4/configure.in diff -u php4/configure.in:1.366 php4/configure.in:1.367 --- php4/configure.in:1.366 Tue Aug 6 18:05:38 2002 +++ php4/configure.in Fri Aug 9 17:02:36 2002 @@ -1,4 +1,4 @@ -dnl ## $Id: configure.in,v 1.366 2002/08/06 22:05:38 markonen Exp $ -*- sh -*- +dnl ## $Id: configure.in,v 1.367 2002/08/09 21:02:36 wez Exp $ -*- sh -*- dnl ## Process this file with autoconf to produce a configure script. divert(1) @@ -447,6 +447,7 @@ mkstemp \ mmap \ nl_langinfo \ +poll \ putenv \ realpath \ random \

« previous php.cvs (#13238) next »