diff --git a/gradle.properties b/gradle.properties index 38d551f..9061d67 100644 --- a/gradle.properties +++ b/gradle.properties @@ -36,26 +36,26 @@ PASSWORD=xintai2018 RELEASE=true # AI CLOUD 云平台 # 工具类 -MOGO_UTILS_VERSION=1.4.4.8 +MOGO_UTILS_VERSION=1.4.4.9 # 网络请求 -MOGO_NETWORK_VERSION=1.4.4.8 +MOGO_NETWORK_VERSION=1.4.4.9 # 网络DNS -MOGO_HTTPDNS_VERSION=1.4.4.8 +MOGO_HTTPDNS_VERSION=1.4.4.9 # 鉴权 -MOGO_PASSPORT_VERSION=1.4.4.8 +MOGO_PASSPORT_VERSION=1.4.4.9 # 常链接 -MOGO_SOCKET_VERSION=1.4.4.8 +MOGO_SOCKET_VERSION=1.4.4.9 # 数据采集 -MOGO_REALTIME_VERSION=1.4.4.8 +MOGO_REALTIME_VERSION=1.4.4.9 # 探路,道路事件发布,获取 -MOGO_TANLU_VERSION=1.4.4.8 +MOGO_TANLU_VERSION=1.4.4.9 # 直播推流 -MOGO_LIVE_VERSION=1.4.4.8 +MOGO_LIVE_VERSION=1.4.4.9 # 直播拉流 -MOGO_TRAFFICLIVE_VERSION=1.4.4.8 +MOGO_TRAFFICLIVE_VERSION=1.4.4.9 # 定位服务 -MOGO_LOCATION_VERSION=1.4.4.8 +MOGO_LOCATION_VERSION=1.4.4.9 # 远程通讯模块 -MOGO_TELEMATIC_VERSION=1.4.4.8 +MOGO_TELEMATIC_VERSION=1.4.4.9 # v2x -MOGO_V2X_VERSION=1.4.4.8 +MOGO_V2X_VERSION=1.4.4.9 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 56c0ba0..4d47e85 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 @@ -5,6 +5,8 @@ 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; import com.mogo.telematic.client.NettyTcpClient; @@ -56,6 +58,11 @@ public class NSDNettyManager { 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 NSDNettyManager() { } @@ -233,14 +240,12 @@ public class NSDNettyManager { String hostAddress = info.getHost().getHostAddress(); Logger.d(TAG, "NSD查询到指定服务器信息ip为:" + hostAddress + ",port为:" + port); //获取到指定的地址,进行Netty的连接 - connectNettyServer(hostAddress, port, uuid); - - if (info.getServiceName().startsWith(SERVER_NAME)) { - //扫描到以后停止 - if (mNsdClient != null) { - mNsdClient.stopServiceDiscovery(); - mNsdClient = null; - } + if (Thread.currentThread() == Looper.getMainLooper().getThread()) { + new Thread(() -> { + connectServer(info, port, hostAddress, uuid); + }).start(); + } else { + connectServer(info, port, hostAddress, uuid); } } } @@ -260,6 +265,17 @@ public class NSDNettyManager { } + private void connectServer(NsdServiceInfo info, int port, String hostAddress, String uuid) { + connectNettyServer(hostAddress, port, uuid); + if (info.getServiceName().startsWith(SERVER_NAME)) { + //扫描到以后停止 + if (mNsdClient != null) { + mNsdClient.stopServiceDiscovery(); + mNsdClient = null; + } + } + } + private void scanDriverServer(String uuid, NettyClientListener listener) { // 启动发现司机屏幕任务 mDiscoveryTask = new DiscoveryDriverTask(); @@ -320,6 +336,9 @@ public class NSDNettyManager { Logger.e(TAG, "Netty Server的ip不能为空!"); return; } + if (!isCanceled) { + mTimer.cancel(); + } if (mNettyTcpClient == null) { mNettyTcpClient = new NettyTcpClient.Builder() .setHost(serverAddress) //设置服务端地址 @@ -345,8 +364,11 @@ public class NSDNettyManager { mClientListener.onClientStatusConnectChanged(statusCode, content, channel); } if (statusCode == STATUS_CONNECT_CLOSED) { - // 开启重连倒计时 - new CustomTimer(COUNTDOWN_TIME, INTERVAL_TIME).start(); + sMainHandler.post(() -> { + // 开启重连倒计时 + isCanceled = false; + mTimer.start(); + }); } } }); @@ -401,7 +423,7 @@ public class NSDNettyManager { // 停止局域网内扫描 mNsdClient.stopServiceDiscovery(); } - if (mNettyTcpClient != null && (mNettyTcpClient.isConnecting() || mNettyTcpClient.getConnectStatus())) { + if (mNettyTcpClient != null) { // 断开已连接上的 mNettyTcpClient.disconnect(); mNettyTcpClient = null; @@ -441,6 +463,7 @@ public class NSDNettyManager { public void onFinish() { // 倒计时结束关闭客户端EventGroup,重新扫描ip cancel(); + 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 8e9d469..f8234b7 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 @@ -5,6 +5,7 @@ import static com.mogo.telematic.MogoLengthFrameDecoder.LENGTH_FIELD_SIZE; import static com.mogo.telematic.MogoLengthFrameDecoder.MAX_FRAME_LENGTH; import static com.mogo.telematic.MogoProtocolMsg.IDENTITY_REGIST; +import android.os.Looper; import android.text.TextUtils; import com.elegant.log.simplelog.Logger; @@ -125,16 +126,22 @@ public class NettyTcpClient { Logger.d(TAG, "正在连接中,connect return"); return; } - Thread clientThread = new Thread("client-Netty") { - @Override - public void run() { - super.run(); - isNeedReconnect = true; - reconnectNum = MAX_CONNECT_TIMES; - connectServer(); - } - }; - clientThread.start(); + if (Thread.currentThread() == Looper.getMainLooper().getThread()) { + Thread clientThread = new Thread("client-Netty") { + @Override + public void run() { + super.run(); + isNeedReconnect = true; + reconnectNum = MAX_CONNECT_TIMES; + connectServer(); + } + }; + clientThread.start(); + } else { + isNeedReconnect = true; + reconnectNum = MAX_CONNECT_TIMES; + connectServer(); + } } private void initBootstrap() { @@ -273,8 +280,11 @@ public class NettyTcpClient { public void disconnect() { Logger.e(TAG, "disconnect"); isNeedReconnect = false; - group.shutdownGracefully(); - group = null; + isConnected = false; + if (group != null) { + group.shutdownGracefully(); + group = null; + } bootstrap = null; }