Esempio n. 1
0
 private bool StreamInvocationMessagesEqual(StreamInvocationMessage x, StreamInvocationMessage y)
 {
     return(SequenceEqual(x.Headers, y.Headers) &&
            string.Equals(x.InvocationId, y.InvocationId, StringComparison.Ordinal) &&
            string.Equals(x.Target, y.Target, StringComparison.Ordinal) &&
            ArgumentListsEqual(x.Arguments, y.Arguments));
 }
Esempio n. 2
0
        private async Task InvokeStreamCore(string methodName, InvocationRequest irq, object[] args, CancellationToken cancellationToken)
        {
            AssertConnectionValid();

            Log.PreparingStreamingInvocation(_logger, irq.InvocationId, methodName, irq.ResultType.FullName, args.Length);

            var invocationMessage = new StreamInvocationMessage(irq.InvocationId, methodName, args);

            // I just want an excuse to use 'irq' as a variable name...
            Log.RegisteringInvocation(_logger, invocationMessage.InvocationId);

            _connectionState.AddInvocation(irq);

            // Trace the full invocation
            Log.IssuingInvocation(_logger, invocationMessage.InvocationId, irq.ResultType.FullName, methodName, args);

            try
            {
                await SendHubMessage(invocationMessage, cancellationToken);
            }
            catch (Exception ex)
            {
                Log.FailedToSendInvocation(_logger, invocationMessage.InvocationId, ex);
                _connectionState.TryRemoveInvocation(invocationMessage.InvocationId, out _);
                irq.Fail(ex);
            }
        }
Esempio n. 3
0
        public void Protocol_Should_Handle_StreamInvocationMessage_With_ProtobufObject_As_Argument(params string[] data)
        {
            var logger       = NullLogger <ProtobufHubProtocol> .Instance;
            var binder       = new Mock <IInvocationBinder>();
            var protobufType = new List <Type> {
                typeof(TestMessage)
            };

            var protobufHubProtocol = new ProtobufHubProtocol(protobufType, logger);
            var writer = new ArrayBufferWriter <byte>();

            var arguments = Helpers.GetProtobufTestMessages(data);
            var streamInvocationMessage = new StreamInvocationMessage(INVOCATION_ID, TARGET, arguments);

            protobufHubProtocol.WriteMessage(streamInvocationMessage, writer);
            var encodedMessage = new ReadOnlySequence <byte>(writer.WrittenSpan.ToArray());
            var result         = protobufHubProtocol.TryParseMessage(ref encodedMessage, binder.Object, out var resultStreamInvocationMessage);

            Assert.True(result);
            Assert.NotNull(resultStreamInvocationMessage);
            Assert.IsType <StreamInvocationMessage>(resultStreamInvocationMessage);
            Assert.Equal(INVOCATION_ID, ((StreamInvocationMessage)resultStreamInvocationMessage).InvocationId);
            Assert.Equal(TARGET, ((StreamInvocationMessage)resultStreamInvocationMessage).Target);

            var args = ((StreamInvocationMessage)resultStreamInvocationMessage).Arguments;

            Assert.NotEmpty(args);
            Assert.Equal(arguments.Length, args.Length);

            for (var i = 0; i < args.Length; i++)
            {
                Assert.Equal(arguments[i], args[i]);
            }
        }
 private bool StreamInvocationMessagesEqual(StreamInvocationMessage x, StreamInvocationMessage y)
 {
     return(string.Equals(x.InvocationId, y.InvocationId, StringComparison.Ordinal) &&
            string.Equals(x.Target, y.Target, StringComparison.Ordinal) &&
            ArgumentListsEqual(x.Arguments, y.Arguments) &&
            x.NonBlocking == y.NonBlocking);
 }
