[Update]支持连接指定ip的server,支持client向server请求车牌号
This commit is contained in:
@@ -11,6 +11,7 @@ public class MogoProtocolMsg {
|
||||
public static final int IDENTITY_REGIST = 2;
|
||||
// 同步美化模式状态
|
||||
public static final int SYNC_MODE_STATUS = 3;
|
||||
public static final int REQ_CAR_NUMBER = 4;
|
||||
|
||||
private int protocolType;
|
||||
private int bodyLength;
|
||||
|
||||
@@ -263,6 +263,23 @@ public class NSDNettyManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接指定ip服务端
|
||||
* @param ip
|
||||
*/
|
||||
public void connectSpecifiedServer(String ip, int port, String uuid, NettyClientListener listener) {
|
||||
if (mNsdClient != null) {
|
||||
// 停止局域网内扫描
|
||||
mNsdClient.stopServiceDiscovery();
|
||||
}
|
||||
if (mNettyTcpClient != null && (mNettyTcpClient.isConnecting() || mNettyTcpClient.getConnectStatus())) {
|
||||
// 断开已连接上的
|
||||
mNettyTcpClient.disconnect();
|
||||
mNettyTcpClient = null;
|
||||
}
|
||||
connectNettyServer(ip, port, uuid, listener);
|
||||
}
|
||||
|
||||
public static String bytesToHexFun(byte[] bytes, int length) {
|
||||
StringBuilder buf = new StringBuilder(length * 2);
|
||||
for (int i = 0; i < length; i++) {// 使用String的format方法进行转换
|
||||
|
||||
@@ -219,16 +219,16 @@ public class NettyTcpClient {
|
||||
|
||||
private NettyClientListener mProxyListener = new NettyClientListener() {
|
||||
@Override
|
||||
public void onMessageResponseClient(Object msg, String sign) {
|
||||
public void onMessageResponseClient(Object msg, String sign, Channel channel) {
|
||||
if (listener != null) {
|
||||
listener.onMessageResponseClient(msg, sign);
|
||||
listener.onMessageResponseClient(msg, sign, channel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClientStatusConnectChanged(int statusCode, String sign) {
|
||||
public void onClientStatusConnectChanged(int statusCode, String sign, Channel channel) {
|
||||
if (listener != null) {
|
||||
listener.onClientStatusConnectChanged(statusCode, sign);
|
||||
listener.onClientStatusConnectChanged(statusCode, sign, channel);
|
||||
}
|
||||
if (statusCode == ConnectState.STATUS_CONNECT_SUCCESS && !TextUtils.isEmpty(mSign)) {
|
||||
byte[] signByteArr = mSign.getBytes();
|
||||
|
||||
@@ -26,7 +26,7 @@ public class NsdClient {
|
||||
private final Context mContext;
|
||||
private final String mServiceName;
|
||||
private final IServerFound mIServerFound;
|
||||
|
||||
private boolean isRegisterDiscovery;
|
||||
/**
|
||||
* 用来存储解析后的网络对象列表,包含完整数据
|
||||
*/
|
||||
@@ -57,6 +57,7 @@ public class NsdClient {
|
||||
mNsdManager = (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);
|
||||
initializeDiscoveryListener();
|
||||
mNsdManager.discoverServices(NSD_SERVER_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
|
||||
isRegisterDiscovery = true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,13 +68,13 @@ public class NsdClient {
|
||||
mDiscoveryListener = new NsdManager.DiscoveryListener() {
|
||||
@Override
|
||||
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
|
||||
mNsdManager.stopServiceDiscovery(this);
|
||||
isRegisterDiscovery = false;
|
||||
Logger.e(TAG, "onStartDiscoveryFailed()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
|
||||
mNsdManager.stopServiceDiscovery(this);
|
||||
isRegisterDiscovery = false;
|
||||
Logger.e(TAG, "onStopDiscoveryFailed()");
|
||||
}
|
||||
|
||||
@@ -85,6 +86,7 @@ public class NsdClient {
|
||||
@Override
|
||||
public void onDiscoveryStopped(String serviceType) {
|
||||
Logger.e(TAG, "onDiscoveryStopped()");
|
||||
isRegisterDiscovery = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,10 +144,12 @@ public class NsdClient {
|
||||
@Override
|
||||
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
||||
Logger.e(TAG, "onResolveFailed()");
|
||||
isRegisterDiscovery = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceResolved(NsdServiceInfo serviceInfo) {
|
||||
isRegisterDiscovery = false;
|
||||
int port = serviceInfo.getPort();
|
||||
InetAddress host = serviceInfo.getHost();
|
||||
String serviceName = serviceInfo.getServiceName();
|
||||
@@ -165,7 +169,11 @@ public class NsdClient {
|
||||
|
||||
|
||||
public void stopServiceDiscovery() {
|
||||
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
|
||||
// 必须要加isRegisterDiscovery判断,
|
||||
// 否则停止扫描的时候会去mListenerMap查找是否存在,不存在的话会崩溃
|
||||
if (mNsdManager != null && isRegisterDiscovery) {
|
||||
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IServerFound {
|
||||
|
||||
@@ -72,7 +72,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) {
|
||||
Logger.d(TAG, "channelActive");
|
||||
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_SUCCESS, mSign);
|
||||
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_SUCCESS, mSign, ctx.channel());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +99,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
|
||||
NettyTcpClient.sSERVER_SN = sn;
|
||||
} else {
|
||||
if (listener != null) {
|
||||
listener.onMessageResponseClient(msg, mSign);
|
||||
listener.onMessageResponseClient(msg, mSign, ctx.channel());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,7 +111,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
Logger.e(TAG, "NettyClientHandler#exceptionCaught is:" + cause.getMessage());
|
||||
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_ERROR, mSign);
|
||||
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_ERROR, mSign, ctx.channel());
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.mogo.telematic.client.listener;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
/**
|
||||
* TCP状态变化监听
|
||||
*/
|
||||
@@ -11,12 +13,12 @@ public interface NettyClientListener<T> {
|
||||
* @param msg 消息
|
||||
* @param sign tcp 客户端的标识,因为一个应用程序可能有很多个长链接
|
||||
*/
|
||||
void onMessageResponseClient(T msg, String sign);
|
||||
void onMessageResponseClient(T msg, String sign, Channel channel);
|
||||
|
||||
/**
|
||||
* 当服务状态发生变化时触发
|
||||
* @param statusCode 状态变化
|
||||
* @param sign tcp 客户端的标识,因为一个应用程序可能有很多个长链接
|
||||
*/
|
||||
void onClientStatusConnectChanged(int statusCode, String sign);
|
||||
void onClientStatusConnectChanged(int statusCode, String sign, Channel channel);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user