コード例 #1
0
ファイル: ServerBootstrap.cs プロジェクト: ywscr/Uragano
        public async Task StartAsync()
        {
            try
            {
                var bootstrap = new DotNetty.Transport.Bootstrapping.ServerBootstrap();
                if (UraganoOptions.DotNetty_Enable_Libuv.Value)
                {
                    var dispatcher = new DispatcherEventLoopGroup();
                    BossGroup   = dispatcher;
                    WorkerGroup = new WorkerEventLoopGroup(dispatcher);
                    bootstrap.Channel <TcpServerChannel>();
                }
                else
                {
                    BossGroup   = new MultithreadEventLoopGroup(1);
                    WorkerGroup = new MultithreadEventLoopGroup(UraganoOptions.DotNetty_Event_Loop_Count.Value);
                    bootstrap.Channel <TcpServerSocketChannel>();
                }

                bootstrap
                .Group(BossGroup, WorkerGroup)
                .Option(ChannelOption.SoBacklog, UraganoOptions.Server_DotNetty_Channel_SoBacklog.Value)
                .ChildOption(ChannelOption.Allocator, PooledByteBufferAllocator.Default)
                .ChildOption(ChannelOption.ConnectTimeout, UraganoOptions.DotNetty_Connect_Timeout.Value)
                .ChildHandler(new ActionChannelInitializer <IChannel>(channel =>
                {
                    var pipeline = channel.Pipeline;
                    if (ServerSettings.X509Certificate2 != null)
                    {
                        pipeline.AddLast(TlsHandler.Server(ServerSettings.X509Certificate2));
                    }

                    //pipeline.AddLast(new LoggingHandler("SRV-CONN"));
                    pipeline.AddLast(new LengthFieldPrepender(4));
                    pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4));
                    pipeline.AddLast(new MessageDecoder <IInvokeMessage>(Codec));
                    pipeline.AddLast(new MessageEncoder <IServiceResult>(Codec));
                    pipeline.AddLast(new ServerMessageHandler(ServiceFactory, ServiceProvider, Logger, Codec, ServerSettings));
                }));

                EndPoint endPoint;
                if (IPAddress.TryParse(ServerSettings.Address, out var iPAddress))
                {
                    endPoint = new IPEndPoint(iPAddress, ServerSettings.Port);
                }
                else
                {
                    endPoint = new DnsEndPoint(ServerSettings.Address, ServerSettings.Port);
                }
                Channel = await bootstrap.BindAsync(endPoint);

                Logger.LogInformation($"Uragano server listening {ServerSettings}");
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Start server[{ServerSettings}] failed!");
            }
        }
コード例 #2
0
        static async Task RunServer()
        {
            //定义一个NIO的Selecter,这里叫住Group。全局控制。
            var bossGroup   = new DotNetty.Transport.Channels.MultithreadEventLoopGroup(1);
            var workerGroup = new DotNetty.Transport.Channels.MultithreadEventLoopGroup();

            try
            {
                var bootstrap = new DotNetty.Transport.Bootstrapping.ServerBootstrap();//服务启动器
                bootstrap.Group(bossGroup, workerGroup);
                bootstrap.Channel <DotNetty.Transport.Channels.Sockets.TcpServerSocketChannel>();
                bootstrap.Option(DotNetty.Transport.Channels.ChannelOption.SoBacklog, 100);//接受的客户端最大连接数
                bootstrap.ChildHandler(new DotNetty.Transport.Channels.ActionChannelInitializer <DotNetty.Transport.Channels.Sockets.ISocketChannel>(channel =>
                {
                    DotNetty.Transport.Channels.IChannelPipeline pipeline = channel.Pipeline;
                    pipeline.AddLast(new MessageHandler());
                }));

                var bootstrapChannel = await bootstrap.BindAsync(8007);

                Console.WriteLine("Service has been runing ....  press Enter to stop. enput message boardcast...");
                while (true)
                {
                    var line = Console.ReadLine();
                    if (line == "exit")
                    {
                        await bootstrapChannel.CloseAsync();

                        break;
                    }
                    MessageHandler.ClientList.ForEach(ch => ch.WriteAndFlushAsync(DotNetty.Buffers.Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(line))));
                }
            }
            finally
            {
                Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
            }
        }