byteferry / sdds_php
A PHP library to work with stream decode and encode according to the SDDS Specification.
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-11-15 00:22:55 UTC
README
/$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$
/$$__ $$| $$__ $$| $$__ $$ /$$__ $$
| $$ \__/| $$ \ $$| $$ \ $$| $$ \__/
| $$$$$$ | $$ | $$| $$ | $$| $$$$$$
\____ $$| $$ | $$| $$ | $$ \____ $$
/$$ \ $$| $$ | $$| $$ | $$ /$$ \ $$
| $$$$$$/| $$$$$$$/| $$$$$$$/| $$$$$$/
\______/ |_______/ |_______/ \______/
SDDS for PHP
A PHP library to work with stream decode and encode according to the SDDS Specification.
Introduction
What is SDDS?
- Data Structure
SDDS is short word of "stream data dynamic structure".
- DSL
SDDS is a DSL (domain-specific language) .
The schema of SDDS is human-readable and machine and machine-readable.
- Parse Engine
SDDS is a parse engine coded according the SDDS Specification. You can rapid complete the developing of the binary communication program.
Features
-
Multi-protocol support SDDS PHP could enabled the programs to support multiple communication protocols. Each protocol is a channel of the program.
-
Data types Support all data types required by the SDDS specification.
-
Endianness support
You only need to configure Endianness(Big-Endian, Little-Endian) in the SDDS Schema. If there are individual endianness that are inconsistent, you can also configure them to the corresponding nodes.
-
Charset support By default, UTF-8 is used, and you can also specify different encodings.
-
Event extension All custom functions can be implemented with the corresponding EventHandler.
-
Formula expression Formula expression is supported before encoding or after decoding.
-
Easy to debug
The default integration larapack/dd, you can use the browser to debug requests and output to the page via dd.
Why SDDS?
Usually we have to write different programs for different communication protocols. However, if you use SDDS, the programs you basically use are the same. And, SDDS has been implemented for you. All you have to do is expand the data stream operations, data types, bit operations, etc. that are not implemented by SDDS. And, through the JSON definition of SDDS Schema. When protocol had been changed, you only need to modify the SDDS Schema without modifying your code. SDDS can greatly speed up your development.
Quick tutorial
Install
Via Composer
$ composer require byteferry/sdds_php:dev-master
Usage
-
Step 1: write SDDS Schema according to SDDS Specification .
-
Step 2: Write the code to call SDDS PHP
- implement the Channel class:
For example: the channel class is named 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;
}
}
- Call your DecodeChannel
$channel = DecodeChannel::getInstance();
$packet_decoder = $channel->getDecoder($your_channel_name,$your_schema_file);
$data = $packet_decoder->decode($stream_data);
- Debug your SDDS Schema with DD using the http method through the browser. (To facilitate rapid development, we integrated Larapack/dd)
- Integrate with your communication program.
Advanced usage
Sometimes the existing code doesn't fully meet your needs. At this point, you need to extend the SDDS engine with EventHandler. A typical EventHandler looks like this:
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.
}
}
There are 4 kind of EventHandler, See Sdds\Constants\EventTypeConstants for details.
We use them to meet different needs:
-
DateNode: When we need to add a logical processing of a node.
-
Packet: When we want to extend packet processing.
-
Stream: When we want to add a custom type, or to add before_action or after_action
-
Bitwise: When we want to add a bit operation to a node.
After we have these EventHandlers, we need to register these EventHandlers before using.
Then, the DecoderChannel looks like this:
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)
}
}
Please see the source code in the examples directory for more details.
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Security
If you discover any security related issues, please create an issue in the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.