Пример #1
0
        /// <summary>
        /// 客户端往服务端发送数据时对数据进行加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public override Task Send(string data)
        {
            if (_cryptor == null)
            {
                throw new InvalidOperationException("通信加密尚未初始化。");
            }
            //加密传输数据
            HubInvocation hubData = this.JsonDeserializeObject <HubInvocation>(data);

            JToken[] args = hubData.Args;
            if (args != null && args.Length > 0)
            {
                string        encrypt = _cryptor.EncryptData(this.JsonSerializeObject(args));
                List <JToken> @params = new List <JToken>()
                {
                    JToken.FromObject(encrypt, JsonSerializer)
                };
                if (args.Length > 1)
                {
                    @params = @params.Concat(new JToken[args.Length - 1]).ToList();
                }
                hubData.Args = @params.ToArray();
                data         = this.JsonSerializeObject(hubData);
            }

            return(base.Send(data));
        }
Пример #2
0
        /// <summary>
        /// 客户端往服务端发送数据时对数据进行加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public override Task Send(string data)
        {
            if (_cryptor == null)
            {
                throw new InvalidOperationException("通信加密尚未初始化。");
            }
            //加密传输数据
            HubInvocation hubData = this.JsonDeserializeObject <HubInvocation>(data);

            JToken[] args = hubData.Args;
            if (args.Length > 0)
            {
                string encrypt = _cryptor.EncryptData(this.JsonSerializeObject(args));
                hubData.Args = new[] { JToken.FromObject(encrypt, JsonSerializer) };
                data         = this.JsonSerializeObject(hubData);
            }

            return(base.Send(data));
        }
Пример #3
0
        private static IDictionary<string, object> GetState(HubInvocation deserializedData)
        {
            if (deserializedData.State == null)
            {
                return new Dictionary<string, object>();
            }

            // Get the raw JSON string and check if it's over 4K
            string json = deserializedData.State.ToString();

            if (json.Length > 4096)
            {
                throw new InvalidOperationException(Resources.Error_StateExceededMaximumLength);
            }

            var settings = JsonUtility.CreateDefaultSerializerSettings();
            settings.Converters.Add(new SipHashBasedDictionaryConverter());
            var serializer = JsonSerializer.Create(settings);
            return serializer.Parse<IDictionary<string, object>>(json);
        }
        private static RunData InitializeHubInvocationPayload(RunData runData)
        {
            var jsonSerializer = new JsonSerializer();

            var hubInvocation = new HubInvocation
            {
                Hub        = "EchoHub",
                Method     = "Echo",
                Args       = new[] { JToken.FromObject(runData.Payload, jsonSerializer) },
                CallbackId = ""
            };

            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                jsonSerializer.Serialize(writer, hubInvocation);
                runData.Payload = writer.ToString();
            }

            return(runData);
        }
Пример #5
0
        private static IDictionary <string, object> GetState(HubInvocation deserializedData)
        {
            if (deserializedData.State == null)
            {
                return(new Dictionary <string, object>());
            }

            // Get the raw JSON string and check if it's over 4K
            string json = deserializedData.State.ToString();

            if (json.Length > 4096)
            {
                throw new InvalidOperationException(Resources.Error_StateExceededMaximumLength);
            }

            var settings = JsonUtility.CreateDefaultSerializerSettings();

            settings.Converters.Add(new SipHashBasedDictionaryConverter());
            var serializer = JsonSerializer.Create(settings);

            return(serializer.Parse <IDictionary <string, object> >(json));
        }
Пример #6
0
        /// <summary>
        /// 接收到服务端数据之后对数据进行解密
        /// </summary>
        /// <param name="message"></param>
        protected override void OnMessageReceived(JToken message)
        {
            if (_cryptor == null)
            {
                throw new InvalidOperationException("通信加密尚未初始化。");
            }
            if (message["P"] == null && message["I"] == null)
            {
                HubInvocation invocation = message.ToObject <HubInvocation>(JsonSerializer);
                if (invocation.Args.Length == 1)
                {
                    string encrypt = invocation.Args[0].ToString();
                    if (!encrypt.StartsWith("{") && !encrypt.StartsWith("["))
                    {
                        string   json = _cryptor.DecryptAndVerifyData(encrypt);
                        JToken[] args = this.JsonDeserializeObject <JToken[]>(json);
                        message["A"] = JToken.FromObject(args, JsonSerializer);
                    }
                }
            }

            base.OnMessageReceived(message);
        }
        protected override void OnMessageReceived(JSONNode message)
        {
            // We have to handle progress updates first in order to ensure old clients that receive
            // progress updates enter the return value branch and then no-op when they can't find
            // the callback in the map (because the message["I"[ value will not be a valid callback ID)
            if (message["P"] != null)
            {
                var result = HubResult.FromJson(message);
                Action <HubResult> callback;

                lock (_callbacks)
                {
                    if (!_callbacks.TryGetValue(result.ProgressUpdate.Id, out callback))
                    {
                        Trace(TraceLevels.Messages, "Callback with id " + result.ProgressUpdate.Id + " not found!");
                    }
                }

                callback?.Invoke(result);
            }
            else if (message["I"] != null)
            {
                var result = HubResult.FromJson(message);
                Action <HubResult> callback;

                lock (_callbacks)
                {
                    if (_callbacks.TryGetValue(result.Id, out callback))
                    {
                        _callbacks.Remove(result.Id);
                    }
                    else
                    {
                        Trace(TraceLevels.Messages, "Callback with id " + result.Id + " not found!");
                    }
                }

                if (callback != null)
                {
                    callback(result);
                }
            }
            else
            {
                var      invocation = HubInvocation.FromJson(message);
                HubProxy hubProxy;
                if (_hubs.TryGetValue(invocation.Hub, out hubProxy))
                {
                    if (invocation.State != null)
                    {
                        foreach (var state in invocation.State)
                        {
                            hubProxy[state.Key] = state.Value;
                        }
                    }

                    hubProxy.InvokeEvent(invocation.Method, invocation.Args);
                }

                base.OnMessageReceived(message);
            }
        }