Esempio n. 5
0
        public void Protocol_Should_Handle_StreamInvocationMessage_With_Headers(params string[] kvp)
        {
            var logger       = NullLogger <ProtobufHubProtocol> .Instance;
            var binder       = new Mock <IInvocationBinder>();
            var protobufType = Array.Empty <Type>();

            var protobufHubProtocol = new ProtobufHubProtocol(protobufType, logger);
            var writer = new ArrayBufferWriter <byte>();

            var headers = Helpers.GetHeaders(kvp);
            var streamInvocationMessage = new StreamInvocationMessage(INVOCATION_ID, TARGET, new[] { "foo", "bar" })
            {
                Headers = headers
            };

            protobufHubProtocol.WriteMessage(streamInvocationMessage, writer);
            var encodedMessage = new ReadOnlySequence <byte>(writer.WrittenSpan.ToArray());
            var result         = protobufHubProtocol.TryParseMessage(ref encodedMessage, binder.Object, out var resultStreamInvocationMessage);

            Assert.True(result);
            Assert.NotNull(resultStreamInvocationMessage);
            Assert.IsType <StreamInvocationMessage>(resultStreamInvocationMessage);
            Assert.Equal(INVOCATION_ID, ((StreamInvocationMessage)resultStreamInvocationMessage).InvocationId);
            Assert.Equal(TARGET, ((StreamInvocationMessage)resultStreamInvocationMessage).Target);
            Assert.NotEmpty(((StreamInvocationMessage)resultStreamInvocationMessage).Arguments);
            Assert.Equal("bar", ((StreamInvocationMessage)resultStreamInvocationMessage).Arguments[1]);
            var resultHeaders = ((StreamInvocationMessage)resultStreamInvocationMessage).Headers;

            Assert.NotEmpty(resultHeaders);
            Assert.Equal(resultHeaders.Count, headers.Count);
            Assert.Equal(headers, resultHeaders);
        }
Esempio n. 6
0
        public void Protocol_Should_Handle_StreamInvocationMessage_With_StreamIds(params string[] streamIds)
        {
            var logger       = NullLogger <ProtobufHubProtocol> .Instance;
            var binder       = new Mock <IInvocationBinder>();
            var protobufType = Array.Empty <Type>();

            var protobufHubProtocol = new ProtobufHubProtocol(protobufType, logger);
            var writer = new ArrayBufferWriter <byte>();
            var streamInvocationMessage = new StreamInvocationMessage(INVOCATION_ID, TARGET, new[] { "foo", "bar" }, streamIds);

            protobufHubProtocol.WriteMessage(streamInvocationMessage, writer);
            var encodedMessage = new ReadOnlySequence <byte>(writer.WrittenSpan.ToArray());
            var result         = protobufHubProtocol.TryParseMessage(ref encodedMessage, binder.Object, out var resultStreamInvocationMessage);

            Assert.True(result);
            Assert.NotNull(resultStreamInvocationMessage);
            Assert.IsType <StreamInvocationMessage>(resultStreamInvocationMessage);
            Assert.Equal(TARGET, ((StreamInvocationMessage)resultStreamInvocationMessage).Target);
            Assert.Equal(INVOCATION_ID, ((StreamInvocationMessage)resultStreamInvocationMessage).InvocationId);
            Assert.NotEmpty(((StreamInvocationMessage)resultStreamInvocationMessage).Arguments);
            Assert.Equal("bar", ((StreamInvocationMessage)resultStreamInvocationMessage).Arguments[1]);

            var ids = ((StreamInvocationMessage)resultStreamInvocationMessage).StreamIds;

            Assert.NotEmpty(ids);
            Assert.Equal(streamIds.Length, ids.Length);

            for (var i = 0; i < ids.Length; i++)
            {
                Assert.Equal(streamIds[i], ids[i]);
            }
        }
Esempio n. 7
0
 private void WriteStreamInvocationMessage(StreamInvocationMessage streamInvocationMessage, Packer packer)
 {
     packer.PackArrayHeader(5);
     packer.Pack(HubProtocolConstants.StreamInvocationMessageType);
     packer.PackString(streamInvocationMessage.InvocationId);
     packer.PackDictionary(streamInvocationMessage.Metadata, SerializationContext);
     packer.PackString(streamInvocationMessage.Target);
     packer.PackObject(streamInvocationMessage.Arguments, SerializationContext);
 }
