writeAndFlush 이후 Event가 Decoder에서 다시 읽기를 하고 있습니다. 문제가 뭘까요?

80 views
Skip to first unread message

김정태

unread,
Sep 2, 2022, 9:50:25 AM9/2/22
to Netty Korean User Group
//Handler 등록
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
     LOGGER.info("initChannel ===========>>");
     ChannelPipeline pipeline = socketChannel.pipeline();
     pipeline.addFirst(new HostChannelDecoder());
     pipeline.addLast(new HostChannelHandler());
}

//Decoder Handler
public class HostChannelDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
int iReadLen;
try {
  LOGGER.info("decode Event ===========>>{}", in.readableBytes());
  while (in.readableBytes() >= KBComHeader.LENGTH) {
    LOGGER.info("----------------------------------[{}]", ctx.channel().toString());
    LOGGER.debug("[(Readable:{} | R:{}-W{}) H:{}]", in.readableBytes(),  in.readerIndex(), in.writerIndex(), TypeHelper.byteArrayToHexString(readBytes));
   if (in.readableBytes() >= (iReadLen = Header.parsePacketLength(in))) {
     LOGGER.debug("[Read]:length[{}] > readadbleBytes[{}]", iReadLen, in.readableBytes());
     byte[] readBytes = new byte[iReadLen];
     in.readBytes(readBytes);
     LOGGER.debug("[Read-Data]:{}", new String(readBytes));
     HostAgentManage agentManage = HostSessionManage.MakeAgent(ctx, readBytes);
     out.add(agentManage);
     LOGGER.debug("[decode]:out.add >>>>>>>>>>>>>>{}", agentManage.toString());
    }
  }
} catch (Exception e) {
LOGGER.error("Error:{}", e.getMessage());
}
}

//Inbound Handler-Class
@Component
@ChannelHandler.Sharable
public class HostChannelHandler extends SimpleChannelInboundHandler<HostAgentManage> {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
  // TODO Auto-generated method stub
  LOGGER.debug("channelActive Event ===========>>");
  HostSessionManage.RegisterAgent(ctx);
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, HostAgentManage agent) throws Exception {
  LOGGER.debug("channelRead0 Event ===========>>{}", agent.toString());

  if (agent.getInBytes()==null)
    LOGGER.error("Host-Read Event [agent.getInBytes()==null]");
    byte function = agent.getHeaderFunction();
    LOGGER.debug("Host-Read Event ===========>> function:{}", function);
    switch (function){
    case Header.FUNC_STATUS:{
       agent.ResponseStatus(); //전송할 Data가공 후 Send
       break;
    }
    default: {
       LOGGER.debug("channelRead0 remoteAddress:{}",  ctx.channel().remoteAddress().toString());
    }
    break;
  }
}
}

//전송하는 함수 HostAgentManage.send() 함수
public void send() throws Exception
{
    LOGGER.debug("Host send [{}] ===========>>", outBytes.length);
    hostChannelCtx.writeAndFlush(Unpooled.copiedBuffer(outBytes));
}



