Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Sent"/> class.
        /// </summary>
        /// <param name="bufferSlice">The buffer slice.</param>
        public Sent(BufferSlice bufferSlice)
        {
            if (bufferSlice == null)
            {
                throw new ArgumentNullException("bufferSlice");
            }

            BufferSlice = bufferSlice;
        }
Ejemplo n.º 2
0
        public void TestContains()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello  \tworld.!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length, buffer.Length);
            var reader = new BufferSliceReader(slice);

            reader.Contains(' ');
            Assert.Equal('H', reader.Current);
            Assert.Equal('e', reader.Peek);
        }
Ejemplo n.º 3
0
        public void TestConsumeUntilSpace()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world.!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length, buffer.Length);
            var reader = new BufferSliceReader(slice);

            reader.ConsumeUntil(' ');
            Assert.Equal(' ', reader.Current);
            Assert.Equal('w', reader.Peek);
        }
        public void TestConsumeHe()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world.!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new StringBufferSliceReader(slice, slice.Count);

            reader.Consume('H', 'e');
            Assert.Equal('l', reader.Current);
            Assert.Equal('l', reader.Peek);
        }
        public void TestReadChar()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello  \tworld.!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new StringBufferSliceReader(slice, slice.Count);

            Assert.Equal('H', reader.Read());
            Assert.Equal('e', reader.Current);
            Assert.Equal('l', reader.Peek);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Will serialize messages
        /// </summary>
        /// <param name="message"></param>
        public virtual void Write(object message)
        {
            var formatter = this.formatterFactory.CreateSerializer();
            var buffer    = new BufferSlice(65535);
            var writer    = new SliceStream(buffer);

            formatter.Serialize(message, writer);
            writer.Position = 0;
            this.Send(buffer, (int)writer.Length);
        }
Ejemplo n.º 7
0
        public void assign_without_subscribing_on_MessageReceived_means_that_messages_can_get_lost()
        {
            var slice   = new BufferSlice(new byte[65535], 0, 65535);
            var encoder = Substitute.For <IMessageEncoder>();
            var decoder = Substitute.For <IMessageDecoder>();

            var    sut    = new TcpChannel(slice, encoder, decoder);
            Action actual = () => sut.Assign(_helper.Client);

            actual.ShouldThrow <InvalidOperationException>();
        }
Ejemplo n.º 8
0
        public void TestCompact()
        {
            var buffer = Encoding.ASCII.GetBytes("0123456789");
            var slice  = new BufferSlice(buffer, 0, buffer.Length, buffer.Length);

            slice.Position = 5;
            slice.Compact();
            var str = Encoding.ASCII.GetString(slice.Buffer, 0, 5);

            Assert.Equal("56789", str);
        }
Ejemplo n.º 9
0
        public void ReadSome()
        {
            var slice  = new byte[65535];
            var buffer = new BufferSlice(slice, 0, slice.Length, slice.Length);

            buffer.Position += 10;
            Assert.Equal(65535, buffer.Capacity);
            Assert.Equal(10, buffer.Position);
            Assert.Equal(0, buffer.StartOffset);
            Assert.Equal(65525, buffer.RemainingCapacity);
        }
Ejemplo n.º 10
0
        public void InitWithWrittenBuffer()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world");
            var slice  = new BufferSlice(buffer, 0, buffer.Length, buffer.Length);

            Assert.Equal(buffer.Length, slice.Count);
            Assert.Equal(buffer.Length, slice.Capacity);

            // position = 0
            Assert.Equal(buffer.Length, slice.RemainingLength);
        }
        public void TestConsumeUntilSpaceAndWhiteSpaces()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello  \tworld.!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new StringBufferSliceReader(slice, slice.Count);

            reader.ConsumeUntil(' ');
            reader.ConsumeWhiteSpaces();
            Assert.Equal('w', reader.Current);
            Assert.Equal('o', reader.Peek);
        }
Ejemplo n.º 12
0
        public void Position_SetGet()
        {
            var slice        = new BufferSlice(65535);
            var stream       = new SliceStream(slice);
            var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast.");

            stream.Write(mammasBullar, 0, mammasBullar.Length);

            stream.Position = 2;
            Assert.Equal(2, stream.Position);
        }
