项目作者: byteferry

项目描述 :
A PHP library to work with stream decode and encode according to the SDDS Specification.
高级语言: PHP
项目地址: git://github.com/byteferry/sdds_php.git
创建时间: 2019-04-14T06:23:24Z
项目社区:https://github.com/byteferry/sdds_php

开源协议:MIT License

下载


  1. /$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$
  2. /$$__ $$| $$__ $$| $$__ $$ /$$__ $$
  3. | $$ \__/| $$ \ $$| $$ \ $$| $$ \__/
  4. | $$$$$$ | $$ | $$| $$ | $$| $$$$$$
  5. \____ $$| $$ | $$| $$ | $$ \____ $$
  6. /$$ \ $$| $$ | $$| $$ | $$ /$$ \ $$
  7. | $$$$$$/| $$$$$$$/| $$$$$$$/| $$$$$$/
  8. \______/ |_______/ |_______/ \______/

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

  1. $ 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.

  1. namespace Sdds\Examples\Decode;
  2. use Sdds\Channels\InputChannel;
  3. use Sdds\Channels\ChannelInterface;
  4. class DecodeChannel extends InputChannel implements ChannelInterface
  5. {
  6. /**
  7. * @return mixed
  8. */
  9. public function registerHandlers(){
  10. //If there is no Event, then return directly
  11. return;
  12. }
  13. }
  1. Call your DecodeChannel
  1. $channel = DecodeChannel::getInstance();
  2. $packet_decoder = $channel->getDecoder($your_channel_name,$your_schema_file);
  3. $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:

  1. namespace Sdds\Examples\Decode;
  2. use Sdds\Constants\ActionTypeContants;
  3. use Sdds\Constants\EventTypeConstants;
  4. use Sdds\Dispatcher\EventHandler;
  5. use Sdds\Dispatcher\EventHandlerInterface;
  6. /**
  7. * Class StreamHandler
  8. * @package Sdds\Examples\Decode
  9. * @desc Note: The class name is best to indicate which type of event you want to listen to.
  10. */
  11. class StreamHandler extends EventHandler implements EventHandlerInterface
  12. {
  13. /**
  14. * Listener constructor.
  15. */
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20. /**
  21. * @return mixed
  22. */
  23. public function eventType(){
  24. //Return the type of event you want to listen to
  25. return EventTypeConstants::EVENT_STREAM;
  26. }
  27. /**
  28. * @return mixed
  29. */
  30. public function actionType(){
  31. //Returns the operation type, which is decoding (INPUT)
  32. return ActionTypeContants::INPUT;
  33. }
  34. /**
  35. * Because, here is the decoding, that is, the InputStream is being listened to, so all the currently implemented functions must start with read.
  36. * However, it is not necessary if the implementation is before_action or after_action.
  37. * @param $stream
  38. * @param $length
  39. * @desc The first parameter is always the object you want to listen to, followed by the parameters that the function must have.
  40. */
  41. public function readSome($stream, $length){
  42. //TODO: implement your code here.
  43. }
  44. }

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:

  1. namespace Sdds\Examples\Decode;
  2. use Sdds\Channels\InputChannel;
  3. use Sdds\Channels\ChannelInterface;
  4. class DecodeChannel extends InputChannel implements ChannelInterface
  5. {
  6. /**
  7. * @return bool
  8. */
  9. public function registerHandlers(){
  10. //Get event dispatcher
  11. $dispatcher = $this->getDispatcher();
  12. //Create and register an already implemented EventHandler
  13. $stream_handler = new $StreamHandler();
  14. $dispatcher->registerHandler($stream_handler)
  15. }
  16. }

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.