I currently have a server daemon written in PHP which accepts incoming connections and creates network streams for them using the stream_socket_*
functions and polls active streams using stream_select()
. I'd like to be able to add a zlib filter (using string_filter_append()
) to an arbitrary stream, but when I do, I get an error telling me that stream_select()
can't be used to poll a filtered stream.
How can I get around this limitation?
You can use a pipe, and add the filter to the pipe instead.
This will allow you to use stream_select on the stream, and the pipe will serve as a buffer for zlib.
Read the raw data from the select()ed stream, write it to the pipe, and read the decoded data on the other side :)
list($in, $out) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
stream_filter_append($out, 'zlib.inflate', STREAM_FILTER_READ);
stream_set_blocking($out, 0);
while (stream_select(...)) {
// assuming that $stream is non blocking
stream_copy_to_stream($stream, $in);
$decoded_data = stream_get_contents($out);
}
The same can probably be achieved with a php://memory stream.