Beispiel #1
0
        protected override void CreateEventLoop(int eventLoopCount)
        {
            var dispatcher = new DispatcherEventLoop();

            bossGroup   = new MultithreadEventLoopGroup(_ => dispatcher, 1);
            workerGroup = new WorkerEventLoopGroup(dispatcher, eventLoopCount);
        }
Beispiel #2
0
        public void SetUp(BenchmarkContext context)
        {
            TaskScheduler.UnobservedTaskException += (sender, args) => Console.WriteLine(args.Exception);

            var dispatcher = new DispatcherEventLoop();

            this.serverGroup = new MultithreadEventLoopGroup(_ => dispatcher, 1);
            this.workerGroup = new WorkerEventLoopGroup(dispatcher);
            this.clientGroup = new MultithreadEventLoopGroup(_ => new EventLoop(), 1);

            this.message = Encoding.UTF8.GetBytes("ABC");

            this.inboundThroughputCounter  = context.GetCounter(InboundThroughputCounterName);
            this.outboundThroughputCounter = context.GetCounter(OutboundThroughputCounterName);
            var counterHandler = new CounterHandlerInbound(this.inboundThroughputCounter);

            this.signal = new ManualResetEventSlimReadFinishedSignal(this.resetEvent);

            // reserve up to 10mb of 16kb buffers on both client and server; we're only sending about 700k worth of messages
            this.serverBufferAllocator = new PooledByteBufferAllocator();
            this.clientBufferAllocator = new PooledByteBufferAllocator();

            ServerBootstrap sb = new ServerBootstrap()
                                 .Group(this.serverGroup, this.workerGroup)
                                 .Channel <TcpServerChannel>()
                                 .ChildOption(ChannelOption.Allocator, this.serverBufferAllocator)
                                 .ChildHandler(new ActionChannelInitializer <TcpChannel>(channel =>
            {
                channel.Pipeline
                .AddLast(GetEncoder())
                .AddLast(GetDecoder())
                .AddLast(counterHandler)
                .AddLast(new ReadFinishedHandler(this.signal, WriteCount));
            }));

            Bootstrap cb = new Bootstrap()
                           .Group(this.clientGroup)
                           .Channel <TcpChannel>()
                           .Option(ChannelOption.Allocator, this.clientBufferAllocator)
                           .Handler(new ActionChannelInitializer <TcpChannel>(
                                        channel =>
            {
                channel.Pipeline
                .AddLast(GetEncoder())
                .AddLast(GetDecoder())
                .AddLast(new CounterHandlerOutbound(this.outboundThroughputCounter));
            }));

            // start server
            this.serverChannel = sb.BindAsync(TestAddress).Result;

            // connect to server
            this.clientChannel = cb.ConnectAsync(this.serverChannel.LocalAddress).Result;
        }
Beispiel #3
0
        static async Task RunServerAsync()
        {
            ExampleHelper.SetConsoleLogger();

            IEventLoopGroup bossGroup;
            IEventLoopGroup workerGroup;

            if (ServerSettings.UseLibuv)
            {
                var dispatcher = new DispatcherEventLoop();
                bossGroup   = new MultithreadEventLoopGroup(_ => dispatcher, 1);
                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(ServerSettings.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)));
            }
        }