| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package com.its.cctv.xnettcp.cctvserver.codec;
- import com.its.app.utils.NettyUtils;
- import com.its.cctv.config.RunningConfig;
- import com.its.cctv.entity.TbCctvCtlr;
- import com.its.cctv.global.AppRepository;
- import com.its.cctv.xnettcp.cctvserver.handler.CctvServerIdleStatePacketHandler;
- import io.netty.buffer.ByteBuf;
- import io.netty.channel.Channel;
- import io.netty.channel.ChannelHandler;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.handler.codec.MessageToByteEncoder;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.slf4j.MDC;
- import org.springframework.stereotype.Component;
- @Slf4j
- @RequiredArgsConstructor
- @Component
- @ChannelHandler.Sharable
- public class CctvTcpServerEncoder extends MessageToByteEncoder<Object> {
- private final RunningConfig runningConfig;
- @Override
- protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf byteBuf) throws Exception {
- String ipAddress = NettyUtils.getRemoteIpAddress(ctx.channel());
- Channel channel = ctx.channel();
- TbCctvCtlr obj = AppRepository.getInstance().getCtlrIpMap().get(ipAddress);
- if (obj == null) {
- log.error("DsrcTcpServerEncoder.encode: Unknown Controller IP: {}. will be close.", ipAddress);
- CctvServerIdleStatePacketHandler.disconnectChannel(null, channel);
- return;
- }
- MDC.put("id", obj.getLogKey());
- if (obj.getChannel() != null && (channel != obj.getChannel())) {
- log.error("DsrcTcpServerEncoder.encode: {}, channel error, curr: {}, old: {}", ipAddress, channel.toString(), obj.getChannel().toString());
- CctvServerIdleStatePacketHandler.disconnectChannel(obj, channel);
- return;
- }
- if (!channel.isOpen() || !channel.isActive()) {
- log.error("DsrcTcpServerEncoder.encode: {}, isOpen: {}, isActive: {}. [{}]", ipAddress, channel.isOpen(), channel.isActive(), obj.getLogKey());
- CctvServerIdleStatePacketHandler.disconnectChannel(obj, channel);
- return;
- }
- }
- }
|