[Update]优化连接状态
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -110,8 +110,7 @@ public class NSDNettyManager {
|
||||
nettyTcpServer.setListener(mDefaultServerListener);
|
||||
nettyTcpServer.start();
|
||||
} else {
|
||||
Logger.i(TAG, "服务端已开启,断开连接!");
|
||||
NettyTcpServer.getInstance().disconnect();
|
||||
Logger.i(TAG, "服务端已开启,不要重复启动!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -289,7 +289,7 @@ public class NettyTcpClient {
|
||||
* @return 获取TCP连接状态
|
||||
*/
|
||||
public boolean getConnectStatus() {
|
||||
return isConnected;
|
||||
return channel != null && channel.isOpen() && channel.isActive();
|
||||
}
|
||||
|
||||
public boolean isConnecting() {
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user