실행 후 로그 현황입니다.
[2022-09-02 20:00:15.723] [INFO ] [nioEventLoopGroup-2-1] s.u.s.n.HostChannelInitializer initChannel ===========>>
[2022-09-02 20:00:15.733] [DEBUG] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelHandler channelActive Event  ===========>>
[2022-09-02 20:00:15.750] [DEBUG] [nioEventLoopGroup-2-1] s.u.agent.HostSessionManage RegisterAgent >> HandleID[6c2e3e7a]
[2022-09-02 20:00:15.750] [DEBUG] [nioEventLoopGroup-2-1] s.u.agent.HostSessionManage RegisterAgent >> HOST Connect[1] ========================>>>>>>>>>>>>>>>>>>>>
[2022-09-02 20:00:15.756] [DEBUG] [nioEventLoopGroup-2-1] io.netty.util.Recycler -Dio.netty.recycler.maxCapacityPerThread: 4096
[2022-09-02 20:00:15.756] [DEBUG] [nioEventLoopGroup-2-1] io.netty.util.Recycler -Dio.netty.recycler.ratio: 8
[2022-09-02 20:00:15.756] [DEBUG] [nioEventLoopGroup-2-1] io.netty.util.Recycler -Dio.netty.recycler.chunkSize: 32
[2022-09-02 20:00:15.756] [DEBUG] [nioEventLoopGroup-2-1] io.netty.util.Recycler -Dio.netty.recycler.blocking: false
[2022-09-02 20:00:15.764] [DEBUG] [nioEventLoopGroup-2-1] i.netty.buffer.AbstractByteBuf -Dio.netty.buffer.checkAccessible: true
[2022-09-02 20:00:15.764] [DEBUG] [nioEventLoopGroup-2-1] i.netty.buffer.AbstractByteBuf -Dio.netty.buffer.checkBounds: true
[2022-09-02 20:00:15.765] [DEBUG] [nioEventLoopGroup-2-1] i.n.u.ResourceLeakDetectorFactory Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@4a12e58a
[2022-09-02 20:00:15.775] [INFO ] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder decode Event  ===========>>46
[2022-09-02 20:00:15.776] [INFO ] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder ----------------------------------[[id: 0x6c2e3e7a, L:/192.168.2.71:28580 - R:/10.200.2.112:8400]]
[2022-09-02 20:00:15.779] [DEBUG] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder [(Readable:46 | R:0-W46) H:303030343153544b3132fa01415554483031202020303020202020202020]
Header:parsePacketLength(ByteBuf inBuffer) >>>>>>>>>
[2022-09-02 20:00:15.779] [DEBUG] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder [Read]:length[46] > readadbleBytes[46]
[2022-09-02 20:00:15.779] [DEBUG] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder [Read-Data]:00041STK12�AUTH01   00       ST    2000158800
Header:setPacketData() >>>>>>>>>00041STK12�AUTH01   00
[2022-09-02 20:00:15.780] [DEBUG] [nioEventLoopGroup-2-1] s.u.agent.HostSessionManage MakeAgent >> Get - HandleID[6c2e3e7a]
[2022-09-02 20:00:15.780] [DEBUG] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder [decode]:out.add >>>>>>>>>>>>>>HandleID[null]
[2022-09-02 20:00:15.781] [DEBUG] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelHandler channelRead0 Event  ===========>>HandleID[null]
[2022-09-02 20:00:15.781] [DEBUG] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelHandler Host-Read Event  ===========>>  function:83
[2022-09-02 20:00:15.781] [DEBUG] [nioEventLoopGroup-2-1] s.u.agent.HostAgentManage ResponseStatus ===========>>
[2022-09-02 20:00:15.885] [DEBUG] [nioEventLoopGroup-2-1] s.u.agent.HostAgentManage flushCom : packetLength:[108]

@@ 정상적으로 전송한 로그
[2022-09-02 20:00:15.885] [DEBUG] [nioEventLoopGroup-2-1] s.u.agent.HostAgentManage Host send [108]  ===========>> 

@@다시 Decoder에서 Byte를 읽고 있음 ㅠㅠ;;;
[2022-09-02 20:00:15.996] [INFO ] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder decode Event  ===========>>108
[2022-09-02 20:00:15.996] [INFO ] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder ----------------------------------[[id: 0x6c2e3e7a, L:/192.168.2.71:28580 - R:/10.200.2.112:8400]]
[2022-09-02 20:00:15.996] [DEBUG] [nioEventLoopGroup-2-1] s.u.s.netty.HostChannelDecoder [(Readable:108 | R:0-W108) H:303031303356544b3132fa01494e56543031202020303020202020202020]


전송한 108 Byte를 다시 decoder에서 읽고 있습니다.
뭐가 문제인지 정말 모르겠네요. ㅠㅠ;;;;

고수님들의 조언 부탁드립니다.

Reply all
Reply to author
Forward
0 new messages