update 电单车协议

This commit is contained in:
Guoqs
2024-07-15 11:33:11 +08:00
parent 450d6799a6
commit 2186b3e857
8 changed files with 192 additions and 69 deletions

View File

@@ -0,0 +1,98 @@
package com.jsowell.netty.server;
import com.jsowell.common.constant.Constants;
import com.jsowell.netty.server.yunkuaichong.NettyServerChannelInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
@Slf4j
@Component
public class NettyServerManager implements CommandLineRunner {
@Resource
private NettyServerChannelInitializer nettyServerChannelInitializer;
@Override
public void run(String... args) throws Exception {
startNettyServer(Constants.SOCKET_IP, 9011);
startElectricBikeNettyServer(Constants.SOCKET_IP, 9012);
}
public void startNettyServer(String host, int port) {
new Thread(() -> {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_REUSEADDR, true)
.childHandler(nettyServerChannelInitializer)
.localAddress(new InetSocketAddress(host, port));
ChannelFuture future = bootstrap.bind(port).sync();
if (future.isSuccess()) {
log.info("NettyServer启动成功, 开始监听端口:{}", port);
} else {
log.error("NettyServer启动失败", future.cause());
}
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("NettyServer.start error", e);
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}).start();
}
public void startElectricBikeNettyServer(String host, int port) {
new Thread(() -> {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_REUSEADDR, true)
.childHandler(nettyServerChannelInitializer)
.localAddress(new InetSocketAddress(host, port));
ChannelFuture future = bootstrap.bind(port).sync();
if (future.isSuccess()) {
log.info("ElectricBikeNettyServer启动成功, 开始监听端口:{}", port);
} else {
log.error("ElectricBikeNettyServer启动失败", future.cause());
}
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("ElectricBikeNettyServer.start error", e);
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}).start();
}
}

View File

@@ -20,7 +20,7 @@ import java.net.InetSocketAddress;
@Slf4j
@Component
@Order(5)
@Order(7)
public class MqttSever implements CommandLineRunner {
@Override

View File

@@ -16,8 +16,9 @@ import javax.annotation.Resource;
import java.net.InetSocketAddress;
@Slf4j
@Component
@Order(2)
@Deprecated
// @Component
// @Order(3)
public class NettyServer implements CommandLineRunner {
@Resource
private NettyServerChannelInitializer nettyServerChannelInitializer;

View File

@@ -21,7 +21,7 @@ public class NettyServerChannelInitializer extends ChannelInitializer<SocketChan
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
// pipeline.addLast("frameDecoder",new CustomDecoder());
pipeline.addLast("frameDecoder", new StartAndLengthFieldFrameDecoder(0x68));
pipeline.addLast("frameDecoder", new StartAndLengthFieldFrameDecoder());
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayDecoder());
//读超时时间设置为10s0表示不监控