diff --git a/pom.xml b/pom.xml index 099d4e3..d9cdb7d 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,11 @@ 1.15 + + javolution + javolution + 5.5.1 + diff --git a/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java b/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java index 8b28c72..4dddd24 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java +++ b/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java @@ -10,6 +10,7 @@ import org.apache.commons.lang3.RandomStringUtils; import org.apache.tomcat.util.buf.HexUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import java.util.List; @@ -19,6 +20,9 @@ public class MyDecoder extends ByteToMessageDecoder { private final int BASE_LENGTH = 2 + 8 + 8 + 1 + 4 + 2; + @Value("${print_recv_data:0}") + Integer printRecvData; + @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { try { @@ -29,9 +33,12 @@ public class MyDecoder extends ByteToMessageDecoder { } final String uuid = RandomStringUtils.randomAlphanumeric(16); -// ByteBuf forPrint = in.copy(); -// log.info("###### 会话:{}, 客户:{}, 上行原始报文 ######\n {}", uuid, ctx.channel().id().asShortText(), ByteBufUtil.hexDump(forPrint)); -// forPrint.release(); + + if(printRecvData > 0) { + ByteBuf forPrint = in.copy(); + log.info("###### 会话:{}, 客户:{}, 上行原始报文 ######\n {}", uuid, ctx.channel().id().asShortText(), ByteBufUtil.hexDump(forPrint)); + forPrint.release(); + } do { byte[] start = new byte[2]; diff --git a/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java b/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java index 7724742..6916933 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java +++ b/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java @@ -16,6 +16,7 @@ import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.util.CharsetUtil; import io.netty.util.internal.StringUtil; import org.apache.commons.lang3.StringUtils; @@ -34,6 +35,7 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.ByteArrayInputStream; +import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; @@ -75,6 +77,9 @@ public class NettyServer { @Value("${iip_server.authDevice.url}") String iipAuthDeviceUrl; + @Value("${seperating_packages:0}") + Integer seperatingPackages; + private int serverPort; public void init() { @@ -98,6 +103,9 @@ public class NettyServer { .childHandler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) { + if(seperatingPackages > 0) { + ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, Integer.MAX_VALUE, 19, 4, 2, 0, true)); + } ch.pipeline().addLast(new MyDecoder()); nettyServerHandler = new NettyServerHandler(NettyServer.this); ch.pipeline().addLast(nettyServerHandler); diff --git a/src/main/java/com/inspect/tcpserver/util/ProtoDef.java b/src/main/java/com/inspect/tcpserver/util/ProtoDef.java new file mode 100644 index 0000000..fc8f0b1 --- /dev/null +++ b/src/main/java/com/inspect/tcpserver/util/ProtoDef.java @@ -0,0 +1,86 @@ +package com.inspect.tcpserver.util; + +import javolution.io.Struct; +import org.apache.commons.codec.binary.Hex; + +import java.nio.ByteOrder; + +public class ProtoDef extends Struct { + Unsigned16 mark = new Unsigned16(); + Signed64 sendSeq = new Signed64(); + Signed64 recvSeq = new Signed64(); + Signed8 session = new Signed8(); + Signed32 xmlLength = new Signed32(); + + //一定要加上这个,不然会出现对齐的问题 + @Override + public boolean isPacked() { + return true; + } + + //设置为小端格式 + @Override + public ByteOrder byteOrder() { + return ByteOrder.LITTLE_ENDIAN; + } + + //测试 + public static void main(String[] args) { + String xml = +// "\n" + +// " INSPECT-SERVER-001\n" + +// " DRONE-001\n" + +// " 20001\n" + +// " DRONE-001\n" + +// " 3\n" + +// " \n" + +// " \n" + +// ""; + +// "\n" + +// " G100-001\n" + +// " L100-001\n" + +// " 61\n" + +// " \n" + +// " \n" + +// " \n" + +// " \n" + +// " \n" + +// " \n" + +// ""; + + "\n" + + "\n" + + " G100-001\n" + + " L100-001\n" + + " 61\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + String dataHex = HexUtils.ascii2hex(xml); + int length = dataHex.length() / 2; + + ProtoDef proto = new ProtoDef(); + proto.mark.set(0x90EB); + proto.sendSeq.set(0x3c); + proto.recvSeq.set(0x39); + proto.session.set((byte)0); + proto.xmlLength.set(length); + + byte[] bytes = new byte[proto.getByteBuffer().limit()]; + proto.getByteBuffer().get(bytes); + String protoHex = Hex.encodeHexString(bytes, true); + + System.out.println(protoHex + dataHex + "eb90"); + } +}