Example #1
0
        static async Task RunServerAsync()
        {
            IEventLoopGroup group, workGroup;
            var             dispatcher = new DispatcherEventLoopGroup();

            group     = dispatcher;
            workGroup = new WorkerEventLoopGroup(dispatcher);

            try
            {
                var bootstrap = new ServerBootstrap()
                                .Group(group, workGroup)
                                .Channel <TcpServerChannel>()
                                .Option(ChannelOption.SoBacklog, 8192)
                                .Handler(new LoggingHandler(LogLevel.INFO))
                                .ChildHandler(new ActionChannelInitializer <IChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    pipeline.AddLast(new HttpServerCodec());
                    pipeline.AddLast(new HttpObjectAggregator(65536));
                    pipeline.AddLast(new WebSockerServerHandler());
                }));
                IChannel bootstrapChannel = await bootstrap.BindAsync(IPAddress.IPv6Any, 5004);

                Console.WriteLine($"Tcp started. Listening on {bootstrapChannel.LocalAddress}");
                Console.ReadLine();
                await bootstrapChannel.CloseAsync();
            }
            finally
            {
                workGroup.ShutdownGracefullyAsync().Wait();
                group.ShutdownGracefullyAsync().Wait();
            }
        }
Example #2
0
        static async Task RunServerAsync()
        {
            IEventLoopGroup group, workGroup;
            var             dispatcher = new DispatcherEventLoopGroup();

            group     = dispatcher;
            workGroup = new WorkerEventLoopGroup(dispatcher);

            try
            {
                var bootstrap = new ServerBootstrap()
                                .Group(group, workGroup)
                                .Channel <TcpServerChannel>()
                                .Option(ChannelOption.SoBacklog, 8192)
                                .Handler(new LoggingHandler(LogLevel.INFO))
                                .ChildHandler(new ActionChannelInitializer <IChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    pipeline.AddLast(new DelimiterBasedFrameDecoder(8192, Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes("&sup;"))));
                    pipeline.AddLast(new StringEncoder());
                    pipeline.AddLast(new StringDecoder());
                    pipeline.AddLast(new SocketServerHandler());
                }));
                IChannel bootstrapChannel = await bootstrap.BindAsync(IPAddress.IPv6Any, 5004);

                Console.WriteLine($"Tcp started. Listening on {bootstrapChannel.LocalAddress}");
                Console.ReadLine();
                await bootstrapChannel.CloseAsync();
            }
            finally
            {
                workGroup.ShutdownGracefullyAsync().Wait();
                group.ShutdownGracefullyAsync().Wait();
            }
        }
Example #3
0
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await bootstrapChannel.CloseAsync();

            var quietPeriod     = configuration.QuietPeriodTimeSpan;
            var shutdownTimeout = configuration.ShutdownTimeoutTimeSpan;
            await workerGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);

            await bossGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);
        }
Example #4
0
        private async Task ShutdownGroupAsync()
        {
            if (_bossGroup == null || _workerGroup == null)
            {
                throw new NotImplementedException("Need initialize netty server");
            }

            await Task.WhenAll(
                _bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1)),
                _workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1))
                );
        }
Example #5
0
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await bootstrapChannel.CloseAsync();

            var quietPeriod     = configuration.QuietPeriodTimeSpan;
            var shutdownTimeout = configuration.ShutdownTimeoutTimeSpan;
            await workerGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);

            await bossGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);

            foreach (var item in Provider.GetServices <IRpcClient>())
            {
                await item.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);
            }
        }
