1. 重写格式化代码
This commit is contained in:
parent
b1e49391e8
commit
3eb0572969
|
@ -28,11 +28,9 @@ public class ChannelInit<T> extends ChannelInitializer<SocketChannel> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(SocketChannel channel) {
|
protected void initChannel(SocketChannel channel) {
|
||||||
channel.pipeline()
|
channel.pipeline().addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS)).addLast("decode",
|
||||||
.addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS))
|
new YuanRongProtocolDecode())
|
||||||
.addLast("decode", new YuanRongProtocolDecode())
|
.addLast("startFlag", new ProtocolStartEncode(2)).addLast("encode", new YuanRongProtocolEncode<T>())
|
||||||
.addLast("startFlag", new ProtocolStartEncode(2))
|
|
||||||
.addLast("encode", new YuanRongProtocolEncode<T>())
|
|
||||||
.addLast("message", messageHandler);
|
.addLast("message", messageHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,8 +45,13 @@ public class MessageHandler<T> extends SimpleChannelInboundHandler<BaseBinaryPro
|
||||||
|
|
||||||
if (evt instanceof IdleStateEvent idleStateEvent) {
|
if (evt instanceof IdleStateEvent idleStateEvent) {
|
||||||
if (idleStateEvent.state() == IdleState.ALL_IDLE) {
|
if (idleStateEvent.state() == IdleState.ALL_IDLE) {
|
||||||
log.info("{}:: Trigger Heart Signe", ctx.channel()
|
log.info("{}:: Trigger Heart Signe", ctx.channel().id());
|
||||||
.id());
|
MessageContent<HeartProtocol> msgCont = MessageContent.<HeartProtocol>builder().msgBody(
|
||||||
|
new HeartProtocol()).build();
|
||||||
|
BaseBinaryProtocol<HeartProtocol> h = BaseBinaryProtocol.<HeartProtocol>builder().msgContent(msgCont)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ctx.writeAndFlush(h);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
super.userEventTriggered(ctx, evt);
|
super.userEventTriggered(ctx, evt);
|
||||||
|
@ -56,9 +61,7 @@ public class MessageHandler<T> extends SimpleChannelInboundHandler<BaseBinaryPro
|
||||||
@Override
|
@Override
|
||||||
public void channelRead0(ChannelHandlerContext ctx, BaseBinaryProtocol message) throws Exception {
|
public void channelRead0(ChannelHandlerContext ctx, BaseBinaryProtocol message) throws Exception {
|
||||||
//获取到线程池eventLoop,添加线程,执行
|
//获取到线程池eventLoop,添加线程,执行
|
||||||
ctx.channel()
|
ctx.channel().eventLoop().execute(() -> {
|
||||||
.eventLoop()
|
|
||||||
.execute(() -> {
|
|
||||||
//长时间操作,不至于长时间的业务操作导致Handler阻塞
|
//长时间操作,不至于长时间的业务操作导致Handler阻塞
|
||||||
//log.info("{}:: Receive Message: {}", ctx.channel().id(), HelperUtils.bytesToHexString(message
|
//log.info("{}:: Receive Message: {}", ctx.channel().id(), HelperUtils.bytesToHexString(message
|
||||||
// .getStart()));
|
// .getStart()));
|
||||||
|
@ -67,23 +70,14 @@ public class MessageHandler<T> extends SimpleChannelInboundHandler<BaseBinaryPro
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
InetSocketAddress sa = (InetSocketAddress) ctx.channel()
|
InetSocketAddress sa = (InetSocketAddress) ctx.channel().remoteAddress();
|
||||||
.remoteAddress();
|
|
||||||
//List<ControlDevice> list = controlDeviceMapper.selectAll();
|
//List<ControlDevice> list = controlDeviceMapper.selectAll();
|
||||||
log.info("{}:: Connected <-- {}:{}", ctx.channel()
|
log.info("{}:: Connected <-- {}:{}", ctx.channel().id(), sa.getAddress().getHostAddress(), sa.getPort());
|
||||||
.id(), sa.getAddress()
|
controlDeviceMapper.addControlDevice(ControlDevice.builder().deviceType(ControlDeviceTypeName.DEVICE_SOCKET_TCP)
|
||||||
.getHostAddress(), sa.getPort());
|
.deviceAddr(sa.getAddress().getHostAddress()).build());
|
||||||
controlDeviceMapper.addControlDevice(ControlDevice.builder()
|
MessageContent<HeartProtocol> msgCont = MessageContent.<HeartProtocol>builder().msgBody(new HeartProtocol())
|
||||||
.deviceType(ControlDeviceTypeName.DEVICE_SOCKET_TCP)
|
|
||||||
.deviceAddr(sa.getAddress()
|
|
||||||
.getHostAddress())
|
|
||||||
.build());
|
|
||||||
|
|
||||||
BaseBinaryProtocol<HeartProtocol> h = BaseBinaryProtocol.<HeartProtocol>builder()
|
|
||||||
.msgContent(MessageContent.<HeartProtocol>builder()
|
|
||||||
.msgBody(new HeartProtocol())
|
|
||||||
.build())
|
|
||||||
.build();
|
.build();
|
||||||
|
BaseBinaryProtocol<HeartProtocol> h = BaseBinaryProtocol.<HeartProtocol>builder().msgContent(msgCont).build();
|
||||||
|
|
||||||
ctx.writeAndFlush(h);
|
ctx.writeAndFlush(h);
|
||||||
super.channelActive(ctx);
|
super.channelActive(ctx);
|
||||||
|
@ -91,11 +85,8 @@ public class MessageHandler<T> extends SimpleChannelInboundHandler<BaseBinaryPro
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
InetSocketAddress sa = (InetSocketAddress) ctx.channel()
|
InetSocketAddress sa = (InetSocketAddress) ctx.channel().remoteAddress();
|
||||||
.remoteAddress();
|
log.info("{}:: Disonnected <-- {}", ctx.channel().id(), sa.getAddress().getHostAddress());
|
||||||
log.info("{}:: Disonnected <-- {}", ctx.channel()
|
|
||||||
.id(), sa.getAddress()
|
|
||||||
.getHostAddress());
|
|
||||||
super.channelActive(ctx);
|
super.channelActive(ctx);
|
||||||
ctx.close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,9 +25,7 @@ public class ProtocolStartEncode extends LengthFieldPrepender {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
|
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
|
||||||
out.add(ctx.alloc()
|
out.add(ctx.alloc().buffer(BaseBinaryProtocol.START.length()).writeBytes(BaseBinaryProtocol.START.getBytes()));
|
||||||
.buffer(BaseBinaryProtocol.START.length())
|
|
||||||
.writeBytes(BaseBinaryProtocol.START.getBytes()));
|
|
||||||
super.encode(ctx, msg, out);
|
super.encode(ctx, msg, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,13 +27,11 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
|
||||||
ByteBuf byteBuf) {
|
ByteBuf byteBuf) {
|
||||||
|
|
||||||
if (baseBinaryProtocol == null || baseBinaryProtocol.getMsgContent() == null ||
|
if (baseBinaryProtocol == null || baseBinaryProtocol.getMsgContent() == null ||
|
||||||
baseBinaryProtocol.getMsgContent()
|
baseBinaryProtocol.getMsgContent().getMsgBody() == null) {
|
||||||
.getMsgBody() == null) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
T msgBody = baseBinaryProtocol.getMsgContent()
|
T msgBody = baseBinaryProtocol.getMsgContent().getMsgBody();
|
||||||
.getMsgBody();
|
|
||||||
|
|
||||||
byteBuf.writeByte(BaseBinaryProtocol.VERSIN); // 协议版本号
|
byteBuf.writeByte(BaseBinaryProtocol.VERSIN); // 协议版本号
|
||||||
byteBuf.writeInt(0); // 接收设备主ID
|
byteBuf.writeInt(0); // 接收设备主ID
|
||||||
|
@ -45,21 +43,15 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
|
||||||
byteBuf.writeInt(0); // 消息状态码
|
byteBuf.writeInt(0); // 消息状态码
|
||||||
|
|
||||||
if (msgBody instanceof HeartProtocol) {
|
if (msgBody instanceof HeartProtocol) {
|
||||||
byteBuf.writeByte(ControlCommandName.COMMAND_HEART.getValue()
|
byteBuf.writeByte(ControlCommandName.COMMAND_HEART.getValue().byteValue()); // 消息类型
|
||||||
.byteValue()); // 消息类型
|
|
||||||
byteBuf.writeShort(0); // 消息内容长度
|
byteBuf.writeShort(0); // 消息内容长度
|
||||||
} else if (msgBody instanceof SensorControlProtocol msg) {
|
} else if (msgBody instanceof SensorControlProtocol msg) {
|
||||||
|
|
||||||
byteBuf.writeByte(ControlCommandName.COMMAND_SENSOR.getValue()
|
byteBuf.writeByte(ControlCommandName.COMMAND_SENSOR.getValue().byteValue()); // 消息类型
|
||||||
.byteValue()); // 消息类型
|
|
||||||
|
|
||||||
if (!msg.getControllContents()
|
if (!msg.getControllContents().isEmpty()) {
|
||||||
.isEmpty()) {
|
byteBuf.writeShort(13 + 4 * msg.getControllContents().size()); // 消息内容长度
|
||||||
byteBuf.writeShort(13 + 4 * msg.getControllContents()
|
byteBuf.writeByte(msg.getControlTunnel().getValue().byteValue());
|
||||||
.size()); // 消息内容长度
|
|
||||||
byteBuf.writeByte(msg.getControlTunnel()
|
|
||||||
.getValue()
|
|
||||||
.byteValue());
|
|
||||||
byteBuf.writeBytes(msg.getTunnelAddr());
|
byteBuf.writeBytes(msg.getTunnelAddr());
|
||||||
byteBuf.writeByte(msg.getMonth());
|
byteBuf.writeByte(msg.getMonth());
|
||||||
byteBuf.writeByte(msg.getDays());
|
byteBuf.writeByte(msg.getDays());
|
||||||
|
@ -67,42 +59,31 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
|
||||||
byteBuf.writeByte(msg.getMinute());
|
byteBuf.writeByte(msg.getMinute());
|
||||||
byteBuf.writeByte(msg.getProvince());
|
byteBuf.writeByte(msg.getProvince());
|
||||||
byteBuf.writeByte(msg.getCity());
|
byteBuf.writeByte(msg.getCity());
|
||||||
byteBuf.writeByte(msg.getControlMode()
|
byteBuf.writeByte(msg.getControlMode().getValue().byteValue());
|
||||||
.getValue()
|
|
||||||
.byteValue());
|
|
||||||
byteBuf.writeByte(msg.getControlCommand());
|
byteBuf.writeByte(msg.getControlCommand());
|
||||||
byteBuf.writeByte(msg.getNControlInfo());
|
byteBuf.writeByte(msg.getNControlInfo());
|
||||||
for (var c : msg.getControllContents()) {
|
for (var c : msg.getControllContents()) {
|
||||||
byteBuf.writeByte(c.getDistrictsCode());
|
byteBuf.writeByte(c.getDistrictsCode());
|
||||||
byteBuf.writeByte(c.getControlAction()
|
byteBuf.writeByte(c.getControlAction().getValue().byteValue());
|
||||||
.getValue()
|
|
||||||
.byteValue());
|
|
||||||
byteBuf.writeShort(c.getSensorId());
|
byteBuf.writeShort(c.getSensorId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (msgBody instanceof QuerySensorProtocol msg) {
|
} else if (msgBody instanceof QuerySensorProtocol msg) {
|
||||||
byteBuf.writeByte(ControlCommandName.COMMAND_QUERY_SENSOR.getValue()
|
byteBuf.writeByte(ControlCommandName.COMMAND_QUERY_SENSOR.getValue().byteValue()); // 消息类型
|
||||||
.byteValue()); // 消息类型
|
|
||||||
|
|
||||||
if (!msg.getControllContents()
|
if (!msg.getControllContents().isEmpty()) {
|
||||||
.isEmpty()) {
|
byteBuf.writeShort(2 + 4 * msg.getControllContents().size()); // 消息内容长度
|
||||||
byteBuf.writeShort(2 + 4 * msg.getControllContents()
|
|
||||||
.size()); // 消息内容长度
|
|
||||||
byteBuf.writeByte(msg.getProvince());
|
byteBuf.writeByte(msg.getProvince());
|
||||||
byteBuf.writeByte(msg.getCity());
|
byteBuf.writeByte(msg.getCity());
|
||||||
byteBuf.writeByte(msg.getNControlInfo());
|
byteBuf.writeByte(msg.getNControlInfo());
|
||||||
for (var c : msg.getControllContents()) {
|
for (var c : msg.getControllContents()) {
|
||||||
byteBuf.writeByte(c.getDistrictsCode());
|
byteBuf.writeByte(c.getDistrictsCode());
|
||||||
byteBuf.writeByte(c.getControlAction()
|
byteBuf.writeByte(c.getControlAction().getValue().byteValue());
|
||||||
.getValue()
|
|
||||||
.byteValue());
|
|
||||||
byteBuf.writeShort(c.getSensorId());
|
byteBuf.writeShort(c.getSensorId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.info("XXXXX Encode: {}", baseBinaryProtocol.getMsgContent()
|
log.info("XXXXX Encode: {}", baseBinaryProtocol.getMsgContent().getMsgBody().getClass());
|
||||||
.getMsgBody()
|
|
||||||
.getClass());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("\n{}", ByteBufUtil.prettyHexDump(byteBuf));
|
log.info("\n{}", ByteBufUtil.prettyHexDump(byteBuf));
|
||||||
|
|
|
@ -51,15 +51,10 @@ public class TcpServer implements ISocketServer {
|
||||||
*/
|
*/
|
||||||
private void tcpServer(int port) {
|
private void tcpServer(int port) {
|
||||||
try {
|
try {
|
||||||
new ServerBootstrap().group(boosGroup, workerGroup)
|
new ServerBootstrap().group(boosGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(
|
||||||
.channel(NioServerSocketChannel.class)
|
new InetSocketAddress(port)).childHandler(channelInit).option(ChannelOption.SO_BACKLOG, 128)
|
||||||
.localAddress(new InetSocketAddress(port))
|
.childOption(ChannelOption.TCP_NODELAY, false).childOption(ChannelOption.SO_KEEPALIVE,
|
||||||
.childHandler(channelInit)
|
true).bind().sync();
|
||||||
.option(ChannelOption.SO_BACKLOG, 128)
|
|
||||||
.childOption(ChannelOption.TCP_NODELAY, false)
|
|
||||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
||||||
.bind()
|
|
||||||
.sync();
|
|
||||||
log.info("Netty TCP Server Beginning Listen: {}", port);
|
log.info("Netty TCP Server Beginning Listen: {}", port);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
|
|
Loading…
Reference in New Issue