如何将PHP的stream_select()与zlib过滤器一起使用? [英] How do I use PHP's stream_select() with a zlib filter?

查看:167
本文介绍了如何将PHP的stream_select()与zlib过滤器一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个用PHP编写的服务器守护进程,它使用 stream_socket _ * 函数接受传入连接并为它们创建网络流,并使用 stream_select()。我希望能够添加一个zlib过滤器(使用 string_filter_append())到一个任意的流,但是当我这样做,我得到一个错误告诉我, stream_select()不能用于查询过滤的流。



如何解决这个限制?您可以使用一个管道,并将过滤器添加到管道中。

解决方案



从select()函数中读取原始数据流,把它写到管道,并读取解码的数据在另一边:)

  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(...)){
//假设$ stream是非阻塞的
stream_copy_to_stream($ stream,$ in);

$ decoded_data = stream_get_contents($ out);

$ / code $ / pre
$ b $ p用php:// memory stream也可以达到同样的效果。 / p>

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.

这篇关于如何将PHP的stream_select()与zlib过滤器一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