Example #6
0
        private static async Task RunServerAsync()
        {
            InternalLoggerFactory.DefaultFactory.AddProvider(new ConsoleLoggerProvider((s, level) => true, false));

            var dispatcher  = new DispatcherEventLoopGroup();
            var bossGroup   = dispatcher;
            var workerGroup = new WorkerEventLoopGroup(dispatcher);

            var bootstrap = new ServerBootstrap();

            try
            {
                bootstrap
                .Group(bossGroup, workerGroup)
                .Channel <TcpServerChannel>()
                .Option(ChannelOption.SoBacklog, 100)
                .Handler(new LoggingHandler("SRV-LSTN"))
                .ChildHandler(new ActionChannelInitializer <IChannel>(channel =>
                {
                    var pipeline = channel.Pipeline;

                    pipeline.AddLast(new LoggingHandler("SRV-CONN"));
                    pipeline.AddLast("framing-enc", new LengthFieldPrepender(2));
                    pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
                    //pipeline.AddLast("mqtt-encoder", new MqttEncoder());
                    //pipeline.AddLast("mqtt-decoder", new MqttDecoder(true, 256 * 1024));
                    //pipeline.AddLast("string-encoder", new StringEncoder());
                    //pipeline.AddLast("string-decoder", new StringDecoder());
                    pipeline.AddLast("request-encoder", new RequestEncoder());
                    pipeline.AddLast("request-decoder", new RequestDecoder());
                    pipeline.AddLast("echo", new EchoServerHandler());
                }));

                var boundChannel = await bootstrap.BindAsync(9998);

                Console.ReadLine();

                await boundChannel.CloseAsync();
            }
            finally
            {
                await Task.WhenAll(
                    bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1)),
                    workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1))
                    );
            }
        }
Example #7
0
        static async Task RunServerAsync()
        {
            IEventLoopGroup bossGroup;
            IEventLoopGroup workerGroup;

            var dispatcher = new DispatcherEventLoopGroup();

            bossGroup   = dispatcher;
            workerGroup = new WorkerEventLoopGroup(dispatcher);


            try
            {
                var bootstrap = new ServerBootstrap();
                bootstrap.Group(bossGroup, workerGroup);
                bootstrap.Channel <TcpServerChannel>();

                bootstrap
                .Option(ChannelOption.SoBacklog, 100)
                .Handler(new LoggingHandler("SRV-LSTN"))
                .ChildHandler(new ActionChannelInitializer <IChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    pipeline.AddLast(new LoggingHandler("SRV-CONN"));
                    pipeline.AddLast("framing-enc", new LengthFieldPrepender(2));
                    pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));

                    pipeline.AddLast("echo", new SocketServerHandler());
                }));

                IChannel boundChannel = await bootstrap.BindAsync(port);



                Console.ReadLine();

                await boundChannel.CloseAsync();
            }
            finally
            {
                await Task.WhenAll(
                    bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)),
                    workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
            }
        }
Example #8
0
        static async Task RunServerAsync()
        {
            ExampleHelper.SetConsoleLogger();

            // 申明一个主回路调度组
            var dispatcher = new DispatcherEventLoopGroup();

            /*
             * Netty 提供了许多不同的 EventLoopGroup 的实现用来处理不同的传输。
             * 在这个例子中我们实现了一个服务端的应用,因此会有2个 NioEventLoopGroup 会被使用。
             * 第一个经常被叫做‘boss’,用来接收进来的连接。第二个经常被叫做‘worker’,用来处理已经被接收的连接,一旦‘boss’接收到连接,就会把连接信息注册到‘worker’上。
             * 如何知道多少个线程已经被使用,如何映射到已经创建的 Channel上都需要依赖于 IEventLoopGroup 的实现,并且可以通过构造函数来配置他们的关系。
             */

            // 主工作线程组,设置为1个线程
            IEventLoopGroup bossGroup = dispatcher; // (1)
            // 子工作线程组,设置为1个线程
            IEventLoopGroup workerGroup = new WorkerEventLoopGroup(dispatcher);

            try
            {
                // 声明一个服务端Bootstrap,每个Netty服务端程序,都由ServerBootstrap控制,通过链式的方式组装需要的参数
                var serverBootstrap = new ServerBootstrap(); // (2)
                // 设置主和工作线程组
                serverBootstrap.Group(bossGroup, workerGroup);

                if (ServerSettings.UseLibuv)
                {
                    // 申明服务端通信通道为TcpServerChannel
                    serverBootstrap.Channel <TcpServerChannel>(); // (3)
                }

                serverBootstrap
                // 设置网络IO参数等
                .Option(ChannelOption.SoBacklog, 100)     // (5)

                // 在主线程组上设置一个打印日志的处理器
                .Handler(new LoggingHandler("SRV-LSTN"))

                // 设置工作线程参数
                .ChildHandler(

                    /*
                     * ChannelInitializer 是一个特殊的处理类,他的目的是帮助使用者配置一个新的 Channel。
                     * 也许你想通过增加一些处理类比如DiscardServerHandler 来配置一个新的 Channel 或者其对应的ChannelPipeline 来实现你的网络程序。
                     * 当你的程序变的复杂时,可能你会增加更多的处理类到 pipline 上,然后提取这些匿名类到最顶层的类上。
                     */
                    new ActionChannelInitializer <IChannel>(    // (4)
                        channel =>
                {
                    /*
                     * 工作线程连接器是设置了一个管道,服务端主线程所有接收到的信息都会通过这个管道一层层往下传输,
                     * 同时所有出栈的消息 也要这个管道的所有处理器进行一步步处理。
                     */
                    IChannelPipeline pipeline = channel.Pipeline;

                    // 添加日志拦截器
                    pipeline.AddLast(new LoggingHandler("SRV-CONN"));

                    // 添加出栈消息,通过这个handler在消息顶部加上消息的长度。
                    // LengthFieldPrepender(2):使用2个字节来存储数据的长度。
                    pipeline.AddLast("framing-enc", new LengthFieldPrepender(2));

                    /*
                     * 入栈消息通过该Handler,解析消息的包长信息,并将正确的消息体发送给下一个处理Handler
                     * 1,InitialBytesToStrip = 0,       //读取时需要跳过的字节数
                     * 2,LengthAdjustment = -5,         //包实际长度的纠正,如果包长包括包头和包体,则要减去Length之前的部分
                     * 3,LengthFieldLength = 4,         //长度字段的字节数 整型为4个字节
                     * 4,LengthFieldOffset = 1,         //长度属性的起始(偏移)位
                     * 5,MaxFrameLength = int.MaxValue, //最大包长
                     */
                    pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));

                    // 业务handler
                    pipeline.AddLast("echo", new EchoServerHandler());
                }));

                // bootstrap绑定到指定端口的行为就是服务端启动服务,同样的Serverbootstrap可以bind到多个端口
                IChannel boundChannel = await serverBootstrap.BindAsync(ServerSettings.Port); // (6)

                Console.WriteLine("wait the client input");
                Console.ReadLine();

                // 关闭服务
                await boundChannel.CloseAsync();
            }
            finally
            {
                // 释放指定工作组线程
                await Task.WhenAll( // (7)
                    bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)),
                    workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1))
                    );
            }
        }
