Esempio n. 1
0
        void DoParse(byte[] p_data)
        {
            // Utf8JsonReader不能用在异步方法内!
            Utf8JsonReader reader = new Utf8JsonReader(p_data);

            // [
            reader.Read();
            ApiName = reader.ReadAsString();
            if (string.IsNullOrEmpty(ApiName) || (Api = Silo.GetMethod(ApiName)) == null)
            {
                // 未找到对应方法
                throw new Exception($"Api方法“{ApiName}”不存在!");
            }

            var method = Api.Method.GetParameters();

            if (method.Length > 0)
            {
                // 确保和Api的参数个数、类型相同
                // 类型不同时 执行类型转换 或 直接创建派生类实例!如Row -> User, Table -> Table<User>
                int index = 0;
                Args = new object[method.Length];
                while (index < method.Length && reader.Read() && reader.TokenType != JsonTokenType.EndArray)
                {
                    // 参数支持派生类型!
                    Args[index] = JsonRpcSerializer.Deserialize(ref reader, method[index].ParameterType);
                    index++;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 解析结果,Utf8JsonReader不能用在异步方法内!
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="p_data"></param>
        /// <returns></returns>
        T ParseResult <T>(byte[] p_data)
        {
            T              val    = default(T);
            RpcResult      result = new RpcResult();
            Utf8JsonReader reader = new Utf8JsonReader(p_data);

            try
            {
                // [
                reader.Read();

                // 0成功,1错误,2警告提示
                result.ResultType = (RpcResultType)reader.ReadAsInt();

                // 耗时,非调试状态为0
                reader.Read();
                result.Elapsed = reader.GetInt32().ToString();

                reader.Read();
                if (result.ResultType == RpcResultType.Value)
                {
                    // 结果
                    val = JsonRpcSerializer.Deserialize <T>(ref reader);
                }
                else
                {
                    // 错误或提示信息
                    result.Info = reader.GetString();
                }
            }
            catch
            {
                result.ResultType = RpcResultType.Error;
                result.Info       = "返回Json内容结构不正确!";
            }

#if !SERVER
            // 输出监视信息
            string content = null;
            if (Kit.TraceRpc || result.ResultType == RpcResultType.Error)
            {
                // 输出详细内容
                content = Encoding.UTF8.GetString(p_data);
            }
            Kit.Trace(TraceOutType.RpcRecv, string.Format("{0}—{1}ms", _methodName, result.Elapsed), content, _svcName);

            // ⚡ 为服务器标志
            if (result.ResultType == RpcResultType.Message)
            {
                throw new KnownException("⚡" + result.Info);
            }
#endif

            if (result.ResultType == RpcResultType.Value)
            {
                return(val);
            }
            throw new ServerException("服务器异常", result.Info);
        }
Esempio n. 3
0
        static List <string> ParseResult(byte[] p_data)
        {
            // Utf8JsonReader不能用在异步方法内!
            var reader = new Utf8JsonReader(p_data);

            reader.Read();
            return(JsonRpcSerializer.Deserialize(ref reader) as List <string>);
        }
Esempio n. 4
0
        public void Call(string p_msg)
        {
            if (string.IsNullOrEmpty(p_msg))
            {
                Log.Warning("服务器推送内容为空");
                return;
            }

            string     method = "";
            MethodInfo mi     = null;

            object[] args = null;
            try
            {
                var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(p_msg));
                // [
                reader.Read();
                method = reader.ReadAsString();

                if (string.IsNullOrEmpty(method) || (mi = GetMethod(method.ToLower())) == null)
                {
                    Log.Warning($"服务器推送方法 {method} 不存在");
                    return;
                }

                var pars = mi.GetParameters();
                if (pars.Length > 0)
                {
                    // 确保和Api的参数个数、类型相同
                    // 类型不同时 执行类型转换 或 直接创建派生类实例!如Row -> User, Table -> Table<User>
                    int index = 0;
                    args = new object[pars.Length];
                    while (index < pars.Length && reader.Read() && reader.TokenType != JsonTokenType.EndArray)
                    {
                        // 参数支持派生类型!
                        args[index] = JsonRpcSerializer.Deserialize(ref reader, pars[index].ParameterType);
                        index++;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Warning(ex, "解析服务器推送时异常");
            }

            Kit.Trace(TraceOutType.ServerPush, $"{method}—推送", Kit.TraceRpc ? p_msg : null);

            try
            {
                object tgt = Activator.CreateInstance(mi.DeclaringType);
                Kit.RunAsync(() => mi.Invoke(tgt, args));
            }
            catch (Exception ex)
            {
                Log.Warning(ex, $"调用推送处理方法{method}时异常");
            }
        }
Esempio n. 5
0
        public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
        {
            // 删除临时文件
            if (File.Exists(_tempFile))
            {
                try
                {
                    File.Delete(_tempFile);
                }
                catch { }
            }

            var resp = task.Response as NSHttpUrlResponse;

            if (error != null ||
                resp == null ||
                (resp.StatusCode != 200 && resp.StatusCode != 201))
            {
                // 暂时简化错误提示
                string ex = $"{(resp == null ? "" : "状态码:" + resp.StatusCode.ToString() + "\r\n")}{(error == null ? "" : error.Description)}";
                _result.TrySetException(new Exception(ex));
                return;
            }

            var data = _dataResponse.ToArray();

            if (data != null && data.Length > 0)
            {
                var reader = new Utf8JsonReader(data);
                reader.Read();
                var ls = JsonRpcSerializer.Deserialize(ref reader) as List <string>;
                _result.SetResult(ls);
            }
            else
            {
                _result.SetResult(null);
            }
        }