[Opt]解决偶现司乘屏连不上的问题
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -37,11 +37,6 @@ public class NsdClient {
|
||||
*/
|
||||
private final ArrayList<NsdServiceInfo> 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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<MogoProtocol
|
||||
private final boolean isSendHeartBeat;
|
||||
private NettyClientListener listener;
|
||||
private final String mSign;
|
||||
private final Object heartBeatData;
|
||||
private Object heartBeatData;
|
||||
|
||||
public NettyClientHandler(@NotNull NettyClientListener listener, String sign, boolean isSendHeartBeat, Object heartBeatData) {
|
||||
this.listener = listener;
|
||||
@@ -34,37 +38,32 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
|
||||
* @param ctx ChannelHandlerContext
|
||||
* @param evt IdleStateEvent
|
||||
*/
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
if (evt instanceof IdleStateEvent) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
switch (event.state()) {
|
||||
case WRITER_IDLE:
|
||||
case READER_IDLE:
|
||||
sendHeartbeat(ctx);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
super.userEventTriggered(ctx, evt);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendHeartbeat(ChannelHandlerContext ctx) {
|
||||
if (isSendHeartBeat) {
|
||||
if (heartBeatData == null) {
|
||||
MogoProtocolMsg heartData = new MogoProtocolMsg(MogoProtocolMsg.NORMAL_DATA, 2, new byte[]{0x00, 0x00});
|
||||
ctx.channel().writeAndFlush(heartData);
|
||||
} else if (heartBeatData instanceof MogoProtocolMsg) {
|
||||
ctx.channel().writeAndFlush(heartBeatData);
|
||||
} else {
|
||||
Logger.e(TAG, "userEventTriggered: heartBeatData type error");
|
||||
}
|
||||
} else {
|
||||
Logger.d(TAG, "不发送心跳");
|
||||
}
|
||||
}
|
||||
// @Override
|
||||
// public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
// if (evt instanceof IdleStateEvent) {
|
||||
// sendHeartbeat(ctx);
|
||||
// } else {
|
||||
// super.userEventTriggered(ctx, evt);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void sendHeartbeat(ChannelHandlerContext ctx) {
|
||||
// if (isSendHeartBeat) {
|
||||
// if (heartBeatData == null) {
|
||||
// heartBeatData = new MogoProtocolMsg(MogoProtocolMsg.NORMAL_DATA, 2, new byte[]{0x00, 0x00});
|
||||
// }
|
||||
// ctx.writeAndFlush(heartBeatData).addListener((ChannelFutureListener) future -> {
|
||||
// if (!future.isSuccess()) {
|
||||
// listener.onClientStatusConnectChanged(STATUS_CONNECT_CLOSED,
|
||||
// "channelInactive",
|
||||
// ctx.channel());
|
||||
// future.channel().close();
|
||||
// }
|
||||
// });
|
||||
// } else {
|
||||
// Logger.d(TAG, "不发送心跳");
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* <p>客户端上线</p>
|
||||
|
||||
Reference in New Issue
Block a user