How to use PHP stream_select () with zlib filter?

Currently, I have a daemon server written in PHP that accepts incoming connections and creates network streams for them using stream_socket_* functions and polls active streams using stream_select() . I would like to add a zlib filter (using string_filter_append() ) to an arbitrary stream, but when I do this, I get an error message indicating that stream_select() cannot be used to poll the filtered stream.

How can I get around this limitation?

+6
source share
1 answer

You can use a channel and add a filter to the channel instead.

This will allow you to use stream_select in the stream, and the channel will serve as a buffer for zlib.

Read the raw data from the select () ed stream, write it in the pipe and read the decoded data from 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 be achieved using php: // memory stream.

+3
source

Source: https://habr.com/ru/post/895984/


All Articles