NettyUtils.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.tsi.app.common.xnet;
  2. import io.netty.channel.Channel;
  3. import io.netty.channel.EventLoopGroup;
  4. import io.netty.channel.epoll.Epoll;
  5. import io.netty.channel.epoll.EpollEventLoopGroup;
  6. import io.netty.channel.epoll.EpollServerSocketChannel;
  7. import io.netty.channel.epoll.EpollSocketChannel;
  8. import io.netty.channel.nio.NioEventLoopGroup;
  9. import io.netty.channel.socket.ServerSocketChannel;
  10. import io.netty.channel.socket.SocketChannel;
  11. import io.netty.channel.socket.nio.NioServerSocketChannel;
  12. import io.netty.channel.socket.nio.NioSocketChannel;
  13. import io.netty.util.concurrent.DefaultThreadFactory;
  14. import java.net.InetSocketAddress;
  15. public final class NettyUtils {
  16. private NettyUtils() {}
  17. public static String getAddress(Channel ch) {
  18. String localIp = "local-unknown";
  19. String remoteIp = "remote-unknown";
  20. int localPort = 0;
  21. int remotePort = 0;
  22. InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress();
  23. if (localAddr != null) {
  24. localIp = localAddr.getAddress().getHostAddress();
  25. localPort = localAddr.getPort();
  26. }
  27. InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress();
  28. if (remoteAddr != null) {
  29. remoteIp = remoteAddr.getAddress().getHostAddress();
  30. remotePort = remoteAddr.getPort();
  31. }
  32. return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]";
  33. }
  34. public static String getRemoteAddress(Channel ch) {
  35. String ip = getRemoteIpAddress(ch);
  36. int port = getRemotePort(ch);
  37. return "[Remote #(" + ip + ":" + port + ")]";
  38. }
  39. public static String getLocalAddress(Channel ch) {
  40. String ip = getLocalIpAddress(ch);
  41. int port = getLocalPort(ch);
  42. return "[Local #(" + ip + ":" + port + ")]";
  43. }
  44. public static String getRemoteIpAddress(Channel ch) {
  45. String ip = "255.255.255.255";
  46. InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
  47. if (inetAddr != null) {
  48. ip = inetAddr.getAddress().getHostAddress();
  49. }
  50. return ip;
  51. }
  52. public static long getRemoteIpAddressToLong(Channel ch) {
  53. String[] ipAddressInArray = getRemoteIpAddress(ch).split("\\.");
  54. long result = 0;
  55. for (int i = 0; i < ipAddressInArray.length; i++) {
  56. int power = 3 - i;
  57. int ip = Integer.parseInt(ipAddressInArray[i]);
  58. result += ip * Math.pow(256, power);
  59. }
  60. return result;
  61. }
  62. public static int getRemotePort(Channel ch) {
  63. int port = 0;
  64. InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
  65. if (inetAddr != null) {
  66. port = inetAddr.getPort();
  67. }
  68. return port;
  69. }
  70. public static String getLocalIpAddress(Channel ch) {
  71. String ip = "127.0.0.1";
  72. InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
  73. if (inetAddr != null) {
  74. ip = inetAddr.getAddress().getHostAddress();
  75. }
  76. return ip;
  77. }
  78. public static int getLocalPort(Channel ch) {
  79. int port = 0;
  80. InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
  81. if (inetAddr != null) {
  82. port = inetAddr.getPort();
  83. }
  84. return port;
  85. }
  86. public static boolean isEpollAvailable() {
  87. // Netty epoll transport does not work with WSL (Windows Sybsystem for Linux) yet.
  88. /*
  89. boolean HAS_WSLENV = System.getenv("WSLENV") != null;
  90. return Epoll.isAvailable() && !HAS_WSLENV;
  91. */
  92. return Epoll.isAvailable();
  93. }
  94. public static EventLoopGroup newEventLoopGroup(int nThreads, String threadPoolName) {
  95. if (isEpollAvailable()) {
  96. if (threadPoolName.equals("")) {
  97. return new EpollEventLoopGroup(nThreads);
  98. }
  99. else {
  100. return new EpollEventLoopGroup(nThreads, new DefaultThreadFactory("epo"+threadPoolName));
  101. }
  102. } else {
  103. if (threadPoolName.equals("")) {
  104. return new NioEventLoopGroup(nThreads);
  105. }
  106. else {
  107. return new NioEventLoopGroup(nThreads, new DefaultThreadFactory("nio" + threadPoolName));
  108. }
  109. }
  110. }
  111. public static Class<? extends SocketChannel> getSocketChannel() {
  112. if (isEpollAvailable()) {
  113. return EpollSocketChannel.class;
  114. } else {
  115. return NioSocketChannel.class;
  116. }
  117. }
  118. public static Class<? extends ServerSocketChannel> getServerSocketChannel() {
  119. if (isEpollAvailable()) {
  120. return EpollServerSocketChannel.class;
  121. } else {
  122. return NioServerSocketChannel.class;
  123. }
  124. }
  125. }