Browse Source

add utils

master
lijiuwei 7 months ago
parent
commit
2bb3d0babd
4 changed files with 109 additions and 3 deletions
  1. +5
    -0
      pom.xml
  2. +10
    -3
      src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java
  3. +8
    -0
      src/main/java/com/inspect/tcpserver/tcp/NettyServer.java
  4. +86
    -0
      src/main/java/com/inspect/tcpserver/util/ProtoDef.java

+ 5
- 0
pom.xml View File

@ -88,6 +88,11 @@
<version>1.15</version>
</dependency>
<dependency>
<groupId>javolution</groupId>
<artifactId>javolution</artifactId>
<version>5.5.1</version>
</dependency>
</dependencies>
<dependencyManagement>


+ 10
- 3
src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java View File

@ -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<Object> 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];


+ 8
- 0
src/main/java/com/inspect/tcpserver/tcp/NettyServer.java View File

@ -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<SocketChannel>() {
@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);


+ 86
- 0
src/main/java/com/inspect/tcpserver/util/ProtoDef.java View File

@ -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 =
// "<PatrolDevice>\n" +
// " <SendCode>INSPECT-SERVER-001</SendCode>\n" +
// " <ReceiveCode>DRONE-001</ReceiveCode>\n" +
// " <Type>20001</Type>\n" +
// " <Code>DRONE-001</Code>\n" +
// " <Command>3</Command>\n" +
// " <Time>2025-03-10 15:58:30</Time>\n" +
// " <Items><Item value=\"1\"/></Items>\n" +
// "</PatrolDevice>";
// "<PatrolDevice>\n" +
// " <SendCode>G100-001</SendCode>\n" +
// " <ReceiveCode>L100-001</ReceiveCode>\n" +
// " <Type>61</Type>\n" +
// " <Code></Code>\n" +
// " <Command></Command>\n" +
// " <Time>2025-05-27 14:50:58</Time>\n" +
// " <Items>\n" +
// " <Item task_patrolled_id=\"1587_20250527102418\" task_name=\"无人机测试\" task_code=\"1587\" patroldevice_name=\"接地极无人机\" patroldevice_code=\"HR000000103\" device_name=\"接地极线路阻波器电抗器L1电容器C1_红外\" device_id=\"1499\" recognition_type=\"4\" file_type=\"1\" file_path=\"/1/2025/05/27/1587/FIR/1499_HR000000103_20250527145056.jpg\" material_id=\"\" value_type=\"0\" value=\"\" value_unit=\"\" unit=\"\" time=\"2025-05-27 14:50:56\" rectangle=\"\" valid=\"1\"/>\n" +
// " </Items>\n" +
// "</PatrolDevice>";
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<PatrolDevice>\n" +
" <SendCode>G100-001</SendCode>\n" +
" <ReceiveCode>L100-001</ReceiveCode>\n" +
" <Type>61</Type>\n" +
" <Code></Code>\n" +
" <Command></Command>\n" +
" <Time>2025-05-27 14:50:58</Time>\n" +
" <Items>\n" +
" <Item task_patrolled_id=\"1587_20250527102418\" task_name=\"无人机测试\" task_code=\"1587\" patroldevice_name=\"接地极无人机\" patroldevice_code=\"HR000000103\" device_name=\"接地极线路阻波器电抗器L1电容器C1_红外\" device_id=\"1499\" recognition_type=\"4\" file_type=\"1\" file_path=\"/1/2025/05/27/1587/FIR/1499_HR000000103_20250527145056.jpg\" material_id=\"\" value_type=\"0\" value=\"\" value_unit=\"\" unit=\"\" time=\"2025-05-27 14:50:56\" rectangle=\"\" valid=\"1\"/>\n" +
" <Item task_patrolled_id=\"1587_20250527102418\" task_name=\"无人机测试\" task_code=\"1587\" patroldevice_name=\"接地极无人机\" patroldevice_code=\"HR000000103\" device_name=\"接地极线路阻波器管母_红外\" device_id=\"1500\" recognition_type=\"4\" file_type=\"1\" file_path=\"/1/2025/05/27/1587/FIR/1500_HR000000103_20250527145056.jpg\" material_id=\"\" value_type=\"0\" value=\"\" value_unit=\"\" unit=\"\" time=\"2025-05-27 14:50:56\" rectangle=\"\" valid=\"1\"/>\n" +
" <Item task_patrolled_id=\"1587_20250527102418\" task_name=\"无人机测试\" task_code=\"1587\" patroldevice_name=\"接地极无人机\" patroldevice_code=\"HR000000103\" device_name=\"接地极整体测温_红外\" device_id=\"1501\" recognition_type=\"4\" file_type=\"1\" file_path=\"/1/2025/05/27/1587/FIR/1501_HR000000103_20250527145056.jpg\" material_id=\"\" value_type=\"0\" value=\"\" value_unit=\"\" unit=\"\" time=\"2025-05-27 14:50:56\" rectangle=\"\" valid=\"1\"/>\n" +
" <Item task_patrolled_id=\"1587_20250527102418\" task_name=\"无人机测试\" task_code=\"1587\" patroldevice_name=\"接地极无人机\" patroldevice_code=\"HR000000103\" device_name=\"接地极线路阻波器电抗器L1电容器C1_外观\" device_id=\"1652\" recognition_type=\"3\" file_type=\"2\" file_path=\"/1/2025/05/27/1587/CCD/1652_HR000000103_20250527145056.jpg\" material_id=\"\" value_type=\"0\" value=\"\" value_unit=\"\" unit=\"\" time=\"2025-05-27 14:50:56\" rectangle=\"\" valid=\"1\"/>\n" +
" <Item task_patrolled_id=\"1587_20250527102418\" task_name=\"无人机测试\" task_code=\"1587\" patroldevice_name=\"接地极无人机\" patroldevice_code=\"HR000000103\" device_name=\"接地极线路阻波器管母_外观\" device_id=\"1653\" recognition_type=\"3\" file_type=\"2\" file_path=\"/1/2025/05/27/1587/CCD/1653_HR000000103_20250527145056.jpg\" material_id=\"\" value_type=\"0\" value=\"\" value_unit=\"\" unit=\"\" time=\"2025-05-27 14:50:56\" rectangle=\"\" valid=\"1\"/>\n" +
" <Item task_patrolled_id=\"1587_20250527102418\" task_name=\"无人机测试\" task_code=\"1587\" patroldevice_name=\"接地极无人机\" patroldevice_code=\"HR000000103\" device_name=\"接地极整体测温_外观\" device_id=\"1654\" recognition_type=\"3\" file_type=\"2\" file_path=\"/1/2025/05/27/1587/CCD/1654_HR000000103_20250527145056.jpg\" material_id=\"\" value_type=\"0\" value=\"\" value_unit=\"\" unit=\"\" time=\"2025-05-27 14:50:56\" rectangle=\"\" valid=\"1\"/>\n" +
" </Items>\n" +
"</PatrolDevice>";
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");
}
}

Loading…
Cancel
Save