package com.yfd.platform.component.nettyclient;

import com.yfd.platform.component.nettyclient.MyMessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;

public class MyDelimiterBasedFrameDecoder extends DelimiterBasedFrameDecoder {

    public MyDelimiterBasedFrameDecoder(int maxFrameLength, ByteBuf delimiter) {
        super(maxFrameLength, delimiter);
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        ByteBuf frame = (ByteBuf) super.decode(ctx, in);
        MyMessageProtocol msg = new MyMessageProtocol();
        if (frame == null) {
            return null;
        }
        if (frame.readableBytes() >= 19) {
            //读取SenderSerialNo字段
            msg.setSenderSerialNo(frame.readLongLE());
            //读取ReceiverSerialNo字段
            msg.setReceiverSerialNo(frame.readLongLE());
            //读取Sessionflag字段
            msg.setSessionflag(frame.readByte());
            //读取length字段
            int length = frame.readIntLE();
            msg.setXmllen(length);
            //读取body
            byte []bytes = new byte[length];
            frame.readBytes(bytes);
            msg.setXmlcontent(bytes);
        }
        return msg;

    }
}


