diff --git a/samples/HessianReader/Program.cs b/samples/HessianReader/Program.cs index f41dbd6..0bffc92 100644 --- a/samples/HessianReader/Program.cs +++ b/samples/HessianReader/Program.cs @@ -33,9 +33,22 @@ namespace HessianReader } Console.WriteLine("------------------------------------------------------------"); - Console.ReadKey(); + + RpcResponse response = new RpcResponse { + RequestId = Guid.NewGuid().ToString("N"), Result = ReturnT.Failed("ABCDEFG") + }; + using (var stream2 = new MemoryStream()) + { + HessianSerializer.SerializeResponse(stream2,response); + stream2.Position = 0; + + var s2 =HessianSerializer.DeserializeResponse(stream2); + Console.WriteLine(JsonConvert.SerializeObject(s2)); + } + Console.WriteLine("------------------------------------------------------------"); + Console.ReadKey(); /** * * Console.WriteLine("---------------------------------------------------------------"); diff --git a/src/DotXxlJob.Core/AdminClient.cs b/src/DotXxlJob.Core/AdminClient.cs index 52dc0fe..3474865 100644 --- a/src/DotXxlJob.Core/AdminClient.cs +++ b/src/DotXxlJob.Core/AdminClient.cs @@ -114,25 +114,29 @@ namespace DotXxlJob.Core continue; } - RpcResponse res; + RpcResponse res = null; try { res = HessianSerializer.DeserializeResponse(resStream); } catch (Exception ex) { - _logger.LogError("des"); + _logger.LogError(ex,"DeserializeResponse error:"+ex.Message); + } + + if (res == null) + { + + return ReturnT.Failed("response is nul"); } if (res.IsError) { - throw new Exception(res.error); - } - else - { - return rpcResponse.result as ReturnT; + return ReturnT.Failed(res.ErrorMsg); } + + return res.Result as ReturnT; } } throw new Exception("xxl-rpc server address not accessable."); diff --git a/src/DotXxlJob.Core/Constants.cs b/src/DotXxlJob.Core/Constants.cs index 0ecb03e..80de582 100644 --- a/src/DotXxlJob.Core/Constants.cs +++ b/src/DotXxlJob.Core/Constants.cs @@ -5,6 +5,7 @@ namespace DotXxlJob.Core internal static class Constants { public const string RpcRequestJavaFullName = "com.xxl.rpc.remoting.net.params.XxlRpcRequest"; + public const string RpcResponseJavaFullName = "com.xxl.rpc.remoting.net.params.XxlRpcResponse"; public const string XxlLogsDefaultRootDirectory = "xxl-job-logs"; public const string HandleLogsDirectory = "HandlerLogs"; @@ -23,7 +24,7 @@ namespace DotXxlJob.Core //Admin集群中的某台机器熔断后间隔多长时间再重试 public static TimeSpan AdminServerReconnectInterval = TimeSpan.FromMinutes(3); //Admin集群中的某台机器请求失败多少次后熔断 - public const int AdminServerCircuitFaildTimes = 3; + public const int AdminServerCircuitFailedTimes = 3; public static TimeSpan JobThreadWaitTime = TimeSpan.FromSeconds(90); diff --git a/src/DotXxlJob.Core/Enums.cs b/src/DotXxlJob.Core/Enums.cs deleted file mode 100644 index 7080897..0000000 --- a/src/DotXxlJob.Core/Enums.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace DotXxlJob.Core -{ - -} \ No newline at end of file diff --git a/src/DotXxlJob.Core/HessianSerializer.cs b/src/DotXxlJob.Core/HessianSerializer.cs index e0abe71..c14342c 100644 --- a/src/DotXxlJob.Core/HessianSerializer.cs +++ b/src/DotXxlJob.Core/HessianSerializer.cs @@ -18,6 +18,11 @@ namespace DotXxlJob.Core private static readonly Dictionary triggerProperties = new Dictionary(); + private static readonly Dictionary responseProperties = + new Dictionary(); + + private static readonly Dictionary returnProperties = + new Dictionary(); static HessianSerializer() { var typeInfo = typeof(RpcRequest).GetTypeInfo(); @@ -55,6 +60,42 @@ namespace DotXxlJob.Core triggerProperties.Add(attribute.Name,property); } + + var rspTypeInfo = typeof(RpcResponse).GetTypeInfo(); + foreach (var property in rspTypeInfo.DeclaredProperties) + { + var attribute = property.GetCustomAttribute(); + + if (null == attribute) + { + continue; + } + + if (!property.CanRead || !property.CanWrite) + { + continue; + } + + responseProperties.Add(attribute.Name,property); + } + + var retTypeInfo = typeof(ReturnT).GetTypeInfo(); + foreach (var property in retTypeInfo.DeclaredProperties) + { + var attribute = property.GetCustomAttribute(); + + if (null == attribute) + { + continue; + } + + if (!property.CanRead || !property.CanWrite) + { + continue; + } + + returnProperties.Add(attribute.Name,property); + } } public static RpcRequest DeserializeRequest(Stream stream) @@ -87,7 +128,11 @@ namespace DotXxlJob.Core } else { - if (item.Item2 is HessianObject ) + if (item.Item1 == "parameterTypes") + { + request.ParameterTypes = item.Item2 as List; + } + else if (item.Item2 is HessianObject ) { request.Parameters = new List(); @@ -100,7 +145,7 @@ namespace DotXxlJob.Core } else { - throw new HessianException($"unknown item :{item.Item1}"); + throw new HessianException($"unknown item :{item.Item1},{item.Item2.GetType()}"); } } } @@ -175,7 +220,7 @@ namespace DotXxlJob.Core return true; } - if (typeof (String) == typeInfo.AsType()) + if (typeof (string) == typeInfo.AsType()) { return true; } @@ -185,7 +230,76 @@ namespace DotXxlJob.Core public static RpcResponse DeserializeResponse(Stream resStream) { - + var rsp = new RpcResponse(); + + try + { + var deserializer = new Deserializer(resStream); + var classDef = deserializer.ReadValue() as ClassDef; + if (!Constants.RpcResponseJavaFullName.Equals(classDef.Name)) + { + throw new HessianException($"unknown class :{classDef.Name}"); + } + if (responseProperties.Count != classDef.Fields.Length) + { + throw new HessianException($"unknown class :{classDef.Name}, field count not match ${responseProperties.Count} !={classDef.Fields.Length}"); + } + + //obj serialize + if (deserializer.ReadValue() is HessianObject hessianObject) + { + foreach (var item in hessianObject) + { + if (responseProperties.TryGetValue(item.Item1, out var p)) + { + if (IsSimpleType(p.PropertyType.GetTypeInfo())) + { + p.SetValue(rsp,item.Item2); + } + else + { + if (item.Item2 is ClassDef resultClassDef ) + { + + //TODO:这里要做成动态的话 ,可以注册所有的实体到对应的字典中,不过这里只有这个类型哦 + if (resultClassDef.Name != "com.xxl.job.core.biz.model.ReturnT") + { + throw new HessianException($"not expected parameter type [{resultClassDef.Name}]"); + } + + if (!(deserializer.ReadValue() is HessianObject resultData)) + { + throw new HessianException("not expected parameter type ,data is null"); + } + ReturnT data = new ReturnT(); + foreach (var field in resultData) + { + if (returnProperties.TryGetValue(field.Item1, out var tgPropertyInfo)) + { + tgPropertyInfo.SetValue(data,field.Item2); + } + } + + rsp.Result = data; + } + else + { + throw new HessianException($"unknown item :{item.Item1},{item.Item2.GetType()}"); + } + } + } + + } + } + + + } + catch (EndOfStreamException) + { + //没有数据可读了 + } + + return rsp; } } diff --git a/src/DotXxlJob.Core/LogResult.cs b/src/DotXxlJob.Core/LogResult.cs deleted file mode 100644 index 42ba8c9..0000000 --- a/src/DotXxlJob.Core/LogResult.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DotXxlJob.Core -{ - public class LogResult - { - - } -} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Model/AddressEntity.cs b/src/DotXxlJob.Core/Model/AddressEntity.cs index aa2399d..5f885e6 100644 --- a/src/DotXxlJob.Core/Model/AddressEntity.cs +++ b/src/DotXxlJob.Core/Model/AddressEntity.cs @@ -18,7 +18,7 @@ namespace DotXxlJob.Core.Model if (DateTime.UtcNow.Subtract(LastFailedTime.Value) > Constants.AdminServerReconnectInterval) return true; - if (FailedTimes < Constants.AdminServerCircuitFaildTimes) + if (FailedTimes < Constants.AdminServerCircuitFailedTimes) return true; return false; diff --git a/src/DotXxlJob.Core/Model/ReturnT.cs b/src/DotXxlJob.Core/Model/ReturnT.cs index da07b93..b2021b6 100644 --- a/src/DotXxlJob.Core/Model/ReturnT.cs +++ b/src/DotXxlJob.Core/Model/ReturnT.cs @@ -16,8 +16,8 @@ namespace DotXxlJob.Core public ReturnT(int code, string msg) { - this.Code = code; - this.Msg = msg; + Code = code; + Msg = msg; } diff --git a/src/DotXxlJob.Core/Model/TriggerParam.cs b/src/DotXxlJob.Core/Model/TriggerParam.cs index b918e81..06d84bf 100644 --- a/src/DotXxlJob.Core/Model/TriggerParam.cs +++ b/src/DotXxlJob.Core/Model/TriggerParam.cs @@ -6,7 +6,7 @@ namespace DotXxlJob.Core.Model [DataContract(Name = "com.xxl.job.core.biz.model.TriggerParam")] public class TriggerParam { - static readonly long SerialVersionUID = 42L; + //static readonly long SerialVersionUID = 42L; [DataMember(Name = "jobId",Order = 1)] public int JobId { get; set; } diff --git a/src/Hessian.NET/ListPreamble.cs b/src/Hessian.NET/ListPreamble.cs deleted file mode 100644 index 05f2413..0000000 --- a/src/Hessian.NET/ListPreamble.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Hessian.Net -{ - public enum ListPreamble - { - None, - VarList, - FixList, - VarListUntyped, - FixListUntyped, - CompactFixList, - CompactFixListUntyped - } -} \ No newline at end of file