package com.zy.core.netty.handle; import com.zy.core.netty.domain.ChPackage; import com.zy.core.netty.properties.TcpProperties; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 国标编码器 * 此解码器将会生成校验码 * 处理方式: 异或和 * Created by vincent on 2019-04-02 */ @Slf4j @Component @ChannelHandler.Sharable public class ProtocolEncoder extends MessageToByteEncoder { @Autowired private TcpProperties tcpProperties; @Override protected void encode(ChannelHandlerContext ctx, Object obj, ByteBuf out) { boolean upgradeLog = true; if (obj instanceof ByteBuf){ out.writeBytes((ByteBuf) obj); } else if (obj instanceof byte[]) { out.writeBytes((byte[]) obj); } else if (obj instanceof ChPackage) { } // if (tcpProperties.isPrintPacLog() || upgradeLog){ // log.info("uuid={} 下行 >>> {}", getVin(out), ByteBufUtil.hexDump(out)); // } } // private String getVin(ByteBuf byteBuf){ // byte[] bytes = new byte[PackagePart.UNIQUENO.getLen()]; // byteBuf.markReaderIndex(); // byteBuf.readerIndex(PackagePart.UNIQUENO.getStartIndex()); // byteBuf.readBytes(bytes); // byteBuf.resetReaderIndex(); // return RadixTools.bytesToStr(bytes); // } }