TaskNettyClient.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using DotNetty.Transport.Bootstrapping;
  2. using DotNetty.Transport.Channels;
  3. using DotNetty.Transport.Channels.Sockets;
  4. using System;
  5. using System.Net;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using DotNetty.Codecs.Protobuf;
  10. using Message = Cn.Sagacloud.Proto.Message;
  11. namespace NettyClient
  12. {
  13. public class TaskNettyClient
  14. {
  15. private string ip;
  16. private int port;
  17. IChannel clientChannel;
  18. MultithreadEventLoopGroup group;
  19. Bootstrap bootstrap;
  20. public static bool isConnected = true;
  21. public TaskNettyClient(string ip, int port)
  22. {
  23. this.ip = ip;
  24. this.port = port;
  25. }
  26. public async Task RunClientAsync(ChannelHandlerAdapter channelHandler)
  27. {
  28. group = new MultithreadEventLoopGroup();
  29. try
  30. {
  31. bootstrap = new Bootstrap();
  32. bootstrap
  33. .Group(group)
  34. .Channel<TcpSocketChannel>()
  35. .Option(ChannelOption.TcpNodelay, true)
  36. .Handler(
  37. new ActionChannelInitializer<ISocketChannel>(
  38. channel =>
  39. {
  40. //var protobufEncoder = new ProtobufEncoder();
  41. IChannelPipeline pipeline = channel.Pipeline;
  42. pipeline.AddLast(new ProtobufVarint32LengthFieldPrepender());
  43. pipeline.AddLast(new ProtobufVarint32FrameDecoder());
  44. pipeline.AddLast("encoder", new ProtobufEncoder());
  45. pipeline.AddLast("decoder", new ProtobufDecoder(Message.Parser));
  46. pipeline.AddLast("simple", channelHandler);
  47. }));
  48. clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(ip), port));
  49. //await clientChannel.CloseAsync();
  50. }
  51. catch (Exception ex)
  52. {
  53. Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace + $"ip:{ip};port{port}");
  54. }
  55. finally
  56. {
  57. //await Task.WhenAll(group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
  58. }
  59. }
  60. public async Task CloseAsync()
  61. {
  62. try
  63. {
  64. await clientChannel.CloseAsync();
  65. }
  66. catch { }
  67. finally
  68. {
  69. await Task.WhenAll(group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
  70. }
  71. }
  72. public static void Main(string[] args)
  73. {
  74. TaskNettyClient client = new TaskNettyClient("47.94.89.44", 6666);
  75. SimpleMessageHandler simpleHandler = new SimpleMessageHandler();
  76. client.RunClientAsync(simpleHandler).Wait();
  77. while (true)
  78. {
  79. Thread.Sleep(15000);
  80. //Console.WriteLine("aaaaaa");
  81. client.CloseAsync().Wait();
  82. // 重启
  83. //client.RunClientAsync(simpleHandler).Wait();
  84. }
  85. }
  86. }
  87. public static class NettyHelper
  88. {
  89. //static NettyHelper()
  90. //{
  91. // Configuration = new ConfigurationBuilder()
  92. // // .SetBasePath(ProcessDirectory)
  93. // // .AddJsonFile("appsettings.json")
  94. // .Build();
  95. //}
  96. //public static IConfigurationRoot Configuration { get; }
  97. //public static void SetConsoleLogger() => InternalLoggerFactory.DefaultFactory.AddProvider(new ConsoleLoggerProvider((s, level) => true, false));
  98. //public static void SetConsoleLogger()
  99. //{
  100. // InternalLoggerFactory.DefaultFactory.AddProvider(
  101. // new ConsoleLoggerProvider((s, level) => { level = Microsoft.Extensions.Logging.LogLevel.Error; return true; }, false)
  102. // );
  103. //}
  104. }
  105. }