com.firefly.net.tcp.codec.flex.decode.DisconnectionFrameParser Maven / Gradle / Ivy
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();
}
}