Пример #1
0
 public MicroMessageClient(IMessageSerializer serializer)
 {
     _decoder = new MicroMessageDecoder(serializer);
     _encoder = new MicroMessageEncoder(serializer);
     _channel = new TcpChannel(new BufferSlice(new byte[65535], 0, 65535), _encoder, _decoder);
     _channel.Disconnected    = OnDisconnect;
     _channel.MessageSent     = OnSendCompleted;
     _channel.MessageReceived = OnChannelMessageReceived;
     _decoder.MessageReceived = OnMessageReceived;
     _args.Completed         += OnConnect;
 }
        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 too_small_buffer_requires_multiple_sends()
        {
            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 + "string".Length + msg.Length - 520);
        }
        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 reset_after_each_successful_message()
        {
            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 + "string".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 + "string".Length + msg.Length - 5);
        }