[Update]优化连接状态

This commit is contained in:
chenfufeng
2022-05-19 14:59:53 +08:00
parent ffe91524b1
commit d64a627bce
7 changed files with 32 additions and 21 deletions

View File

@@ -36,26 +36,26 @@ PASSWORD=xintai2018
RELEASE=true
# AI CLOUD 云平台
# 工具类
MOGO_UTILS_VERSION=1.3.58
MOGO_UTILS_VERSION=1.3.59
# 网络请求
MOGO_NETWORK_VERSION=1.3.58
MOGO_NETWORK_VERSION=1.3.59
# 网络DNS
MOGO_HTTPDNS_VERSION=1.3.58
MOGO_HTTPDNS_VERSION=1.3.59
# 鉴权
MOGO_PASSPORT_VERSION=1.3.58
MOGO_PASSPORT_VERSION=1.3.59
# 常链接
MOGO_SOCKET_VERSION=1.3.58
MOGO_SOCKET_VERSION=1.3.59
# 数据采集
MOGO_REALTIME_VERSION=1.3.58
MOGO_REALTIME_VERSION=1.3.59
# 探路,道路事件发布,获取
MOGO_TANLU_VERSION=1.3.58
MOGO_TANLU_VERSION=1.3.59
# 直播推流
MOGO_LIVE_VERSION=1.3.58
MOGO_LIVE_VERSION=1.3.59
# 直播拉流
MOGO_TRAFFICLIVE_VERSION=1.3.58
MOGO_TRAFFICLIVE_VERSION=1.3.59
# 定位服务
MOGO_LOCATION_VERSION=1.3.58
MOGO_LOCATION_VERSION=1.3.59
# 远程通讯模块
MOGO_TELEMATIC_VERSION=1.3.58
MOGO_TELEMATIC_VERSION=1.3.59
# v2x
MOGO_V2X_VERSION=1.3.58
MOGO_V2X_VERSION=1.3.59

View File

@@ -12,6 +12,7 @@ public class MogoProtocolMsg {
// 同步美化模式状态
public static final int SYNC_MODE_STATUS = 3;
public static final int REQ_CAR_NUMBER = 4;
public static final int REQ_MAC_ADDRESS = 5;
private int protocolType;
private int bodyLength;

View File

@@ -110,8 +110,7 @@ public class NSDNettyManager {
nettyTcpServer.setListener(mDefaultServerListener);
nettyTcpServer.start();
} else {
Logger.i(TAG, "服务端已开启,断开连接");
NettyTcpServer.getInstance().disconnect();
Logger.i(TAG, "服务端已开启,不要重复启动");
}
}

View File

@@ -289,7 +289,7 @@ public class NettyTcpClient {
* @return 获取TCP连接状态
*/
public boolean getConnectStatus() {
return isConnected;
return channel != null && channel.isOpen() && channel.isActive();
}
public boolean isConnecting() {

View File

@@ -6,6 +6,8 @@ import com.mogo.telematic.client.NettyTcpClient;
import com.mogo.telematic.client.listener.NettyClientListener;
import com.mogo.telematic.client.status.ConnectState;
import org.jetbrains.annotations.NotNull;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
@@ -19,7 +21,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
private final String mSign;
private final Object heartBeatData;
public NettyClientHandler(NettyClientListener listener, String sign, boolean isSendHeartBeat, Object heartBeatData) {
public NettyClientHandler(@NotNull NettyClientListener listener, String sign, boolean isSendHeartBeat, Object heartBeatData) {
this.listener = listener;
this.mSign = sign;
this.isSendHeartBeat = isSendHeartBeat;
@@ -85,7 +87,9 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
protected void channelRead0(ChannelHandlerContext ctx, MogoProtocolMsg msg) throws Exception {
if (msg.getProtocolType() == MogoProtocolMsg.IDENTITY_REGIST) {
String sn = new String(msg.getBody());
NettyTcpClient.sSERVER_SN = sn;
synchronized (NettyTcpClient.class) {
NettyTcpClient.sSERVER_SN = sn;
}
} else {
if (listener != null) {
listener.onMessageResponseClient(msg, mSign, ctx.channel());

View File

@@ -12,7 +12,7 @@ import kotlin.math.max
class ReconnectHandler(
private val mNettyClient: NettyTcpClient,
private val listener: NettyClientListener<*>?
private val listener: NettyClientListener<*>
) : ChannelInboundHandlerAdapter() {
private val maxRetries = Int.MAX_VALUE
@@ -33,7 +33,7 @@ class ReconnectHandler(
@Throws(Exception::class)
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
Logger.e(TAG, "ReconnectHandler#exceptionCaught cause message is:${cause.message}")
listener?.onClientStatusConnectChanged(
listener.onClientStatusConnectChanged(
ConnectState.STATUS_CONNECT_ERROR,
cause.message,
ctx.channel()
@@ -45,7 +45,14 @@ class ReconnectHandler(
@Throws(Exception::class)
override fun channelInactive(ctx: ChannelHandlerContext) {
Logger.d(TAG, "ReconnectHandler channelInactive ...")
listener?.onClientStatusConnectChanged(STATUS_CONNECT_CLOSED, "channelInactive", ctx.channel())
synchronized(NettyTcpClient::class.java) {
NettyTcpClient.sSERVER_SN = ""
}
listener.onClientStatusConnectChanged(
STATUS_CONNECT_CLOSED,
"channelInactive",
ctx.channel()
)
var allowRetry = false
if (retries < maxRetries) {
allowRetry = true

View File

@@ -51,7 +51,7 @@ public class NettyTcpServer {
// private boolean connectStatus;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private boolean isServerStart;
private volatile boolean isServerStart;
public static NettyTcpServer getInstance() {
if (instance == null) {