How do I use PHP's stream_select() with a zlib filter?

时光怂恿深爱的人放手 提交于 2019-12-04 23:13:44

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7207484/how-do-i-use-phps-stream-select-with-a-zlib-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!