Example #1
0
        public void half_body()
        {
            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 - 5 + HeaderLengthSize;
            sut.ProcessReadBytes(args);
            args.Offset          += args.BytesTransferred;
            args.BytesTransferred = 5;
            sut.ProcessReadBytes(args);

            actual.Should().NotBeNull();
            actual.Should().Be("Hello world");
        }
Example #2
0
        public void Send_one_messages()
        {
            var    slice1   = new BufferSlice(new byte[65535], 0, 65535);
            var    encoder1 = new MicroMessageEncoder(new DataContractMessageSerializer());
            var    decoder1 = new MicroMessageDecoder(new DataContractMessageSerializer());
            var    slice2   = new BufferSlice(new byte[65535], 0, 65535);
            var    encoder2 = new MicroMessageEncoder(new DataContractMessageSerializer());
            var    decoder2 = new MicroMessageDecoder(new DataContractMessageSerializer());
            var    evt      = new ManualResetEvent(false);
            string actual   = null;
            var    sut2     = new TcpChannel(slice2, encoder2, decoder2);

            sut2.MessageReceived += (channel, message) =>
            {
                actual = message.ToString();
                evt.Set();
            };
            sut2.Assign(_helper.Server);

            var sut1 = new TcpChannel(slice1, encoder1, decoder1);

            sut1.MessageReceived += (channel, message) => { };
            sut1.Assign(_helper.Client);
            sut1.Send("Hello".PadRight(1000));

            evt.WaitOne(500).Should().BeTrue();
            actual.Should().StartWith("Hello");
        }
Example #3
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;
 }
Example #4
0
        public void codec_a_string_message()
        {
            object receivedMessage = null;
            var    encoderSlice    = new BufferSlice(new byte[65535], 0, 65535);
            var    serializer      = new DataContractMessageSerializer();
            var    encoder         = new MicroMessageEncoder(serializer, encoderSlice);
            var    decoder         = new MicroMessageDecoder(serializer)
            {
                MessageReceived = o => receivedMessage = o
            };
            var expected    = "Hello world";
            var encoderArgs = new SocketBufferFake();

            encoder.Prepare(expected);
            encoder.Send(encoderArgs);
            encoderArgs.BytesTransferred = encoderArgs.Count;
            decoder.ProcessReadBytes(encoderArgs);

            receivedMessage.Should().Be(expected);
        }
Example #5
0
        public void Send_500_messages()
        {
            var slice1   = new BufferSlice(new byte[65535], 0, 65535);
            var encoder1 = new MicroMessageEncoder(new DataContractMessageSerializer());
            var decoder1 = new MicroMessageDecoder(new DataContractMessageSerializer());
            var slice2   = new BufferSlice(new byte[65535], 0, 65535);
            var encoder2 = new MicroMessageEncoder(new DataContractMessageSerializer());
            var decoder2 = new MicroMessageDecoder(new DataContractMessageSerializer());
            var evt      = new ManualResetEvent(false);
            var messages = new List <object>();
            var sut1     = new TcpChannel(slice1, encoder1, decoder1);

            sut1.MessageReceived += (channel, message) => { };
            sut1.Assign(_helper.Client);
            var sut2 = new TcpChannel(slice2, encoder2, decoder2);

            sut2.MessageReceived += (channel, message) =>
            {
                messages.Add(message);
                if (messages.Count == 500)
                {
                    evt.Set();
                }
            };
            sut2.Assign(_helper.Server);

            for (int i = 0; i < 500; i++)
            {
                sut1.Send("Hello" + i + "".PadRight(1000));
            }

            sut1.Send("Hello world");

            evt.WaitOne(500).Should().BeTrue();
            for (int i = 0; i < 500; i++)
            {
                messages[i].ToString().Should().StartWith("Hello" + i);
            }
        }
