CenterTcpServerIdleStateHandler.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.its.vds.xnettcp.center.handler;
  2. import com.its.app.utils.NettyUtils;
  3. import io.netty.channel.Channel;
  4. import io.netty.channel.ChannelHandler;
  5. import io.netty.channel.ChannelHandlerContext;
  6. import io.netty.handler.timeout.IdleStateHandler;
  7. import lombok.extern.slf4j.Slf4j;
  8. @Slf4j
  9. @ChannelHandler.Sharable
  10. public class CenterTcpServerIdleStateHandler extends IdleStateHandler {
  11. public CenterTcpServerIdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) {
  12. super(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds);
  13. }
  14. @Override
  15. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  16. String ipAddress = NettyUtils.getRemoteIpAddress(ctx.channel());
  17. int port = NettyUtils.getRemotePort(ctx.channel());
  18. log.info("Connect: {}", NettyUtils.getRemoteAddress(ctx.channel()));
  19. }
  20. /*
  21. * 클라이언트와의 연결 종료 처리
  22. */
  23. @Override
  24. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  25. String ipAddress = NettyUtils.getRemoteIpAddress(ctx.channel());
  26. int port = NettyUtils.getRemotePort(ctx.channel());
  27. log.info("Disconnect: {}", NettyUtils.getRemoteAddress(ctx.channel()));
  28. super.channelInactive(ctx);
  29. }
  30. /*
  31. * Remote 에서 연결을 강제로 종료하면 이벤트 발생
  32. */
  33. @Override
  34. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  35. String ipAddress = NettyUtils.getRemoteIpAddress(ctx.channel());
  36. int port = NettyUtils.getRemotePort(ctx.channel());
  37. log.info("exceptionCaught: {}", NettyUtils.getRemoteAddress(ctx.channel()));
  38. super.exceptionCaught(ctx, cause);
  39. }
  40. /*
  41. * 클라이언트와의 연결 종료를 위해 호출하는 함수
  42. */
  43. public static void disconnectChannel(Channel channel) {
  44. String ipAddress = NettyUtils.getRemoteIpAddress(channel);
  45. int port = NettyUtils.getRemotePort(channel);
  46. log.info("disconnectChannel: {}", NettyUtils.getRemoteAddress(channel));
  47. }
  48. }