Example #9
0
 protected override Task StopAsync(TimeSpan quietPeriod, TimeSpan timeout)
 {
     return(Task.WhenAll(
                bossGroup.ShutdownGracefullyAsync(quietPeriod, timeout),
                workerGroup.ShutdownGracefullyAsync(quietPeriod, timeout)));
 }
Example #10
0
        static async Task RunServerAsync()
        {
            // ExampleHelper.SetConsoleLogger();

            IEventLoopGroup bossGroup;
            IEventLoopGroup workerGroup;

            //if (ServerSettings.UseLibuv)
            {
                var dispatcher = new DispatcherEventLoopGroup();
                bossGroup   = dispatcher;
                workerGroup = new WorkerEventLoopGroup(dispatcher);
            }
            //else
            //{
            //    bossGroup = new MultithreadEventLoopGroup(1);
            //    workerGroup = new MultithreadEventLoopGroup();
            //}

            //X509Certificate2 tlsCertificate = null;
            //if (ServerSettings.IsSsl)
            //{
            //    tlsCertificate = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
            //}
            try
            {
                var bootstrap = new ServerBootstrap();
                bootstrap.Group(bossGroup, workerGroup);

                //if (ServerSettings.UseLibuv)
                {
                    bootstrap.Channel <TcpServerChannel>();
                }
                //else
                //{
                //    bootstrap.Channel<TcpServerSocketChannel>();
                //}

                bootstrap
                .Option(ChannelOption.SoBacklog, 100)
                .Handler(new LoggingHandler("SRV-LSTN"))
                .ChildHandler(new ActionChannelInitializer <IChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    //if (tlsCertificate != null)
                    //{
                    //    pipeline.AddLast("tls", TlsHandler.Server(tlsCertificate));
                    //}
                    pipeline.AddLast(new LoggingHandler("SRV-CONN"));
                    pipeline.AddLast("framing-enc", new LengthFieldPrepender(2));
                    pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));

                    pipeline.AddLast("echo", new EchoServerHandler());
                }));

                IChannel boundChannel = await bootstrap.BindAsync(8007);

                Console.ReadLine();

                await boundChannel.CloseAsync();
            }
            finally
            {
                await Task.WhenAll(
                    bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)),
                    workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
            }
        }