Esempio n. 8
0
        public void Setup()
        {
            var logger = NullLogger <ProtobufHubProtocol> .Instance;
            var types  = new[] { typeof(BenchMessage) };

            _hubProtocol = new ProtobufHubProtocol(types, logger);

            var data = new BenchMessage
            {
                Email  = "*****@*****.**",
                Data   = new string('@', 512),
                Length = 256,
                Price  = 23451.5436d,
                Time   = new Google.Protobuf.WellKnownTypes.Timestamp()
                {
                    Seconds = DateTime.UtcNow.Second
                }
            };

            switch (InputArgument)
            {
            case MessageArgument.NoArguments:
                _streamInvocationMessage = new StreamInvocationMessage("123", "BenchmarkTarget", Array.Empty <object>());
                break;

            case MessageArgument.IntArguments:
                _streamInvocationMessage = new StreamInvocationMessage("123", "BenchmarkTarget", new object[] { int.MinValue, 0, int.MaxValue });
                break;

            case MessageArgument.DoubleArguments:
                _streamInvocationMessage = new StreamInvocationMessage("123", "BenchmarkTarget", new object[] { double.MinValue, 0.5d, double.MaxValue });
                break;

            case MessageArgument.StringArguments:
                _streamInvocationMessage = new StreamInvocationMessage("123", "BenchmarkTarget", new object[] { "Foo", "Bar", new string('#', 512) });
                break;

            case MessageArgument.ProtobufArguments:
                _streamInvocationMessage = new StreamInvocationMessage("123", "BenchmarkTarget", new object[] { data, data, data });
                break;

            case MessageArgument.FewArguments:
                _streamInvocationMessage = new StreamInvocationMessage("123", "BenchmarkTarget", new object[] { data, "FooBar", 1 });
                break;

            case MessageArgument.ManyArguments:
                _streamInvocationMessage = new StreamInvocationMessage("123", "BenchmarkTarget", new object[] { data, "FooBar", 1, 234.543d, data, "*****@*****.**", 4242, 21.123456d, data });
                break;

            case MessageArgument.LargeArguments:
                data.Data = new string('@', 4096);
                _streamInvocationMessage = new StreamInvocationMessage("123", "BenchmarkTarget", new object[] { data, new string('L', 10240) });
                break;
            }

            _serializedMessageRef = _hubProtocol.GetMessageBytes(_streamInvocationMessage);
        }
Esempio n. 9
0
    private void WriteStreamInvocationMessage(StreamInvocationMessage message, Utf8JsonWriter writer)
    {
        WriteInvocationId(message, writer);
        writer.WriteString(TargetPropertyNameBytes, message.Target);

        WriteArguments(message.Arguments, writer);

        WriteStreamIds(message.StreamIds, writer);
    }
Esempio n. 10
0
    private void WriteStreamInvocationMessage(StreamInvocationMessage message, JsonTextWriter writer)
    {
        WriteInvocationId(message, writer);
        writer.WritePropertyName(TargetPropertyName);
        writer.WriteValue(message.Target);

        WriteArguments(message.Arguments, writer);

        WriteStreamIds(message.StreamIds, writer);
    }
Esempio n. 11
0
        private void WriteStreamInvocationMessage(StreamInvocationMessage message, JsonTextWriter writer)
        {
            writer.WriteStartObject();
            WriteHubInvocationMessageCommon(message, writer, HubProtocolConstants.StreamInvocationMessageType);
            writer.WritePropertyName(TargetPropertyName);
            writer.WriteValue(message.Target);

            WriteArguments(message.Arguments, writer);

            writer.WriteEndObject();
        }
Esempio n. 12
0
        private void WriteStreamInvocationMessage(StreamInvocationMessage message, ref MessagePackWriter writer)
        {
            writer.WriteArrayHeader(6);

            writer.Write(HubProtocolConstants.StreamInvocationMessageType);
            PackHeaders(ref writer, message.Headers);
            writer.Write(message.InvocationId);
            writer.Write(message.Target);

            writer.WriteArrayHeader(message.Arguments.Length);
            foreach (var arg in message.Arguments)
            {
                SerializeArgument(ref writer, arg);
            }

            WriteStreamIds(message.StreamIds, ref writer);
        }
