|
@@ -13,49 +13,48 @@ import io.netty.channel.Channel;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
|
|
/**
|
|
|
- * 16 进制 2进制 抽象
|
|
|
+ * 16 进制 2进制 抽象
|
|
|
*/
|
|
|
public abstract class HexBinaryAcceptanceHandlerAdapter extends AcceptanceInboundHandlerAdapter {
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(HexBinaryAcceptanceHandlerAdapter.class);
|
|
|
- @Override
|
|
|
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
- super.channelRead(ctx, msg);
|
|
|
- ByteBuf in = (ByteBuf) msg;
|
|
|
- if (!in.isReadable()) {
|
|
|
- return;
|
|
|
- }
|
|
|
- printAcceptanceData(in,ctx);
|
|
|
-
|
|
|
- try {
|
|
|
- handle(in,ctx.channel());
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
- } finally {
|
|
|
- in.release();
|
|
|
- //ReferenceCountUtil.release(msg);// 显示丢弃已经接受的消息
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- ctx.flush();
|
|
|
- }
|
|
|
-
|
|
|
- protected void printAcceptanceData(ByteBuf dataByteBuf,ChannelHandlerContext ctx) {
|
|
|
- //ByteBuf dataByteBufCopy = dataByteBuf.copy();
|
|
|
- dataByteBuf.markReaderIndex();
|
|
|
- byte[] dataByteArray = new byte[dataByteBuf.readableBytes()];
|
|
|
- dataByteBuf.readBytes(dataByteArray);
|
|
|
- String printHexBinary = DatatypeConverter.printHexBinary(dataByteArray);
|
|
|
- if (channelDeviceMap.get(ctx.channel())!=null) {
|
|
|
- MDC.put(MDC_DEVICEID, channelDeviceMap.get(ctx.channel()));
|
|
|
- logger.info("设备: [{}] 传入数据为 : {}", channelDeviceMap.get(ctx.channel()), printHexBinary);
|
|
|
- }
|
|
|
- else {
|
|
|
- logger.info("设备传入数据为 : {}", printHexBinary);
|
|
|
- }
|
|
|
- dataByteBuf.resetReaderIndex();
|
|
|
- //dataByteBufCopy.release();
|
|
|
- }
|
|
|
-
|
|
|
- abstract protected void handle(ByteBuf in, Channel channel) throws Exception;
|
|
|
-
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(HexBinaryAcceptanceHandlerAdapter.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
+ super.channelRead(ctx, msg);
|
|
|
+ ByteBuf in = (ByteBuf) msg;
|
|
|
+ if (!in.isReadable()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ printAcceptanceData(in, ctx);
|
|
|
+
|
|
|
+ try {
|
|
|
+ handle(in, ctx.channel());
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ } finally {
|
|
|
+ in.release();
|
|
|
+ ReferenceCountUtil.release(msg);// 显示丢弃已经接受的消息
|
|
|
+ }
|
|
|
+
|
|
|
+ ctx.flush();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void printAcceptanceData(ByteBuf dataByteBuf, ChannelHandlerContext ctx) {
|
|
|
+ //ByteBuf dataByteBufCopy = dataByteBuf.copy();
|
|
|
+ dataByteBuf.markReaderIndex();
|
|
|
+ byte[] dataByteArray = new byte[dataByteBuf.readableBytes()];
|
|
|
+ dataByteBuf.readBytes(dataByteArray);
|
|
|
+ String printHexBinary = DatatypeConverter.printHexBinary(dataByteArray);
|
|
|
+ if (channelDeviceMap.get(ctx.channel()) != null) {
|
|
|
+ MDC.put(MDC_DEVICEID, channelDeviceMap.get(ctx.channel()));
|
|
|
+ logger.info("设备: [{}] 传入数据为 : {}", channelDeviceMap.get(ctx.channel()), printHexBinary);
|
|
|
+ } else {
|
|
|
+ logger.info("设备传入数据为 : {}", printHexBinary);
|
|
|
+ }
|
|
|
+ dataByteBuf.resetReaderIndex();
|
|
|
+ //dataByteBufCopy.release();
|
|
|
+ }
|
|
|
+
|
|
|
+ abstract protected void handle(ByteBuf in, Channel channel) throws Exception;
|
|
|
+
|
|
|
}
|