mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 19:15:35 +08:00
commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package com.jsowell.netty.server.yunkuaichong;
|
||||
|
||||
import com.jsowell.common.constant.Constants;
|
||||
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.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class NettyServer implements CommandLineRunner {
|
||||
@Resource
|
||||
private NettyServerChannelInitializer nettyServerChannelInitializer;
|
||||
|
||||
@Order(value = 1)
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
InetSocketAddress address = new InetSocketAddress(Constants.SOCKET_IP, Constants.SOCKET_PORT);
|
||||
this.start(address);
|
||||
}
|
||||
|
||||
public void start(InetSocketAddress address) {
|
||||
// log.info("========NettyServer.start order 1");
|
||||
//配置服务端的NIO线程组
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
||||
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
|
||||
try {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap() // //启动NIO服务的辅助启动类
|
||||
.group(bossGroup, workerGroup) // 绑定线程池
|
||||
.channel(NioServerSocketChannel.class) // 启动服务时, 通过反射创建一个NioServerSocketChannel对象
|
||||
|
||||
/*
|
||||
===> 服务器初始化时执行, 属于AbstracBootstrap的方法
|
||||
*/
|
||||
.handler(new LoggingHandler(LogLevel.DEBUG)) // handler在初始化时就会执行,可以设置打印日志级别
|
||||
// 设置tcp缓冲区, 可连接队列大小
|
||||
.option(ChannelOption.SO_BACKLOG, 128) //服务端接受连接的队列长度,如果队列已满,客户端连接将被拒绝
|
||||
.option(ChannelOption.SO_REUSEADDR, true) //允许重复使用本地地址和端口
|
||||
|
||||
/*
|
||||
===> 客户端连接成功之后执行, 属于ServerBootstrap的方法,继承自AbstractBootstrap
|
||||
*/
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true) //两小时没有数据通信时, 启用心跳保活机制探测客户端是否连接有效
|
||||
.childOption(ChannelOption.SO_REUSEADDR, true)
|
||||
.childHandler(nettyServerChannelInitializer)//编码解码
|
||||
|
||||
// 地址
|
||||
.localAddress(address);
|
||||
|
||||
// 绑定端口,开始接收进来的连接
|
||||
ChannelFuture future = bootstrap.bind(address.getPort()).sync();
|
||||
if (future.isSuccess()) {
|
||||
log.info("NettyServer启动成功, 开始监听端口:{}", address.getPort());
|
||||
} else {
|
||||
log.error("NettyServer启动失败", future.cause());
|
||||
}
|
||||
//关闭channel和块,直到它被关闭
|
||||
future.channel().closeFuture().sync();
|
||||
} catch (Exception e) {
|
||||
log.error("NettyServer.start error", e);
|
||||
bossGroup.shutdownGracefully();
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.jsowell.netty.server.yunkuaichong;
|
||||
|
||||
import com.jsowell.netty.decoder.StartAndLengthFieldFrameDecoder;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.bytes.ByteArrayDecoder;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {
|
||||
|
||||
@Resource
|
||||
NettyServerHandler nettyServerHandler;
|
||||
|
||||
@Override
|
||||
protected void initChannel(SocketChannel channel) throws Exception {
|
||||
ChannelPipeline pipeline = channel.pipeline();
|
||||
// pipeline.addLast("decoder",new CustomDecoder());
|
||||
pipeline.addLast("frameDecoder", new StartAndLengthFieldFrameDecoder(0x68));
|
||||
pipeline.addLast("decoder", new ByteArrayDecoder());
|
||||
pipeline.addLast("encoder", new ByteArrayDecoder());
|
||||
//读超时时间设置为10s,0表示不监控
|
||||
pipeline.addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
|
||||
pipeline.addLast("handler", nettyServerHandler);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package com.jsowell.netty.server.yunkuaichong;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.jsowell.common.enums.ykc.PileChannelEntity;
|
||||
import com.jsowell.common.util.BytesUtil;
|
||||
import com.jsowell.common.util.YKCUtils;
|
||||
import com.jsowell.netty.service.yunkuaichong.YKCBusinessService;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelId;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import io.netty.handler.timeout.ReadTimeoutException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* netty服务端处理类
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
@Slf4j
|
||||
@Component
|
||||
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
@Autowired
|
||||
private YKCBusinessService ykcService;
|
||||
|
||||
/**
|
||||
* 管理一个全局map,保存连接进服务端的通道数量
|
||||
*/
|
||||
private static final ConcurrentHashMap<ChannelId, ChannelHandlerContext> CHANNEL_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
private final List<String> notPrintFrameTypeList = Lists.newArrayList("0x03");
|
||||
|
||||
/**
|
||||
* 有客户端连接服务器会触发此函数
|
||||
* 连接被建立并且准备进行通信时被调用
|
||||
*/
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) {
|
||||
InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
|
||||
String clientIp = insocket.getAddress().getHostAddress();
|
||||
int clientPort = insocket.getPort();
|
||||
//获取连接通道唯一标识
|
||||
ChannelId channelId = ctx.channel().id();
|
||||
//如果map中不包含此连接,就保存连接
|
||||
if (CHANNEL_MAP.containsKey(channelId)) {
|
||||
log.info("客户端【{}】是连接状态,连接通道数量: {}", channelId, CHANNEL_MAP.size());
|
||||
} else {
|
||||
//保存连接
|
||||
CHANNEL_MAP.put(channelId, ctx);
|
||||
log.info("客户端【{}】, 连接netty服务器[IP:{}--->PORT:{}], 连接通道数量: {}", channelId, clientIp, clientPort, CHANNEL_MAP.size());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 有客户端发消息会触发此函数
|
||||
*/
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
// log.info("加载客户端报文=== channelId:" + ctx.channel().id() + ", msg:" + msg);
|
||||
// 下面可以解析数据,保存数据,生成返回报文,将需要返回报文写入write函数
|
||||
byte[] arr = (byte[]) msg;
|
||||
// 获取帧类型
|
||||
String frameType = YKCUtils.frameType2Str(BytesUtil.copyBytes(arr, 5, 1));
|
||||
// 获取序列号域
|
||||
int serialNumber = BytesUtil.bytesToIntLittle(BytesUtil.copyBytes(arr, 2, 2));
|
||||
|
||||
// new
|
||||
String hexString = DatatypeConverter.printHexBinary(arr);
|
||||
|
||||
// 心跳包0x03日志太多,造成日志文件过大,改为不打印
|
||||
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
|
||||
log.info("【<<<<<平台收到消息<<<<<】channel:{}, 帧类型:{}, 序列号域:{}, 报文:{}, new报文:{}, msg:{}",
|
||||
ctx.channel().id(), frameType, serialNumber, BytesUtil.binary(arr, 16), hexString, Arrays.toString(arr));
|
||||
}
|
||||
|
||||
// 处理数据
|
||||
byte[] result = ykcService.process(arr, ctx.channel());
|
||||
if (Objects.nonNull(result)) {
|
||||
// 响应客户端
|
||||
ByteBuf buffer = ctx.alloc().buffer().writeBytes(result);
|
||||
this.channelWrite(ctx.channel().id(), buffer);
|
||||
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
|
||||
log.info("【>>>>>平台响应消息>>>>>】:{}", BytesUtil.binary(result, 16));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 有客户端终止连接服务器会触发此函数
|
||||
*/
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) {
|
||||
InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
|
||||
String clientIp = insocket.getAddress().getHostAddress();
|
||||
ChannelId channelId = ctx.channel().id();
|
||||
//包含此客户端才去删除
|
||||
if (CHANNEL_MAP.containsKey(channelId)) {
|
||||
ykcService.exit(channelId);
|
||||
//删除连接
|
||||
CHANNEL_MAP.remove(channelId);
|
||||
log.info("客户端【{}】, 退出netty服务器[IP:{}--->PORT:{}], 连接通道数量: {}", channelId, clientIp, insocket.getPort(), CHANNEL_MAP.size());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { // (2)
|
||||
// Channel incoming = ctx.channel();
|
||||
// log.info("handlerAdded: handler被添加到channel的pipeline connect:" + incoming.remoteAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { // (3)
|
||||
// Channel incoming = ctx.channel();
|
||||
// log.info("handlerRemoved: handler从channel的pipeline中移除 connect:" + incoming.remoteAddress());
|
||||
// ChannelMapByEntity.removeChannel(incoming);
|
||||
// ChannelMap.removeChannel(incoming);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
||||
Channel channel = ctx.channel();
|
||||
// log.info("channel:【{}】读数据完成", channel.id());
|
||||
super.channelReadComplete(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务端给客户端发送消息
|
||||
*
|
||||
* @param channelId 连接通道唯一id
|
||||
* @param msg 需要发送的消息内容
|
||||
*/
|
||||
public void channelWrite(ChannelId channelId, Object msg) throws Exception {
|
||||
ChannelHandlerContext ctx = CHANNEL_MAP.get(channelId);
|
||||
if (ctx == null) {
|
||||
log.info("通道【{}】不存在", channelId);
|
||||
return;
|
||||
}
|
||||
if (msg == null || msg == "") {
|
||||
log.info("服务端响应空的消息");
|
||||
return;
|
||||
}
|
||||
//将客户端的信息直接返回写入ctx
|
||||
ctx.write(msg);
|
||||
//刷新缓存区
|
||||
ctx.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
String socketString = ctx.channel().remoteAddress().toString();
|
||||
ChannelId channelId = ctx.channel().id();
|
||||
String pileSn = PileChannelEntity.getPileSnByChannelId(channelId.asLongText());
|
||||
if (evt instanceof IdleStateEvent) { // 超时事件
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
boolean flag = false;
|
||||
if (event.state() == IdleState.READER_IDLE) { // 读
|
||||
flag = true;
|
||||
log.info("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, READER_IDLE 读超时", socketString, channelId, pileSn);
|
||||
} else if (event.state() == IdleState.WRITER_IDLE) { // 写
|
||||
flag = true;
|
||||
log.info("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, WRITER_IDLE 写超时", socketString, channelId, pileSn);
|
||||
} else if (event.state() == IdleState.ALL_IDLE) { // 全部
|
||||
flag = true;
|
||||
log.info("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, ALL_IDLE 总超时", socketString, channelId, pileSn);
|
||||
}
|
||||
// if (flag) {
|
||||
// ctx.channel().close();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发生异常会触发此函数
|
||||
*/
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
ChannelId channelId = ctx.channel().id();
|
||||
log.error("发生异常 channelId:{}", channelId.asShortText(), cause);
|
||||
cause.printStackTrace();
|
||||
// 如果桩连到平台,在1分钟内没有发送数据过来,会报ReadTimeoutException异常
|
||||
if (cause instanceof ReadTimeoutException) {
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("Connection timeout 【{}】", ctx.channel().remoteAddress());
|
||||
}
|
||||
log.info("【{}】发生了错误, 此连接被关闭, 此时连通数量: {}", channelId, CHANNEL_MAP.size());
|
||||
ctx.channel().close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user