| | |
| | | package com.ruoyi.dataInterchange.netty.client; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.ruoyi.dataInterchange.model.DOWNConnectReq; |
| | | import com.ruoyi.dataInterchange.model.enu.DataType; |
| | | import com.ruoyi.dataInterchange.util.jtt809.common.ByteArrayUtil; |
| | | import com.ruoyi.dataInterchange.util.jtt809.common.Jtt809Constant; |
| | | import com.ruoyi.dataInterchange.util.jtt809.common.Jtt809Util; |
| | | import com.ruoyi.dataInterchange.util.jtt809.decoder.Jtt809Decoder; |
| | | import com.ruoyi.dataInterchange.util.jtt809.encoder.Jtt809Encoder; |
| | | import com.ruoyi.dataInterchange.util.jtt809.gnsscenter.GnssCenterService; |
| | | import com.ruoyi.dataInterchange.util.jtt809.packet.common.OuterPacket; |
| | | import com.ruoyi.dataInterchange.wapper.UPConnect; |
| | | import io.netty.bootstrap.Bootstrap; |
| | | import io.netty.buffer.ByteBuf; |
| | | import io.netty.buffer.Unpooled; |
| | |
| | | import io.netty.channel.socket.SocketChannel; |
| | | import io.netty.channel.socket.nio.NioSocketChannel; |
| | | import io.netty.handler.timeout.IdleStateHandler; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.net.InetSocketAddress; |
| | | import java.util.concurrent.TimeUnit; |
| | |
| | | * @author zhibing.pu |
| | | * @Date 2025/3/3 20:11 |
| | | */ |
| | | @Slf4j |
| | | public class NettyClient { |
| | | /** |
| | | * 连接IP |
| | |
| | | * |
| | | * @throws Exception |
| | | */ |
| | | public void start(int code) throws Exception { |
| | | public void start(int inferiorPlatformId, int verifyCode) throws Exception { |
| | | EventLoopGroup nioEventLoopGroup = null; |
| | | try { |
| | | //创建Bootstrap对象用来引导启动客户端 |
| | |
| | | // 空闲检测处理器 触发空闲状态事件 读空闲:5秒 写空闲:7秒 读写空闲:10秒 |
| | | pipeline.addLast(new IdleStateHandler(5, 7, 3, TimeUnit.SECONDS)); |
| | | // 处理器 |
| | | // pipeline.addLast("handler", new NettyHandle()); |
| | | pipeline.addLast("handler", new NettyClientHandler()); |
| | | } |
| | | }); |
| | | // • 调用Bootstrap.connect()来连接服务器 |
| | | ChannelFuture f = bootstrap.connect().sync(); |
| | | //将通道添加到缓存中,便于后期直接使用 |
| | | Channel channel = f.channel(); |
| | | ChannelMap.addClientChannel(code, channel); |
| | | ChannelMap.addClientChannel(inferiorPlatformId, channel); |
| | | log.info("从链路连接成功"); |
| | | |
| | | //构建从链路请求 |
| | | DOWNConnectReq downConnectReq = new DOWNConnectReq(); |
| | | downConnectReq.setVerifyCode(verifyCode); |
| | | log.info("从链路连接请求({}):{}", DataType.DOWN_CONNECT_REQ.getCode(), JSON.toJSONString(downConnectReq)); |
| | | byte[] body = downConnectReq.encode(); |
| | | OuterPacket out = new OuterPacket(DataType.DOWN_CONNECT_REQ.getCode(), body); |
| | | channel.writeAndFlush(out); |
| | | channel.flush(); |
| | | |
| | | //缓存从链路地址 |
| | | UPConnect upConnect = new UPConnect(); |
| | | upConnect.setDownLinkIp(host); |
| | | upConnect.setDownLinkPort(port); |
| | | upConnect.setVerifyCode(verifyCode); |
| | | ChannelMap.addIpAndPort(inferiorPlatformId, upConnect); |
| | | |
| | | // • 最后关闭EventLoopGroup来释放资源 |
| | | f.channel().closeFuture().sync(); |
| | | } finally { |