1. 重写格式化代码

This commit is contained in:
HuangXin 2023-08-12 12:48:50 +08:00
parent 3eb0572969
commit 38f8973963
5 changed files with 31 additions and 15 deletions

View File

@ -28,9 +28,12 @@ public class ChannelInit<T> extends ChannelInitializer<SocketChannel> {
@Override @Override
protected void initChannel(SocketChannel channel) { protected void initChannel(SocketChannel channel) {
channel.pipeline().addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS)).addLast("decode", channel.pipeline()
.addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS))
.addLast("decode",
new YuanRongProtocolDecode()) 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);
} }
} }

View File

@ -46,9 +46,11 @@ 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().id()); log.info("{}:: Trigger Heart Signe", ctx.channel().id());
MessageContent<HeartProtocol> msgCont = MessageContent.<HeartProtocol>builder().msgBody( MessageContent<HeartProtocol> msgCont = MessageContent.<HeartProtocol>builder()
new HeartProtocol()).build(); .msgBody(new HeartProtocol())
BaseBinaryProtocol<HeartProtocol> h = BaseBinaryProtocol.<HeartProtocol>builder().msgContent(msgCont) .build();
BaseBinaryProtocol<HeartProtocol> h = BaseBinaryProtocol.<HeartProtocol>builder()
.msgContent(msgCont)
.build(); .build();
ctx.writeAndFlush(h); ctx.writeAndFlush(h);
@ -73,9 +75,12 @@ public class MessageHandler<T> extends SimpleChannelInboundHandler<BaseBinaryPro
InetSocketAddress sa = (InetSocketAddress) ctx.channel().remoteAddress(); InetSocketAddress sa = (InetSocketAddress) ctx.channel().remoteAddress();
//List<ControlDevice> list = controlDeviceMapper.selectAll(); //List<ControlDevice> list = controlDeviceMapper.selectAll();
log.info("{}:: Connected <-- {}:{}", ctx.channel().id(), sa.getAddress().getHostAddress(), sa.getPort()); log.info("{}:: Connected <-- {}:{}", ctx.channel().id(), sa.getAddress().getHostAddress(), sa.getPort());
controlDeviceMapper.addControlDevice(ControlDevice.builder().deviceType(ControlDeviceTypeName.DEVICE_SOCKET_TCP) controlDeviceMapper.addControlDevice(ControlDevice.builder()
.deviceAddr(sa.getAddress().getHostAddress()).build()); .deviceType(ControlDeviceTypeName.DEVICE_SOCKET_TCP)
MessageContent<HeartProtocol> msgCont = MessageContent.<HeartProtocol>builder().msgBody(new HeartProtocol()) .deviceAddr(sa.getAddress().getHostAddress())
.build());
MessageContent<HeartProtocol> msgCont = MessageContent.<HeartProtocol>builder()
.msgBody(new HeartProtocol())
.build(); .build();
BaseBinaryProtocol<HeartProtocol> h = BaseBinaryProtocol.<HeartProtocol>builder().msgContent(msgCont).build(); BaseBinaryProtocol<HeartProtocol> h = BaseBinaryProtocol.<HeartProtocol>builder().msgContent(msgCont).build();

View File

@ -23,7 +23,8 @@ import org.springframework.stereotype.Component;
@Slf4j @Slf4j
public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryProtocol<T>> { public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryProtocol<T>> {
@Override @Override
protected void encode(ChannelHandlerContext channelHandlerContext, BaseBinaryProtocol<T> baseBinaryProtocol, protected void encode(ChannelHandlerContext channelHandlerContext,
BaseBinaryProtocol<T> baseBinaryProtocol,
ByteBuf byteBuf) { ByteBuf byteBuf) {
if (baseBinaryProtocol == null || baseBinaryProtocol.getMsgContent() == null || if (baseBinaryProtocol == null || baseBinaryProtocol.getMsgContent() == null ||

View File

@ -51,10 +51,15 @@ public class TcpServer implements ISocketServer {
*/ */
private void tcpServer(int port) { private void tcpServer(int port) {
try { try {
new ServerBootstrap().group(boosGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress( new ServerBootstrap().group(boosGroup, workerGroup)
new InetSocketAddress(port)).childHandler(channelInit).option(ChannelOption.SO_BACKLOG, 128) .channel(NioServerSocketChannel.class)
.childOption(ChannelOption.TCP_NODELAY, false).childOption(ChannelOption.SO_KEEPALIVE, .localAddress(new InetSocketAddress(port))
true).bind().sync(); .childHandler(channelInit)
.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());

View File

@ -69,7 +69,9 @@ public final class CommonEnumHandler<E extends EnumerationBase> extends BaseType
* @throws SQLException the sql exception * @throws SQLException the sql exception
*/ */
@Override @Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, E e, public void setNonNullParameter(PreparedStatement preparedStatement,
int i,
E e,
JdbcType jdbcType) throws SQLException { JdbcType jdbcType) throws SQLException {
preparedStatement.setInt(i, e.getValue()); preparedStatement.setInt(i, e.getValue());
} }