public void partial_header()
        {
            var args = new SocketBufferFake();
            var body = "Hello world";
            var type = body.GetType().AssemblyQualifiedName;
            var serializer = new StringSerializer();
            object actual = null;
            BitConverter2.GetBytes(MicroMessageEncoder.FixedHeaderLength + type.Length, args.Buffer, 0);
            args.Buffer[2] = MicroMessageDecoder.Version;
            BitConverter2.GetBytes(body.Length, args.Buffer, 3);
            args.Buffer[7] = (byte)(sbyte)type.Length;
            Encoding.ASCII.GetBytes(type, 0, type.Length, args.Buffer, HeaderLengthSize+MicroMessageEncoder.FixedHeaderLength);
            Encoding.ASCII.GetBytes(body, 0, body.Length, args.Buffer, HeaderLengthSize+type.Length + MicroMessageEncoder.FixedHeaderLength);

            var sut = new MicroMessageDecoder(serializer);
            sut.MessageReceived = o => actual = o;
            args.BytesTransferred = MicroMessageDecoder.FixedHeaderLength - 1;
            sut.ProcessReadBytes(args);

            args.Offset += args.BytesTransferred;
            args.BytesTransferred = body.Length + type.Length + 1 + HeaderLengthSize;
            sut.ProcessReadBytes(args);


            actual.Should().NotBeNull();
            actual.Should().Be("Hello world");
        }
        public void complete_header_then_half_typename_then_full_message()
        {
            var args = new SocketBufferFake();
            var body = "Hello world";
            var type = body.GetType().AssemblyQualifiedName;
            var serializer = new StringSerializer();
            object actual = null;
            BitConverter2.GetBytes(MicroMessageEncoder.FixedHeaderLength + type.Length, args.Buffer, 0); //header length
            args.Buffer[2] = MicroMessageDecoder.Version; 
            BitConverter2.GetBytes(body.Length, args.Buffer, 3); //content lkength
            args.Buffer[7] = (byte)(sbyte)type.Length; // type len
            Encoding.ASCII.GetBytes(type, 0, type.Length, args.Buffer, HeaderLengthSize + MicroMessageEncoder.FixedHeaderLength); // type name
            Encoding.ASCII.GetBytes(body, 0, body.Length, args.Buffer, HeaderLengthSize + type.Length + MicroMessageEncoder.FixedHeaderLength); //body

            var sut = new MicroMessageDecoder(serializer);
            sut.MessageReceived = o => actual = o;
            args.BytesTransferred = sizeof(short) + MicroMessageDecoder.FixedHeaderLength;
            sut.ProcessReadBytes(args);

            args.Offset += args.BytesTransferred;
            args.BytesTransferred = type.Length - 31;
            sut.ProcessReadBytes(args);

            args.Offset += args.BytesTransferred;
            args.BytesTransferred = 31;
            sut.ProcessReadBytes(args);

            args.Offset += args.BytesTransferred;
            args.BytesTransferred = body.Length;
            sut.ProcessReadBytes(args);


            actual.Should().NotBeNull();
            actual.Should().Be("Hello world");
        }
        public void can_clear_even_without_shit_being_used()
        {
            var serializer = new StringSerializer();
            var slice = new BufferSlice(new byte[65535], 0, 65535);
            var msg = "Hello world";
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);
            sut.Clear();
            sut.Prepare(msg);
            sut.Send(buffer);

            var field = sut.GetType().GetField("_bodyStream", BindingFlags.Instance | BindingFlags.NonPublic);
            ((Stream)field.GetValue(sut)).CanWrite.Should().BeTrue();
        }
        public void dont_close_our_internal_stream_as_we_reuse_it()
        {
            var serializer = new StringSerializer();
            var slice = new BufferSlice(new byte[65535], 0, 65535);
            var msg = "Hello world";
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);
            sut.Prepare(msg);
            sut.Send(buffer);
            sut.Clear();

            var field = sut.GetType().GetField("_bodyStream", BindingFlags.Instance | BindingFlags.NonPublic);
            ((Stream) field.GetValue(sut)).CanWrite.Should().BeTrue();
        }
        public void partial_send__continue_sending_rest_of_the_buffer_before_doing_anything_else()
        {
            var serializer = new StringSerializer();
            var slice = new BufferSlice(new byte[65535], 0, 65535);
            var msg = "Hello world";
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);
            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(10);
            sut.Send(buffer);

            buffer.Offset.Should().Be(10);
            buffer.Count.Should().Be(2 + MicroMessageEncoder.FixedHeaderLength + "string".Length + msg.Length - 10, "2 for the header length");
        }
        public void close_external_Stream_as_we_take_over_ownership_from_the_dev()
        {
            var serializer = new StringSerializer();
            var slice = new BufferSlice(new byte[65535], 0, 65535);
            var msg = new MemoryStream();
            var text = Encoding.ASCII.GetBytes("Hello world");
            msg.Write(text, 0, text.Length);
            msg.Position = 0;
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);
            sut.Prepare(msg);
            sut.Send(buffer);
            sut.Clear();

            var field = sut.GetType().GetField("_bodyStream", BindingFlags.Instance | BindingFlags.NonPublic);
            if (field == null) throw new ArgumentNullException("field");
            var value = ((Stream)field.GetValue(sut));
            value.Should().BeNull();
        }
        public void write_a_complete_string_message_directly()
        {
            var serializer = new StringSerializer();
            var slice = new BufferSlice(new byte[65535], 0, 65535);
            var msg = "Hello world";
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);
            sut.Prepare(msg);
            sut.Send(buffer);

            var headerLen = MicroMessageEncoder.FixedHeaderLength + "string".Length;
            buffer.Buffer[0].Should().Be((byte)headerLen);
            buffer.Buffer[2].Should().Be(1, "first version");
            BitConverter.ToInt32(buffer.Buffer, 3).Should().Be(msg.Length);
            buffer.Buffer[7].Should().Be((byte)"string".Length);
            Encoding.ASCII.GetString(buffer.Buffer, 8, "string".Length).Should().Be("string");
            Encoding.ASCII.GetString(buffer.Buffer, MicroMessageEncoder.FixedHeaderLength + "string".Length + 2, msg.Length)
                .Should()
                .Be(msg);
        }
        public void write_a_complete_string_message_directly()
        {
            var contentType = "text/plain;type=System.String";
            var serializer = new StringSerializer();
            var slice = new BufferSlice(new byte[65535], 0, 65535);
            var msg = "Hello world";
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);
            sut.Prepare(msg);
            sut.Send(buffer);

            var headerLen = MicroMessageEncoder.FixedHeaderLength + contentType.Length;
            BitConverter.ToInt16(buffer.Buffer, 0).Should().Be((short)headerLen);
            buffer.Buffer[2].Should().Be(1, "first version");
            BitConverter.ToInt32(buffer.Buffer, 3).Should().Be(msg.Length);
            buffer.Buffer[7].Should().Be((byte)contentType.Length);
            Encoding.ASCII.GetString(buffer.Buffer, 8, contentType.Length).Should().Be(contentType);
            Encoding.ASCII.GetString(buffer.Buffer, 2 + MicroMessageEncoder.FixedHeaderLength + contentType.Length, msg.Length)
                .Should()
                .Be(msg);
        }