Ejemplo n.º 13
0
        public void InitUsingByteArray()
        {
            var initial       = "Hello world!";
            var initialBuffer = Encoding.ASCII.GetBytes(initial);
            var slice         = new BufferSlice(initialBuffer, 0, initialBuffer.Length);
            var stream        = new SliceStream(slice, initialBuffer.Length);

            Assert.Equal(initial.Length, stream.Length);
            Assert.Equal(initial.Length, ((IBufferWrapper)stream).Capacity);
            Assert.Equal(0, stream.Position);
        }
Ejemplo n.º 14
0
        public void initialize_own_buffer()
        {
            var buffer = new byte[10];

            var sut = new BufferSlice(buffer, 0, 10);

            Assert.Same(buffer, sut.Buffer);
            sut.Offset.Should().Be(0);
            sut.Capacity.Should().Be(10);
            sut.Buffer.Should().BeEquivalentTo(buffer);
        }
        public void ReadLineWithSingleNewLine()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello\nWorld!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new StringBufferSliceReader(slice, buffer.Length);

            var actual  = reader.ReadLine();
            var actual2 = reader.ReadToEnd();

            Assert.Equal("Hello", actual);
            Assert.Equal("World!", actual2);
        }
Ejemplo n.º 16
0
        public void Parse()
        {
            var buffer = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nSERVER: LOCALHOST\r\n\r\n");
            var slice  = new BufferSlice(buffer, 0, buffer.Length);
            var reader = new SliceStream(slice);


            var parser = new HttpHeaderParser();

            parser.HeaderParsed += (sender, args) => Console.WriteLine(args.Name + ": " + args.Value);
            parser.Parse(reader);
        }
Ejemplo n.º 17
0
        public void TestInit()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world.!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length, buffer.Length);
            var reader = new BufferSliceReader(slice);

            Assert.Equal(slice.Count, reader.Length);
            Assert.Equal(slice.RemainingLength, reader.RemainingLength);
            Assert.Equal('H', reader.Current);
            Assert.Equal('e', reader.Peek);
            Assert.True(reader.HasMore);
        }
Ejemplo n.º 18
0
        public void Assign_should_work_after_subscription()
        {
            var    slice   = new BufferSlice(new byte[65535], 0, 65535);
            var    encoder = Substitute.For <IMessageEncoder>();
            var    decoder = Substitute.For <IMessageDecoder>();
            object expected;

            var sut = new TcpChannel(slice, encoder, decoder);

            sut.MessageReceived += (channel, message) => expected = message;
            sut.Assign(_helper.Client);
        }
Ejemplo n.º 19
0
        public void TestConsumeEnd()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello  \tworld.!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length, buffer.Length);
            var reader = new BufferSliceReader(slice);

            reader.ReadUntil('!');
            Assert.Equal('!', reader.Current);
            reader.Consume();
            Assert.True(reader.EndOfFile);
            Assert.Equal(0, reader.RemainingLength);
        }
Ejemplo n.º 20
0
        public void Write_ThenMovePositionOutOfRange()
        {
            var slice  = new BufferSlice(65535);
            var stream = new SliceStream(slice);

            var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast.");

            stream.Write(mammasBullar, 0, mammasBullar.Length);


            Assert.Throws <ArgumentOutOfRangeException>(() => stream.Position = mammasBullar.Length + 1);
            Assert.Throws <ArgumentOutOfRangeException>(() => stream.Position = -1);
        }
Ejemplo n.º 21
0
        public void send_message()
        {
            var    slice    = new BufferSlice(new byte[65535], 0, 65535);
            var    encoder  = new FakeEncoder();
            var    decoder  = new FakeDecoder();
            object expected = null;

            var sut = new TcpChannel(slice, encoder, decoder);

            sut.MessageReceived += (channel, message) => expected = message;
            sut.Assign(_helper.Client);
            sut.Send("Hello world");
        }
        public void TestConsume()
        {
            var buffer = Encoding.ASCII.GetBytes("Hello world.!");
            var slice  = new BufferSlice(buffer, 0, buffer.Length);

            var reader = new StringBufferSliceReader(slice, slice.Count);

            reader.Consume();
            Assert.Equal(slice.Count, reader.Length);
            Assert.Equal(slice.Offset - slice.Count, reader.RemainingLength);
            Assert.Equal('e', reader.Current);
            Assert.Equal('l', reader.Peek);
            Assert.True(reader.HasMore);
        }
