首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
关于开启open_length_check=true情况下,是否还需在onreceive还需进行合包处理?
```php function onReceive($serv, $fd, $reactor_id, $data) { if (!isset($this->_buffer[$fd]) or $this->_buffer[$fd] === '') { //超过buffer区的最大长度了 if (count($this->_buffer) >= $this->buffer_maxlen) { $n = 0; foreach ($this->_buffer as $k => $v) { $this->close($k); $n++; //清理完毕 if ($n >= $this->buffer_clear_num) { break; } } $this->log("clear $n buffer"); } //解析包头 $header = unpack(self::HEADER_STRUCT, substr($data, 0, self::HEADER_SIZE)); //错误的包头 if ($header === false) { $this->close($fd); } $header['fd'] = $fd; $this->_headers[$fd] = $header; //长度错误 if ($header['length'] - self::HEADER_SIZE > $this->packet_maxlen or strlen($data) > $this->packet_maxlen) { return $this->sendErrorMessage($fd, self::ERR_TOOBIG); } //加入缓存区 $this->_buffer[$fd] = substr($data, self::HEADER_SIZE); } else { $this->_buffer[$fd] .= $data; } //长度不足 if (strlen($this->_buffer[$fd]) < $this->_headers[$fd]['length']) { return true; } //数据解包 $request = self::decode($this->_buffer[$fd], $this->_headers[$fd]['type']); if ($request === false) { $this->sendErrorMessage($fd, self::ERR_UNPACK); } //执行远程调用 else { //当前请求的头 self::$requestHeader = $_header = $this->_headers[$fd]; //调用端环境变量 if (!empty($request['env'])) { self::$clientEnv = $request['env']; } //socket信息 self::$clientEnv['_socket'] = $this->server->connection_info($_header['fd']); $response = $this->call($request, $_header); //发送响应 $ret = $this->server->send($fd, self::encode($response, $_header['type'], $_header['uid'], $_header['serid'])); if ($ret === false) { trigger_error("SendToClient failed. code=".$this->server->getLastError()." params=".var_export($request, true)."\nheaders=".var_export($_header, true), E_USER_WARNING); } //退出进程 if (self::$stop) { exit(0); } } //清理缓存 unset($this->_buffer[$fd], $this->_headers[$fd]); return true; } ``` 代码来自https://gitee.com/swoole/swoole_framework/blob/master/libs/Swoole/Protocol/RPCServer.php 是否还需代码31 41 44行处理,open_length_check开启后只有完整包才会回调onReceive吗?
发布于3年前 · 8 次浏览 · 来自
提问
006
```php function onReceive($serv, $fd, $reactor_id, $data) { if (!isset($this->_buffer[$fd]) or $this->_buffer[$fd] === '') { //超过buffer区的最大长度了 if (count($this->_buffer) >= $this->buffer_maxlen) { $n = 0; foreach ($this->_buffer as $k => $v) { $this->close($k); $n++; //清理完毕 if ($n >= $this->buffer_clear_num) { break; } } $this->log("clear $n buffer"); } //解析包头 $header = unpack(self::HEADER_STRUCT, substr($data, 0, self::HEADER_SIZE)); //错误的包头 if ($header === false) { $this->close($fd); } $header['fd'] = $fd; $this->_headers[$fd] = $header; //长度错误 if ($header['length'] - self::HEADER_SIZE > $this->packet_maxlen or strlen($data) > $this->packet_maxlen) { return $this->sendErrorMessage($fd, self::ERR_TOOBIG); } //加入缓存区 $this->_buffer[$fd] = substr($data, self::HEADER_SIZE); } else { $this->_buffer[$fd] .= $data; } //长度不足 if (strlen($this->_buffer[$fd]) < $this->_headers[$fd]['length']) { return true; } //数据解包 $request = self::decode($this->_buffer[$fd], $this->_headers[$fd]['type']); if ($request === false) { $this->sendErrorMessage($fd, self::ERR_UNPACK); } //执行远程调用 else { //当前请求的头 self::$requestHeader = $_header = $this->_headers[$fd]; //调用端环境变量 if (!empty($request['env'])) { self::$clientEnv = $request['env']; } //socket信息 self::$clientEnv['_socket'] = $this->server->connection_info($_header['fd']); $response = $this->call($request, $_header); //发送响应 $ret = $this->server->send($fd, self::encode($response, $_header['type'], $_header['uid'], $_header['serid'])); if ($ret === false) { trigger_error("SendToClient failed. code=".$this->server->getLastError()." params=".var_export($request, true)."\nheaders=".var_export($_header, true), E_USER_WARNING); } //退出进程 if (self::$stop) { exit(0); } } //清理缓存 unset($this->_buffer[$fd], $this->_headers[$fd]); return true; } ``` 代码来自https://gitee.com/swoole/swoole_framework/blob/master/libs/Swoole/Protocol/RPCServer.php 是否还需代码31 41 44行处理,open_length_check开启后只有完整包才会回调onReceive吗?
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2021-09-26
鲁飞
是的,配合其他选项一起使用,例如: ```php $server->set([ 'open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_max_length' => 800000, ]); ```
赞
0
回复
微信公众号
热门内容
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。