cvs: /php4/ext/standard file.c fsock.c fsock.h
| From: | Andrei Zmievski | Date: | Sat, 15 Jan 2000 19:41:47 +0000 |
| Subject: | cvs: /php4/ext/standard file.c fsock.c fsock.h | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-9156@lists.php.net to get a copy of this message | ||
andrei Sat Jan 15 11:41:47 2000 EDT
Modified files:
/php4/ext/standard file.c fsock.c fsock.h
Log:
Added ability to set timeout on socket read operations. New function
set_socket_timeout(socket, seconds, microseconds) sets the timeout.
@- Added ability to set timeout on socket read operations through
@ set_socket_timeout() function. (Andrei)
Index: php4/ext/standard/file.c
diff -u php4/ext/standard/file.c:1.56 php4/ext/standard/file.c:1.57
--- php4/ext/standard/file.c:1.56 Thu Jan 6 07:27:03 2000
+++ php4/ext/standard/file.c Sat Jan 15 11:41:17 2000
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: file.c,v 1.56 2000/01/06 15:27:03 thies Exp $ */
+/* $Id: file.c,v 1.57 2000/01/15 19:41:17 andrei Exp $ */
/* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
@@ -240,7 +240,7 @@
PHP_FE(flock, NULL)
PHP_FE(get_meta_tags, NULL)
PHP_FE(set_socket_blocking, NULL)
-#if (0 && defined(HAVE_SYS_TIME_H) && HAVE_SETSOCKOPT &&
defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO))
+#if HAVE_SYS_TIME_H
PHP_FE(set_socket_timeout, NULL)
#endif
#if 0 /* needs to be rethought 991221 thies@digicol.de */
@@ -879,32 +879,40 @@
/* {{{ proto int set_socket_timeout(int socket descriptor, int timeout )
NYI */
-#if (0 && defined(HAVE_SYS_TIME_H) && HAVE_SETSOCKOPT &&
defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO))
+#if HAVE_SYS_TIME_H
/* this doesn't work, as it appears those properties are read-only :( */
PHP_FUNCTION(set_socket_timeout)
{
- pval *socket,*timeout;
- int type, *sock;
+ zval **socket, **seconds, **microseconds;
+ int type;
+ void *what;
+ int socketd = 0;
struct timeval t;
- if (ARG_COUNT(ht)!=2 || zend_get_parameters(ht, 2, &socket, &timeout)==FAILURE) {
+ if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 3 ||
+ zend_get_parameters_ex(ZEND_NUM_ARGS(), &socket, &seconds, µseconds)==FAILURE) {
WRONG_PARAM_COUNT;
}
- convert_to_long(socket);
- convert_to_long(timeout);
+
+ what = zend_fetch_resource(socket, -1, "File-Handle", &type, 1, le_socket);
+ ZEND_VERIFY_RESOURCE(what);
+ socketd = *(int *)what;
- sock = zend_list_find(socket->value.lval, &type);
- if (type!=le_socket) {
- php_error(E_WARNING,"%d is not a socket id",socket->value.lval);
- RETURN_FALSE;
+ convert_to_long_ex(seconds);
+ t.tv_sec = (*seconds)->value.lval;
+
+ if (ZEND_NUM_ARGS() == 3) {
+ convert_to_long_ex(microseconds);
+ t.tv_usec = (*microseconds)->value.lval % 1000000;
+ t.tv_sec += (*microseconds)->value.lval / 1000000;
}
- t.tv_sec = timeout->value.lval;
- t.tv_usec = 0;
- setsockopt(*sock,SOL_SOCKET,SO_SNDTIMEO,(void *) &t,sizeof(struct timeval));
- setsockopt(*sock,SOL_SOCKET,SO_RCVTIMEO,(void *) &t,sizeof(struct timeval));
+ else
+ t.tv_usec = 0;
+
+ php_sockset_timeout(socketd, &t);
RETURN_TRUE;
}
-#endif
+#endif /* HAVE_SYS_TIME_H */
/* }}} */
/* {{{ proto string fgets(int fp, int length)
Index: php4/ext/standard/fsock.c
diff -u php4/ext/standard/fsock.c:1.37 php4/ext/standard/fsock.c:1.38
--- php4/ext/standard/fsock.c:1.37 Mon Jan 10 11:56:23 2000
+++ php4/ext/standard/fsock.c Sat Jan 15 11:41:17 2000
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: fsock.c,v 1.37 2000/01/10 19:56:23 zeev Exp $ */
+/* $Id: fsock.c,v 1.38 2000/01/15 19:41:17 andrei Exp $ */
/* Synced with php 3.0 revision 1.121 1999-06-18 [ssb] */
/* Synced with php 3.0 revision 1.133 1999-07-21 [sas] */
@@ -116,6 +116,8 @@
char persistent;
char is_blocked;
size_t chunk_size;
+ struct timeval timeout;
+ int timeout_event;
};
typedef struct php_sockbuf php_sockbuf;
@@ -459,6 +461,7 @@
sock->persistent = persistent;
sock->is_blocked = 1;
sock->chunk_size = FG(def_chunk_size);
+ sock->timeout.tv_sec = -1;
FG(phpsockbuf) = sock;
return sock;
@@ -528,13 +531,25 @@
static void php_sockwait_for_data(php_sockbuf *sock)
{
fd_set fdr, tfdr;
+ int retval;
+ struct timeval timeout;
FD_ZERO(&fdr);
FD_SET(sock->socket, &fdr);
+ sock->timeout_event = 0;
while(1) {
tfdr = fdr;
- if(select(sock->socket + 1, &tfdr, NULL, NULL, NULL) == 1)
+ timeout = sock->timeout;
+ if (timeout.tv_sec == -1)
+ retval = select(sock->socket + 1, &tfdr, NULL, NULL, NULL);
+ else {
+ retval = select(sock->socket + 1, &tfdr, NULL, NULL, &timeout);
+ if (retval == 0)
+ sock->timeout_event = 1;
+ }
+
+ if(retval == 1 || retval == 0)
break;
}
}
@@ -555,6 +570,8 @@
if(sock->is_blocked) {
php_sockwait_for_data(sock);
+ if (sock->timeout_event)
+ return 0;
}
/* read at a maximum sock->chunk_size */
@@ -577,7 +594,7 @@
static void php_sockread_total(php_sockbuf *sock, size_t maxread)
{
- while(!sock->eof && TOREAD(sock) < maxread) {
+ while(!sock->eof && TOREAD(sock) < maxread && !sock->timeout_event) {
php_sockread_internal(sock);
}
}
@@ -607,6 +624,13 @@
sock->is_blocked = mode;
return old;
+}
+
+void php_sockset_timeout(int socket, struct timeval *timeout)
+{
+ SOCK_FIND(sock, socket);
+
+ sock->timeout = *timeout;
}
#define SOCK_FIND_AND_READ_MAX(max) \
Index: php4/ext/standard/fsock.h
diff -u php4/ext/standard/fsock.h:1.21 php4/ext/standard/fsock.h:1.22
--- php4/ext/standard/fsock.h:1.21 Fri Dec 17 20:01:14 1999
+++ php4/ext/standard/fsock.h Sat Jan 15 11:41:17 2000
@@ -27,7 +27,7 @@
| Jim Winstead (jimw@php.net) |
+----------------------------------------------------------------------+
*/
-/* $Id: fsock.h,v 1.21 1999/12/18 04:01:14 zeev Exp $ */
+/* $Id: fsock.h,v 1.22 2000/01/15 19:41:17 andrei Exp $ */
/* Synced with php 3.0 revision 1.24 1999-06-18 [ssb] */
@@ -67,6 +67,7 @@
int php_sock_fgetc(int socket);
int php_is_persistent_sock(int);
int php_sockset_blocking(int socket, int mode);
+void php_sockset_timeout(int socket, struct timeval *timeout);
int php_sockdestroy(int socket);
int php_sock_close(int socket);
size_t php_sock_set_def_chunk_size(size_t size);