ByteToMessageCodecHandler.java 821 Bytes
package com.viontech.netty;

import com.viontech.model.BaseModel;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * .
 *
 * @author 谢明辉
 * @date 2020/8/21
 */
@Slf4j
public class ByteToMessageCodecHandler extends ByteToMessageCodec<BaseModel> {


    @Override
    protected void encode(ChannelHandlerContext ctx, BaseModel msg, ByteBuf out) throws Exception {
        msg.to(out);
        log.info("发送消息-长度:[{}],协议:[{}]", msg.getLength(), Integer.toHexString((int)msg.getProtocol()));
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        out.add(BaseModel.from(in));
    }
}