TaskNettyClient.cs 3.9 KB

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