Esempio n. 13
0
        private Task InvokeStreamCore(string methodName, InvocationRequest irq, object[] args)
        {
            ThrowIfConnectionTerminated(irq.InvocationId);

            Log.PreparingStreamingInvocation(_logger, irq.InvocationId, methodName, irq.ResultType.FullName, args.Length);

            var invocationMessage = new StreamInvocationMessage(irq.InvocationId, methodName,
                                                                argumentBindingException: null, arguments: args);

            // I just want an excuse to use 'irq' as a variable name...
            Log.RegisterInvocation(_logger, invocationMessage.InvocationId);

            AddInvocation(irq);

            // Trace the full invocation
            Log.IssueInvocation(_logger, invocationMessage.InvocationId, irq.ResultType.FullName, methodName, args);

            // We don't need to wait for this to complete. It will signal back to the invocation request.
            return(SendHubMessage(invocationMessage, irq));
        }
Esempio n. 14
0
        public void Protocol_Should_Handle_InvocationMessage_Without_Argument(string invocationId, string target)
        {
            var logger       = NullLogger <ProtobufHubProtocol> .Instance;
            var binder       = new Mock <IInvocationBinder>();
            var protobufType = Array.Empty <Type>();

            var protobufHubProtocol = new ProtobufHubProtocol(protobufType, logger);
            var writer = new ArrayBufferWriter <byte>();
            var streamInvocationMessage = new StreamInvocationMessage(invocationId, target, Array.Empty <object>());

            protobufHubProtocol.WriteMessage(streamInvocationMessage, writer);
            var encodedMessage = new ReadOnlySequence <byte>(writer.WrittenSpan.ToArray());
            var result         = protobufHubProtocol.TryParseMessage(ref encodedMessage, binder.Object, out var resultStreamInvocationMessage);

            Assert.True(result);
            Assert.NotNull(resultStreamInvocationMessage);
            Assert.IsType <StreamInvocationMessage>(resultStreamInvocationMessage);
            Assert.Equal(invocationId, ((StreamInvocationMessage)resultStreamInvocationMessage).InvocationId);
            Assert.Equal(target, ((StreamInvocationMessage)resultStreamInvocationMessage).Target);
            Assert.Empty(((StreamInvocationMessage)resultStreamInvocationMessage).Arguments);
        }
Esempio n. 15
0
    private void WriteStreamInvocationMessage(StreamInvocationMessage message, ref MessagePackWriter writer)
    {
        writer.WriteArrayHeader(6);

        writer.Write(HubProtocolConstants.StreamInvocationMessageType);
        PackHeaders(message.Headers, ref writer);
        writer.Write(message.InvocationId);
        writer.Write(message.Target);

        if (message.Arguments is null)
        {
            writer.WriteArrayHeader(0);
        }
        else
        {
            writer.WriteArrayHeader(message.Arguments.Length);
            foreach (var arg in message.Arguments)
            {
                WriteArgument(arg, ref writer);
            }
        }

        WriteStreamIds(message.StreamIds, ref writer);
    }
        private void WriteStreamInvocationMessage(StreamInvocationMessage streamInvocationMessage, IBufferWriter <byte> output)
        {
            var protobufStreamInvocationMessage = new StreamInvocationMessageProtobuf
            {
                InvocationId = streamInvocationMessage.InvocationId,
                Target       = streamInvocationMessage.Target
            };

            if (streamInvocationMessage.Headers != null)
            {
                protobufStreamInvocationMessage.Headers.Add(streamInvocationMessage.Headers);
            }

            if (streamInvocationMessage.StreamIds != null)
            {
                protobufStreamInvocationMessage.StreamIds.Add(streamInvocationMessage.StreamIds.Select(id => id ?? ""));
            }

            var arguments = _argumentSerializer.SerializeArguments(streamInvocationMessage.Arguments);

            var packedMessage = MessageDescriptor.PackMessage(HubProtocolConstants.StreamInvocationMessageType, protobufStreamInvocationMessage.ToByteArray(), arguments);

            output.Write(packedMessage);
        }
        public void StreamInvocationMessageToStringPutsErrorInArgs()
        {
            var hubMessage = new StreamInvocationMessage("3", "echo", ExceptionDispatchInfo.Capture(new Exception("test")), null);

            Assert.Equal("StreamInvocation { InvocationId: \"3\", Target: \"echo\", Arguments: [ Error: test ] }", hubMessage.ToString());
        }
Esempio n. 18
0
 public static partial void ReceivedStreamHubInvocation(ILogger logger, StreamInvocationMessage invocationMessage);