Example #9
0
        public void complete_package()
        {
            var    args       = new SocketBufferFake();
            var    body       = "Hello world";
            var    type       = body.GetType().AssemblyQualifiedName;
            var    serializer = new StringSerializer();
            object actual     = null;

            BitConverter2.GetBytes(MicroMessageEncoder.FixedHeaderLength + type.Length, args.Buffer, 0);
            args.Buffer[2] = MicroMessageDecoder.Version;
            BitConverter2.GetBytes(body.Length, args.Buffer, 3);
            args.Buffer[7] = (byte)(sbyte)type.Length;
            Encoding.ASCII.GetBytes(type, 0, type.Length, args.Buffer, HeaderLengthSize + MicroMessageEncoder.FixedHeaderLength);
            Encoding.ASCII.GetBytes(body, 0, body.Length, args.Buffer, HeaderLengthSize + type.Length + MicroMessageEncoder.FixedHeaderLength);

            var sut = new MicroMessageDecoder(serializer);

            sut.MessageReceived   = o => actual = o;
            args.BytesTransferred = body.Length + type.Length + MicroMessageEncoder.FixedHeaderLength + HeaderLengthSize;
            sut.ProcessReadBytes(args);

            actual.Should().NotBeNull();
            actual.Should().Be("Hello world");
        }
