我目前有一个用PHP编写的服务器守护进程,它接受传入的连接并使用stream_socket_*
函数为它们创建网络流,并使用stream_select()
轮询活动流。我想能够向任意流添加zlib过滤器(使用string_filter_append()
),但是当我这样做时,会出现错误提示,告诉我不能使用stream_select()
来轮询过滤流。
我该如何规避这个限制?
您可以使用管道,将过滤器添加到管道中。
这将允许您在流上使用stream_select,并且该管道将作为zlib的缓冲区。
从select()ed流中读取原始数据,将其写入管道中,并在另一侧读取解码后的数据 :)
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);
}