Пример #1
0
        public async Task Listen(int port)
        {
            var bossGroup   = new MultithreadEventLoopGroup(1);
            var workerGroup = new MultithreadEventLoopGroup();
            //try
            //{
            var bootstrap = new ServerBootstrap();

            bootstrap
            .Group(bossGroup, workerGroup)
            .Channel <TcpServerSocketChannel>()
            .Option(ChannelOption.SoBacklog, 100)
            .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
            {
                IChannelPipeline pipeline = channel.Pipeline;
                pipeline.AddLast(new LengthFieldPrepender(4));
                pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4));

                ServerMessageHandler handler = new ServerMessageHandler();
                handler.Handle += new RpcDefaultRequestHandler().Handle;
                pipeline.AddLast(handler);
            }));

            IChannel boundChannel = await bootstrap.BindAsync(port);

            //}
            //finally
            //{
            //    await bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            //    await workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            //}
        }
Пример #2
0
        public async Task Listen(int port)
        {
            BossGroup   = new MultithreadEventLoopGroup(1);
            WorkerGroup = new MultithreadEventLoopGroup();
            var bootstrap = new ServerBootstrap();

            bootstrap
            .Group(BossGroup, WorkerGroup)
            .Channel <TcpServerSocketChannel>()
            .Option(ChannelOption.SoBacklog, 100)
            .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
            {
                IChannelPipeline pipeline = channel.Pipeline;
                pipeline.AddLast(new LengthFieldPrepender(4));
                pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4));

                ServerMessageHandler handler = new ServerMessageHandler();
                handler.Handle += new RpcDefaultRequestHandler().Handle;
                pipeline.AddLast(handler);
            }));

            BoundChannel = await bootstrap.BindAsync(port);
        }