承接 sqrt-cat/proto-bin 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sqrt-cat/proto-bin

最新稳定版本:v1.0.0

Composer 安装命令:

composer require sqrt-cat/proto-bin

包简介

A PHP library similar to protobuf for binary protocol packaging/unpacking, which can package biz data into binary format for efficient transmission. Implemented using native PHP with no other external dependencies. Supports data types such as fixed length and variable length.

README 文档

README

ProtoBin 是一个使用PHP原生开发的二进制协议的序列化库。在 PHP 前提下,理念与 Protobuf 类似,但更加简单易用。

支持数据类型

定长类

const TYPE_TINYINT = 'tinyint'; const TYPE_INT16 = 'int16'; const TYPE_INT32 = 'int32'; const TYPE_INT64 = 'int64';

变长类

const TYPE_STRING = 'string'; const TYPE_TEXT = 'text';

你可能会吐槽为何没有非标量数据类型,其实可以序列化后用text类型来存储。

示例代码

# 安装依赖
composer require sqrt-cat/proto-bin

消息体定义

<?php
use SqrtCat\ProtoBin\ProtocolMessage;
use SqrtCat\ProtoBin\ProtocolType;

class RegisterRequest extends ProtocolMessage
{
    public $account;
    public $password;
    public $age;

    // 参数项位序 accountBin passwordBin ageBin
    public static $paramNameMapping = [
        0 => 'account',
        1 => 'password',
        2 => 'age',
    ];

    // 参数类型
    public static $paramProtocolTypeMapping = [
        'account'  => ProtocolType::TYPE_STRING,
        'password' => ProtocolType::TYPE_STRING,
        'age'      => ProtocolType::TYPE_TINYINT,
    ];

    public function setAccount($account)
    {
        $paramType        = static::getParamType('account');
        $this->accountBin = ProtocolType::pack($account, $paramType);
    }

    public function getAccount()
    {
        return $this->account;
    }

    public function setPassword($password)
    {
        $paramType         = static::getParamType('password');
        $this->passwordBin = ProtocolType::pack($password, $paramType);
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setAge($age)
    {
        $paramType    = static::getParamType('age');
        $this->ageBin = ProtocolType::pack($age, $paramType);
    }

    public function getAge()
    {
        return $this->age;
    }
}

打包/解包业务数据

<?php
// 你的业务数据
$data = [
    'account'  => 'sqrtcat',
    'password' => '123456',
    'age'      => 29,
];

var_dump(http_build_query($data));// 文本表单模式的数据字节数
var_dump(json_encode($data));// 文本json模式的数据字节数

// 二进制协议打包
$registerRequest = new RegisterRequest();
$registerRequest->setAccount('sqrtcat');
$registerRequest->setPassword('123456');
$registerRequest->setAge(29);
$dataBin = $registerRequest->packToBinStream();
var_dump($dataBin);// 二进制协议数据字节数

// 解包二进制协议
$registerRequest->unpackFromBinStream($dataBin);
echo $registerRequest->getAccount() . PHP_EOL;
echo $registerRequest->getPassword() . PHP_EOL;
echo $registerRequest->getAge() . PHP_EOL;

对比二进制的传输协议与表单、文本类协议的字节数,可以发现二进制协议的传输效率更高。

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-12