Program.cs 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DotNetty.Handlers.Timeout;
  7. using DotNetty.Transport.Bootstrapping;
  8. using DotNetty.Transport.Channels;
  9. using DotNetty.Transport.Channels.Sockets;
  10. using NotNettyServer;
  11. namespace TestServer
  12. {
  13. class Program
  14. {
  15. private static int Port = 4001;
  16. static void Main(string[] args)
  17. {
  18. Console.WriteLine("Server");
  19. RunServerAsync().Wait();
  20. }
  21. static async Task RunServerAsync()
  22. {
  23. var bossGroup = new MultithreadEventLoopGroup(1);
  24. var workerGroup = new MultithreadEventLoopGroup();
  25. try
  26. {
  27. var bootstrap = new ServerBootstrap();
  28. bootstrap
  29. .Group(bossGroup, workerGroup)
  30. .Channel<TcpServerSocketChannel>()
  31. .Option(ChannelOption.SoBacklog, 1024)
  32. .ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel =>
  33. {
  34. IChannelPipeline pipeline = channel.Pipeline;
  35. // pipeline.AddLast("framing-enc", new LengthFieldPrepender(10));
  36. // pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 10, 2, 1, 0));
  37. pipeline.AddLast("idle", new IdleStateHandler(10, 20, 15));
  38. pipeline.AddLast("echo", new ServerHandler());
  39. }));
  40. IChannel boundChannel = await bootstrap.BindAsync(Port);
  41. Console.ReadLine();
  42. await boundChannel.CloseAsync();
  43. }
  44. finally
  45. {
  46. await Task.WhenAll(
  47. bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)),
  48. workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
  49. }
  50. }
  51. }
  52. }