Example #6
0
        public void two_messages()
        {
            var buf = new Byte[] //two msgs with "Hello world" as content
            {
                96, 0, 1, 88, 0, 0, 0, 90, 83, 121, 115, 116, 101, 109, 46, 83, 116, 114, 105, 110, 103, 44, 32, 109, 115,
                99, 111, 114, 108, 105, 98, 44, 32, 86, 101, 114, 115, 105, 111, 110, 61, 52, 46, 48, 46, 48, 46, 48, 44,
                32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, 108,
                105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 98, 55, 55, 97, 53, 99, 53, 54, 49, 57, 51, 52, 101,
                48, 56, 57, 60, 115, 116, 114, 105, 110, 103, 32, 120, 109, 108, 110, 115, 61, 34, 104, 116, 116, 112,
                58, 47, 47, 115, 99, 104, 101, 109, 97, 115, 46, 109, 105, 99, 114, 111, 115, 111, 102, 116, 46, 99, 111,
                109, 47, 50, 48, 48, 51, 47, 49, 48, 47, 83, 101, 114, 105, 97, 108, 105, 122, 97, 116, 105, 111, 110,
                47, 34, 62, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 60, 47, 115, 116, 114, 105, 110, 103,
                62,
                96, 0, 1, 88, 0, 0, 0, 90, 83, 121, 115, 116, 101, 109, 46, 83, 116, 114, 105, 110, 103, 44, 32, 109, 115,
                99, 111, 114, 108, 105, 98, 44, 32, 86, 101, 114, 115, 105, 111, 110, 61, 52, 46, 48, 46, 48, 46, 48, 44,
                32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, 108,
                105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 98, 55, 55, 97, 53, 99, 53, 54, 49, 57, 51, 52, 101,
                48, 56, 57, 60, 115, 116, 114, 105, 110, 103, 32, 120, 109, 108, 110, 115, 61, 34, 104, 116, 116, 112,
                58, 47, 47, 115, 99, 104, 101, 109, 97, 115, 46, 109, 105, 99, 114, 111, 115, 111, 102, 116, 46, 99, 111,
                109, 47, 50, 48, 48, 51, 47, 49, 48, 47, 83, 101, 114, 105, 97, 108, 105, 122, 97, 116, 105, 111, 110,
                47, 34, 62, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 60, 47, 115, 116, 114, 105, 110, 103,
                62
            };
            var buffer = new SocketBufferFake();

            buffer.SetBuffer(buf, 0, buf.Length);
            buffer.BytesTransferred = buf.Length;
            var msgs = new List <string>();

            var sut = new MicroMessageDecoder(new DataContractMessageSerializer());

            sut.MessageReceived = o => msgs.Add((string)o);
            sut.ProcessReadBytes(buffer);

            msgs.Count.Should().Be(2);
            msgs[0].Should().StartWith("Hello world");
            msgs[1].Should().StartWith("Hello world");
        }
Example #7
0
        public void codec_a_custom_type()
        {
            object receivedMessage = null;
            var    encoderSlice    = new BufferSlice(new byte[65535], 0, 65535);
            var    serializer      = new DataContractMessageSerializer();
            var    encoder         = new MicroMessageEncoder(serializer, encoderSlice);
            var    decoder         = new MicroMessageDecoder(serializer)
            {
                MessageReceived = o => receivedMessage = o
            };
            var expected = new CustomType {
                Name = "Arne"
            };
            var encoderArgs = new SocketBufferFake();

            encoder.Prepare(expected);
            encoder.Send(encoderArgs);
            encoderArgs.BytesTransferred = encoderArgs.Count;
            decoder.ProcessReadBytes(encoderArgs);

            receivedMessage.Should().BeOfType <CustomType>();
            receivedMessage.As <CustomType>().Name.Should().Be(expected.Name);
        }
Example #8
0
        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");
        }
Example #9
0
        public void codec_a_byte_buffer()
        {
            object receivedMessage = null;
            var    encoderSlice    = new BufferSlice(new byte[65535], 0, 65535);
            var    serializer      = new DataContractMessageSerializer();
            var    encoder         = new MicroMessageEncoder(serializer, encoderSlice);
            var    decoder         = new MicroMessageDecoder(serializer)
            {
                MessageReceived = o => receivedMessage = o
            };
            var expected    = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
            var encoderArgs = new SocketBufferFake();

            encoder.Prepare(expected);
            encoder.Send(encoderArgs);
            encoderArgs.BytesTransferred = encoderArgs.Count;
            decoder.ProcessReadBytes(encoderArgs);

            var ms  = (MemoryStream)receivedMessage;
            var buf = ms.GetBuffer();

            expected.Should().BeSubsetOf(buf);
            ms.Length.Should().Be(expected.Length);
        }