35 lines
1.2 KiB
PHP
35 lines
1.2 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ywxapp<admin@ywxapp.cn>
|
|
// +----------------------------------------------------------------------
|
|
// app/Protocols/ProtobufProtocol.php
|
|
namespace addon\wxchat\Protocols;
|
|
|
|
/**
|
|
* ProtobufProtocol 类
|
|
*
|
|
* @author ywxapp <admin@ywxapp.cn>
|
|
*/
|
|
class ProtobufProtocol
|
|
{
|
|
|
|
public static function input($recv_buffer, $connection) {
|
|
if (strlen($recv_buffer) < 4) return 0;
|
|
$len = unpack('N', substr($recv_buffer, 0, 4))[1]; // 大端序4字节长度头
|
|
return $len + 4; // 返回本次包完整长度
|
|
}
|
|
|
|
|
|
public static function decode($recv_buffer, $connection) {
|
|
return substr($recv_buffer, 4); // 剥离长度头,返回纯Protobuf字节流
|
|
}
|
|
|
|
|
|
public static function encode($data, $connection) {
|
|
return pack('N', strlen($data)) . $data; // 添加4字节长度头
|
|
}
|
|
} |