|
|
|
@ -13,7 +13,6 @@ import com.platform.service.clientmanage.ClientManager;
|
|
|
|
|
import com.platform.service.clientmanage.HaborClient;
|
|
|
|
|
import com.platform.util.*;
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.buffer.ByteBufUtil;
|
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
import io.netty.channel.*;
|
|
|
|
|
import io.netty.handler.timeout.IdleState;
|
|
|
|
@ -120,15 +119,41 @@ public class InMessageHandler1 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
@Override
|
|
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg0) throws Exception {
|
|
|
|
|
String address = getAddress(ctx);
|
|
|
|
|
|
|
|
|
|
final ByteBuf msgBuf = (ByteBuf) msg0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 上线握手阶段
|
|
|
|
|
if (!sessionCache.addToSnMap.containsKey(address)) { // 判断连接是否已经建立,如未建立则先握手
|
|
|
|
|
try {
|
|
|
|
|
handshake(ctx, msgBuf, address);
|
|
|
|
|
msgBuf.release();
|
|
|
|
|
return; // 握手结束立即返回
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// 已经建立连接且非地面站控制信息,则进行数据转发
|
|
|
|
|
transfer(ctx, msgBuf, address);
|
|
|
|
|
}
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
ReferenceCountUtil.safeRelease(msgBuf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (msgBuf.getByte(0) != (byte) 0xAA || msgBuf.getByte(1) != (byte) 0x44) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
BaseClient client = ClientManager.getClient(ctx.channel());
|
|
|
|
|
if (client == null || client.getClientType() != ClientTypeEnum.HABOR) {
|
|
|
|
|
if (msgBuf != null) {
|
|
|
|
|
msgBuf.release();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
// 直接传递当前引用,不需要增加引用计数
|
|
|
|
|
ctx.fireChannelRead(msgBuf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**处理哈勃、地面站的握手信息*/
|
|
|
|
|
private boolean handshake(ChannelHandlerContext ctx, ByteBuf msgBuf, String address) {
|
|
|
|
|
if (msgBuf.getByte(0) != (byte) 0xAA || msgBuf.getByte(1) != (byte) 0x44) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
byte[] msg = new byte[msgBuf.readableBytes()];
|
|
|
|
|
msgBuf.readBytes(msg);
|
|
|
|
|
log.info("【接收握手原数据】 " + ByteUtils.bytes2HexString(msg));
|
|
|
|
@ -151,17 +176,19 @@ public class InMessageHandler1 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
typeName = "地面站";
|
|
|
|
|
deviceCode = new String(msg, 6, 12, Charset.defaultCharset()).toUpperCase(); // 地面站
|
|
|
|
|
protocolAck(ctx, (byte) 1);
|
|
|
|
|
log.info("{} {} 握手成功: {}", typeName, deviceCode, ByteUtils.bytes2HexString(msg));
|
|
|
|
|
} else if (type.equals("2")) {
|
|
|
|
|
typeName = "哈勃终端";
|
|
|
|
|
deviceCode = new String(msg, 12, 6, Charset.defaultCharset()).toUpperCase(); // 哈勃上线
|
|
|
|
|
protocolAck(ctx, (byte) 1);
|
|
|
|
|
log.info("{} {} 握手成功: {}", typeName, deviceCode, ByteUtils.bytes2HexString(msg));
|
|
|
|
|
haborSignIntoCac(deviceCode, ctx.channel());
|
|
|
|
|
} else {
|
|
|
|
|
protocolAck(ctx, (byte) 0); // 握手失败
|
|
|
|
|
return;
|
|
|
|
|
log.info("握手失败: {}", ByteUtils.bytes2HexString(msg));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.info("【读取握手数据】======> deviceCode = " + deviceCode + " type = " + typeName);
|
|
|
|
|
|
|
|
|
|
// 删除旧的连接。如果存在异常断开的时候,旧连接并未清除。这里在上线的时候检查并清除
|
|
|
|
|
String addr0 = sessionCache.snToAddMap.get(deviceCode);
|
|
|
|
@ -175,16 +202,14 @@ public class InMessageHandler1 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
sessionCache.addToSnMap.put(address, deviceCode); // ip-mac
|
|
|
|
|
sessionCache.snToAddMap.put(deviceCode, address); // mac- ip
|
|
|
|
|
sessionCache.tcpSession.put(address, ctx.channel()); // ip - channel
|
|
|
|
|
|
|
|
|
|
return; // 握手结束立即返回
|
|
|
|
|
} finally {
|
|
|
|
|
msgBuf.release();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// 已经建立连接且非地面站控制信息,则进行数据转发
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
/**地面站和哈勃之间的数据转发(原哈勃服务功能)*/
|
|
|
|
|
private void transfer(ChannelHandlerContext ctx, ByteBuf msgBuf, String address) throws RuntimeException {
|
|
|
|
|
final ByteBuf bufToTransfer = msgBuf.duplicate(); // 转发,保留原始索引
|
|
|
|
|
String sn = sessionCache.addToSnMap.get(address); // sn =mac
|
|
|
|
|
|
|
|
|
|
// 监控相关
|
|
|
|
|
if (!sessionCache.snToMonitorMap.isEmpty()) {
|
|
|
|
|
Optional<Channel> sendChannelOpt = sessionCache.findMonitorChannel(address);
|
|
|
|
@ -204,7 +229,6 @@ public class InMessageHandler1 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!sessionCache.mappingListMap.isEmpty() && sessionCache.mappingListMap.get(sn) != null) {
|
|
|
|
|
if (debug) {
|
|
|
|
|
// log.info("【转发数据】======> " + ByteBufUtil.hexDump(msgBuf));
|
|
|
|
@ -222,8 +246,6 @@ public class InMessageHandler1 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
// TODO 2025/1/17: 需优化
|
|
|
|
|
if (!future.isSuccess()) {
|
|
|
|
|
bufToTransfer.release();
|
|
|
|
|
} else {
|
|
|
|
|
// log.info("发送成功: {}, {} ", sendChannel.remoteAddress(), ByteBufUtil.hexDump(bufToTransfer));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
@ -231,18 +253,6 @@ public class InMessageHandler1 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseClient client = ClientManager.getClient(ctx.channel());
|
|
|
|
|
if (client == null || client.getClientType() != ClientTypeEnum.HABOR) {
|
|
|
|
|
if (msgBuf != null) {
|
|
|
|
|
msgBuf.release();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
// 直接传递当前引用,不需要增加引用计数
|
|
|
|
|
ctx.fireChannelRead(msgBuf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 接入中心指控系统, 上线
|
|
|
|
|
*/
|
|
|
|
|