public async Task Test()
        {
            int heartbeatIntervalSecond = 60;

            InternalLoggerFactory.DefaultFactory = _loggerFactory;//.AddConsoleLogger();

            //实际要考虑事件循环的共用,事件循环的关闭问题
            MultithreadEventLoopGroup workerGroup = new MultithreadEventLoopGroup(8);

            workerGroup.Start();

            var bootstrap = new ClientBootstrap();

            bootstrap
            .Group(workerGroup)
            .Config(ConfigConstant.HeartbeatIntervalSecond, heartbeatIntervalSecond)
            .Config(ConfigConstant.ConnectTimeoutSecond, 10)
            .Channel <TcpSocketChannel>()
            .WorkerHandler(channel =>
            {
                //channel.Pipeline.AddLast("MessageDecoder", new MessageDecoder());
                channel.Pipeline.AddLast("MessageBaseDecoder", new LengthFieldBasedFrameDecoder(int.MaxValue, 4, 4));
                channel.Pipeline.AddLast("MessageDecoder", new MessageDecoder());

                channel.Pipeline.AddLast("MessageEncoder", new MessageEncoder());
                channel.Pipeline.AddLast("IdleStateHandler", new IdleStateHandler(0, 0, heartbeatIntervalSecond));
                channel.Pipeline.AddLast("ClientHeartbeatHandler", new ClientHeartbeatHandler());
                channel.Pipeline.AddLast("ServerHandler", new ClientHandler());

                //便于理解 这里都是addlist 入站是从上外下执行的,出站是从下往上执行的
            });

            //这里创建连接 如果连接失败 会抛异常。如果连接成功以后,连接再断开会触发ChannelInactive 事件
            //可以管理连接封装起来,首次连接失败 通过捕获异常进行重试,连接后再断开在ChannelInactive 事件中 进行连接重试
            var client = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(ip), port));

            await Test(bootstrap);
        }
Exemple #2
0
        public async Task Test()
        {
            InternalLoggerFactory.DefaultFactory = _loggerFactory;//.AddConsoleLogger();

            BossGroup   = new MultithreadEventLoopGroup(1);
            WorkerGroup = new MultithreadEventLoopGroup();
            BossGroup.Start();
            WorkerGroup.Start();

            var bootstrap = new ServerBootstrap();

            bootstrap
            .Group(BossGroup, WorkerGroup)
            .Config(ConfigConstant.Backlog, 10240)
            .Config(ConfigConstant.ConnectTimeoutSecond, 10)
            .Config(ConfigConstant.HeartbeatIntervalSecond, heartbeatIntervalSecond)
            .Config(ConfigConstant.BufferSize, 256)
            .Channel <TcpServerSocketChannel>()
            .BossHandler(channel =>
            {
                channel.Pipeline.AddLast("BossHandler", new BossHandler());
            })
            .WorkerHandler(channel =>
            {
                // channel.Pipeline.AddLast("MessageDecoder", new TestDecoder());
                channel.Pipeline.AddLast("MessageBaseDecoder", new LengthFieldBasedFrameDecoder(int.MaxValue, 4, 4));
                channel.Pipeline.AddLast("MessageDecoder", new MessageDecoder());


                channel.Pipeline.AddLast("MessageEncoder", new MessageEncoder());
                channel.Pipeline.AddLast("IdleStateHandler", new IdleStateHandler(0, 0, heartbeatIntervalSecond + 5));
                channel.Pipeline.AddLast("ServerHeartbeatHandler", new ServerHeartbeatHandler());
                //channel.Pipeline.AddLast("AuthHandler", new AuthHandler());
                channel.Pipeline.AddLast("ServerHandler", new ServerHandler());
            });
            var ip = "127.0.0.1";
            //ip = "192.168.111.133";
            int port = 8007;

            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
            ServerChannel = await bootstrap.BindAsync(new IPEndPoint(IPAddress.Parse(ip), port));

            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
            //IChannel serverChannel = new TcpServerSocketChannel();

            ////初始化handler
            //Action<IChannel> child = (channel) =>
            //{

            //    channel.Pipeline.AddLast("MessageDecoder", new MessageDecoder());
            //    channel.Pipeline.AddLast("MessageDecoder", new MessageEncoder());
            //    channel.Pipeline.AddLast("ServerHandler", new ServerHandler());
            //};

            ////服务端监听socket的channelhandler
            //serverChannel.Pipeline.AddLast("ServerBootstrapAcceptor", new ServerBootstrapAcceptor(workerGroup, child));
            ////注册事件循环
            //await ((IChannelUnsafe)serverChannel).UnsafeRegisterAsync(bossGroup.GetNext());

            //var ip = "127.0.0.1";
            //int port = 8007;
            //await serverChannel.BindAsync(new IPEndPoint(IPAddress.Parse(ip), port));
        }