TaskNettyClient.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 = NettyClient.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 TaskNettyClient(string ip, int port)
  21. {
  22. this.ip = ip;
  23. this.port = port;
  24. }
  25. public async Task RunClientAsync(ChannelHandlerAdapter channelHandler)
  26. {
  27. group = new MultithreadEventLoopGroup();
  28. try
  29. {
  30. bootstrap = new Bootstrap();
  31. bootstrap
  32. .Group(group)
  33. .Channel<TcpSocketChannel>()
  34. .Option(ChannelOption.TcpNodelay, true)
  35. .Handler(
  36. new ActionChannelInitializer<ISocketChannel>(
  37. channel =>
  38. {
  39. //var protobufEncoder = new ProtobufEncoder();
  40. IChannelPipeline pipeline = channel.Pipeline;
  41. pipeline.AddLast(new ProtobufVarint32LengthFieldPrepender());
  42. pipeline.AddLast(new ProtobufVarint32FrameDecoder());
  43. pipeline.AddLast("encoder", new ProtobufEncoder());
  44. pipeline.AddLast("decoder", new ProtobufDecoder(Message.Parser));
  45. pipeline.AddLast("simple", channelHandler);
  46. }));
  47. clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(ip), port));
  48. //await clientChannel.CloseAsync();
  49. }
  50. catch (Exception ex)
  51. {
  52. MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace + $"ip:{ip};port{port}");
  53. }
  54. finally
  55. {
  56. //await Task.WhenAll(group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
  57. }
  58. }
  59. public async Task CloseAsync()
  60. {
  61. try
  62. {
  63. await clientChannel.CloseAsync();
  64. }
  65. catch { }
  66. finally
  67. {
  68. await Task.WhenAll(group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
  69. }
  70. }
  71. public static void Main(string[] args)
  72. {
  73. TaskNettyClient client = new TaskNettyClient("127.0.0.1", 6666);
  74. SimpleMessageHandler simpleHandler = new SimpleMessageHandler();
  75. client.RunClientAsync(simpleHandler).Wait();
  76. while (true)
  77. {
  78. Thread.Sleep(15000);
  79. client.CloseAsync().Wait();
  80. // 重启
  81. //client.RunClientAsync(new SimpleHandler()).Wait();
  82. }
  83. }
  84. }
  85. public static class NettyHelper
  86. {
  87. //static NettyHelper()
  88. //{
  89. // Configuration = new ConfigurationBuilder()
  90. // // .SetBasePath(ProcessDirectory)
  91. // // .AddJsonFile("appsettings.json")
  92. // .Build();
  93. //}
  94. //public static IConfigurationRoot Configuration { get; }
  95. //public static void SetConsoleLogger() => InternalLoggerFactory.DefaultFactory.AddProvider(new ConsoleLoggerProvider((s, level) => true, false));
  96. //public static void SetConsoleLogger()
  97. //{
  98. // InternalLoggerFactory.DefaultFactory.AddProvider(
  99. // new ConsoleLoggerProvider((s, level) => { level = Microsoft.Extensions.Logging.LogLevel.Error; return true; }, false)
  100. // );
  101. //}
  102. }
  103. }