Esempio n. 19
0
 public static void ReceivedStreamHubInvocation(ILogger logger, StreamInvocationMessage invocationMessage)
 {
     _receivedStreamHubInvocation(logger, invocationMessage, null);
 }
Esempio n. 20
0
        private HubMessage ParseMessage(string message)
        {
            ReceivedMessage receivedMessage;

            try
            {
                receivedMessage = JsonConvert.DeserializeObject <ReceivedMessage>(message, PayloadSerializerSettings);
                if (receivedMessage.Type == null ||
                    receivedMessage.Type < HubProtocolConstants.InvocationMessageType ||
                    receivedMessage.Type > HubProtocolConstants.CloseMessageType)
                {
                    throw new JsonSerializationException($"type:{receivedMessage.Type}");
                }
            }
            catch (JsonSerializationException)
            {
                Log.Debug("客户端与服务器的协议不一致");
                throw;
            }
            HubMessage hubMessage = null;

            switch (receivedMessage.Type)
            {
            case HubProtocolConstants.InvocationMessageType:
            {
                try
                {
                    InvocationMessage invocation = JsonConvert.DeserializeObject <InvocationMessage>(message, PayloadSerializerSettings);
                    hubMessage = invocation;
                }
                catch (JsonSerializationException)
                {
                    Log.Debug($"客户端与服务器的协议不一致。消息类型:{HubProtocolConstants.InvocationMessageType.ToString()}");
                    throw;
                }
            }
            break;

            case HubProtocolConstants.StreamItemMessageType:
            {
                try
                {
                    StreamItemMessage streamItem = JsonConvert.DeserializeObject <StreamItemMessage>(message, PayloadSerializerSettings);
                    hubMessage = streamItem;
                }
                catch (JsonSerializationException)
                {
                    Log.Debug($"客户端与服务器的协议不一致。消息类型:{HubProtocolConstants.StreamItemMessageType.ToString()}");
                    throw;
                }
            }
            break;

            case HubProtocolConstants.CompletionMessageType:
            {
                try
                {
                    CompletionMessage completion = JsonConvert.DeserializeObject <CompletionMessage>(message, PayloadSerializerSettings);
                    if (completion.Result != null)
                    {
                        completion.HasResult = true;
                    }
                    hubMessage = completion;
                }
                catch (JsonSerializationException)
                {
                    Log.Debug($"客户端与服务器的协议不一致。消息类型:{HubProtocolConstants.CompletionMessageType.ToString()}");
                    throw;
                }
            }
            break;

            case HubProtocolConstants.StreamInvocationMessageType:
            {
                try
                {
                    StreamInvocationMessage streamInvocation = JsonConvert.DeserializeObject <StreamInvocationMessage>(message, PayloadSerializerSettings);
                    hubMessage = streamInvocation;
                }
                catch (JsonSerializationException)
                {
                    Log.Debug($"客户端与服务器的协议不一致。消息类型:{HubProtocolConstants.StreamInvocationMessageType.ToString()}");
                    throw;
                }
            }
            break;

            case HubProtocolConstants.CancelInvocationMessageType:
            {
                try
                {
                    CancelInvocationMessage cancelInvocation = JsonConvert.DeserializeObject <CancelInvocationMessage>(message, PayloadSerializerSettings);
                    hubMessage = cancelInvocation;
                }
                catch (JsonSerializationException)
                {
                    Log.Debug($"客户端与服务器的协议不一致。消息类型:{HubProtocolConstants.CancelInvocationMessageType.ToString()}");
                    throw;
                }
            }
            break;

            case HubProtocolConstants.PingMessageType:
            {
                hubMessage = PingMessage.Instance;
            }
            break;

            case HubProtocolConstants.CloseMessageType:
            {
                try
                {
                    CloseMessage close = JsonConvert.DeserializeObject <CloseMessage>(message, PayloadSerializerSettings);
                    hubMessage = close;
                }
                catch (JsonSerializationException)
                {
                    Log.Debug($"客户端与服务器的协议不一致。消息类型:{HubProtocolConstants.CloseMessageType.ToString()}");
                    throw;
                }
            }
            break;

            default:
            {
                Log.Debug("客户端与服务器的协议不一致。未知的消息类型!");
            }
            break;
            }

            return(hubMessage);
        }