当前位置:网站首页>php socket通信中stream_select方法的理解
php socket通信中stream_select方法的理解
2022-06-25 22:05:00 【ndrandy】
关于php stream_select方法的研究理解
function stream_select (array &$read, array &$write, array &$except, $tv_sec, $tv_usec = null) {
}stream_select函数返回的条件为:
- $read数组中的任何一个连接发生可读事件
- $write数组中任何一个连接发生可写事件
- $except数组中的任何一个连接有带外数据到来(OOB)
- 当前进程收到系统信号(signal), 如:pcntl_alarm产生的闹钟信号,也就是说信号可以中断stream_select的调用,使其立即返回
- stream_select阻塞等待时间超过($tv_sec+$tv_usec)的时间总值,如果$tv_sec=&null时,则无限阻塞直到上面4种返回条件中的任何一种发生了
返回值
- 返回值为监听的所有连接中发生状态改变的连接数,比如当有3个连接发生可写事件,导致方法返回,则返回值为3(integer类型)
- 如果是被系统信号中断返回值为false并输出警告(被信号中断).
- 如果等待直到超时都没任何事件发生,则返回0
关于带外数据
- 带外数据长度只支持1个字符,用于发生紧急的数据,比如暂停这类的功能,就是发生一个紧急字符优先发送到对端,进而紧急处理
stream_select是如何判断连接可读可写的
- 每一个socket连接都有一个读缓冲区(readBuffer)和一个写缓冲区(writeBuffer). 每个缓冲区都有一个低水位标记. 接收缓存区低水位标记(用于判断可读)和发送缓存区低水位标记(用于判断可写)stream_select底层通过遍历所有待监听的socket连接,并通过下面的方式判断每个连接的可读可写:
- 可读: 当readBuffer中的数据量”大等于”接收低水位标记值时. 默认接收低水位标记值=1; 还有另一种情况发生可读事件, 就是对端关闭连接时, 这时可以使用 !is_resource($fd) || feof($fd)==true 判断连接已经断开
- 可写: 当writeBuffer中剩余的可用空间”大等于”发送低水位标记值时, 默认发送低水位标记值=2048
边栏推荐
- Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
- Extraction system apk
- qtcreator 格式化代码
- Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
- Wireshark对IMAP抓包分析
- Mutual conversion between QT utf8 and Unicode encoding, and the Unicode encoding output format is &xxxxx
- 平衡二叉树AVL
- mysql集群
- C. Yet Another Card Deck-Educational Codeforces Round 107 (Rated for Div. 2)
- hiberate架构介绍及环境搭建(非常详细)
猜你喜欢

如何设计产品的roadmap?

Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)

jdbc常见异常及错误解决办法汇总

史上最简单的录屏转gif小工具LICEcap,要求不高可以试试

QT Chinese and English use different fonts respectively

idea Kotlin版本升级

idea 查看单元测试覆盖率

QT custom implemented calendar control

DPVS-FullNAT模式管理篇

Leaky API interface practical development series (13): gooseneck cloud service php-api two-dimensional array parameter transfer solution
随机推荐
What is Unified Extensible Firmware Interface (UEFI)?
idea Kotlin版本升级
C# IO Stream 流(二)扩展类_封装器
流数据
Qt自定义实现的日历控件
C2. k-LCM (hard version)-Codeforces Round #708 (Div. 2)
Qtcreator formatting code
OpenResty篇01-入门简介和安装配置
MySQL自定义函数实例
库项目和App项目中清单文件的包名不要相同
debezium
php性能优化
C. Fibonacci Words-April Fools Day Contest 2021
平衡二叉树AVL
分享一个OSGeo4W64下载好的库,基于qgis3.10的
SSL/TLS、对称加密和非对称加密和TLSv1.3
Ad20 learning notes I
Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
xtrabackup的备份还原
Use of xinchida ble 5.0 Low Power Bluetooth module (at command serial port transparent transmission) rsbrs02abr