Ejemplo n.º 23
0
        public void should_listen_on_the_decoder_event()
        {
            var    slice    = new BufferSlice(new byte[65535], 0, 65535);
            var    encoder  = Substitute.For <IMessageEncoder>();
            var    decoder  = new FakeDecoder();
            object expected = null;

            var sut = new TcpChannel(slice, encoder, decoder);

            sut.MessageReceived += (channel, message) => expected = message;
            decoder.MessageReceived("Hello");

            expected.Should().Be("Hello");
        }
Ejemplo n.º 24
0
        public void assign_without_subscribing_on_MessageReceived_means_that_messages_can_get_lost()
        {
            var slice   = new BufferSlice(new byte[65535], 0, 65535);
            var encoder = Substitute.For <IMessageEncoder>();
            var decoder = Substitute.For <IMessageDecoder>();
            var stream  = new SslStream(new NetworkStream(_helper.Server));

            stream.BeginAuthenticateAsServer(_certificate, OnAuthenticated, stream);

            var    sut    = CreateClientChannel(slice, encoder, decoder);
            Action actual = () => sut.Assign(_helper.Client);

            actual.ShouldThrow <InvalidOperationException>();
        }
Ejemplo n.º 25
0
        public void Write_Single()
        {
            var slice  = new BufferSlice(65535);
            var stream = new SliceStream(slice);

            var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast.");

            stream.Write(mammasBullar, 0, mammasBullar.Length);

            Assert.Equal(mammasBullar.Length, stream.Position);
            Assert.Equal(mammasBullar.Length, stream.Length);

            // must be able to write after the last byte.
            stream.Position = mammasBullar.Length;
        }
Ejemplo n.º 26
0
        public void Read_OneTime()
        {
            var slice        = new BufferSlice(65535);
            var stream       = new SliceStream(slice);
            var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast.");

            stream.Write(mammasBullar, 0, mammasBullar.Length);

            var buffer = new byte[10];

            stream.Position = 0;
            stream.Read(buffer, 0, 6);

            Assert.Equal("Mammas", Encoding.UTF8.GetString(buffer, 0, 6));
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TcpChannel"/> class.
 /// </summary>
 /// <param name="pipeline">The pipeline used to send messages upstream.</param>
 /// <param name="pool">Buffer pool.</param>
 public TcpChannel(IPipeline pipeline, BufferPool pool)
 {
     _pipeline = pipeline;
     _pool     = pool;
     Pipeline.SetChannel(this);
     if (pool == null)
     {
         _readBuffer = new BufferSlice(new byte[65535], 0, 65535, 0);
     }
     else
     {
         _readBuffer = pool.PopSlice();
     }
     _stream = new PeekableMemoryStream(_readBuffer.Buffer, _readBuffer.StartOffset, _readBuffer.Capacity);
 }
Ejemplo n.º 28
0
        public void Create()
        {
            var slice  = new byte[65535];
            var buffer = new BufferSlice(slice, 0, slice.Length, slice.Length);

            Assert.Equal(65535, buffer.Capacity);
            Assert.Equal(0, buffer.Position);
            Assert.Equal(0, buffer.StartOffset);
            Assert.Equal(65535, buffer.RemainingCapacity);

            // since offset = 0
            Assert.Equal(65535, buffer.RemainingLength);

            Assert.Equal(65535, buffer.Count);
        }
        public void should_fail_if_client_do_not_send_handshake_within_given_time_Frame()
        {
            var certificate   = CertificateHelper.Create();
            var clientServer  = ClientServerHelper.Create();
            var buffer        = new BufferSlice(new byte[65535], 0, 65535);
            var serverChannel = new TcpChannel(buffer, new FakeEncoder(), new FakeDecoder());


            var sut = new ServerSideSslStreamBuilder(certificate);

            sut.HandshakeTimeout = TimeSpan.FromMilliseconds(100);
            Action actual = () => sut.Build(serverChannel, clientServer.Server);

            actual.ShouldThrow <InvalidOperationException>();
        }
Ejemplo n.º 30
0
        public void Assign_should_work_after_subscription()
        {
            var    slice   = new BufferSlice(new byte[65535], 0, 65535);
            var    encoder = Substitute.For <IMessageEncoder>();
            var    decoder = Substitute.For <IMessageDecoder>();
            object expected;
            var    stream = new SslStream(new NetworkStream(_helper.Server));

            stream.BeginAuthenticateAsServer(_certificate, OnAuthenticated, stream);

            var sut = CreateClientChannel(slice, encoder, decoder);

            sut.MessageReceived += (channel, message) => expected = message;
            sut.Assign(_helper.Client);
        }