[fix]解决连接指定ip出现的问题
This commit is contained in:
@@ -5,6 +5,7 @@ import static com.mogo.telematic.client.status.ConnectState.STATUS_CONNECT_SUCCE
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.nsd.NsdServiceInfo;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -57,6 +58,8 @@ public class NSDNettyManager implements TelematicHandler.ITelematicListener {
|
||||
private NettyClientListener mClientListener;
|
||||
private volatile boolean isCanceled = true;
|
||||
|
||||
private volatile AbstractDiscoveryTask discoveryTask;
|
||||
|
||||
private NSDNettyManager() {
|
||||
TelematicHandler.init();
|
||||
TelematicHandler.setListener(this);
|
||||
@@ -274,7 +277,8 @@ public class NSDNettyManager implements TelematicHandler.ITelematicListener {
|
||||
|
||||
private void scanDriverServer(String uuid, NettyClientListener listener) {
|
||||
// 启动发现司机屏幕任务
|
||||
AbstractDiscoveryTask discoveryTask = new DiscoveryDriverTask();
|
||||
cancelTask();
|
||||
discoveryTask = new DiscoveryDriverTask();
|
||||
network_ip = NetInfo.getUnsignedLongFromIp(currentIp);
|
||||
mClientListener = listener;
|
||||
|
||||
@@ -411,24 +415,28 @@ public class NSDNettyManager implements TelematicHandler.ITelematicListener {
|
||||
*/
|
||||
public void connectSpecifiedServer(String ip, int port, String uuid, NettyClientListener listener) {
|
||||
mClientListener = listener;
|
||||
if (mNsdClient != null) {
|
||||
// 停止局域网内扫描
|
||||
mNsdClient.stopServiceDiscovery();
|
||||
if (mIsTaxi) {
|
||||
if (mNsdClient != null) {
|
||||
// 停止局域网内扫描
|
||||
mNsdClient.stopServiceDiscovery();
|
||||
}
|
||||
} else {
|
||||
cancelTask();
|
||||
}
|
||||
if (mNettyTcpClient != null) {
|
||||
// 断开已连接上的
|
||||
mNettyTcpClient.disconnect();
|
||||
mNettyTcpClient = null;
|
||||
}
|
||||
TelematicHandler.removeDelay();
|
||||
TelematicHandler.removeReconnect();
|
||||
connectNettyServer(ip, port, uuid);
|
||||
}
|
||||
|
||||
public static String bytesToHexFun(byte[] bytes, int length) {
|
||||
StringBuilder buf = new StringBuilder(length * 2);
|
||||
for (int i = 0; i < length; i++) {// 使用String的format方法进行转换
|
||||
buf.append(String.format("%02x", new Integer(bytes[i] & 0xFF)));
|
||||
private void cancelTask() {
|
||||
if (discoveryTask != null && discoveryTask.getStatus() == AsyncTask.Status.RUNNING) {
|
||||
discoveryTask.cancel(true);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -455,6 +463,8 @@ public class NSDNettyManager implements TelematicHandler.ITelematicListener {
|
||||
public void reconnectServer() {
|
||||
Log.e(TAG, "收到reconnectServer事件");
|
||||
TelematicHandler.removeReconnect();
|
||||
mNettyTcpClient.reconnectServer();
|
||||
if (mNettyTcpClient != null) {
|
||||
mNettyTcpClient.reconnectServer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +158,6 @@ public class NettyTcpClient {
|
||||
ch.pipeline().addLast(new IdleStateHandler(heartBeatInterval, heartBeatInterval, 0, TimeUnit.SECONDS));
|
||||
ch.pipeline().addLast(new HeartbeatHandler(mProxyListener));
|
||||
}
|
||||
ch.pipeline().addLast(new ReconnectHandler(mProxyListener));
|
||||
// decoder
|
||||
ch.pipeline().addLast(new MogoLengthFrameDecoder(MAX_FRAME_LENGTH,
|
||||
LENGTH_FIELD_OFFSET,
|
||||
@@ -166,6 +165,7 @@ public class NettyTcpClient {
|
||||
// encoder
|
||||
ch.pipeline().addLast(new MogoMessageEncoder());
|
||||
ch.pipeline().addLast(new NettyClientHandler(mProxyListener, mSign));
|
||||
ch.pipeline().addLast(new ReconnectHandler(mProxyListener));
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -25,39 +25,6 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
|
||||
this.mSign = sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设定IdleStateHandler心跳检测每x秒进行一次读检测,
|
||||
*
|
||||
* @param ctx ChannelHandlerContext
|
||||
* @param evt IdleStateEvent
|
||||
*/
|
||||
// @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 {
|
||||
// Log.d(TAG, "不发送心跳");
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* <p>客户端上线</p>
|
||||
*
|
||||
|
||||
@@ -118,7 +118,6 @@ public class DiscoveryDriverTask extends AbstractDiscoveryTask {
|
||||
|
||||
private void launch(long i) {
|
||||
if (!mPool.isShutdown()) {
|
||||
Log.d(TAG, "launch: 子线程:" + NetInfo.getIpFromLongUnsigned(i));
|
||||
mPool.execute(new CheckRunnable(NetInfo.getIpFromLongUnsigned(i)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user