FAST protocol analysis (1) A simple example of FAST codec

 nonsense

The documents that can be searched are less than my hair. There is an OpenFast package that specifically analyzes this thing (C++ is QuickFast), but the official website cannot be opened, and the search is full of wind power simulation things (also called the name OpenFast).

Even if I downloaded an OpenFast source code + package + document, I can't be sure if this thing is what I want, anyway, it's a headache.

popular understanding

For the FAST protocol, you can check the "Full Text Reference of the Chinese Version of the FAST 1.1 Specification" for details. It is recommended to read it. Even if you don't need to understand the principle, you have to read the book if you want to know which types are supported.

In layman's terms, packaging and unpacking can be understood as a fast compression and decompression algorithm, or commonly known as serialization. If you use Json strings, there will be many problems such as symbol redundancy. Even if you use the value transfer method of the get request, it will be a long string. When the amount of data is large, traffic will be generated. For problems such as bandwidth and delay, the optimal solution is of course a byte with 8 bits. The first bit is 0 for what, 1 for what, and the second bit for 0 and 1 for what. However, this method is more scalable Low, the agreement cost is relatively high.

The main purpose of FAST is to convert an object into a string and compress the string into a binary stream according to the type template, which is what it means anyway.

Package

openfast-1.1.2-bin.zip

Download address: OpenFAST - Browse /openfast4j at SourceForge.net

After decompression, it comes with a document, but this document is a bit hard to describe...

openfast-1.1.2-bin\openfast-1.1.2\docs\api\index.html

maven1.1.1 version

<dependency>
    <groupId>org.openfast</groupId>
    <artifactId>openfast</artifactId>
    <version>1.1.1</version>
</dependency>

play

1. Process

(1) Template (MessageTemplate), in fact, is to define some fields and types. If you don’t want to write it dead in the code layer, you can write it as a configuration file. The more friendly one is an xml file. Don’t be afraid of trouble. You can also create a json file or a yml file yourself. some type of.

(2) Context (Context), one encoder/decoder, do not share.

(3) Encoding (FastEncoder)

(4) Decoding (FastDecoder)

After collecting all these things, it should be no problem to make the simplest encoding and decoding.

2. Start directly

        //模板
        MessageTemplate template = new MessageTemplate("",
                new Field[] {
                        new Scalar("1", Type.I32, Operator.COPY, ScalarValue.UNDEFINED, false),
                        new Scalar("2", Type.I32, Operator.DELTA, ScalarValue.UNDEFINED, false),
                });

        //上下文
        Context context = new Context();
        //注册模板
        context.registerTemplate(113, template);
        //编码器
        FastEncoder encoder = new FastEncoder(context);
        Message message = new Message(template);
        message.setInteger(1, 109);
        message.setInteger(2, 29470);
        //编码
        byte[] outByte=encoder.encode(message);

        InputStream sbs = new ByteArrayInputStream(outByte);
        //解码器
        FastDecoder fastDecoder=new FastDecoder(context,sbs);
        //解码
        Message msg111=fastDecoder.readMessage();
        log.info("解码的消息1:{}",msg111.getInt(1));
        log.info("解码的消息2:{}",msg111.getInt(2));

The final running result:

解码的消息1:109
解码的消息2:58940

Guess you like

Origin blog.csdn.net/qq_33601179/article/details/122324385