byteferry / sdds_php
一个用于根据SDDS规范进行流解码和编码的PHP库。
Requires
- php: ~7.1
- larapack/dd: 1.*
Requires (Dev)
- phpunit/phpunit: ^5.6
- squizlabs/php_codesniffer: ^2.7
This package is auto-updated.
Last update: 2024-09-14 23:58:08 UTC
README
/$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$
/$$__ $$| $$__ $$| $$__ $$ /$$__ $$
| $$ \__/| $$ \ $$| $$ \ $$| $$ \__/
| $$$$$$ | $$ | $$| $$ | $$| $$$$$$
\____ $$| $$ | $$| $$ | $$ \____ $$
/$$ \ $$| $$ | $$| $$ | $$ /$$ \ $$
| $$$$$$/| $$$$$$$/| $$$$$$$/| $$$$$$/
\______/ |_______/ |_______/ \______/
SDDS for PHP
一个用于根据SDDS规范进行流解码和编码的PHP库。
介绍
什么是SDDS?
- 数据结构
SDDS是“流数据动态结构”的缩写。
- 领域特定语言(DSL)
SDDS是一个领域特定语言(DSL)。
SDDS的模式是可读的,并且对人和机器都是可读的。
- 解析引擎
SDDS是一个按照SDDS规范编写的解析引擎。您可以用它快速完成二进制通信程序的开发。
特性
-
多协议支持 SDDS PHP可以使程序支持多种通信协议。每个协议都是程序的一个通道。
-
数据类型 支持SDDS规范所需的所有数据类型。
-
字节序支持
您只需在SDDS模式中配置字节序(大端,小端)。如果有不一致的字节序,您也可以将它们配置到相应的节点。
-
字符集支持 默认使用UTF-8,也可以指定不同的编码。
-
事件扩展 所有自定义函数都可以通过相应的EventHandler实现。
-
公式表达式 编码或解码前后都支持公式表达式。
-
易于调试
默认集成larapack/dd,您可以使用浏览器调试请求并通过dd将输出输出到页面。
为什么选择SDDS?
通常我们不得不为不同的通信协议编写不同的程序。但是,如果您使用SDDS,您基本上使用的程序是相同的。SDDS已经为您实现了。您需要做的只是扩展SDDS没有实现的数据流操作、数据类型、位操作等。通过SDDS Schema的JSON定义。当协议发生变化时,您只需要修改SDDS Schema而无需修改代码。SDDS可以大大加快您的开发速度。
快速教程
安装
通过Composer
$ composer require byteferry/sdds_php:dev-master
使用
-
步骤1:根据SDDS规范编写SDDS Schema。
-
步骤2:编写调用SDDS PHP的代码
- 实现Channel类
例如:通道类命名为DecoderChannel。
namespace Sdds\Examples\Decode;
use Sdds\Channels\InputChannel;
use Sdds\Channels\ChannelInterface;
class DecodeChannel extends InputChannel implements ChannelInterface
{
/**
* @return mixed
*/
public function registerHandlers(){
//If there is no Event, then return directly
return;
}
}
- 调用您的DecodeChannel
$channel = DecodeChannel::getInstance();
$packet_decoder = $channel->getDecoder($your_channel_name,$your_schema_file);
$data = $packet_decoder->decode($stream_data);
- 使用浏览器通过http方法通过DD调试您的SDDS Schema。 (为了方便快速开发,我们集成了Larapack/dd)
- 将其集成到您的通信程序中。
高级使用
有时现有的代码并不能完全满足您的需求。在这种情况下,您需要通过EventHandler扩展SDDS引擎。 一个典型的EventHandler看起来像这样
namespace Sdds\Examples\Decode;
use Sdds\Constants\ActionTypeContants;
use Sdds\Constants\EventTypeConstants;
use Sdds\Dispatcher\EventHandler;
use Sdds\Dispatcher\EventHandlerInterface;
/**
* Class StreamHandler
* @package Sdds\Examples\Decode
* @desc Note: The class name is best to indicate which type of event you want to listen to.
*/
class StreamHandler extends EventHandler implements EventHandlerInterface
{
/**
* Listener constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* @return mixed
*/
public function eventType(){
//Return the type of event you want to listen to
return EventTypeConstants::EVENT_STREAM;
}
/**
* @return mixed
*/
public function actionType(){
//Returns the operation type, which is decoding (INPUT)
return ActionTypeContants::INPUT;
}
/**
* Because, here is the decoding, that is, the InputStream is being listened to, so all the currently implemented functions must start with read.
* However, it is not necessary if the implementation is before_action or after_action.
* @param $stream
* @param $length
* @desc The first parameter is always the object you want to listen to, followed by the parameters that the function must have.
*/
public function readSome($stream, $length){
//TODO: implement your code here.
}
}
有4种EventHandler,详见Sdds\Constants\EventTypeConstants。
我们使用它们来满足不同的需求
-
DateNode:当我们需要添加对节点的逻辑处理时。
-
Packet:当我们想要扩展包处理时。
-
Stream:当我们想要添加自定义类型,或者添加before_action或after_action时。
-
Bitwise:当我们想要向节点添加位操作时。
我们有了这些EventHandler之后,我们需要在使用之前注册这些EventHandler。
然后,DecoderChannel看起来像这样
namespace Sdds\Examples\Decode;
use Sdds\Channels\InputChannel;
use Sdds\Channels\ChannelInterface;
class DecodeChannel extends InputChannel implements ChannelInterface
{
/**
* @return bool
*/
public function registerHandlers(){
//Get event dispatcher
$dispatcher = $this->getDispatcher();
//Create and register an already implemented EventHandler
$stream_handler = new $StreamHandler();
$dispatcher->registerHandler($stream_handler)
}
}
请参阅示例目录中的源代码以获取更多详细信息。
贡献
请参阅CONTRIBUTING和CONDUCT获取详细信息。
安全
如果您发现任何与安全相关的问题,请在问题跟踪器中创建一个问题。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。