docker-webman-jsonrpc/jsonrpc/Protocol.php
2023-10-19 15:12:20 +08:00

67 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
// 这个文件需要单独在compose.json中的autoload->files中引用
// "./jsonnl/Protocol.php"
namespace Protocols;
/**
* RPC 协议解析 相关
* 协议格式为 [json字符串\n]
* @author walkor <worker-man@qq.com>
* */
class JsonNL
{
/**
* 检查包的完整性
* 如果能够得到包长则返回包的在buffer中的长度否则返回0继续等待数据
* @param string $buffer
*/
public static function input($buffer)
{
// 获得换行字符"\n"位置
$pos = strpos($buffer, "\n");
// 没有换行符无法得知包长返回0继续等待数据
if ($pos === false) {
return 0;
}
// 有换行符,返回当前包长(包含换行符)
return $pos + 1;
}
/**
* 打包,当向客户端发送数据的时候会自动调用
* @param string $buffer
* @return string
*/
public static function encode($buffer)
{
// json序列化并加上换行符作为请求结束的标记
return json_encode($buffer) . "\n";
}
/**
* 解包当接收到的数据字节数等于input返回的值大于0的值自动调用
* 并传递给onMessage回调函数的$data参数
* @param string $buffer
* @return string
*/
public static function decode($buffer)
{
// 去掉换行,还原成数组
return json_decode(trim($buffer), true);
}
}