tianhe1986/fatbinarybuffer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tianhe1986/fatbinarybuffer

Composer 安装命令:

composer require tianhe1986/fatbinarybuffer

包简介

binary buffer read and write

README 文档

README

FatBinaryBuffer is a lightweight library for reading/writing binary string.

Requires

PHP 7.0 or higher

Installation

 composer require tianhe1986/fatbinarybuffer

and then in your code

require_once __DIR__ . '/vendor/autoload.php';
use FatBinaryBuffer\FatBinaryBuffer;

Support

It supports char/unsigned char, short/unsigned short, int32/uint32, int64/uint64, string, string with length.
Do not support float and double!

Usage

big endian vs little endian

$binaryBufferBE = new FatBinaryBuffer(true); //big endian
$binaryBufferLE = new FatBinaryBuffer(false); //little endian

buffer opration

$binaryBuffer = new FatBinaryBuffer();

//set buffer, would calculate length and offset automaticly
$buffer = base64_decode('XXXXXX');
$binaryBuffer->setBuffer($buffer);

//get buffer
$buffer = $binaryBuffer->getBuffer();

// clear buffer
$binaryBuffer->clear();

// set offset, and then continue to read/write from this position, take byte as unit
$binaryBuffer->setOffset(4); // read/write from the 4th byte

// rewind, just set offset 0
$binaryBuffer->rewind();

writing data

$binaryBuffer = new FatBinaryBuffer(true);

// char and unsigned char, must pass a number, not a char, or you can use `ord` function for transformation
$binaryBuffer->writeChar(100);
$binaryBuffer->writeUChar(225);

// short and unsigned short
$binaryBuffer->writeShort(-1234);
$binaryBuffer->writeUShort(1236);

// int32 and uint32
$binaryBuffer->writeInt32(-999);
$binaryBuffer->writeUInt32(1098);

// int64 and uint64
$binaryBuffer->writeInt64(-98765);
$binaryBuffer->writeUInt64(888);

// string, write its length as a uint32 value first, and then the real string
$binaryBuffer->writeString("张学友!张学友!我们爱你!");

// string with given length, padding with NUL
$binaryBuffer->writeStringByLength("Hello world", 3); // would get 'Hel' when reading

reading data

$newBuffer = new FatBinaryBuffer();
$newBuffer->setBuffer($binaryBuffer->getBuffer());

// char and unsigned char
$char = $newBuffer->readChar();
$uchar = $newBuffer->readUChar();

// short and unsigned short
$short = $newBuffer->readShort();
$ushort = $newBuffer->readUShort();

// int32 and uint32
$a = $newBuffer->readInt32();
$b = $newBuffer->readUInt32();

// int64 and uint64
$a = $newBuffer->readInt64();
$b = $newBuffer->readUInt64();

// string, read its length first, and then the real string
$str = $newBuffer->readString();

// read string with given length, would remove NUL at the end of the string
$str = $newBuffer->readStringByLength(3);

writing/reading with endian (version >= 1.1)

From 1.1, short/ushort/int32/uint32/int64/uint64 cound handle with giving endian.

//whatever initial endian
$binaryBuffer = new FatBinaryBuffer((mt_rand(1, 10) % 2) === 0);

//write with big endian
$binaryBuffer->writeShort(-1234, true);

//write with little endian
$binaryBuffer->writeUShort(1236, false);

//read with big endian
$binaryBuffer->readInt32(true);

//read with little endian
$binaryBuffer->readUInt64(false);

combine with websocket server

Taking Workerman for exmaple.

require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Protocols\Websocket;
use FatBinaryBuffer\FatBinaryBuffer;

$ws_worker = new Worker("websocket://0.0.0.0:13001");

// 4 processes
$ws_worker->count = 4;

$ws_worker->onConnect = function($connection)
{
    $connection->onWebSocketConnect = function($connection , $http_header)
    {
        $connection->websocketType = Websocket::BINARY_TYPE_ARRAYBUFFER;
    };
};

