断链问题,防止客户端将设备误杀
This commit is contained in:
@@ -49,10 +49,15 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
|
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
|
||||||
String clientIp = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getClientIp();
|
String clientIp = "no-ip";
|
||||||
logger.info("接收服务器响应msg:[" + msg + "]");
|
|
||||||
try {
|
try {
|
||||||
|
clientIp = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getClientIp();
|
||||||
|
logger.info("接收服务器响应msg:[" + msg + "]");
|
||||||
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
|
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
|
||||||
|
if (message == null || message.getMessageType() == null) {
|
||||||
|
logger.warn("收到无法解析的消息,忽略。来源IP:[" + clientIp + "], 内容:[" + msg + "]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
switch (message.getMessageType()) {
|
switch (message.getMessageType()) {
|
||||||
case HB:
|
case HB:
|
||||||
HeartBeat hb = JsonUtils.decode(message.getMessage(),HeartBeat.class);
|
HeartBeat hb = JsonUtils.decode(message.getMessage(),HeartBeat.class);
|
||||||
@@ -104,12 +109,15 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) {
|
public void channelInactive(ChannelHandlerContext ctx) {
|
||||||
|
ChannelParam param = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get();
|
||||||
|
if (param != null && param.getVenueId() != null) {
|
||||||
String clientIP = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
|
String clientIP = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
|
||||||
logger.error("Client ip [" + clientIP + "] has inactive");
|
logger.error("Client ip [" + clientIP + "] has inactive");
|
||||||
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
||||||
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
||||||
messageService.Offline(deviceName,venueId);
|
messageService.Offline(deviceName,venueId);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理异常、一般需要关闭通道
|
* 处理异常、一般需要关闭通道
|
||||||
@@ -121,11 +129,12 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||||
logger.error("ServerHandler exceptionCaught",cause);
|
logger.error("ServerHandler exceptionCaught",cause);
|
||||||
Channel channel = ctx.channel();
|
Channel channel = ctx.channel();
|
||||||
|
ChannelParam param = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get();
|
||||||
// Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
// Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
||||||
// String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
// String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
||||||
// DeviceType deviceType = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceType();
|
// DeviceType deviceType = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceType();
|
||||||
// messageService.Offline(deviceName,venueId,deviceType);
|
// messageService.Offline(deviceName,venueId,deviceType);
|
||||||
if(channel.isActive()) {
|
if(channel.isActive() && param != null && param.getVenueId() != null) {
|
||||||
// 错误产生,关闭连接
|
// 错误产生,关闭连接
|
||||||
ctx.close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
@@ -141,12 +150,16 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
IdleState state = ((IdleStateEvent) evt).state();
|
IdleState state = ((IdleStateEvent) evt).state();
|
||||||
if (state == IdleState.READER_IDLE){
|
if (state == IdleState.READER_IDLE){
|
||||||
logger.info("IdleStateEvent READER_IDLE 超时");
|
logger.info("IdleStateEvent READER_IDLE 超时");
|
||||||
|
ChannelParam param = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get();
|
||||||
|
if (param != null && param.getVenueId() != null) {
|
||||||
|
// 原有逻辑
|
||||||
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId();
|
||||||
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName();
|
||||||
messageService.Offline(deviceName,venueId);
|
messageService.Offline(deviceName,venueId);
|
||||||
ctx.channel().close();
|
ctx.channel().close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Set<String> connections = messageService.countConnection();
|
Set<String> connections = messageService.countConnection();
|
||||||
logger.info("count connected device ! the count is " + connections.size() + " and they are + [" + connections.toString() + "]" );
|
logger.info("count connected device ! the count is " + connections.size() + " and they are + [" + connections.toString() + "]" );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -416,7 +416,7 @@ public class ServerMessageHandlerAdapter implements MessageService {
|
|||||||
* 判断是否有该通道
|
* 判断是否有该通道
|
||||||
*/
|
*/
|
||||||
public boolean contains(String deviceName, Integer venueId) {
|
public boolean contains(String deviceName, Integer venueId) {
|
||||||
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId + NettyConstant.SPIT_WORD;
|
String clientId = deviceName + NettyConstant.SPIT_WORD + venueId;
|
||||||
return links.containsKey(clientId);
|
return links.containsKey(clientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ public class OldDoorService implements DoorService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void WatchEvent(ConnectorDetial connectorDetial, INData inData) {
|
public void WatchEvent(ConnectorDetial connectorDetial, INData inData) {
|
||||||
logger.error("WatchEvent open door ...");
|
logger.info("WatchEvent open door ...");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.sv.netty;
|
|||||||
import com.sv.intergration.MessageService;
|
import com.sv.intergration.MessageService;
|
||||||
import com.sv.netty.config.VenueMessage;
|
import com.sv.netty.config.VenueMessage;
|
||||||
import com.sv.netty.utils.JsonUtils;
|
import com.sv.netty.utils.JsonUtils;
|
||||||
import io.netty.channel.Channel;
|
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
@@ -53,8 +52,16 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
@Override
|
@Override
|
||||||
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
|
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
|
||||||
logger.info("get server msg:[" + msg + "]");
|
logger.info("get server msg:[" + msg + "]");
|
||||||
|
try {
|
||||||
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
|
VenueMessage message = JsonUtils.decode(msg, VenueMessage.class);
|
||||||
|
if (message == null || message.getMessageType() == null) {
|
||||||
|
logger.error("收到无法解析的消息,忽略: " + msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
MessageService.getInstance().execute(message);
|
MessageService.getInstance().execute(message);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("处理服务器消息异常,不影响连接: " + msg, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,7 +73,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws UnknownHostException {
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws UnknownHostException {
|
||||||
if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
|
if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
|
||||||
IdleStateEvent event = (IdleStateEvent) evt;
|
IdleStateEvent event = (IdleStateEvent) evt;
|
||||||
if (event.state() == IdleState.ALL_IDLE) {
|
if (event.state() == IdleState.WRITER_IDLE) {
|
||||||
ctx.write(MessageHandler.sendHeartBeat());
|
ctx.write(MessageHandler.sendHeartBeat());
|
||||||
ctx.flush();
|
ctx.flush();
|
||||||
}
|
}
|
||||||
@@ -81,12 +88,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||||
logger.info("ClientHandler exceptionCaught");
|
logger.error("ClientHandler exceptionCaught, 保持连接不关闭", cause);
|
||||||
cause.printStackTrace();
|
|
||||||
Channel channel = ctx.channel();
|
|
||||||
if(channel.isActive()) {
|
|
||||||
ctx.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user