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

com.firefly.net.tcp.codec.flex.decode.DisconnectionFrameParser 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.DisconnectionFrame;
import com.firefly.net.tcp.codec.flex.protocol.Frame;
import com.firefly.utils.lang.Pair;

import java.nio.ByteBuffer;

import static com.firefly.net.tcp.codec.flex.protocol.DisconnectionFrame.DISCONNECTION_FRAME_HEADER_LENGTH;

/**
 * @author Pengtao Qiu
 */
public class DisconnectionFrameParser extends AbstractPayloadFrameParser {

    protected byte code;

    @Override
    protected Result parseFrameHeader(ByteBuffer buffer) {
        if (buffer.remaining() < DISCONNECTION_FRAME_HEADER_LENGTH) {
            return Result.UNDERFLOW;
        }

        code = buffer.get();

        short length = buffer.getShort();
        payloadLength = Frame.removeEndFlag(length);
        state = State.PAYLOAD;

        if (buffer.hasRemaining()) {
            return Result.OVERFLOW;
        } else {
            return Result.COMPLETE;
        }
    }

    @Override
    protected Pair generateResult(ByteBuffer buffer) {
        Pair pair = new Pair<>();
        pair.second = new DisconnectionFrame(frame, code, data);

        if (buffer.hasRemaining()) {
            pair.first = Result.OVERFLOW;
        } else {
            pair.first = Result.COMPLETE;
        }

        reset();
        return pair;
    }

    protected void reset() {
        code = 0;
        super.reset();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy