update 电单车协议

This commit is contained in:
Guoqs
2024-07-15 17:10:48 +08:00
parent b9ec56a202
commit 6487bb8bdc
8 changed files with 199 additions and 79 deletions

View File

@@ -3,21 +3,51 @@ package com.jsowell.netty.server.electricbicycles;
import com.jsowell.netty.domain.ChargingPileMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.net.InetSocketAddress;
import java.time.Instant;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.ConcurrentHashMap;
@ChannelHandler.Sharable
@Slf4j
@Component
public class ChargingPileHandler extends ChannelInboundHandlerAdapter {
/**
* 管理一个全局map保存连接进服务端的通道数量
*/
private static final ConcurrentHashMap<ChannelId, ChannelHandlerContext> CHANNEL_MAP = new ConcurrentHashMap<>();
/**
* 有客户端连接服务器会触发此函数
* 连接被建立并且准备进行通信时被调用
*/
@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("Handler:{}, 客户端【{}】是连接状态,连接通道数量: {}", this.getClass().getSimpleName(), channelId, CHANNEL_MAP.size());
} else {
//保存连接
CHANNEL_MAP.put(channelId, ctx);
log.info("Handler:{}, 客户端【{}】, 连接netty服务器【IP:{}, PORT:{}】, 连接通道数量: {}", this.getClass().getSimpleName(), channelId, clientIp, clientPort, CHANNEL_MAP.size());
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
log.info("加载客户端报文=== channelId:{}, mag:{}", ctx.channel().id(), msg.toString());
if (!(msg instanceof ChargingPileMessage)) {
return;
}

View File

@@ -3,6 +3,7 @@ package com.jsowell.netty.server.electricbicycles;
import com.jsowell.netty.decoder.ChargingPileDecoder;
import com.jsowell.netty.decoder.ProtocolDnyDecoder;
import com.jsowell.netty.decoder.StartAndLengthFieldFrameDecoder;
import com.jsowell.netty.decoder.StartAndLengthFieldFrameDecoder2;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
@@ -26,12 +27,12 @@ public class ElectricBicyclesServerChannelInitializer extends ChannelInitializer
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
// pipeline.addLast("frameDecoder",new CustomDecoder());
pipeline.addLast("frameDecoder", new ChargingPileDecoder());
pipeline.addLast("frameDecoder", new StartAndLengthFieldFrameDecoder());
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayDecoder());
//读超时时间设置为10s0表示不监控
pipeline.addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
pipeline.addLast("handler", electricBicyclesServerHandler);
pipeline.addLast("handler", chargingPileHandler);
}
}