$ws_worker->onMessage = function($connection, $data)
{
    $socketData = new FatBinaryBuffer(false);
    $socketData->setBuffer($data);
    $opcode = $socketData->readUInt32();
    echo $opcode."\n";
    echo $socketData->readUInt32()."\n";
    echo $socketData->readUInt32()."\n";
    if ($opcode === 21102) {
        echo $socketData->readUInt32()."\n";
        $newData = new FatBinaryBuffer(false);
        $newData->writeUInt32(21103);
        $newData->writeUInt32(16);
        $newData->writeUInt32(11);
        $newData->writeUInt32(777);
        $connection->send($newData->getBuffer());
    } else if ($opcode === 21104) {
        $len = $socketData->readUShort();
        echo "string len ".$len."\n";
        echo $socketData->readStringByLength($len)."\n";
        echo $socketData->readChar()."\n";
        echo $socketData->readUShort()."\n";
        echo $socketData->readInt32()."\n";
        echo $socketData->readStringByLength(5)."\n";
    }
};
Worker::runAll();

Using Layabox as a typescript client for testing. It works fine.

import Socket = Laya.Socket;
import Byte = Laya.Byte;

class TestSocket {
    private socket: Socket;
    private output: Byte;
    private byte: Byte;
    private headerLength:number = 12;

    constructor() {
        this.byte = new Byte();
        this.byte.endian = Byte.LITTLE_ENDIAN;
        this.connect();
    }

    private connect(): void {
        this.socket = new Socket();
        this.socket.connectByUrl("ws://localhost:13001");

        this.output = this.socket.output;
        

        this.socket.on(Laya.Event.OPEN, this, this.onSocketOpen);
        this.socket.on(Laya.Event.CLOSE, this, this.onSocketClose);
        this.socket.on(Laya.Event.MESSAGE, this, this.onMessageReveived);
        this.socket.on(Laya.Event.ERROR, this, this.onConnectError);
    }

    private onSocketOpen(): void {
        console.log("Connected");

        this.byte.clear();
        this.byte.writeUint32(21102);
        this.byte.writeUint32(16);
        this.byte.writeUint32(657);
        this.byte.writeUint32(10);

        this.socket.send(this.byte.buffer);
        this.byte.clear();
    }

    private onSocketClose(): void {
        console.log("Socket closed");
    }

    private onMessageReveived(message: any): void {
        console.log("Message from server:");
        if (typeof message == "string") {
            console.log(message);
        }
        else if (message instanceof ArrayBuffer) {
            let byte = new Byte(message);
            let messageId = byte.getUint32();
			let messageLength = byte.getUint32() - this.headerLength;
			let userId = byte.getUint32();

			console.log(messageId, messageLength, userId);
            this.handleMessage(byte, messageId);
        }
        this.socket.input.clear();
    }

    private onConnectError(e: Event): void {
        console.log("error");
    }

    protected handleMessage(byte:Byte, messageId:number):void
    {
        if (messageId === 21103) {
            let serverTime:number = byte.getUint32();
            console.log("recieve heart beat " + serverTime);

            this.byte.clear();
            let str = "You are shock!我爱黎明!!!";
            let newByte = new Byte();
            newByte.writeUTFString(str);
            newByte.writeByte(111);
            newByte.writeUint16(65500);
            newByte.writeInt32(-70);
            newByte.writeUTFBytes("topod");

            let len = this.headerLength + newByte.length;
            this.byte.writeUint32(21104);
            this.byte.writeUint32(len);
            this.byte.writeUint32(657);
            this.byte.writeArrayBuffer(newByte.buffer);

            this.socket.send(this.byte.buffer);
            this.byte.clear();
        }
    }
}

tianhe1986/fatbinarybuffer 适用场景与选型建议

tianhe1986/fatbinarybuffer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.93k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Buffer」 「string」 「binary」 「read」 「write」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tianhe1986/fatbinarybuffer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tianhe1986/fatbinarybuffer 我们能提供哪些服务?
定制开发 / 二次开发

基于 tianhe1986/fatbinarybuffer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-26