示例#1
0
        public async Task RunEngineAsync(int port, IBasicHandler basicHandler, ICustomizeHandler handler)
        {
            bossGroup   = new MultithreadEventLoopGroup(1);
            workerGroup = new MultithreadEventLoopGroup();
            MessageDispatcher                 dispatcher   = new MessageDispatcher(this.BasicController, basicHandler, handler);
            List <ChannelServerHandler>       handlers     = new List <ChannelServerHandler>();
            RingObject <ChannelServerHandler> ringHandlers = null;

            if (!useDefaultThread)//此处相比默认handler增加了工作线程,默认工作线程为cpu核心*2
            {
                for (int i = 0; i < workThreadCount; i++)
                {
                    WorkerEngine <RequestInfo> workerEngine = new WorkerEngine <RequestInfo>(3000, 1, dispatcher.Process);
                    workerEngine.Start();
                    handlers.Add(new ChannelServerHandler(this.userManager, workerEngine));
                }
                ringHandlers = new RingObject <ChannelServerHandler>(handlers);
            }
            else
            {
                var cChannelHandlers = new List <ChannelServerHandler>()
                {
                    new ChannelServerHandler(this.userManager, dispatcher)
                };
                ringHandlers = new RingObject <ChannelServerHandler>(cChannelHandlers);
            }
            var bootstrap = new ServerBootstrap();

            bootstrap
            .Group(bossGroup, workerGroup)
            .Channel <TcpServerSocketChannel>()
            //.Option(ChannelOption.SoBacklog, 100)
            //.Handler(new LoggingHandler(LogLevel.INFO))
            .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
            {
                IChannelPipeline pipeline = channel.Pipeline;
                pipeline.AddLast(OFrameDecoder.NewOFrameDecoder());
                pipeline.AddLast(ringHandlers.GetNext());
            }));

            IChannel bootstrapChannel = await bootstrap.BindAsync(port);
        }
示例#2
0
        public async Task RunEngineAsync(string ip, int port, ICustomizeHandler handler)
        {
            this.channelClientHandler = new ChannelClientHandler(handler);
            this.BasicOutter          = new BasicOutter(channelClientHandler.MessageBus);
            var group     = new MultithreadEventLoopGroup();
            var bootstrap = new Bootstrap();

            bootstrap
            .Group(group)
            .Channel <TcpSocketChannel>()
            .Option(ChannelOption.TcpNodelay, true)
            .Handler(new ActionChannelInitializer <ISocketChannel>(channel =>
            {
                IChannelPipeline pipeline = channel.Pipeline;
                //pipeline.AddLast(new LoggingHandler());
                pipeline.AddLast(OFrameDecoder.NewOFrameDecoder());
                pipeline.AddLast(this.channelClientHandler);
                pipeline.AddLast();
            }));

            IChannel clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(ip), port));
        }