Example #10
0
        public void write_a_complete_string_message_directly()
        {
            var contentType = "text/plain;type=System.String";
            var serializer  = new StringSerializer();
            var slice       = new BufferSlice(new byte[65535], 0, 65535);
            var msg         = "Hello world";
            var buffer      = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);

            var headerLen = MicroMessageEncoder.FixedHeaderLength + contentType.Length;

            BitConverter.ToInt16(buffer.Buffer, 0).Should().Be((short)headerLen);
            buffer.Buffer[2].Should().Be(1, "first version");
            BitConverter.ToInt32(buffer.Buffer, 3).Should().Be(msg.Length);
            buffer.Buffer[7].Should().Be((byte)contentType.Length);
            Encoding.ASCII.GetString(buffer.Buffer, 8, contentType.Length).Should().Be(contentType);
            Encoding.ASCII.GetString(buffer.Buffer, 2 + MicroMessageEncoder.FixedHeaderLength + contentType.Length, msg.Length)
            .Should()
            .Be(msg);
        }
Example #11
0
        public void reset_after_each_successful_message()
        {
            var contentType = "text/plain;type=System.String";
            var serializer  = new StringSerializer();
            var slice       = new BufferSlice(new byte[520], 0, 520);
            var msg         = "Hello world";
            var buffer      = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);

            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(13);
            sut.Send(buffer);
            sut.OnSendCompleted(2 + MicroMessageEncoder.FixedHeaderLength + contentType.Length + msg.Length - 13);
            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(5);
            sut.Send(buffer);


            buffer.Offset.Should().Be(5);
            buffer.Count.Should().Be(2 + MicroMessageEncoder.FixedHeaderLength + contentType.Length + msg.Length - 5);
        }
        public void reset_after_each_successful_message()
        {
            var contentType = "text/plain;type=System.String";
            var serializer = new StringSerializer();
            var slice = new BufferSlice(new byte[520], 0, 520);
            var msg = "Hello world";
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);
            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(13);
            sut.Send(buffer);
            sut.OnSendCompleted(2 + MicroMessageEncoder.FixedHeaderLength + contentType.Length + msg.Length - 13);
            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(5);
            sut.Send(buffer);
            

            buffer.Offset.Should().Be(5);
            buffer.Count.Should().Be(2 + MicroMessageEncoder.FixedHeaderLength + contentType.Length + msg.Length - 5);
        }
        public void too_small_buffer_requires_multiple_sends()
        {
            var contentType = "text/plain;type=System.String";
            var serializer = new StringSerializer();
            var slice = new BufferSlice(new byte[520], 0, 520);
            var msg = "Hello world".PadRight(520);
            var buffer = new SocketBufferFake();

            var sut = new MicroMessageEncoder(serializer, slice);
            sut.Prepare(msg);
            sut.Send(buffer);
            sut.OnSendCompleted(520).Should().BeFalse();
            sut.Send(buffer);

            buffer.Offset.Should().Be(0);
            // headerlength + fixed header length + content type length + content type - sent in first batch
            buffer.Count.Should().Be(2 + MicroMessageEncoder.FixedHeaderLength + contentType.Length + msg.Length - 520);
        }