package com.fzzy.protocol.youxian0.client;
|
|
import com.fzzy.api.utils.BytesUtil;
|
import com.fzzy.api.utils.SpringUtil;
|
import com.fzzy.protocol.youxian0.analysis.AnalysisService;
|
import com.fzzy.protocol.youxian0.service.Youxian0GatewayGrainService;
|
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.net.InetSocketAddress;
|
|
|
@Slf4j
|
public class ClientHandler extends SimpleChannelInboundHandler<Object> {
|
|
private AnalysisService analysisService;
|
|
@Override
|
public void channelActive(ChannelHandlerContext ctx) {
|
InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel()
|
.remoteAddress();
|
|
log.info("连接终成功,IP={},port={}", socketAddress.getAddress()
|
.getHostAddress(), socketAddress.getPort());
|
}
|
|
@Override
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
super.channelInactive(ctx);
|
|
InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
log.info("连接终端掉线,IP={},port={}", socketAddress.getAddress(), socketAddress.getPort());
|
//ClientEngine.defaultChannel = null;
|
|
}
|
|
@Override
|
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
|
byte[] bytes = (byte[]) msg;
|
|
InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
String strMsg = BytesUtil.bytesToString(bytes);
|
|
log.info("终端返回信息,IP={},port={},msg={}", socketAddress.getAddress(), socketAddress.getPort(), strMsg);
|
|
if (null == analysisService) {
|
analysisService = SpringUtil.getBean(AnalysisService.class);
|
}
|
|
try{
|
analysisService.analysis(socketAddress.getAddress(), socketAddress.getPort(), strMsg);
|
}catch (Exception e){
|
log.error(e.getMessage(),e);
|
}
|
|
}
|
|
@Override
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
ctx.close();
|
}
|
|
}
|