Net_Socket and select()
| From: | Scott Barron | Date: | Mon, 17 May 2004 12:02:58 +0000 |
| Subject: | Net_Socket and select() | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-29312@lists.php.net to get a copy of this message | ||
Hello,
I would like to see a wrapper for the select() system call in Net_Socket. I'm
quite willing to add this, but I would like some opinions about how it should
be added. Here is my idea, please feel free to comment. Also, if I should be
making a feature request using the bug reporting system please tell me.
It looks like stream_select can be used for this. Since this takes 3 arrays of
descriptors to read/write/error from I propose the following:
Three constants (I'm not married to these names):
define('NET_SOCKET_READ', 2);
define('NET_SOCKET_WRITE', 4);
define('NET_SOCKET_ERROR', 8)
The function (sans error checking etc):
Net_Socket::select($state, $tv_sec, $tv_usec=0) {
$read = NULL;
/* Etc, for write/exception */
if ($state & NET_SOCKET_READ) {
$read[] = $this->fp;
}
/* And so on for the other two */
stream_select($read, $write, $error, $tv_sec, $tv_usec);
/* Return as discussed below */
}
The call:
$sock->select(NET_SOCKET_READ|NET_SOCKET_ERROR, 1);
The thing I haven't thought through very much is what to return. Perhaps just
an OR of the constants describing which the socket is ready to do (since it's
an abstraction there's really only ever going to be one socket checked by
select()). So, for example, if you ask it to select for reading and writing as
in the call above it would return NET_SOCKET_READ|NET_SOCKET_WRITE if the
socket is ready for both.
Does this sound like a good idea to anyone else? If so I can submit a patch,
otherwise we can discuss how better to do it or why it shouldn't be done.
Thanks
-Scott