NettyClient.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.mes.netty;
  2. import io.netty.bootstrap.Bootstrap;
  3. import io.netty.channel.*;
  4. import io.netty.channel.nio.NioEventLoopGroup;
  5. import io.netty.channel.socket.SocketChannel;
  6. import io.netty.channel.socket.nio.NioSocketChannel;
  7. import io.netty.handler.codec.string.StringEncoder;
  8. import java.util.concurrent.TimeUnit;
  9. import com.mes.ui.MesClient;
  10. public class NettyClient {
  11. public SocketChannel socketChannel;
  12. public ChannelFuture future;
  13. private static EventLoopGroup group;
  14. private static Bootstrap bootstrap;
  15. private volatile boolean connecting = false;
  16. private void ensureBootstrap() {
  17. if (group != null && !group.isShutdown()) {
  18. return;
  19. }
  20. group = new NioEventLoopGroup();
  21. bootstrap = new Bootstrap();
  22. bootstrap.group(group)
  23. .channel(NioSocketChannel.class)
  24. .option(ChannelOption.TCP_NODELAY, true)
  25. .option(ChannelOption.SO_KEEPALIVE, true)
  26. .handler(new ChannelInitializer<SocketChannel>() {
  27. @Override
  28. protected void initChannel(SocketChannel ch) {
  29. ch.pipeline()
  30. .addLast(new XDecoder())
  31. .addLast(new StringEncoder())
  32. .addLast(new NettyClientHandler());
  33. }
  34. })
  35. .remoteAddress(MesClient.mes_server_ip, MesClient.mes_tcp_port);
  36. }
  37. public void resetConnection() {
  38. connecting = false;
  39. future = null;
  40. socketChannel = null;
  41. }
  42. public boolean isChannelActive() {
  43. return future != null && future.channel() != null && future.channel().isActive();
  44. }
  45. public void run(Object msg) {
  46. if (isChannelActive()) {
  47. future.channel().writeAndFlush(msg);
  48. return;
  49. }
  50. if (connecting) {
  51. if (group != null && !group.isShutdown()) {
  52. group.schedule(() -> run(msg), 300, TimeUnit.MILLISECONDS);
  53. }
  54. return;
  55. }
  56. connecting = true;
  57. ensureBootstrap();
  58. System.out.println("客户端正在连接服务端...");
  59. future = bootstrap.connect();
  60. future.addListener((ChannelFutureListener) future1 -> {
  61. connecting = false;
  62. if (future1.isSuccess()) {
  63. MesClient.tcp_connect_flag = true;
  64. MesClient.connect_request_flag = false;
  65. System.out.println("连接Netty服务端成功");
  66. future1.channel().writeAndFlush(msg);
  67. } else {
  68. MesClient.tcp_connect_flag = false;
  69. MesClient.connect_request_flag = true;
  70. System.out.println("连接失败,进行断线重连");
  71. if (future1.channel() != null) {
  72. future1.channel().eventLoop().schedule(() -> run(msg), 10, TimeUnit.SECONDS);
  73. } else if (group != null) {
  74. group.schedule(() -> run(msg), 10, TimeUnit.SECONDS);
  75. }
  76. }
  77. MesClient.setTcpStatus();
  78. });
  79. socketChannel = (SocketChannel) future.channel();
  80. }
  81. }