Beispiel #1
0
        void SimpleEcho0(ServerBootstrap sb, Bootstrap cb, bool additionalExecutor, bool autoRead)
        {
            var sh = new EchoHandler(autoRead, this.data, this.output);
            var ch = new EchoHandler(autoRead, this.data, this.output);

            if (additionalExecutor)
            {
                sb.ChildHandler(new ActionChannelInitializer <TcpChannel>(channel =>
                {
                    channel.Pipeline.AddLast(this.group, sh);
                }));
                cb.Handler(new ActionChannelInitializer <TcpChannel>(channel =>
                {
                    channel.Pipeline.AddLast(this.group, ch);
                }));
            }
            else
            {
                sb.ChildHandler(sh);
                sb.Handler(new ErrorOutputHandler(this.output));
                cb.Handler(ch);
            }
            sb.ChildOption(ChannelOption.AutoRead, autoRead);
            cb.Option(ChannelOption.AutoRead, autoRead);

            // start server
            Task <IChannel> task = sb.BindAsync(LoopbackAnyPort);

            Assert.True(task.Wait(DefaultTimeout), "Server bind timed out");
            this.serverChannel = task.Result;
            Assert.NotNull(this.serverChannel.LocalAddress);
            var endPoint = (IPEndPoint)this.serverChannel.LocalAddress;

            // connect to server
            task = cb.ConnectAsync(endPoint);
            Assert.True(task.Wait(DefaultTimeout), "Connect to server timed out");
            this.clientChannel = task.Result;
            Assert.NotNull(this.clientChannel.LocalAddress);

            for (int i = 0; i < this.data.Length;)
            {
                int         length = Math.Min(this.random.Next(1024 * 64), this.data.Length - i);
                IByteBuffer buf    = Unpooled.WrappedBuffer(this.data, i, length);
                this.clientChannel.WriteAndFlushAsync(buf);
                i += length;
            }

            Assert.True(Task.WhenAll(ch.Completion, sh.Completion).Wait(DefaultTimeout), "Echo read/write timed out");
        }
Beispiel #2
0
        private static async Task TestFixedLengthEcho0(ServerBootstrap sb, Bootstrap cb, bool autoRead)
        {
            EchoHandler sh = new EchoHandler(autoRead);
            EchoHandler ch = new EchoHandler(autoRead);

            sb.ChildOption(ChannelOption.AutoRead, autoRead);
            sb.ChildHandler(new ActionChannelInitializer <IChannel>(sch =>
            {
                sch.Pipeline.AddLast("decoder", new FixedLengthFrameDecoder(1024));
                sch.Pipeline.AddAfter("decoder", "handler", sh);
            }));

            cb.Option(ChannelOption.AutoRead, autoRead);
            cb.Handler(new ActionChannelInitializer <IChannel>(sch =>
            {
                sch.Pipeline.AddLast("decoder", new FixedLengthFrameDecoder(1024));
                sch.Pipeline.AddAfter("decoder", "handler", ch);
            }));

            IChannel sc = await sb.BindAsync();

            IChannel cc = await cb.ConnectAsync(sc.LocalAddress);

            for (int i = 0; i < s_data.Length;)
            {
                int length = Math.Min(s_random.Next(1024 * 3), s_data.Length - i);
                cc.WriteAndFlushAsync(Unpooled.WrappedBuffer(s_data, i, length)).Ignore();
                i += length;
            }

            while (ch._counter < s_data.Length)
            {
                if (sh._exception.Value != null)
                {
                    break;
                }
                if (ch._exception.Value != null)
                {
                    break;
                }

                Thread.Sleep(50);
            }

            while (sh._counter < s_data.Length)
            {
                if (sh._exception.Value != null)
                {
                    break;
                }
                if (ch._exception.Value != null)
                {
                    break;
                }

                Thread.Sleep(50);
            }

            await sh._channel.CloseAsync();

            await ch._channel.CloseAsync();

            await sc.CloseAsync();

            if (sh._exception.Value != null && !(sh._exception.Value is SocketException || (sh._exception.Value is ChannelException chexc && chexc.InnerException is OperationException) || sh._exception.Value is OperationException))
            {
                throw sh._exception.Value;
            }
            if (ch._exception.Value != null && !(ch._exception.Value is SocketException || (sh._exception.Value is ChannelException chexc1 && chexc1.InnerException is OperationException) || sh._exception.Value is OperationException))
            {
                throw ch._exception.Value;
            }
            if (sh._exception.Value != null)
            {
                throw sh._exception.Value;
            }
            if (ch._exception.Value != null)
            {
                throw ch._exception.Value;
            }
        }
Beispiel #3
0
 public void SetEchoHandler(EchoHandler action)
 {
     m_EchoHandler = action;
 }
Beispiel #4
0
        private async Task TestSimpleEcho0(
            ServerBootstrap sb, Bootstrap cb, bool additionalExecutor, bool voidPromise, bool autoRead)
        {
            EchoHandler sh = new EchoHandler(autoRead);
            EchoHandler ch = new EchoHandler(autoRead);

            if (additionalExecutor)
            {
                sb.ChildHandler(new ActionChannelInitializer <IChannel>(c =>
                {
                    c.Pipeline.AddLast(_group, sh);
                }));
                cb.Handler(new ActionChannelInitializer <IChannel>(c =>
                {
                    c.Pipeline.AddLast(_group, ch);
                }));
            }
            else
            {
                sb.ChildHandler(sh);
                sb.Handler(new ChannelHandlerAdapter());
                cb.Handler(ch);
            }
            sb.ChildOption(ChannelOption.AutoRead, autoRead);
            cb.Option(ChannelOption.AutoRead, autoRead);

            IChannel sc = await sb.BindAsync();

            IChannel cc = await cb.ConnectAsync(sc.LocalAddress);

            for (int i = 0; i < s_data.Length;)
            {
                int         length = Math.Min(s_random.Next(1024 * 64), s_data.Length - i);
                IByteBuffer buf    = Unpooled.WrappedBuffer(s_data, i, length);
                if (voidPromise)
                {
                    Assert.Equal(cc.VoidPromise().Task, cc.WriteAndFlushAsync(buf, cc.VoidPromise()));
                }
                else
                {
                    Assert.NotEqual(cc.VoidPromise().Task, cc.WriteAndFlushAsync(buf));
                }
                i += length;
            }

            while (ch._counter < s_data.Length)
            {
                if (sh._exception.Value != null)
                {
                    break;
                }
                if (ch._exception.Value != null)
                {
                    break;
                }

                Thread.Sleep(50);
            }

            while (sh._counter < s_data.Length)
            {
                if (sh._exception.Value != null)
                {
                    break;
                }
                if (ch._exception.Value != null)
                {
                    break;
                }

                Thread.Sleep(50);
            }

            await sh._channel.CloseAsync();

            await ch._channel.CloseAsync();

            await sc.CloseAsync();

            if (sh._exception.Value != null && !(sh._exception.Value is SocketException || (sh._exception.Value is ChannelException chexc && chexc.InnerException is OperationException) || sh._exception.Value is OperationException))
            {
                throw sh._exception.Value;
            }
            if (ch._exception.Value != null && !(ch._exception.Value is SocketException || (sh._exception.Value is ChannelException chexc1 && chexc1.InnerException is OperationException) || sh._exception.Value is OperationException))
            {
                throw ch._exception.Value;
            }
            if (sh._exception.Value != null)
            {
                throw sh._exception.Value;
            }
            if (ch._exception.Value != null)
            {
                throw ch._exception.Value;
            }
        }