byteferry/sdds_php

A PHP library to work with stream decode and encode according to the SDDS Specification.

v0.1.0-alpha 2019-09-14 11:57 UTC

This package is auto-updated.

Last update: 2024-04-14 23:09:32 UTC


README

  /$$$$$$  /$$$$$$$  /$$$$$$$   /$$$$$$ 
 /$$__  $$| $$__  $$| $$__  $$ /$$__  $$
| $$  \__/| $$  \ $$| $$  \ $$| $$  \__/
|  $$$$$$ | $$  | $$| $$  | $$|  $$$$$$ 
 \____  $$| $$  | $$| $$  | $$ \____  $$
 /$$  \ $$| $$  | $$| $$  | $$ /$$  \ $$
|  $$$$$$/| $$$$$$$/| $$$$$$$/|  $$$$$$/
 \______/ |_______/ |_______/  \______/ 
 

SDDS for PHP

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

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

  1. 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;
    }
}
  1. Call your DecodeChannel
    $channel = DecodeChannel::getInstance();
    $packet_decoder = $channel->getDecoder($your_channel_name,$your_schema_file);
    $data = $packet_decoder->decode($stream_data);
    
  1. Debug your SDDS Schema with DD using the http method through the browser. (To facilitate rapid development, we integrated Larapack/dd)   
  2. 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.