|
stream_select
Runs the equivalent of the select() system call on the given
arrays of streams with a timeout specified by tv_sec and tv_usec
(PHP 4 >= 4.3.0, PHP 5)
Code Examples / Notes » stream_selectben
You can key on file descriptors just fine by casting them to an int or a string, which returns what you would expect.
mbaynton
The documentation states that: You do not need to pass every array to stream_select(). You can leave it out and use an empty array or NULL instead. Also do not forget that those arrays are passed by reference and will be modified after stream_select() returns. This appears to be incorrect, at least in PHP 5.1.6, because PHP sees these parameters are passed by reference and thus insists on a bona fide variable for the reference to refer to: Fatal error: Only variables can be passed by reference is what you get if you pass NULL or anything else literally. maxdamantus
Simple stream_select wrapper.. Returns the first stream in the array, and sets parameter 2 to the key (So that it is easy to identify what received data): <?php function select($array, &$vkey, $timeout=0){ $select = array(); $null = NULL; foreach($array as $key => $sock){ $x = count($select); $select[$x] = $sock; $keys[$x] = $key; } if(stream_select($select, $null, $null, $timeout)){ foreach($keys as $key){ if($array[$key] == $select[0]){ $vkey = $key; return($select[0]); } } } } $streams = array("foo" => $stream_one, "bar" => $stream_two); // Create an array of two (already existant) streams. if($new = select($streams, $key, 60)){ //Sets $new to the resource that next gets new data, and $key to either "foo", or "bar", depending which one it is. echo $key.":".stream_get_line($new, 2048)."\n"; } ?> phpdoc
Please note that, on return, the key of "read" will be zero based, serially numbered according to the streams for which there is read data ready only. In other words, if you want to know which of the original streams placed in "read" is ready, there is no immediate way of knowing that. If you want to know which of the original stream is which, you can either use "==", or possibly set a reverse map array, in which the stream is the key, and the key to the original "read" array is the data. maartenwolzak
Note that you should change the calctimeout function below to divide the outcome by 1.000.000 otherwise you'll be waiting for two years instead of one minute for the socket to timeout... <?php // The function to calculate the timeout function calctimeout($maxtime, $starttime) { return ($maxtime - ((microtime(true) - $starttime) * 1000000))/1000000; } ?> aidan
Note that contrary to what the previous poster said, one is not able to use a stream resource as a key for an array. Rather, if you want to know which socket you are dealing with, consider using code similar to this: <?php $sockets = array("sock_1" => $sock1, "sock_2" => $sock2, "sock_3" => $sock_3); $read = $write = $error = $sockets; $num = stream_select($read, $write, $error, 10); if ($n > 0) { foreach ($read as $r) { $key = array_search($r, $sockets); // $key will be "sock_1", "sock_2", "sock_3", etc. } } ?> Hope that helps someone out there! bluej100@gmail
@mbaynton at gmail dot com A handy syntactic trick: <?php $r = Array($stream1, $stream2); stream_select($r, $w = null, $x = null, 1337); ?> I've seen it recommended elsewhere in the documentation for clarifying magic arguments so maintainers don't have to go check the function itself, but it also solves your problem here. |
Change Languagestream_bucket_append stream_bucket_make_writeable stream_bucket_new stream_bucket_prepend stream_context_create stream_context_get_default stream_context_get_options stream_context_set_option stream_context_set_params stream_copy_to_stream stream_encoding stream_filter_append stream_filter_prepend stream_filter_register stream_filter_remove stream_get_contents stream_get_filters stream_get_line stream_get_meta_data stream_get_transports stream_get_wrappers stream_register_wrapper stream_resolve_include_path stream_select stream_set_blocking stream_set_timeout stream_set_write_buffer stream_socket_accept stream_socket_client stream_socket_enable_crypto stream_socket_get_name stream_socket_pair stream_socket_recvfrom stream_socket_sendto stream_socket_server stream_socket_shutdown stream_wrapper_register stream_wrapper_restore stream_wrapper_unregister |