예제 #1
0
파일: Program.cs 프로젝트: softchar/HiPush
        private static void CreateClient()
        {
            var channelConfig = new ChannelConfig()
            {
                AutoReceiving          = true,
                PenddingMessageCounter = 102400,
                ReceivingBufferSize    = 1024 * 64,
                SendingBufferSize      = 1024 * 64
            };


            var workGroup = new MutlEventloopGroup(1);

            var bootstrap = new ClientBootstrap();

            bootstrap
            .Group(workGroup)
            .Channel <TcpClientChannel>()
            .Config(channelConfig)
            .Pipeline(pipeline =>
            {
                pipeline.AddLast("Tls", new TlsHandler());
                pipeline.AddLast("Enc", new LengthMessageEncoder());
                pipeline.AddLast("Dec", new LengthMessageDecoder());
                pipeline.AddLast("MyChannelHandler", new MyChannelHandler());
            });
            bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse("192.168.1.103"), 46456));
        }
예제 #2
0
        public void LocalChannel_WriteAsync_should_fail_fast_on_closed_channel()
        {
            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();

            cb.Group(_group1).Channel <LocalChannel>().Handler(new TestHandler());

            sb.Group(_group2).Channel <LocalServerChannel>().ChildHandler(new ActionChannelInitializer <LocalChannel>(
                                                                              channel => { channel.Pipeline.AddLast(new TestHandler()); }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;
                var latch = new CountdownEvent(1);

                // Connect to the server
                cc = cb.ConnectAsync(sc.LocalAddress).Result;

                // Close the channel and write something
                cc.CloseAsync().Wait();
                var ag =
                    Assert.Throws <AggregateException>(() => { cc.WriteAndFlushAsync(new object()).Wait(); }).Flatten();
                Assert.IsType <ClosedChannelException>(ag.InnerException);
            }
            finally
            {
                CloseChannel(cc);
                CloseChannel(sc);
            }
        }
예제 #3
0
        public void TcpSocketChannel_can_connect_to_TcpServerSocketChannel()
        {
            IEventLoopGroup group1 = new MultithreadEventLoopGroup(2);
            IEventLoopGroup group2 = new MultithreadEventLoopGroup(2);

            var cb         = new ClientBootstrap();
            var sb         = new ServerBootstrap();
            var reads      = 100;
            var resetEvent = new ManualResetEventSlim();

            cb.Group(group1)
            .Channel <TcpSocketChannel>()
            .Handler(
                new ActionChannelInitializer <TcpSocketChannel>(
                    channel =>
            {
                channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                .AddLast(new LengthFieldPrepender(4, false))
                .AddLast(new IntCodec())
                .AddLast(new TestHandler());
            }));

            sb.Group(group2)
            .Channel <TcpServerSocketChannel>()
            .ChildHandler(
                new ActionChannelInitializer <TcpSocketChannel>(
                    channel =>
            {
                channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                .AddLast(new LengthFieldPrepender(4, false))
                .AddLast(new IntCodec())
                .AddLast(new ReadCountAwaiter(resetEvent, reads))
                .AddLast(new TestHandler());
            }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;

                // Connect to the server
                cc = cb.ConnectAsync(sc.LocalAddress).Result;

                foreach (var read in Enumerable.Range(0, reads))
                {
                    cc.WriteAndFlushAsync(read);
                }
                Assert.True(resetEvent.Wait(15000));
            }
            finally
            {
                CloseChannel(cc);
                CloseChannel(sc);
                Task.WaitAll(group1.ShutdownGracefullyAsync(), group2.ShutdownGracefullyAsync());
            }
        }
예제 #4
0
        public void LocalChannel_write_when_WritePromiseComplete_should_still_preserve_order()
        {
            var cb           = new ClientBootstrap();
            var sb           = new ServerBootstrap();
            var messageLatch = new CountdownEvent(2);
            var data1        = Unpooled.WrappedBuffer(new byte[1024]);
            var data2        = Unpooled.WrappedBuffer(new byte[512]);

            try
            {
                cb.Group(_group1)
                .Channel <LocalChannel>()
                .Handler(new TestHandler());

                sb.Group(_group2)
                .Channel <LocalServerChannel>()
                .ChildHandler(new ReadCountdown2(messageLatch, data1, data2));

                IChannel sc = null;
                IChannel cc = null;

                try
                {
                    // Start server
                    sc = sb.BindAsync(TEST_ADDRESS).Result;

                    // Connect to server
                    cc = cb.ConnectAsync(sc.LocalAddress).Result;

                    var ccCpy = cc;

                    // Make sure a write operation is executed in the eventloop
                    cc.Pipeline.LastContext().Executor.Execute(() =>
                    {
                        Logger.Info("Writing message 1");
                        ccCpy.WriteAndFlushAsync(data1.Duplicate().Retain()).ContinueWith(tr =>
                        {
                            Logger.Info("Writing message 2");
                            ccCpy.WriteAndFlushAsync(data2.Duplicate().Retain());
                        });
                    });

                    Assert.True(messageLatch.Wait(TimeSpan.FromSeconds(5)));
                }
                finally
                {
                    CloseChannel(sc);
                    CloseChannel(cc);
                }
            }
            finally
            {
                data1.Release();
                data2.Release();
            }
        }
예제 #5
0
        public void LocalChannel_writes_to_server_should_be_read_by_LocalChannel()
        {
            var cb               = new ClientBootstrap();
            var sb               = new ServerBootstrap();
            var reads            = new[] { -11, 2, 9, 13, 1013, 1, 4 };
            var resetEvent       = new ManualResetEventSlim();
            var accumulatedReads = new List <int>();

            cb.Group(_group1)
            .Channel <LocalChannel>()
            .Handler(
                new ActionChannelInitializer <LocalChannel>(
                    channel =>
            {
                channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                .AddLast(new LengthFieldPrepender(4, false))
                .AddLast(new IntCodec());
            }));

            sb.Group(_group2)
            .Channel <LocalServerChannel>()
            .ChildHandler(
                new ActionChannelInitializer <LocalChannel>(
                    channel =>
            {
                channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                .AddLast(new LengthFieldPrepender(4, false))
                .AddLast(new IntCodec())
                .AddLast(new ReadAssertHandler(accumulatedReads, resetEvent, reads));
            }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;

                // Connect to the server
                cc = cb.ConnectAsync(sc.LocalAddress).Result;

                foreach (var read in reads)
                {
                    cc.WriteAsync(read);
                }
                cc.Flush();
                resetEvent.Wait(200);
                Assert.Equal(reads, accumulatedReads);
            }
            finally
            {
                CloseChannel(sc);
                CloseChannel(cc);
            }
        }
예제 #6
0
        public void LocalChannel_batch_read_should_not_NRE()
        {
            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();
            var reads = 100;
            var resetEvent = new ManualResetEventSlim();

            cb.Group(_group1)
                .Channel<LocalChannel>()
                .Handler(
                    new ActionChannelInitializer<LocalChannel>(
                        channel =>
                        {
                            channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                                .AddLast(new LengthFieldPrepender(4, false))
                                .AddLast(new TestHandler());
                        }));

            sb.Group(_group2)
                .Channel<LocalServerChannel>()
                .ChildHandler(
                    new ActionChannelInitializer<LocalChannel>(
                        channel =>
                        {
                            channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                                .AddLast(new LengthFieldPrepender(4, false))
                                .AddLast(new ReadCountAwaiter(resetEvent, reads))
                                .AddLast(new TestHandler());
                        }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;

                // Connect to the server
                cc = cb.ConnectAsync(sc.LocalAddress).Result;

                foreach (var read in Enumerable.Range(0, reads))
                {
                    cc.WriteAndFlushAsync(Unpooled.Buffer(4).WriteInt(read));
                }
                Assert.True(resetEvent.Wait(5000));
            }
            finally
            {
                CloseChannel(sc);
                CloseChannel(cc);
            }
        }
예제 #7
0
        public void LocalChannel_batch_read_should_not_NRE()
        {
            var cb         = new ClientBootstrap();
            var sb         = new ServerBootstrap();
            var reads      = 100;
            var resetEvent = new ManualResetEventSlim();

            cb.Group(_group1)
            .Channel <LocalChannel>()
            .Handler(
                new ActionChannelInitializer <LocalChannel>(
                    channel =>
            {
                channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                .AddLast(new LengthFieldPrepender(4, false))
                .AddLast(new TestHandler());
            }));

            sb.Group(_group2)
            .Channel <LocalServerChannel>()
            .ChildHandler(
                new ActionChannelInitializer <LocalChannel>(
                    channel =>
            {
                channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                .AddLast(new LengthFieldPrepender(4, false))
                .AddLast(new ReadCountAwaiter(resetEvent, reads))
                .AddLast(new TestHandler());
            }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;

                // Connect to the server
                cc = cb.ConnectAsync(sc.LocalAddress).Result;

                foreach (var read in Enumerable.Range(0, reads))
                {
                    cc.WriteAndFlushAsync(Unpooled.Buffer(4).WriteInt(read));
                }
                Assert.True(resetEvent.Wait(5000));
            }
            finally
            {
                CloseChannel(sc);
                CloseChannel(cc);
            }
        }
예제 #8
0
        public void LocalChannel_should_reuse_LocalAddress()
        {
            for (var i = 0; i < 2; i++)
            {
                var cb = new ClientBootstrap();
                var sb = new ServerBootstrap();

                cb.Group(_group1).Channel <LocalChannel>().Handler(new TestHandler());

                sb.Group(_group2).Channel <LocalServerChannel>().ChildHandler(new ActionChannelInitializer <LocalChannel>(
                                                                                  channel => { channel.Pipeline.AddLast(new TestHandler()); }));

                IChannel sc = null;
                IChannel cc = null;

                try
                {
                    // Start server
                    sc = sb.BindAsync(TEST_ADDRESS).Result;
                    var latch = new CountdownEvent(1);

                    // Connect to the server
                    cc = cb.ConnectAsync(sc.LocalAddress).Result;
                    var cCpy = cc;
                    cc.EventLoop.Execute(o =>
                    {
                        var c = (LocalChannel)o;
                        c.Pipeline.FireChannelRead("Hello, World");
                        latch.Signal();
                    }, cCpy);

                    latch.Wait(TimeSpan.FromSeconds(5));
                    Assert.True(latch.IsSet);

                    CloseChannel(cc);
                    CloseChannel(sc);
                    sc.CloseCompletion.Wait();

                    Assert.Null(LocalChannelRegistry.Get(TEST_ADDRESS));
                }
                finally
                {
                    CloseChannel(cc);
                    CloseChannel(sc);
                }
            }
        }
예제 #9
0
        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);
        }
예제 #10
0
        public void LocalChannel_should_not_fire_channel_active_before_connecting()
        {
            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();

            cb.Group(_group1).Channel <LocalChannel>().Handler(new DummyHandler());

            sb.Group(_group2).Channel <LocalServerChannel>().ChildHandler(new ActionChannelInitializer <LocalChannel>(
                                                                              channel => { channel.Pipeline.AddLast(new TestHandler()); }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;

                cc = cb.Register().Result;

                var promise       = new TaskCompletionSource();
                var assertPromise = new TaskCompletionSource();

                cc.Pipeline.AddLast(new PromiseAssertHandler(promise, assertPromise));

                // Connect to the server
                cc.ConnectAsync(sc.LocalAddress).Wait();

                assertPromise.Task.Wait();
                Assert.True(promise.Task.IsCompleted);
            }
            finally
            {
                CloseChannel(cc);
                CloseChannel(sc);
            }
        }
예제 #11
0
        public void LocalChannel_PeerClose_when_WritePromiseComplete_in_same_eventloop_should_still_preserve_order()
        {
            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();
            var messageLatch = new CountdownEvent(2);
            var data = Unpooled.WrappedBuffer(new byte[1024]);
            var serverLatch = new CountdownEvent(1);
            var serverChannelRef = new AtomicReference<IChannel>();

            try
            {
                cb.Group(_sharedGroup)
                    .Channel<LocalChannel>()
                    .Handler(new TestHandler());

                sb.Group(_sharedGroup)
                    .Channel<LocalServerChannel>()
                    .ChildHandler(new ActionChannelInitializer<LocalChannel>(channel =>
                    {
                        channel.Pipeline.AddLast(new ReadCountdown1(messageLatch, data));
                        serverChannelRef = channel;
                        serverLatch.Signal();
                    }));

                IChannel sc = null;
                IChannel cc = null;

                try
                {
                    // Start server
                    sc = sb.BindAsync(TEST_ADDRESS).Result;

                    // Connect to server
                    cc = cb.ConnectAsync(sc.LocalAddress).Result;
                    serverLatch.Wait(TimeSpan.FromSeconds(5));
                    Assert.True(serverLatch.IsSet);

                    var ccCpy = cc;

                    // Make sure a write operation is executed in the eventloop
                    cc.Pipeline.LastContext().Executor.Execute(() =>
                    {
                        ccCpy.WriteAndFlushAsync(data.Duplicate().Retain()).ContinueWith(tr =>
                        {
                            var severChannelCopy = serverChannelRef.Value;
                            severChannelCopy.CloseAsync();
                        });
                    });

                    Assert.True(messageLatch.Wait(TimeSpan.FromSeconds(5)));
                    Assert.False(cc.IsOpen);
                    Assert.False(serverChannelRef.Value.IsOpen);
                }
                finally
                {
                    CloseChannel(sc);
                    CloseChannel(cc);
                }
            }
            finally
            {
                data.Release();
            }
        }
예제 #12
0
        public void LocalChannel_writes_to_server_should_be_read_by_LocalChannel()
        {
            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();
            var reads = new[] {-11, 2, 9, 13, 1013, 1, 4};
            var resetEvent = new ManualResetEventSlim();
            var accumulatedReads = new List<int>();

            cb.Group(_group1)
                .Channel<LocalChannel>()
                .Handler(
                    new ActionChannelInitializer<LocalChannel>(
                        channel =>
                        {
                            channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                                .AddLast(new LengthFieldPrepender(4, false))
                                .AddLast(new IntCodec());
                        }));

            sb.Group(_group2)
                .Channel<LocalServerChannel>()
                .ChildHandler(
                    new ActionChannelInitializer<LocalChannel>(
                        channel =>
                        {
                            channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                                .AddLast(new LengthFieldPrepender(4, false))
                                .AddLast(new IntCodec())
                                .AddLast(new ReadAssertHandler(accumulatedReads, resetEvent, reads));
                        }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;

                // Connect to the server
                cc = cb.ConnectAsync(sc.LocalAddress).Result;

                foreach (var read in reads)
                {
                    cc.WriteAsync(read);
                }
                cc.Flush();
                resetEvent.Wait(200);
                Assert.Equal(reads, accumulatedReads);
            }
            finally
            {
                CloseChannel(sc);
                CloseChannel(cc);
            }
        }
예제 #13
0
        public void LocalChannel_PeerClose_when_WritePromiseComplete_in_same_eventloop_should_still_preserve_order()
        {
            var cb               = new ClientBootstrap();
            var sb               = new ServerBootstrap();
            var messageLatch     = new CountdownEvent(2);
            var data             = Unpooled.WrappedBuffer(new byte[1024]);
            var serverLatch      = new CountdownEvent(1);
            var serverChannelRef = new AtomicReference <IChannel>();

            try
            {
                cb.Group(_sharedGroup)
                .Channel <LocalChannel>()
                .Handler(new TestHandler());

                sb.Group(_sharedGroup)
                .Channel <LocalServerChannel>()
                .ChildHandler(new ActionChannelInitializer <LocalChannel>(channel =>
                {
                    channel.Pipeline.AddLast(new ReadCountdown1(messageLatch, data));
                    serverChannelRef = channel;
                    serverLatch.Signal();
                }));

                IChannel sc = null;
                IChannel cc = null;

                try
                {
                    // Start server
                    sc = sb.BindAsync(TEST_ADDRESS).Result;

                    // Connect to server
                    cc = cb.ConnectAsync(sc.LocalAddress).Result;
                    serverLatch.Wait(TimeSpan.FromSeconds(5));
                    Assert.True(serverLatch.IsSet);

                    var ccCpy = cc;

                    // Make sure a write operation is executed in the eventloop
                    cc.Pipeline.LastContext().Executor.Execute(() =>
                    {
                        ccCpy.WriteAndFlushAsync(data.Duplicate().Retain()).ContinueWith(tr =>
                        {
                            var severChannelCopy = serverChannelRef.Value;
                            severChannelCopy.CloseAsync();
                        });
                    });

                    Assert.True(messageLatch.Wait(TimeSpan.FromSeconds(5)));
                    Assert.False(cc.IsOpen);
                    Assert.False(serverChannelRef.Value.IsOpen);
                }
                finally
                {
                    CloseChannel(sc);
                    CloseChannel(cc);
                }
            }
            finally
            {
                data.Release();
            }
        }
예제 #14
0
        public void LocalChannel_should_reuse_LocalAddress()
        {
            for (var i = 0; i < 2; i++)
            {
                var cb = new ClientBootstrap();
                var sb = new ServerBootstrap();

                cb.Group(_group1).Channel<LocalChannel>().Handler(new TestHandler());

                sb.Group(_group2).Channel<LocalServerChannel>().ChildHandler(new ActionChannelInitializer<LocalChannel>(
                    channel => { channel.Pipeline.AddLast(new TestHandler()); }));

                IChannel sc = null;
                IChannel cc = null;

                try
                {
                    // Start server
                    sc = sb.BindAsync(TEST_ADDRESS).Result;
                    var latch = new CountdownEvent(1);

                    // Connect to the server
                    cc = cb.ConnectAsync(sc.LocalAddress).Result;
                    var cCpy = cc;
                    cc.EventLoop.Execute(o =>
                    {
                        var c = (LocalChannel) o;
                        c.Pipeline.FireChannelRead("Hello, World");
                        latch.Signal();
                    }, cCpy);

                    latch.Wait(TimeSpan.FromSeconds(5));
                    Assert.True(latch.IsSet);

                    CloseChannel(cc);
                    CloseChannel(sc);
                    sc.CloseCompletion.Wait();

                    Assert.Null(LocalChannelRegistry.Get(TEST_ADDRESS));
                }
                finally
                {
                    CloseChannel(cc);
                    CloseChannel(sc);
                }
            }
        }
예제 #15
0
        public void LocalChannel_WriteAsync_should_fail_fast_on_closed_channel()
        {
            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();

            cb.Group(_group1).Channel<LocalChannel>().Handler(new TestHandler());

            sb.Group(_group2).Channel<LocalServerChannel>().ChildHandler(new ActionChannelInitializer<LocalChannel>(
                channel => { channel.Pipeline.AddLast(new TestHandler()); }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;
                var latch = new CountdownEvent(1);

                // Connect to the server
                cc = cb.ConnectAsync(sc.LocalAddress).Result;

                // Close the channel and write something
                cc.CloseAsync().Wait();
                var ag =
                    Assert.Throws<AggregateException>(() => { cc.WriteAndFlushAsync(new object()).Wait(); }).Flatten();
                Assert.IsType<ClosedChannelException>(ag.InnerException);
            }
            finally
            {
                CloseChannel(cc);
                CloseChannel(sc);
            }
        }
예제 #16
0
        public void LocalChannel_write_when_WritePromiseComplete_should_still_preserve_order()
        {
            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();
            var messageLatch = new CountdownEvent(2);
            var data1 = Unpooled.WrappedBuffer(new byte[1024]);
            var data2 = Unpooled.WrappedBuffer(new byte[512]);

            try
            {
                cb.Group(_group1)
                    .Channel<LocalChannel>()
                    .Handler(new TestHandler());

                sb.Group(_group2)
                    .Channel<LocalServerChannel>()
                    .ChildHandler(new ReadCountdown2(messageLatch, data1, data2));

                IChannel sc = null;
                IChannel cc = null;

                try
                {
                    // Start server
                    sc = sb.BindAsync(TEST_ADDRESS).Result;

                    // Connect to server
                    cc = cb.ConnectAsync(sc.LocalAddress).Result;

                    var ccCpy = cc;

                    // Make sure a write operation is executed in the eventloop
                    cc.Pipeline.LastContext().Executor.Execute(() =>
                    {
                        Logger.Info("Writing message 1");
                        ccCpy.WriteAndFlushAsync(data1.Duplicate().Retain()).ContinueWith(tr =>
                        {
                            Logger.Info("Writing message 2");
                            ccCpy.WriteAndFlushAsync(data2.Duplicate().Retain());
                        });
                    });

                    Assert.True(messageLatch.Wait(TimeSpan.FromSeconds(5)));
                }
                finally
                {
                    CloseChannel(sc);
                    CloseChannel(cc);
                }
            }
            finally
            {
                data1.Release();
                data2.Release();
            }
        }
예제 #17
0
        public void TcpSocketChannel_can_connect_to_TcpServerSocketChannel()
        {
            IEventLoopGroup group1 = new MultithreadEventLoopGroup(2);
            IEventLoopGroup group2 = new MultithreadEventLoopGroup(2);

            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();
            var reads = 100;
            var resetEvent = new ManualResetEventSlim();

            cb.Group(group1)
                .Channel<TcpSocketChannel>()
                .Handler(
                    new ActionChannelInitializer<TcpSocketChannel>(
                        channel =>
                        {
                            channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                                .AddLast(new LengthFieldPrepender(4, false))
                                .AddLast(new IntCodec())
                                .AddLast(new TestHandler());
                        }));

            sb.Group(group2)
                .Channel<TcpServerSocketChannel>()
                .ChildHandler(
                    new ActionChannelInitializer<TcpSocketChannel>(
                        channel =>
                        {
                            channel.Pipeline.AddLast(new LengthFieldBasedFrameDecoder(20, 0, 4, 0, 4))
                                .AddLast(new LengthFieldPrepender(4, false))
                                .AddLast(new IntCodec())
                                .AddLast(new ReadCountAwaiter(resetEvent, reads))
                                .AddLast(new TestHandler());
                        }));

            IChannel sc = null;
            IChannel cc = null;
            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;

                // Connect to the server
                cc = cb.ConnectAsync(sc.LocalAddress).Result;

                foreach (var read in Enumerable.Range(0, reads))
                {
                    cc.WriteAndFlushAsync(read);
                }
                Assert.True(resetEvent.Wait(15000));
            }
            finally
            {
                CloseChannel(cc);
                CloseChannel(sc);
                Task.WaitAll(group1.ShutdownGracefullyAsync(), group2.ShutdownGracefullyAsync());
            }
        }
예제 #18
0
        public void LocalChannel_should_not_fire_channel_active_before_connecting()
        {
            var cb = new ClientBootstrap();
            var sb = new ServerBootstrap();

            cb.Group(_group1).Channel<LocalChannel>().Handler(new DummyHandler());

            sb.Group(_group2).Channel<LocalServerChannel>().ChildHandler(new ActionChannelInitializer<LocalChannel>(
                channel => { channel.Pipeline.AddLast(new TestHandler()); }));

            IChannel sc = null;
            IChannel cc = null;

            try
            {
                // Start server
                sc = sb.BindAsync(TEST_ADDRESS).Result;

                cc = cb.Register().Result;

                var promise = new TaskCompletionSource();
                var assertPromise = new TaskCompletionSource();

                cc.Pipeline.AddLast(new PromiseAssertHandler(promise, assertPromise));

                // Connect to the server
                cc.ConnectAsync(sc.LocalAddress).Wait();

                assertPromise.Task.Wait();
                Assert.True(promise.Task.IsCompleted);
            }
            finally
            {
                CloseChannel(cc);
                CloseChannel(sc);
            }
        }