public async Task Should_be_able_to_process_two_messages_from_the_same_receive()
        {
            var serializer = new JsonTransportSerializer();
            var encoder    = new SimpleProtocolEncoder(serializer);
            var msg1       = new TransportMessage("Test1");
            var msg2       = new TransportMessage("Test2");
            var sender     = Substitute.For <ISendState>();
            await encoder.EncodeMessage(sender, msg1);

            await encoder.EncodeMessage(sender, msg2);

            var buffer       = new byte[65535];
            var bufferOffset = 0;

            foreach (var call in sender.ReceivedCalls())
            {
                var buf    = call.GetArguments()[0].As <byte[]>();
                var offset = call.GetArguments()[1].As <int>();
                var len    = call.GetArguments()[2].As <int>();
                Buffer.BlockCopy(buf, offset, buffer, bufferOffset, len);
                bufferOffset += len;
            }
            var state = new TestReceiveState();

            state.AddBuffer(buffer, 0, bufferOffset);
            state.WriteOffset = bufferOffset;

            var sut     = new SimpleProtocolDecoder(serializer);
            var actual1 = (await sut.Decode(state)).As <TransportMessage>();
            var actual2 = (await sut.Decode(state)).As <TransportMessage>();

            actual1.Body.Should().Be("Test1");
            actual2.Body.Should().Be("Test2");
        }
Пример #2
0
        public TcpMessagingClient()
        {
            _writeArgs      = new SocketAsyncEventArgs();
            _writeAwaitable = new SocketAwaitable(_writeArgs);

            _readArgs      = new SocketAsyncEventArgs();
            _readAwaitable = new SocketAwaitable(_readArgs);

            _encoder = new SimpleProtocolEncoder(_serializer);
            _decoder = new SimpleProtocolDecoder(_serializer);
        }
Пример #3
0
        public MessagingListenerClient(Socket socket, IMessageHandlerInvoker messageHandlerInvoker)
        {
            _socket = socket ?? throw new ArgumentNullException(nameof(socket));
            _messageHandlerInvoker = messageHandlerInvoker;

            // For inbound data
            _readAwaitable = new SocketAwaitable(_readArgs);
            _readArgs.SetBuffer(_readBuffer, 0, _readBuffer.Length);
            _receiver = new SocketReceiver(socket, _readArgs, _readAwaitable, _readBuffer);
            _decoder  = new SimpleProtocolDecoder(_serializer);

            // For outbound data
            _encoder        = new SimpleProtocolEncoder(_serializer);
            _writeArgs      = new SocketAsyncEventArgs();
            _writeAwaitable = new SocketAwaitable(_writeArgs);
            _sendState      = new SocketSender(_socket, _writeArgs, _writeAwaitable);
        }
        public async Task Should_be_able_to_encode_ack()
        {
            var serializer = new JsonTransportSerializer();
            var expectedId = Guid.NewGuid();
            var sender     = Substitute.For <ISendState>();

            var sut = new SimpleProtocolEncoder(serializer);
            await sut.EncodeAck(sender, expectedId);

            var sendBuffer = sender.ReceivedCalls().First().GetArguments()[0].As <byte[]>();
            var guidBytes  = new byte[16];

            Buffer.BlockCopy(sendBuffer, 1, guidBytes, 0, 16);
            var actual = new Guid(guidBytes);

            actual.Should().Be(expectedId);
        }
        public async Task Should_be_able_to_serialize_content()
        {
            var serialiser = new JsonTransportSerializer();
            var sender     = Substitute.For <ISendState>();
            var msg        = new TransportMessage("Hello world");

            var sut = new SimpleProtocolEncoder(serialiser);
            await sut.EncodeMessage(sender, msg);

            var args        = sender.ReceivedCalls().First().GetArguments();
            var headerBytes = args[0].As <byte[]>();

            FindHeader("Content-Type", "application/json", headerBytes).Should().BeTrue();
            FindHeader("Content-Length", msg.Headers["Content-Length"], headerBytes).Should().BeTrue();
            FindHeader("Type-Name", msg.Headers["Type-Name"], headerBytes).Should().BeTrue();

            args = sender.ReceivedCalls().Last().GetArguments();
            var len = (int)args[2];

            Encoding.UTF8.GetString(args[0].As <byte[]>(), 0, len).Should().Be("\"Hello world\"");
        }
        public async Task Should_be_able_to_encode_and_decode()
        {
            var serialiser = new JsonTransportSerializer();
            var sender     = Substitute.For <ISendState>();
            var msg        = new TransportMessage("Hello world");

            var sut = new SimpleProtocolEncoder(serialiser);
            await sut.EncodeMessage(sender, msg);

            var headerCall = sender.ReceivedCalls().First().GetArguments();
            var bodyCall   = sender.ReceivedCalls().Last().GetArguments();

            var receiveState = new TestReceiveState();

            receiveState.AddBuffer(headerCall[0].As <byte[]>(), 0, headerCall[2].As <int>());
            receiveState.AddBuffer(bodyCall[0].As <byte[]>(), 0, bodyCall[2].As <int>());
            var decoder = new SimpleProtocolDecoder(serialiser);
            var actual  = await decoder.Decode(receiveState);

            var actualMessage = actual as TransportMessage;

            actualMessage.Body.Should().Be("Hello world");
        }