[Update]添加Server端的业务身份注册到连接的Client端
This commit is contained in:
@@ -109,6 +109,11 @@ public class NSDNettyManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void startNSDNettyServerWithSN(Context context, NettyServerListener listener, String sn) {
|
||||
NettyTcpServer.sSERVER_SN = sn;
|
||||
startNSDNettyServer(context, listener);
|
||||
}
|
||||
|
||||
public boolean isServerStart() {
|
||||
return NettyTcpServer.getInstance().isServerStart();
|
||||
}
|
||||
@@ -244,6 +249,11 @@ public class NSDNettyManager {
|
||||
}
|
||||
}
|
||||
|
||||
public String getServerSn() {
|
||||
String sn = NettyTcpClient.sSERVER_SN;
|
||||
return sn;
|
||||
}
|
||||
|
||||
public void sendMogoProtocolMsgToServer(MogoProtocolMsg mogoProtocolMsg, final MessageStateListener listener) {
|
||||
if (mNettyTcpClient != null) {
|
||||
mNettyTcpClient.sendMsgToServer(mogoProtocolMsg, listener);
|
||||
|
||||
@@ -36,6 +36,10 @@ import io.netty.handler.timeout.IdleStateHandler;
|
||||
*/
|
||||
public class NettyTcpClient {
|
||||
private static final String TAG = "NettyTcpClient";
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public volatile static String sSERVER_SN = "";
|
||||
|
||||
private Bootstrap bootstrap;
|
||||
|
||||
@@ -59,7 +63,6 @@ public class NettyTcpClient {
|
||||
private boolean isConnecting = false;
|
||||
|
||||
private long reconnectIntervalTime = 5000;
|
||||
private static final Integer CONNECT_TIMEOUT_MILLIS = 5000;
|
||||
|
||||
private String host;
|
||||
private int tcp_port;
|
||||
@@ -224,6 +227,9 @@ public class NettyTcpClient {
|
||||
|
||||
@Override
|
||||
public void onClientStatusConnectChanged(int statusCode, String sign) {
|
||||
if (listener != null) {
|
||||
listener.onClientStatusConnectChanged(statusCode, sign);
|
||||
}
|
||||
if (statusCode == ConnectState.STATUS_CONNECT_SUCCESS && !TextUtils.isEmpty(mSign)) {
|
||||
byte[] signByteArr = mSign.getBytes();
|
||||
MogoProtocolMsg msg = new MogoProtocolMsg(IDENTITY_REGIST, signByteArr.length, signByteArr);
|
||||
@@ -231,9 +237,6 @@ public class NettyTcpClient {
|
||||
Log.d(TAG, "身份注册成功:" + isSuccess);
|
||||
});
|
||||
}
|
||||
if (listener != null) {
|
||||
listener.onClientStatusConnectChanged(statusCode, sign);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.mogo.telematic.client.handler;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.telematic.MogoProtocolMsg;
|
||||
import com.mogo.telematic.client.NettyTcpClient;
|
||||
import com.mogo.telematic.client.listener.NettyClientListener;
|
||||
import com.mogo.telematic.client.status.ConnectState;
|
||||
|
||||
@@ -94,7 +95,14 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, MogoProtocolMsg msg) throws Exception {
|
||||
Log.d(TAG, "Client channelRead0:" + msg.toString());
|
||||
listener.onMessageResponseClient(msg, mSign);
|
||||
if (msg.getProtocolType() == MogoProtocolMsg.IDENTITY_REGIST) {
|
||||
String sn = new String(msg.getBody());
|
||||
NettyTcpClient.sSERVER_SN = sn;
|
||||
} else {
|
||||
if (listener != null) {
|
||||
listener.onMessageResponseClient(msg, mSign);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.mogo.telematic.server.netty;
|
||||
|
||||
import static com.mogo.telematic.MogoProtocolMsg.IDENTITY_REGIST;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.telematic.MogoProtocolMsg;
|
||||
@@ -18,22 +21,19 @@ public class MogoProtocolServerHandler extends SimpleChannelInboundHandler<MogoP
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
||||
// System.out.println("channelReadComplete");
|
||||
}
|
||||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx,
|
||||
Throwable cause) {
|
||||
cause.printStackTrace(); //5
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
//6
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, MogoProtocolMsg msg) throws Exception {
|
||||
Log.d(TAG, "server$channelRead0():" + msg.toString());
|
||||
if (msg.getProtocolType() == MogoProtocolMsg.IDENTITY_REGIST) {
|
||||
if (msg.getProtocolType() == IDENTITY_REGIST) {
|
||||
String sn = new String(msg.getBody());
|
||||
NettyTcpServer.getInstance().putChannelSN(ctx.channel().id(), sn);
|
||||
} else {
|
||||
@@ -55,6 +55,18 @@ public class MogoProtocolServerHandler extends SimpleChannelInboundHandler<MogoP
|
||||
if (mListener != null) {
|
||||
mListener.onChannelConnect(ctx.channel());
|
||||
}
|
||||
String sn = NettyTcpServer.sSERVER_SN;
|
||||
if (!TextUtils.isEmpty(sn)) {
|
||||
byte[] signByteArr = sn.getBytes();
|
||||
MogoProtocolMsg msg = new MogoProtocolMsg(IDENTITY_REGIST, signByteArr.length, signByteArr);
|
||||
NettyTcpServer.getInstance().sendMsgToSpecifiedClient(msg, ctx.channel(), future -> {
|
||||
if (future.isSuccess()) {
|
||||
Log.d(TAG, "Netty Server端注册身份成功!");
|
||||
} else {
|
||||
Log.d(TAG, "Netty Server端注册身份不成功!");
|
||||
}
|
||||
});
|
||||
}
|
||||
// NettyTcpServer.getInstance().setConnectStatus(true);
|
||||
}
|
||||
|
||||
@@ -62,6 +74,7 @@ public class MogoProtocolServerHandler extends SimpleChannelInboundHandler<MogoP
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
Log.e(TAG, "channelInactive");
|
||||
// NettyTcpServer.getInstance().setConnectStatus(false);
|
||||
NettyTcpServer.getInstance().removeChannelSN(ctx.channel().id());
|
||||
if (mListener != null) {
|
||||
mListener.onChannelDisConnect(ctx.channel());
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ public class NettyTcpServer {
|
||||
// client端用来过滤的
|
||||
public static final String SERVER_NAME = "NSD_SERVER";
|
||||
public static final int SERVER_PORT = 1088;
|
||||
public volatile static String sSERVER_SN = "";
|
||||
|
||||
private Channel selectChannel;
|
||||
private CopyOnWriteArrayList<Channel> channelList = new CopyOnWriteArrayList<>();
|
||||
@@ -146,6 +147,14 @@ public class NettyTcpServer {
|
||||
return flag;
|
||||
}
|
||||
|
||||
public boolean sendMsgToSpecifiedClient(MogoProtocolMsg mogoProtocolMsg, Channel channel, ChannelFutureListener listener) {
|
||||
boolean flag = channel != null && channel.isActive();
|
||||
if (flag) {
|
||||
channel.writeAndFlush(mogoProtocolMsg).addListener(listener);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void sendMsgToAllClients(MogoProtocolMsg mogoProtocolMsg) {
|
||||
for (Channel chl : channelList) {
|
||||
if (chl != null && chl.isActive()) {
|
||||
|
||||
Reference in New Issue
Block a user