From f0d12120ace4494516029dd8642c3693e9960179 Mon Sep 17 00:00:00 2001 From: chenfufeng Date: Fri, 3 Mar 2023 15:53:01 +0800 Subject: [PATCH] =?UTF-8?q?[Opt]=E8=A7=A3=E5=86=B3=E5=81=B6=E7=8E=B0?= =?UTF-8?q?=E5=8F=B8=E4=B9=98=E5=B1=8F=E8=BF=9E=E4=B8=8D=E4=B8=8A=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 22 +++---- .../com/mogo/telematic/NSDNettyManager.java | 48 +++++--------- .../mogo/telematic/client/NettyTcpClient.java | 6 +- .../com/mogo/telematic/client/NsdClient.java | 49 +++++---------- .../client/handler/NettyClientHandler.java | 63 +++++++++---------- 5 files changed, 75 insertions(+), 113 deletions(-) diff --git a/gradle.properties b/gradle.properties index fa27432..a80a856 100644 --- a/gradle.properties +++ b/gradle.properties @@ -36,24 +36,24 @@ PASSWORD=xintai2018 RELEASE=true # AI CLOUD 云平台 # 工具类 -MOGO_UTILS_VERSION=1.4.5.6 +MOGO_UTILS_VERSION=1.4.5.7 # 网络请求 -MOGO_NETWORK_VERSION=1.4.5.6 +MOGO_NETWORK_VERSION=1.4.5.7 # 鉴权 -MOGO_PASSPORT_VERSION=1.4.5.6 +MOGO_PASSPORT_VERSION=1.4.5.7 # 常链接 -MOGO_SOCKET_VERSION=1.4.5.6 +MOGO_SOCKET_VERSION=1.4.5.7 # 数据采集 -MOGO_REALTIME_VERSION=1.4.5.6 +MOGO_REALTIME_VERSION=1.4.5.7 # 探路,道路事件发布,获取 -MOGO_TANLU_VERSION=1.4.5.6 +MOGO_TANLU_VERSION=1.4.5.7 # 直播推流 -MOGO_LIVE_VERSION=1.4.5.6 +MOGO_LIVE_VERSION=1.4.5.7 # 直播拉流 -MOGO_TRAFFICLIVE_VERSION=1.4.5.6 +MOGO_TRAFFICLIVE_VERSION=1.4.5.7 # 定位服务 -MOGO_LOCATION_VERSION=1.4.5.6 +MOGO_LOCATION_VERSION=1.4.5.7 # 远程通讯模块 -MOGO_TELEMATIC_VERSION=1.4.5.6 +MOGO_TELEMATIC_VERSION=1.4.5.7 # v2x -MOGO_V2X_VERSION=1.4.5.6 +MOGO_V2X_VERSION=1.4.5.7 diff --git a/libraries/mogo-telematic/src/main/java/com/mogo/telematic/NSDNettyManager.java b/libraries/mogo-telematic/src/main/java/com/mogo/telematic/NSDNettyManager.java index 4d47e85..c7c560d 100644 --- a/libraries/mogo-telematic/src/main/java/com/mogo/telematic/NSDNettyManager.java +++ b/libraries/mogo-telematic/src/main/java/com/mogo/telematic/NSDNettyManager.java @@ -4,8 +4,6 @@ import static com.mogo.telematic.client.status.ConnectState.STATUS_CONNECT_CLOSE import android.content.Context; import android.net.nsd.NsdServiceInfo; -import android.os.CountDownTimer; -import android.os.Handler; import android.os.Looper; import com.elegant.log.simplelog.Logger; @@ -28,7 +26,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFutureListener; -public class NSDNettyManager { +public class NSDNettyManager implements TelematicHandler.ITelematicListener { private static final String TAG = "NSDNettyManager"; @@ -57,13 +55,11 @@ public class NSDNettyManager { private String mUuid; private boolean mIsTaxi; private NettyClientListener mClientListener; - - private static Handler sMainHandler = new Handler(Looper.getMainLooper()); - - private CustomTimer mTimer = new CustomTimer(COUNTDOWN_TIME, INTERVAL_TIME); - private volatile boolean isCanceled= true; + private volatile boolean isCanceled = true; private NSDNettyManager() { + TelematicHandler.init(); + TelematicHandler.setListener(this); } public static NSDNettyManager getInstance() { @@ -337,7 +333,7 @@ public class NSDNettyManager { return; } if (!isCanceled) { - mTimer.cancel(); + TelematicHandler.removeDelay(); } if (mNettyTcpClient == null) { mNettyTcpClient = new NettyTcpClient.Builder() @@ -364,11 +360,9 @@ public class NSDNettyManager { mClientListener.onClientStatusConnectChanged(statusCode, content, channel); } if (statusCode == STATUS_CONNECT_CLOSED) { - sMainHandler.post(() -> { - // 开启重连倒计时 - isCanceled = false; - mTimer.start(); - }); + // 开启重连倒计时 + isCanceled = false; + TelematicHandler.delay(COUNTDOWN_TIME); } } }); @@ -448,24 +442,12 @@ public class NSDNettyManager { } } - public class CustomTimer extends CountDownTimer { - - public CustomTimer(long total, long interval) { - super(total, interval); - } - - @Override - public void onTick(long millisUntilFinished) { - - } - - @Override - public void onFinish() { - // 倒计时结束关闭客户端EventGroup,重新扫描ip - cancel(); - isCanceled = true; - disconnect(); - searchAndConnectServer(mContext, mUuid, mIsTaxi, mClientListener); - } + @Override + public void delayCompleted() { + // 倒计时结束关闭客户端EventGroup,重新扫描ip + TelematicHandler.removeDelay(); + isCanceled = true; + disconnect(); + searchAndConnectServer(mContext, mUuid, mIsTaxi, mClientListener); } } diff --git a/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/NettyTcpClient.java b/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/NettyTcpClient.java index f8234b7..db864a3 100644 --- a/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/NettyTcpClient.java +++ b/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/NettyTcpClient.java @@ -12,6 +12,7 @@ import com.elegant.log.simplelog.Logger; import com.mogo.telematic.MogoLengthFrameDecoder; import com.mogo.telematic.MogoMessageEncoder; import com.mogo.telematic.MogoProtocolMsg; +import com.mogo.telematic.client.handler.HeartbeatHandler; import com.mogo.telematic.client.handler.NettyClientHandler; import com.mogo.telematic.client.handler.ReconnectHandler; import com.mogo.telematic.client.listener.MessageStateListener; @@ -71,7 +72,7 @@ public class NettyTcpClient { /** * 心跳间隔时间 */ - private long heartBeatInterval = 5;//单位秒 + private long heartBeatInterval = 10;//单位秒 /** * 是否发送心跳 @@ -155,7 +156,8 @@ public class NettyTcpClient { public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ReconnectHandler(NettyTcpClient.this, mProxyListener)); if (isSendheartBeat) { - ch.pipeline().addLast("ping", new IdleStateHandler(0, heartBeatInterval, 0, TimeUnit.SECONDS));//5s未发送数据,回调userEventTriggered + ch.pipeline().addLast(new IdleStateHandler(heartBeatInterval, heartBeatInterval, 0, TimeUnit.SECONDS)); + ch.pipeline().addLast(new HeartbeatHandler(mProxyListener)); } // decoder ch.pipeline().addLast(new MogoLengthFrameDecoder(MAX_FRAME_LENGTH, diff --git a/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/NsdClient.java b/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/NsdClient.java index 4e5b841..b037487 100644 --- a/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/NsdClient.java +++ b/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/NsdClient.java @@ -37,11 +37,6 @@ public class NsdClient { */ private final ArrayList mNsdServiceInfoListBefore = new ArrayList<>(); - - private static final int MSG_RESOLVER = 1002; - - private static final int MSG_NULL = 1003; - /** * @param context this * @param serviceName 客户端扫描 指定的地址 暂时没用到 @@ -100,7 +95,10 @@ public class NsdClient { if (serviceInfo.getServiceName().startsWith(mServiceName)) { mNsdManager.resolveService(serviceInfo, getResolveListener()); } else { - mHandler.sendEmptyMessage(MSG_NULL); +// mHandler.sendEmptyMessage(MSG_NULL); + if (mIServerFound != null) { + mIServerFound.onServerFail(); + } } mNsdServiceInfoListBefore.add(serviceInfo); } @@ -112,30 +110,6 @@ public class NsdClient { }; } - private final Handler mHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - super.handleMessage(msg); - switch (msg.what) { - case MSG_RESOLVER: - //回调到主线 进行解析結果的回調 - NsdServiceInfo serviceInfo = (NsdServiceInfo) msg.obj; - if (mIServerFound != null) { - mIServerFound.onServerFound(serviceInfo, serviceInfo.getPort()); - } - Logger.e(TAG, " 指定onServiceFound(" + mServiceName + "): Service Info: --> " + serviceInfo); - - break; - case MSG_NULL: - if (mIServerFound != null) { - mIServerFound.onServerFail(); - } - break; - default: - } - } - }; - /** * 解析未 调用未被解析的 NsdServiceInfo */ @@ -158,11 +132,16 @@ public class NsdClient { mNsdServiceInfoList.add(serviceInfo); - //解析的结果 通过Handler发送到主线程 - Message msg = Message.obtain(); - msg.what = MSG_RESOLVER; - msg.obj = serviceInfo; - mHandler.sendMessageDelayed(msg, 200); +// //解析的结果 通过Handler发送到主线程 +// Message msg = Message.obtain(); +// msg.what = MSG_RESOLVER; +// msg.obj = serviceInfo; +// mHandler.sendMessageDelayed(msg, 200); + + if (mIServerFound != null) { + mIServerFound.onServerFound(serviceInfo, serviceInfo.getPort()); + } + Logger.e(TAG, " 指定onServiceFound(" + mServiceName + "): Service Info: --> " + serviceInfo); } }; } diff --git a/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/handler/NettyClientHandler.java b/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/handler/NettyClientHandler.java index 2347341..2aedf74 100644 --- a/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/handler/NettyClientHandler.java +++ b/libraries/mogo-telematic/src/main/java/com/mogo/telematic/client/handler/NettyClientHandler.java @@ -1,5 +1,7 @@ package com.mogo.telematic.client.handler; +import static com.mogo.telematic.client.status.ConnectState.STATUS_CONNECT_CLOSED; + import com.elegant.log.simplelog.Logger; import com.mogo.telematic.MogoProtocolMsg; import com.mogo.telematic.client.NettyTcpClient; @@ -8,6 +10,8 @@ import com.mogo.telematic.client.status.ConnectState; import org.jetbrains.annotations.NotNull; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.timeout.IdleStateEvent; @@ -19,7 +23,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler { +// if (!future.isSuccess()) { +// listener.onClientStatusConnectChanged(STATUS_CONNECT_CLOSED, +// "channelInactive", +// ctx.channel()); +// future.channel().close(); +// } +// }); +// } else { +// Logger.d(TAG, "不发送心跳"); +// } +// } /** *

客户端上线