All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.net.tcp.codec.flex.decode.AbstractPayloadFrameParser Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.net.tcp.codec.flex.decode;

import com.firefly.net.tcp.codec.flex.protocol.Frame;
import com.firefly.utils.lang.Pair;

import java.nio.ByteBuffer;

/**
 * @author Pengtao Qiu
 */
abstract public class AbstractPayloadFrameParser implements FlexParser {

    protected Frame frame;
    protected State state = State.HEADER;
    protected int payloadLength;
    protected byte[] data;

    public Pair parse(ByteBuffer buffer, Frame header) {
        frame = header;
        while (true) {
            switch (state) {
                case HEADER: {
                    switch (parseFrameHeader(buffer)) {
                        case UNDERFLOW:
                            return new Pair<>(Result.UNDERFLOW, null);
                        case COMPLETE:
                            if (payloadLength == 0) {
                                return generateResult(buffer);
                            } else {
                                return new Pair<>(Result.UNDERFLOW, null);
                            }
                        case OVERFLOW:
                            break;
                    }
                }
                break;
                case PAYLOAD:
                    return parsePayload(buffer);
            }
        }
    }

    abstract protected Result parseFrameHeader(ByteBuffer buffer);

    protected Pair parsePayload(ByteBuffer buffer) {
        if (payloadLength == 0) {
            return generateResult(buffer);
        }

        if (buffer.remaining() < payloadLength) {
            return new Pair<>(Result.UNDERFLOW, null);
        }

        data = new byte[payloadLength];
        buffer.get(data);
        return generateResult(buffer);
    }

    abstract protected Pair generateResult(ByteBuffer buffer);

    protected void reset() {
        frame = null;
        payloadLength = 0;
        data = null;
        state = State.HEADER;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy