Example #1
0
        private void ProcessResults(MessageResult result)
        {
            result.Messages.Enumerate(message => message.IsCommand || message.IsAck,
                                      message =>
            {
                if (message.IsAck)
                {
                    _ackHandler.TriggerAck(message.CommandId);
                }
                else
                {
                    var command = _serializer.Parse <Command>(message.Value);
                    ProcessCommand(command);

                    // Only send the ack if this command is waiting for it
                    if (message.WaitForAck)
                    {
                        // If we're on the same box and there's a pending ack for this command then
                        // just trip it
                        if (!_ackHandler.TriggerAck(message.CommandId))
                        {
                            _bus.Ack(_connectionId, message.Key, message.CommandId).Catch();
                        }
                    }
                }
            });
        }
Example #2
0
        internal static bool TryGetCommand(Message message, IJsonSerializer serializer, out SignalCommand command)
        {
            command = null;
            if (!message.SignalKey.EndsWith(SignalrCommand, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }

            command = message.Value as SignalCommand;

            // Optimization for in memory message store
            if (command != null)
            {
                return true;
            }

            // Otherwise deserialize the message value
            string rawValue = message.Value as string;
            if (rawValue == null)
            {
                return false;
            }

            command = serializer.Parse<SignalCommand>(rawValue);
            return true;
        }
        /// <summary>
        /// Deserializes the JSON to a .NET object.
        /// </summary>
        /// <param name="serializer">The serializer</param>
        /// <typeparam name="T">The <see cref="System.Type"/> of object being deserialized.</typeparam>
        /// <param name="json">The JSON to deserialize</param>
        /// <returns>The deserialized object from the JSON string.</returns>
        public static T Parse <T>(this IJsonSerializer serializer, string json)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            return((T)serializer.Parse(json, typeof(T)));
        }
        /// <summary>
        /// Convert user token policy to service model
        /// </summary>
        /// <param name="policy"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public static AuthenticationMethodModel ToServiceModel(this UserTokenPolicy policy,
                                                               IJsonSerializer serializer)
        {
            if (policy == null)
            {
                return(null);
            }
            var result = new AuthenticationMethodModel {
                Id             = policy.PolicyId,
                SecurityPolicy = policy.SecurityPolicyUri
            };

            switch (policy.TokenType)
            {
            case UserTokenType.Anonymous:
                result.CredentialType = CredentialType.None;
                break;

            case UserTokenType.UserName:
                result.CredentialType = CredentialType.UserName;
                break;

            case UserTokenType.Certificate:
                result.CredentialType = CredentialType.X509Certificate;
                result.Configuration  = policy.IssuerEndpointUrl;
                break;

            case UserTokenType.IssuedToken:
                switch (policy.IssuedTokenType)
                {
                case "http://opcfoundation.org/UA/UserToken#JWT":
                    result.CredentialType = CredentialType.JwtToken;
                    try {
                        // See part 6
                        result.Configuration = serializer.Parse(
                            policy.IssuerEndpointUrl);
                    }
                    catch {
                        // Store as string
                        result.Configuration = policy.IssuerEndpointUrl;
                    }
                    break;

                default:
                    // TODO
                    return(null);
                }
                break;

            default:
                return(null);
            }
            return(result);
        }
Example #5
0
        private Task <bool> HandleServerCommands(MessageResult result)
        {
            result.Messages.Enumerate(m => ServerSignal.Equals(m.Key),
                                      m =>
            {
                var command = _serializer.Parse <ServerCommand>(m.Value);
                OnCommand(command);
            });

            return(TaskAsyncHelper.True);
        }
Example #6
0
        public static object Unwrap(object value, IJsonSerializer serializer)
        {
            var wrappedValue = value as WrappedValue;

            if (wrappedValue != null)
            {
                return(wrappedValue.Value);
            }

            return(serializer.Parse((string)value));
        }
Example #7
0
        private Task <bool> HandleServerCommands(MessageResult result, object state)
        {
            result.Messages.Enumerate <object>(m => ServerSignal.Equals(m.Key),
                                               (s, m) =>
            {
                var command = _serializer.Parse <ServerCommand>(m.Value, m.Encoding);
                OnCommand(command);
            },
                                               state: null);

            return(TaskAsyncHelper.True);
        }
        /// <summary>
        /// Deserializes the JSON to a .NET object.
        /// </summary>
        /// <param name="serializer">The serializer</param>
        /// <typeparam name="T">The <see cref="System.Type"/> of object being deserialized.</typeparam>
        /// <param name="jsonBuffer">The JSON buffer to deserialize</param>
        /// <param name="encoding">The encoding to use.</param>
        /// <returns>The deserialized object from the JSON string.</returns>
        public static T Parse <T>(this IJsonSerializer serializer, ArraySegment <byte> jsonBuffer, Encoding encoding)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            using (var reader = new ArraySegmentTextReader(jsonBuffer, encoding))
            {
                return((T)serializer.Parse(reader, typeof(T)));
            }
        }
        /// <summary>
        /// Deserializes the JSON to a .NET object.
        /// </summary>
        /// <param name="serializer">The serializer</param>
        /// <typeparam name="T">The <see cref="System.Type"/> of object being deserialized.</typeparam>
        /// <param name="json">The JSON to deserialize</param>
        /// <returns>The deserialized object from the JSON string.</returns>
        public static T Parse <T>(this IJsonSerializer serializer, string json)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            using (var reader = new StringReader(json))
            {
                return((T)serializer.Parse(reader, typeof(T)));
            }
        }
Example #10
0
        private SignalCommand GetCommand(Message message)
        {
            var command = message.Value as SignalCommand;

            // Optimization for in memory message store
            if (command != null)
            {
                return(command);
            }

            // Otherwise deserialize the message value
            string value = message.Value as string;

            if (value == null)
            {
                return(null);
            }

            return(_jsonSerializer.Parse <SignalCommand>(value));
        }
Example #11
0
        private Task <bool> HandleServerCommands(MessageResult result)
        {
            for (int i = 0; i < result.Messages.Count; i++)
            {
                for (int j = result.Messages[i].Offset; j < result.Messages[i].Offset + result.Messages[i].Count; j++)
                {
                    Message message = result.Messages[i].Array[j];

                    // Only handle server commands
                    if (ServerSignal.Equals(message.Key))
                    {
                        // Uwrap the command and raise the event
                        var command = _serializer.Parse <ServerCommand>(message.Value);
                        OnCommand(command);
                    }
                }
            }

            return(TaskAsyncHelper.True);
        }
Example #12
0
 /// <summary>
 /// Deserializes the JSON to a .NET object.
 /// </summary>
 /// <param name="serializer">The serializer</param>
 /// <typeparam name="T">The <see cref="System.Type"/> of object being deserialized.</typeparam>
 /// <param name="json">The JSON to deserialize</param>
 /// <returns>The deserialized object from the JSON string.</returns>
 public static T Parse <T>(this IJsonSerializer serializer, string json)
 {
     return((T)serializer.Parse(json, typeof(T)));
 }