From 537c909dfc114c60c8040f12ef8cc6d8b40845c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=81=87=E6=AD=A3=E7=BB=8F=E5=93=A5=E5=93=A5?= Date: Sat, 19 Jan 2019 12:15:50 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=AE=8C=E6=88=90=E4=BC=A0?= =?UTF-8?q?=E8=BE=93=E5=AF=B9=E8=B1=A1=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=E5=92=8C=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/ASPNetCoreExecutor/Startup.cs | 5 +- samples/ASPNetCoreExecutor/appsettings.json | 2 +- samples/HessianReader/NewFile1.txt | 77 +- samples/HessianReader/NewFile2.txt | 74 ++ samples/HessianReader/Program.cs | 63 +- samples/HessianReader/request.dat | Bin 416 -> 528 bytes src/DotXxlJob.Core/AdminClient.cs | 15 +- .../DefaultHandlers/HttpJobHandler.cs | 12 +- .../DefaultJobHandlerFactory.cs | 41 +- .../Extensions/ServiceCollectionExtensions.cs | 8 + src/DotXxlJob.Core/Internal/Constants.cs | 2 + .../Internal/HessianObjectConvert.cs | 126 ++++ .../Internal/HessianSerializer.cs | 246 +------ src/DotXxlJob.Core/Model/AddressEntity.cs | 2 +- src/DotXxlJob.Core/Model/RegistryParam.cs | 2 +- src/DotXxlJob.Core/Model/RpcResponse.cs | 2 +- .../DataContractHessianSerializer.cs | 70 -- src/Hessian.NET/DisposableStreamHandler.cs | 54 -- .../Extension/DateTimeExtension.cs | 56 -- src/Hessian.NET/Extension/Maybe.cs | 75 -- src/Hessian.NET/Hessian.NET.csproj | 7 - src/Hessian.NET/HessianInputReader.cs | 664 ------------------ .../HessianObjectSerializerFactory.cs | 45 -- ...essianObjectWriterFactory.ObjectWriters.cs | 128 ---- src/Hessian.NET/HessianOutputWriter.cs | 321 --------- .../HessianSerializationContext.cs | 27 - src/Hessian.NET/HessianSerializationScheme.cs | 127 ---- src/Hessian.NET/HessianSerializerException.cs | 9 - src/Hessian.NET/HessianSerializerSettings.cs | 13 - src/Hessian.NET/IObjectSerializer.cs | 9 - src/Hessian.NET/IObjectSerializerFactory.cs | 9 - src/Hessian.NET/ISerializationElement.cs | 16 - src/Hessian.NET/LeadingByte.cs | 162 ----- src/Hessian.NET/ListElement.cs | 179 ----- src/Hessian.NET/ListTypeResolver.cs | 66 -- src/Hessian.NET/Marker.cs | 42 -- src/Hessian.NET/ObjectElement.cs | 153 ---- src/Hessian.NET/ObjectPreamble.cs | 17 - src/Hessian.NET/ObjectPropertyComparer.cs | 23 - src/Hessian.NET/PropertyElement.cs | 71 -- .../Specification/AndSpecification.cs | 15 - .../Specification/BinarySpecification.cs | 25 - .../Specification/ISpecification.cs | 16 - .../Specification/NotSpecification.cs | 17 - .../Specification/OrSpecification.cs | 29 - .../Specification/Specification.cs | 19 - .../Specification/SpecificationExtension.cs | 32 - src/Hessian.NET/Throw.cs | 23 - src/Hessian.NET/TypeResolver.cs | 10 - src/Hessian.NET/ValueElement.cs | 33 - src/Hessian/Deserializer.cs | 140 ++-- src/Hessian/Serializer.cs | 6 + 52 files changed, 466 insertions(+), 2919 deletions(-) create mode 100644 samples/HessianReader/NewFile2.txt create mode 100644 src/DotXxlJob.Core/Internal/HessianObjectConvert.cs delete mode 100644 src/Hessian.NET/DataContractHessianSerializer.cs delete mode 100644 src/Hessian.NET/DisposableStreamHandler.cs delete mode 100644 src/Hessian.NET/Extension/DateTimeExtension.cs delete mode 100644 src/Hessian.NET/Extension/Maybe.cs delete mode 100644 src/Hessian.NET/Hessian.NET.csproj delete mode 100644 src/Hessian.NET/HessianInputReader.cs delete mode 100644 src/Hessian.NET/HessianObjectSerializerFactory.cs delete mode 100644 src/Hessian.NET/HessianObjectWriterFactory.ObjectWriters.cs delete mode 100644 src/Hessian.NET/HessianOutputWriter.cs delete mode 100644 src/Hessian.NET/HessianSerializationContext.cs delete mode 100644 src/Hessian.NET/HessianSerializationScheme.cs delete mode 100644 src/Hessian.NET/HessianSerializerException.cs delete mode 100644 src/Hessian.NET/HessianSerializerSettings.cs delete mode 100644 src/Hessian.NET/IObjectSerializer.cs delete mode 100644 src/Hessian.NET/IObjectSerializerFactory.cs delete mode 100644 src/Hessian.NET/ISerializationElement.cs delete mode 100644 src/Hessian.NET/LeadingByte.cs delete mode 100644 src/Hessian.NET/ListElement.cs delete mode 100644 src/Hessian.NET/ListTypeResolver.cs delete mode 100644 src/Hessian.NET/Marker.cs delete mode 100644 src/Hessian.NET/ObjectElement.cs delete mode 100644 src/Hessian.NET/ObjectPreamble.cs delete mode 100644 src/Hessian.NET/ObjectPropertyComparer.cs delete mode 100644 src/Hessian.NET/PropertyElement.cs delete mode 100644 src/Hessian.NET/Specification/AndSpecification.cs delete mode 100644 src/Hessian.NET/Specification/BinarySpecification.cs delete mode 100644 src/Hessian.NET/Specification/ISpecification.cs delete mode 100644 src/Hessian.NET/Specification/NotSpecification.cs delete mode 100644 src/Hessian.NET/Specification/OrSpecification.cs delete mode 100644 src/Hessian.NET/Specification/Specification.cs delete mode 100644 src/Hessian.NET/Specification/SpecificationExtension.cs delete mode 100644 src/Hessian.NET/Throw.cs delete mode 100644 src/Hessian.NET/TypeResolver.cs delete mode 100644 src/Hessian.NET/ValueElement.cs diff --git a/samples/ASPNetCoreExecutor/Startup.cs b/samples/ASPNetCoreExecutor/Startup.cs index 240ea8b..34bd494 100644 --- a/samples/ASPNetCoreExecutor/Startup.cs +++ b/samples/ASPNetCoreExecutor/Startup.cs @@ -22,12 +22,13 @@ namespace ASPNetCoreExecutor { services.AddXxlJobExecutor(Configuration); + services.AddDefaultXxlJobHandlers();// add httpHandler; } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app,ILoggerFactory loggerFactory, IHostingEnvironment env) + public void Configure(IApplicationBuilder app,ILoggingBuilder loggerBuilder, IHostingEnvironment env) { - loggerFactory.AddConsole(); + loggerBuilder.AddConsole(); if (env.IsDevelopment()) { diff --git a/samples/ASPNetCoreExecutor/appsettings.json b/samples/ASPNetCoreExecutor/appsettings.json index 46addc9..4772f08 100644 --- a/samples/ASPNetCoreExecutor/appsettings.json +++ b/samples/ASPNetCoreExecutor/appsettings.json @@ -5,7 +5,7 @@ } }, "xxlJob": { - "adminAddresses":"http://127.0.0.1:8101", + "adminAddresses":"http://127.0.0.1:8080", "appName": "ASPNetCoreExecutor", "port": 5000, "accessToken": "", diff --git a/samples/HessianReader/NewFile1.txt b/samples/HessianReader/NewFile1.txt index bc3c576..ce6b53e 100644 --- a/samples/HessianReader/NewFile1.txt +++ b/samples/HessianReader/NewFile1.txt @@ -3,70 +3,71 @@ ReadClassDefinition Hessian.ClassDef={"Name":"com.xxl.rpc.remoting.net.params.XxlRpcRequest","Fields":["requestId","createMillisTime","accessToken","className","methodName","version","parameterTypes","parameters"]} ------------------------------------------------------------ ------------ 0x60---------------- -ReadObjectCompact ------------- 0x00---------------- -ReadShortString requestId +ReadObjectCompact XxlRpcRequest +------------ 0x30---------------- +ReadMediumString requestId ------------ 0x4c---------------- -ReadLongFull createMillisTime ------------- 0x00---------------- -ReadShortString accessToken +ReadLongFull createMillisTime ------------ 0x00---------------- -ReadShortString className +ReadShortString accessToken +------------ 0x1d---------------- +ReadShortString className ------------ 0x08---------------- -ReadShortString methodName +ReadShortString methodName ------------ 0x4e---------------- -ReadNull version +ReadNull version ------------ 0x71---------------- ReadCompactFixList parameterTypes ------------ 0x43---------------- -ReadClassDefinition +ReadClassDefinition java.lang.Class ------------ 0x61---------------- -ReadObjectCompact +ReadObjectCompact java.lang.Class ------------ 0x0e---------------- -ReadShortString -Hessian.HessianObject=[{"Item1":"requestId","Item2":""},{"Item1":"createMillisTime","Item2":1547809331509},{"Item1":"accessToken","Item2":""},{"Item1":"className","Item2":""},{"Item1":"methodName","Item2":"callback"} -,{"Item1":"version","Item2":null},{"Item1":"parameterTypes","Item2":[{"Name":"java.lang.Clas -s","Fields":["name"]}]},{"Item1":"parameters","Item2":[{"Item1":"name","Item2":"java.util.List"}]}] +ReadShortString name +Hessian.HessianObject=[{"Item1":"requestId","Item2":"e24123be4a76417ca6f41f227532b235"},{"Item1":"createMillisTime","Item2":1547819469003},{"Item1":"accessToken","Item2":""},{"Item1":"className","Item2":"com.xxl.job.core.biz.AdminBiz"},{"It +em1":"methodName","Item2":"callback"},{"Item1":"version","Item2":null},{"Item1":"parameterTypes","Item2":[{"Name":"java.lang.Class","Fields":["name"]}]},{"Item1":"parameters","Item2":[{"Item1":"name","Item2":"java.util.List"}]}] ------------------------------------------------------------ +------------ 0x71---------------- +ReadCompactFixList parameters ------------ 0x72---------------- -ReadCompactFixList List +ReadCompactFixList List ------------ 0x43---------------- ReadClassDefinition HandleCallbackParam ------------ 0x62---------------- -ReadObjectCompact HandleCallbackParam ------------- 0xcc---------------- -ReadIntegerTwoBytes logId +ReadObjectCompact HandleCallbackParam +------------ 0x9b---------------- +ReadIntegerSingleByte logId ------------ 0x4c---------------- ReadLongFull logDateTim ------------ 0x43---------------- -ReadClassDefinition executeResult -System.Collections.Generic.List`1[System.Object]=[{"Name":"com.xxl.job.core.biz.model.HandleCallbackParam","Fields":["logId","logDateTim","executeResult"]},[{"Item1":"logId","Item2":1111},{"Item1":"logDateTim","Item2":1547809331507},{"Item1":"executeResult","Item2":{"Name":"com.xxl.job.core.biz.model.Return -T","Fields":["code","msg","content"]}}]] +ReadClassDefinition executeResult +System.Collections.Generic.List`1[System.Object]=[[{"Name":"com.xxl.job.core.biz.model.HandleCallbackParam","Fields":["logId","logDateTim","executeResult"]},[{"Item1":"logId","Item2":11},{"Item1":"logDateTim","Item2":1547819469000},{"Item1" +:"executeResult","Item2":{"Name":"com.xxl.job.core.biz.model.ReturnT","Fields":["code","msg","content"]}}]]] ------------------------------------------------------------ ------------ 0x63---------------- -ReadObjectCompact ReturnT +ReadObjectCompact executeResult ------------ 0xc8---------------- ReadIntegerTwoBytes code ------------- 0x4e---------------- -ReadNull msg +------------ 0x03---------------- +ReadShortString msg ------------ 0x07---------------- -ReadShortString aaaaaaa -Hessian.HessianObject=[{"Item1":"code","Item2":200},{"Item1":"msg","Item2":null},{"Item1":"content","Item2":"aaaaaaa"}] +ReadShortString content +Hessian.HessianObject=[{"Item1":"code","Item2":200},{"Item1":"msg","Item2":"1bc"},{"Item1":"content","Item2":"acd3323"}] ------------------------------------------------------------ ------------ 0x62---------------- -ReadObjectCompact HandleCallbackParam ------------- 0xd7---------------- -ReadIntegerThreeBytes logId +ReadObjectCompact HandleCallbackParam +------------ 0xa6---------------- +ReadIntegerSingleByte logId ------------ 0x4c---------------- -ReadLongFull logDateTim +ReadLongFull logDateTim ------------ 0x63---------------- ReadObjectCompact executeResult ------------- 0xc8---------------- +------------ 0xc9---------------- ReadIntegerTwoBytes code ------------- 0x4e---------------- -ReadNull msg ------------- 0x06---------------- -ReadShortString content -Hessian.HessianObject=[{"Item1":"logId","Item2":222222},{"Item1":"logDateTim","Item2":1547809331507},{"Item1":"executeResult","Item2":[{"Item1":"code","Item2":200},{"Item1":"msg","Item2":null},{"Item1":"content","Item2":"bbbbbb"}]}] +------------ 0x03---------------- +ReadShortString msg +------------ 0x03---------------- +ReadShortString content +Hessian.HessianObject=[{"Item1":"logId","Item2":22},{"Item1":"logDateTim","Item2":1547819469000},{"Item1":"executeResult","Item2":[{"Item1":"code","Item2":500},{"Item1":"msg","Item2":"cac"},{"Item1":"content","Item2":"aad"}]}] +------------------------------------------------------------ ------------------------------------------------------------ - diff --git a/samples/HessianReader/NewFile2.txt b/samples/HessianReader/NewFile2.txt new file mode 100644 index 0000000..1e00d2b --- /dev/null +++ b/samples/HessianReader/NewFile2.txt @@ -0,0 +1,74 @@ +--------------------------------------------------------------- +------------ 0x43---------------- +ReadClassDefinition +Hessian.ClassDef={"Name":"com.xxl.rpc.remoting.net.params.XxlRpcRequest","Fields":["requestId","createMillisTime","accessToken","className","methodName","version","parameterTypes","parameters"]} +------------------------------------------------------------ +------------ 0x60---------------- +ReadObjectCompact +------------ 0x30---------------- +ReadMediumString +------------ 0x4c---------------- +ReadLongFull +------------ 0x00---------------- +ReadShortString +------------ 0x1d---------------- +ReadShortString +------------ 0x08---------------- +ReadShortString +------------ 0x4e---------------- +ReadNull +------------ 0x71---------------- +ReadCompactFixList +------------ 0x43---------------- +ReadClassDefinition +------------ 0x61---------------- +ReadObjectCompact +------------ 0x0e---------------- +ReadShortString +Hessian.HessianObject=[{"Item1":"requestId","Item2":"e24123be4a76417ca6f41f227532b235"},{"Item1":"createMillisTime","Item2":1547819469003},{"Item1":"accessToken","Item2":""},{"Item1":"className","Item2":"com.xxl.job.core.biz.AdminBiz"},{"I +tem1":"methodName","Item2":"callback"},{"Item1":"version","Item2":null},{"Item1":"parameterTypes","Item2":[{"Name":"java.lang.Class","Fields":["name"]}]},{"Item1":"parameters","Item2":[{"Item1":"name","Item2":"java.util.List"}]}] +------------------------------------------------------------ +------------ 0x71---------------- +ReadCompactFixList +------------ 0x72---------------- +ReadCompactFixList +------------ 0x43---------------- +ReadClassDefinition +------------ 0x62---------------- +ReadObjectCompact +------------ 0x9b---------------- +ReadIntegerSingleByte +------------ 0x4c---------------- +ReadLongFull +------------ 0x43---------------- +ReadClassDefinition +System.Collections.Generic.List`1[System.Object]=[[{"Name":"com.xxl.job.core.biz.model.HandleCallbackParam","Fields":["logId","logDateTim","executeResult"]},[{"Item1":"logId","Item2":11},{"Item1":"logDateTim","Item2":1547819469000},{"Item1 +":"executeResult","Item2":{"Name":"com.xxl.job.core.biz.model.ReturnT","Fields":["code","msg","content"]}}]]] +------------------------------------------------------------ +------------ 0x63---------------- +ReadObjectCompact +------------ 0xc8---------------- +ReadIntegerTwoBytes +------------ 0x03---------------- +ReadShortString +------------ 0x07---------------- +ReadShortString +Hessian.HessianObject=[{"Item1":"code","Item2":200},{"Item1":"msg","Item2":"1bc"},{"Item1":"content","Item2":"acd3323"}] +------------------------------------------------------------ +------------ 0x62---------------- +ReadObjectCompact +------------ 0xa6---------------- +ReadIntegerSingleByte +------------ 0x4c---------------- +ReadLongFull +------------ 0x63---------------- +ReadObjectCompact +------------ 0xc9---------------- +ReadIntegerTwoBytes +------------ 0x03---------------- +ReadShortString +------------ 0x03---------------- +ReadShortString +Hessian.HessianObject=[{"Item1":"logId","Item2":22},{"Item1":"logDateTim","Item2":1547819469000},{"Item1":"executeResult","Item2":[{"Item1":"code","Item2":500},{"Item1":"msg","Item2":"aad"},{"Item1":"content","Item2":"cac"}]}] +------------------------------------------------------------ +------------------------------------------------------------ diff --git a/samples/HessianReader/Program.cs b/samples/HessianReader/Program.cs index 7908d15..f43d6fb 100644 --- a/samples/HessianReader/Program.cs +++ b/samples/HessianReader/Program.cs @@ -12,53 +12,74 @@ namespace HessianReader { static void Main(string[] args) { - byte[] myBinary = File.ReadAllBytes("request.dat"); + /* */ + byte[] myBinary = File.ReadAllBytes("run.dat"); + foreach (var i in myBinary) { Console.Write("0x"); Console.Write(i.ToString("x2")); Console.Write(","); } + Console.WriteLine(Environment.NewLine); - Console.WriteLine("---------------------{0}------------------------------------------",myBinary.Length); + Console.WriteLine("---------------------------------------------------------------"); - + /* + byte[] myBinary; + var callbackParamList = new List { + new HandleCallbackParam { + LogId = 11, + LogDateTime = 1547819469000L, + ExecuteResult =new ReturnT { Code = 200,Content ="acd3323",Msg ="1bc" } + }, + new HandleCallbackParam { + LogId = 22, + LogDateTime = 1547819469000L, + ExecuteResult =new ReturnT { Code = 500,Content ="cac",Msg ="aad" } + } + }; + var request = new RpcRequest { + RequestId ="e24123be4a76417ca6f41f227532b235", + CreateMillisTime = 1547819469003L, + AccessToken = "", + ClassName = "com.xxl.job.core.biz.AdminBiz", + MethodName = "callback", + ParameterTypes = new List {new JavaClass {Name = "java.util.List"}}, + Parameters = new List {callbackParamList} + }; + + using (var stream = new MemoryStream()) + { + HessianSerializer.SerializeRequest(stream,request); + + myBinary = stream.ToArray(); + } + */ using (var stream1 = new MemoryStream(myBinary)) { - //var s1 = HessianSerializer.DeserializeRequest(stream1); + var s1 = HessianSerializer.DeserializeRequest(stream1); + Console.WriteLine("{0}={1}",s1.GetType(),JsonConvert.SerializeObject(s1)); + /* var s = new Deserializer(stream1); - while ( stream1.CanRead) + while ( s.CanRead()) { var o = s.ReadValue(); Console.WriteLine("{0}={1}",o.GetType(),JsonConvert.SerializeObject(o)); Console.WriteLine("------------------------------------------------------------"); } + */ } Console.WriteLine("------------------------------------------------------------"); - - return; - - 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(); /** * diff --git a/samples/HessianReader/request.dat b/samples/HessianReader/request.dat index fd4af5306cfe03126f3609c15c84d2ebcfb01a0b..f3a08a5d39a8dd56dd55fe4190098932fb29942f 100644 GIT binary patch delta 160 zcmZ3$Jb`7x1TO=HR3j5ZBjco0lSFef6GQXlM6)y#!!#o!b5moZBqL)}9|i`-j6|yw zrx|3E^KL^PDoPCeCr@Eo6_*Ps&P7E-5n5kDmBi zZ{mv}-q|n%PB2V^)k4M Callback(List callbackParamList) { - return InvokeRpcService("callback", new List {"java.util.List"}, callbackParamList); + return InvokeRpcService("callback", new List {new JavaClass {Name = Constants.JavaListFulName}}, callbackParamList); } public Task Registry(RegistryParam registryParam) { - return InvokeRpcService("callback", new List {"java.lang.Class"}, registryParam); + return InvokeRpcService("callback", new List {new JavaClass {Name = Constants.JavaClassFulName}}, registryParam); } public Task RegistryRemove(RegistryParam registryParam) { - return InvokeRpcService("callback", new List {"java.lang.Class"}, registryParam); + return InvokeRpcService("callback", new List {new JavaClass {Name = Constants.JavaClassFulName}}, registryParam); } - private async Task InvokeRpcService(string methodName, List parameterTypes, + private async Task InvokeRpcService(string methodName, List parameterTypes, object parameters) { var request = new RpcRequest { + RequestId = Guid.NewGuid().ToString("N"), CreateMillisTime = DateTime.Now.GetTotalMilliseconds(), AccessToken = this._options.AccessToken, ClassName = "com.xxl.job.core.biz.AdminBiz", MethodName = methodName, - ParameterTypes = parameterTypes.ToList(), + ParameterTypes = parameterTypes, Parameters = new List {parameters} }; byte[] postBuf; @@ -90,14 +91,14 @@ namespace DotXxlJob.Core int triedTimes = 0; - using (var client = this._clientFactory.CreateClient()) + using (var client = this._clientFactory.CreateClient("DotXxlJobClient")) { while (triedTimes++ < this._addresses.Count) { var address = this._addresses[this._currentIndex]; this._currentIndex = (this._currentIndex + 1) % this._addresses.Count; - if (!address.CheckAccessable()) + if (!address.CheckAccessible()) continue; Stream resStream; diff --git a/src/DotXxlJob.Core/DefaultHandlers/HttpJobHandler.cs b/src/DotXxlJob.Core/DefaultHandlers/HttpJobHandler.cs index d98d839..0a14399 100644 --- a/src/DotXxlJob.Core/DefaultHandlers/HttpJobHandler.cs +++ b/src/DotXxlJob.Core/DefaultHandlers/HttpJobHandler.cs @@ -6,10 +6,16 @@ namespace DotXxlJob.Core.DefaultHandlers [JobHandler("httpJobHandler")] public class HttpJobHandler:AbsJobHandler { - public override Task Execute(JobExecuteContext context) + public async override Task Execute(JobExecuteContext context) { - context.JobLogger.Log("Get Request Data:{0}",context.JobParameter); - return Task.FromResult(ReturnT.SUCCESS); + if (string.IsNullOrEmpty(context.JobParameter)) + { + return ReturnT.Failed("url is empty"); + } + //判断是否为单URL + context.JobLogger.Log("Get Request Data:{0}",context.JobParameter); + return ReturnT.SUCCESS; } } + } \ No newline at end of file diff --git a/src/DotXxlJob.Core/DefaultJobHandlerFactory.cs b/src/DotXxlJob.Core/DefaultJobHandlerFactory.cs index 71f3b15..23b4363 100644 --- a/src/DotXxlJob.Core/DefaultJobHandlerFactory.cs +++ b/src/DotXxlJob.Core/DefaultJobHandlerFactory.cs @@ -1,12 +1,49 @@ -using DotXxlJob.Core.DefaultHandlers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Microsoft.Extensions.DependencyInjection; namespace DotXxlJob.Core { public class DefaultJobHandlerFactory:IJobHandlerFactory { + private readonly IServiceProvider _provider; + private readonly Dictionary handlersCache = new Dictionary(); + public DefaultJobHandlerFactory(IServiceProvider provider) + { + this._provider = provider; + Initialize(); + } + + private void Initialize() + { + var list = this._provider.GetServices(); + if (list == null || !list.Any()) + { + throw new TypeLoadException("IJobHandlers are not found in IServiceCollection"); + } + + foreach (var handler in list) + { + var jobHandlerAttr = handler.GetType().GetCustomAttribute(); + var handlerName = jobHandlerAttr == null ? handler.GetType().Name : jobHandlerAttr.Name; + if (handlersCache.ContainsKey(handlerName)) + { + throw new Exception($"same IJobHandler' name: [{handlerName}]"); + } + handlersCache.Add(handlerName,handler); + } + + } + public IJobHandler GetJobHandler(string handlerName) { - return new HttpJobHandler(); + if (handlersCache.ContainsKey(handlerName)) + { + return handlersCache[handlerName]; + } + return null; } } } \ No newline at end of file diff --git a/src/DotXxlJob.Core/Extensions/ServiceCollectionExtensions.cs b/src/DotXxlJob.Core/Extensions/ServiceCollectionExtensions.cs index dad9ce1..8ab62ef 100644 --- a/src/DotXxlJob.Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/DotXxlJob.Core/Extensions/ServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using System; using DotXxlJob.Core.Config; +using DotXxlJob.Core.DefaultHandlers; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -23,6 +24,13 @@ namespace DotXxlJob.Core services.Configure(configAction).AddXxlJobExecutorServiceDependency(); return services; } + + public static IServiceCollection AddDefaultXxlJobHandlers(this IServiceCollection services) + { + services.AddSingleton(); + return services; + } + private static IServiceCollection AddXxlJobExecutorServiceDependency(this IServiceCollection services) { diff --git a/src/DotXxlJob.Core/Internal/Constants.cs b/src/DotXxlJob.Core/Internal/Constants.cs index e711daa..ce1673d 100644 --- a/src/DotXxlJob.Core/Internal/Constants.cs +++ b/src/DotXxlJob.Core/Internal/Constants.cs @@ -6,6 +6,8 @@ namespace DotXxlJob.Core { 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 JavaClassFulName = "java.lang.Clas"; + public const string JavaListFulName = "java.util.List"; public const string XxlLogsDefaultRootDirectory = "xxl-job-logs"; public const string HandleLogsDirectory = "HandlerLogs"; diff --git a/src/DotXxlJob.Core/Internal/HessianObjectConvert.cs b/src/DotXxlJob.Core/Internal/HessianObjectConvert.cs new file mode 100644 index 0000000..2adf473 --- /dev/null +++ b/src/DotXxlJob.Core/Internal/HessianObjectConvert.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Reflection; +using System.Runtime.Serialization; +using DotXxlJob.Core.Model; +using Hessian; + +namespace DotXxlJob.Core +{ + public class HessianObjectHelper + { + + private static readonly Dictionary> TransferObjCache + = new Dictionary>(); + private static readonly Dictionary TransferTypeCache + = new Dictionary(); + static HessianObjectHelper() + { + + InitProperties(typeof(RpcRequest)); + + InitProperties(typeof(TriggerParam)); + + InitProperties(typeof(RpcResponse)); + + InitProperties(typeof(ReturnT)); + + InitProperties(typeof(HandleCallbackParam)); + + InitProperties(typeof(JavaClass)); + + InitProperties(typeof(RegistryParam)); + + InitProperties(typeof(LogResult)); + } + private static void InitProperties(Type type) + { + var propertyInfos = new Dictionary(); + var typeInfo = type.GetTypeInfo(); + var classAttr = type.GetCustomAttribute(); + if (classAttr == null) + { + return; + } + + foreach (var property in typeInfo.DeclaredProperties) + { + var attribute = property.GetCustomAttribute(); + + if (null == attribute) + { + continue; + } + + if (!property.CanRead || !property.CanWrite) + { + continue; + } + + propertyInfos.Add(attribute.Name,property); + } + TransferTypeCache.Add(classAttr.Name,type); + TransferObjCache.Add(classAttr.Name,propertyInfos); + } + + public static object GetRealObjectValue(object value) + { + if (value == null || IsSimpleType(value.GetType())) + { + return value; + } + + if (value is HessianObject hessianObject) + { + if(TransferObjCache.TryGetValue(hessianObject.TypeName,out var properties)) + { + var instance = Activator.CreateInstance(TransferTypeCache[hessianObject.TypeName]); + foreach (var (k, v) in hessianObject) + { + if (properties.TryGetValue(k, out var p)) + { + p.SetValue(instance,GetRealObjectValue(v)); + } + } + + return instance; + } + } + + if (IsListType(value.GetType())) + { + var listData = new List(); + + var cList = value as List; + foreach (var cItem in cList) + { + listData.Add(GetRealObjectValue(cItem)); + } + + return listData; + } + throw new HessianException($"unknown item:{value.GetType()}"); + } + + private static bool IsListType(Type type) + { + return typeof(ICollection).IsAssignableFrom(type); + } + private static bool IsSimpleType(Type typeInfo) + { + if (typeInfo.IsValueType || typeInfo.IsEnum || typeInfo.IsPrimitive) + { + return true; + } + + if (typeof (string) == typeInfo) + { + return true; + } + + return false; + } + + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Internal/HessianSerializer.cs b/src/DotXxlJob.Core/Internal/HessianSerializer.cs index 9dead15..97dddcd 100644 --- a/src/DotXxlJob.Core/Internal/HessianSerializer.cs +++ b/src/DotXxlJob.Core/Internal/HessianSerializer.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections; -using System.Collections.Generic; using System.IO; -using System.Net.NetworkInformation; -using System.Reflection; -using System.Runtime.Serialization; using DotXxlJob.Core.Model; using Hessian; @@ -12,61 +6,10 @@ namespace DotXxlJob.Core { public static class HessianSerializer { - private static readonly Dictionary requestProperties = - new Dictionary(); - - private static readonly Dictionary triggerProperties = - new Dictionary(); - - private static readonly Dictionary responseProperties = - new Dictionary(); - - private static readonly Dictionary returnProperties = - new Dictionary(); - - private static readonly Dictionary callbackProperties = - new Dictionary(); - static HessianSerializer() - { - - InitProperties(typeof(RpcRequest), requestProperties); - - InitProperties(typeof(TriggerParam), triggerProperties); - - InitProperties(typeof(RpcResponse), responseProperties); - - InitProperties(typeof(ReturnT), returnProperties); - - - InitProperties(typeof(HandleCallbackParam), callbackProperties); - - } - - - private static void InitProperties(Type type, Dictionary propertyInfos) - { - var typeInfo = type.GetTypeInfo(); - foreach (var property in typeInfo.DeclaredProperties) - { - var attribute = property.GetCustomAttribute(); - - if (null == attribute) - { - continue; - } - - if (!property.CanRead || !property.CanWrite) - { - continue; - } - - propertyInfos.Add(attribute.Name,property); - } - } - + public static RpcRequest DeserializeRequest(Stream stream) { - RpcRequest request = new RpcRequest(); + RpcRequest request = null; try { @@ -76,123 +19,17 @@ namespace DotXxlJob.Core { throw new HessianException($"unknown class :{classDef.Name}"); } - if (requestProperties.Count != classDef.Fields.Length) - { - throw new HessianException($"unknown class :{classDef.Name}, field count not match ${requestProperties.Count} !={classDef.Fields.Length}"); - } - - //obj serialize - if (deserializer.ReadValue() is HessianObject hessianObject) - { - foreach (var item in hessianObject) - { - if (requestProperties.TryGetValue(item.Item1, out var p)) - { - if (IsSimpleType(p.PropertyType.GetTypeInfo())) - { - p.SetValue(request,item.Item2); - } - else - { - if (item.Item1 == "parameterTypes") - { - request.ParameterTypes = item.Item2 as List; - } - else if (item.Item2 is HessianObject ) - { - request.Parameters = new List(); - - var defList = deserializer.ReadValue() as List; - - foreach (var li in defList) - { - ReadParameters(deserializer,request.Parameters, li); - } - } - else - { - throw new HessianException($"unknown item :{item.Item1},{item.Item2.GetType()}"); - } - } - } - - } - } - - + request = HessianObjectHelper.GetRealObjectValue(deserializer.ReadValue()) as RpcRequest; } catch (EndOfStreamException) { //没有数据可读了 } - return request; } - - private static void ReadParameters(Deserializer deserializer,IList list, object item) - { - var itemType = item.GetType(); - if (IsSimpleType(itemType.GetTypeInfo())) - { - list.Add(item); - return; - } - - if (item is ClassDef paramClass) - { - //TODO:这里要做成动态的话 ,可以注册所有的实体到对应的字典中,不过这里只有这个类型哦 - if (paramClass.Name != "com.xxl.job.core.biz.model.TriggerParam") - { - throw new HessianException($"not expected parameter type [{paramClass.Name}]"); - } - - if (!(deserializer.ReadValue() is HessianObject paramData)) - { - throw new HessianException("not expected parameter type ,data is null"); - } - - object val ; - if (paramClass.Name == "com.xxl.job.core.biz.model.TriggerParam") - { - val =new TriggerParam(); - } - else - { - val =new HandleCallbackParam(); - } - - foreach (var (key,value) in paramData) - { - if (triggerProperties.TryGetValue(key, out var tgPropertyInfo)) - { - tgPropertyInfo.SetValue(val,value); - } - } - list.Add(val); - } - else if (item is HessianObject hessianObject) - { - if (hessianObject.TypeName == "com.xxl.job.core.biz.model.HandleCallbackParam") - { - var val =new HandleCallbackParam(); - foreach (var (key,value) in hessianObject) - { - if (triggerProperties.TryGetValue(key, out var tgPropertyInfo)) - { - tgPropertyInfo.SetValue(val,value); - } - } - list.Add(val); - } - } - else - { - - throw new HessianException($"unsupported list item type =[{itemType}]"); - } - - } + + public static void SerializeRequest(Stream stream,RpcRequest req) { var serializer = new Serializer(stream); @@ -205,24 +42,10 @@ namespace DotXxlJob.Core serializer.WriteObject(res); } - private static bool IsSimpleType(TypeInfo typeInfo) - { - if (typeInfo.IsValueType || typeInfo.IsEnum || typeInfo.IsPrimitive) - { - return true; - } - - if (typeof (string) == typeInfo.AsType()) - { - return true; - } - - return false; - } - + public static RpcResponse DeserializeResponse(Stream resStream) { - var rsp = new RpcResponse(); + RpcResponse rsp = null; try { @@ -232,58 +55,7 @@ namespace DotXxlJob.Core { 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"); - } - var data = new ReturnT(); - foreach (var (key, value) in resultData) - { - if (returnProperties.TryGetValue(key, out var tgPropertyInfo)) - { - tgPropertyInfo.SetValue(data,value); - } - } - - rsp.Result = data; - } - else - { - throw new HessianException($"unknown item :{item.Item1},{item.Item2.GetType()}"); - } - } - } - - } - } - + rsp = HessianObjectHelper.GetRealObjectValue(deserializer.ReadValue()) as RpcResponse; } catch (EndOfStreamException) @@ -293,6 +65,8 @@ namespace DotXxlJob.Core return rsp; } + + } diff --git a/src/DotXxlJob.Core/Model/AddressEntity.cs b/src/DotXxlJob.Core/Model/AddressEntity.cs index 5f885e6..098881e 100644 --- a/src/DotXxlJob.Core/Model/AddressEntity.cs +++ b/src/DotXxlJob.Core/Model/AddressEntity.cs @@ -10,7 +10,7 @@ namespace DotXxlJob.Core.Model private int FailedTimes { get; set; } - public bool CheckAccessable() + public bool CheckAccessible() { if (LastFailedTime == null) return true; diff --git a/src/DotXxlJob.Core/Model/RegistryParam.cs b/src/DotXxlJob.Core/Model/RegistryParam.cs index 0bc4fad..05fadb6 100644 --- a/src/DotXxlJob.Core/Model/RegistryParam.cs +++ b/src/DotXxlJob.Core/Model/RegistryParam.cs @@ -2,7 +2,7 @@ using System.Runtime.Serialization; namespace DotXxlJob.Core.Model { - [DataContract(Name = "")] + [DataContract(Name = "com.xxl.job.core.biz.model.RegistryParam")] public class RegistryParam { [DataMember(Name = "registGroup",Order = 1)] diff --git a/src/DotXxlJob.Core/Model/RpcResponse.cs b/src/DotXxlJob.Core/Model/RpcResponse.cs index 1519422..5a172f2 100644 --- a/src/DotXxlJob.Core/Model/RpcResponse.cs +++ b/src/DotXxlJob.Core/Model/RpcResponse.cs @@ -8,7 +8,7 @@ namespace DotXxlJob.Core.Model [DataMember(Name = "requestId",Order = 1)] public string RequestId{ get; set; } [DataMember(Name = "errorMsg",Order = 2)] - public string ErrorMsg; + public string ErrorMsg { get; set; } [DataMember(Name = "result",Order = 3)] public object Result{ get; set; } diff --git a/src/Hessian.NET/DataContractHessianSerializer.cs b/src/Hessian.NET/DataContractHessianSerializer.cs deleted file mode 100644 index 1c92007..0000000 --- a/src/Hessian.NET/DataContractHessianSerializer.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.IO; - -namespace Hessian.Net -{ - /// - /// Serialize objects to binary and deserialize binary to objects using the Caucho Hessian 2.0 Serialization Protocol. - /// - /// - /// This class cannot be inherited. - /// - public sealed class DataContractHessianSerializer - { - private readonly Type type; - - /// - /// Initializes a new instance of the class to serialize or deserialize an object of the specified type and serializer settings. - /// - /// The type of the instance that is serialized or deserialized. - /// The serializer settings. - public DataContractHessianSerializer(Type type, HessianSerializerSettings settings = null) - { - this.type = type; - } - - /// - /// Serializes a specified object to binary data and writes the resulting to a stream. - /// - /// The that is written to. - /// The object that contains the data to write to the stream. - public void WriteObject(Stream stream, object graph) - { - if (null == stream) - { - throw new ArgumentNullException(nameof(stream)); - } - - var factory = new HessianObjectSerializerFactory(); - var scheme = HessianSerializationScheme.CreateFromType(type, factory); - - using (var writer = new HessianOutputWriter(stream)) - { - var context = new HessianSerializationContext(); - scheme.Serialize(writer, graph, context); - } - } - - /// - /// - /// - /// - /// - public object ReadObject(Stream stream) - { - if (null == stream) - { - throw new ArgumentNullException(nameof(stream)); - } - - var factory = new HessianObjectSerializerFactory(); - var scheme = HessianSerializationScheme.CreateFromType(type, factory); - - using (var reader = new HessianInputReader(stream)) - { - var context = new HessianSerializationContext(); - return scheme.Deserialize(reader, context); - } - } - } -} diff --git a/src/Hessian.NET/DisposableStreamHandler.cs b/src/Hessian.NET/DisposableStreamHandler.cs deleted file mode 100644 index 893bf2d..0000000 --- a/src/Hessian.NET/DisposableStreamHandler.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.IO; - -namespace Hessian.Net -{ - /// - /// - /// - public class DisposableStreamHandler : IDisposable - { - private bool disposed; - - protected Stream Stream - { - get; - private set; - } - - protected DisposableStreamHandler(Stream stream) - { - Stream = stream; - } - - /// - /// - /// - public void Dispose() - { - if (!disposed) - { - Dispose(true); - } - } - - protected virtual void Dispose(bool dispose) - { - if (!disposed) - { - try - { - if (dispose) - { - Stream.Dispose(); - Stream = null; - } - } - finally - { - disposed = true; - } - } - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Extension/DateTimeExtension.cs b/src/Hessian.NET/Extension/DateTimeExtension.cs deleted file mode 100644 index d4aeaaf..0000000 --- a/src/Hessian.NET/Extension/DateTimeExtension.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; - -namespace Hessian.Net.Extension -{ - /// - /// - /// - internal static class DateTimeExtension - { - private const long Era = 62135596800000L; - private const long Millis = 60000; - - /// - /// - /// - /// - /// - public static long GetTotalMilliseconds(this DateTime dt) - { - return dt.ToUniversalTime().Ticks / 10000 - Era; - } - - /// - /// - /// - /// - /// - public static int GetTotalMinutes(this DateTime dt) - { - var val = GetTotalMilliseconds(dt); - return (int)(val / Millis); - } - - /// - /// - /// - /// - /// - public static DateTime FromMinutes(int value) - { - var ticks = (value * Millis + Era) * 10000; - return new DateTime(ticks, DateTimeKind.Utc); - } - - /// - /// - /// - /// - /// - public static DateTime FromMilliseconds(long value) - { - var ticks = (value + Era) * 10000; - return new DateTime(ticks, DateTimeKind.Utc); - } - } -} diff --git a/src/Hessian.NET/Extension/Maybe.cs b/src/Hessian.NET/Extension/Maybe.cs deleted file mode 100644 index 60ce9cf..0000000 --- a/src/Hessian.NET/Extension/Maybe.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; - -namespace Hessian.Net.Extension -{ - /// - /// - /// - internal static class Maybe - { - /// - /// - /// - /// - /// - /// - /// - /// - public static TResult With(this TInput value, Func eval) - where TResult : class - { - return (null == value) ? null : eval(value); - } - - /// - /// - /// - /// - /// - /// - /// - /// - /// - public static TResult Return(this TInput value, Func eval, TResult failure) - where TResult : class - { - return (null == value) ? failure : eval(value); - } - - /// - /// - /// - /// - /// - /// - /// - public static TInput If(this TInput value, Predicate condition) - where TInput : class - { - if (null == value) - { - return null; - } - - return condition(value) ? value : null; - } - - /// - /// - /// - /// - /// - /// - /// - public static TInput Unless(this TInput value, Predicate condition) - where TInput : class - { - if (null == value) - { - return null; - } - - return condition(value) ? null : value; - } - } -} diff --git a/src/Hessian.NET/Hessian.NET.csproj b/src/Hessian.NET/Hessian.NET.csproj deleted file mode 100644 index d2a210c..0000000 --- a/src/Hessian.NET/Hessian.NET.csproj +++ /dev/null @@ -1,7 +0,0 @@ - - - - netstandard2.0 - - - diff --git a/src/Hessian.NET/HessianInputReader.cs b/src/Hessian.NET/HessianInputReader.cs deleted file mode 100644 index f42bbb5..0000000 --- a/src/Hessian.NET/HessianInputReader.cs +++ /dev/null @@ -1,664 +0,0 @@ -using System; -using System.IO; -using System.Text; -using Hessian.Net.Extension; - -namespace Hessian.Net -{ - public class HessianInputReader : DisposableStreamHandler - { - private ObjectPreamble preamble; - - public bool IsClassDefinition => ObjectPreamble.ClassDefinition == preamble; - - public bool IsInstanceReference => ObjectPreamble.InstanceReference == preamble; - - public LeadingByte LeadingByte - { - get; - } - - public HessianInputReader(Stream stream) - : base(stream) - { - LeadingByte = new LeadingByte(); - } - - /// - /// Reads value from the stream. - /// - /// The value - public virtual bool ReadBoolean() - { - ReadLeadingByte(); - - if (LeadingByte.IsTrue) - { - return true; - } - - if (!LeadingByte.IsFalse) - { - throw new HessianSerializerException(); - } - - return false; - } - - public virtual int ReadInt32() - { - ReadLeadingByte(); - - if (LeadingByte.IsTinyInt32) - { - return LeadingByte.Data - 144; - } - - if (LeadingByte.IsShortInt32) - { - return ReadShortInt32(); - } - - if (LeadingByte.IsCompactInt32) - { - return ReadCompactInt32(); - } - - if (!LeadingByte.IsUnpackedInt32) - { - throw new HessianSerializerException(); - } - - return ReadUnpackedInt32(); - } - - public virtual long ReadInt64() - { - ReadLeadingByte(); - - if (LeadingByte.IsTinyInt64) - { - return LeadingByte.Data - 224; - } - - if (LeadingByte.IsShortInt64) - { - return ReadShortInt64(); - } - - if (LeadingByte.IsCompactInt64) - { - return ReadCompactInt64(); - } - - if (LeadingByte.IsPackedInt64) - { - return ReadPackedInt64(); - } - - if (!LeadingByte.IsUnpackedInt64) - { - throw new HessianSerializerException(); - } - - return ReadUnpackedInt64(); - } - - /// - /// Reads binary data from stream. - /// - /// The array of bytes. - public virtual byte[] ReadBytes() - { - ReadLeadingByte(); - - if (LeadingByte.IsNull) - { - return null; - } - - if (LeadingByte.IsCompactBinary) - { - return ReadBinaryCompact15(); - } - - var count = 0; - var buffer = new byte[0]; - - while (LeadingByte.IsNonfinalChunkBinary || LeadingByte.IsFinalChunkBinary) - { - var length = GetChunkLength(); - - if (buffer.Length < (count + length)) - { - var temp = new byte[count + length]; - - Buffer.BlockCopy(buffer, 0, temp, 0, buffer.Length); - - buffer = temp; - } - - if (Stream.Read(buffer, count, length) != length) - { - throw new HessianSerializerException(); - } - - count += length; - - if (LeadingByte.IsFinalChunkBinary) - { - break; - } - - ReadLeadingByte(); - } - - return buffer; - } - - /// - /// - /// - /// - public virtual double ReadDouble() - { - ReadLeadingByte(); - - if (LeadingByte.IsZeroDouble) - { - return 0.0d; - } - - if (LeadingByte.IsOneDouble) - { - return 1.0d; - } - - if (LeadingByte.IsTinyDouble) - { - return Stream.ReadByte(); - } - - if (LeadingByte.IsShortDouble) - { - return ReadShortDouble(); - } - - if (LeadingByte.IsCompactDouble) - { - return ReadCompactDouble(); - } - - if (!LeadingByte.IsUnpackedDouble) - { - throw new HessianSerializerException(); - } - - return ReadUnpackedDouble(); - } - - /// - /// - /// - /// - public virtual string ReadString() - { - ReadLeadingByte(); - - if (LeadingByte.IsTinyString) - { - return GetStringChunk(LeadingByte.Data); - } - - if (LeadingByte.IsCompactString) - { - return ReadCompactString(); - } - - var builder = new StringBuilder(); - - while (LeadingByte.IsNonfinalChunkString || LeadingByte.IsFinalChunkString) - { - var chunkLength = GetChunkLength(); - var chunk = GetStringChunk(chunkLength); - - builder.Append(chunk); - - if (LeadingByte.IsFinalChunkString) - { - break; - } - - ReadLeadingByte(); - } - - return builder.ToString(); - } - - /// - /// - /// - /// - public virtual DateTime ReadDateTime() - { - ReadLeadingByte(); - - if (LeadingByte.IsCompactDateTime) - { - var minutes = ReadUnpackedInt32(); - return DateTimeExtension.FromMinutes(minutes); - } - - if (!LeadingByte.IsUnpackedDateTime) - { - throw new HessianSerializerException(); - } - - return DateTimeExtension.FromMilliseconds(ReadUnpackedInt64()); - } - - /// - /// - /// - public void BeginObject() - { - ReadLeadingByte(); - - if (LeadingByte.IsClassDefinition) - { - preamble = ObjectPreamble.ClassDefinition; - } - else if (LeadingByte.IsShortObjectReference || LeadingByte.IsLongObjectReference) - { - preamble = ObjectPreamble.ObjectReference; - } - else if (LeadingByte.IsInstanceReference) - { - preamble = ObjectPreamble.InstanceReference; - } - else - { - throw new HessianSerializerException(); - } - } - - public void EndObject() - { - preamble = ObjectPreamble.None; - } - - public ObjectPreamble BeginList() - { - ReadLeadingByte(); - - if (LeadingByte.IsVarList) - { - preamble = ObjectPreamble.VarList; - } - else if (LeadingByte.IsFixedList) - { - preamble = ObjectPreamble.FixList; - } - else if (LeadingByte.IsVarListUntyped) - { - preamble = ObjectPreamble.VarListUntyped; - } - else if (LeadingByte.IsFixListUntyped) - { - preamble = ObjectPreamble.FixListUntyped; - } - else if (LeadingByte.IsCompactFixList) - { - preamble = ObjectPreamble.CompactFixList; - } - else if (LeadingByte.IsCompactFixListUntyped) - { - preamble = ObjectPreamble.CompactFixListUntyped; - } - else - { - throw new HessianSerializerException(); - } - - return preamble; - - } - - public void EndList() - { - preamble = ObjectPreamble.None; - } - - public void EndClassDefinition() - { - } - - public int ReadObjectReference() - { - ReadLeadingByte(); - - if (LeadingByte.IsShortObjectReference) - { - return LeadingByte.Data - 0x60; - } - - if (!LeadingByte.IsLongObjectReference) - { - throw new HessianSerializerException(); - } - - return ReadInt32(); - } - - public int ReadInstanceReference() - { - return ReadInt32(); - } - - public byte Peek() - { - byte value= (byte)this.Stream.ReadByte(); - this.Stream.Position--; - return value; - } - public byte ReadByte() - { - return (byte)this.Stream.ReadByte(); - } - protected void ReadLeadingByte() - { - var data = Stream.ReadByte(); - - Console.WriteLine(data.ToString("x2")); - if (-1 == data) - { - throw new HessianSerializerException(); - } - - LeadingByte.SetData((byte)data); - } - - private int ReadShortInt32() - { - var data = Stream.ReadByte(); - return ((LeadingByte.Data - 200) << 8) | data; - } - - private int ReadCompactInt32() - { - var value = (LeadingByte.Data - 212) << 16; - - value |= (Stream.ReadByte() << 8); - value |= Stream.ReadByte(); - - return value; - } - - private int ReadUnpackedInt32() - { - var value = Stream.ReadByte() << 24; - - value |= (Stream.ReadByte() << 16); - value |= (Stream.ReadByte() << 8); - value |= Stream.ReadByte(); - - return value; - } - - private long ReadShortInt64() - { - var data = Stream.ReadByte(); - return ((LeadingByte.Data - 248) << 8) | data; - } - - private long ReadCompactInt64() - { - var value = (LeadingByte.Data - 60) << 16; - - value |= (Stream.ReadByte() << 8); - value |= Stream.ReadByte(); - - return value; - } - private long ReadPackedInt64() - { - var value = Stream.ReadByte() << 24; - - value |= (Stream.ReadByte() << 16); - value |= (Stream.ReadByte() << 8); - value |= Stream.ReadByte(); - - return value; - } - - private long ReadUnpackedInt64() - { - var value = (long) Stream.ReadByte() << 56; - - value |= ((long) Stream.ReadByte() << 48); - value |= ((long) Stream.ReadByte() << 40); - value |= ((long) Stream.ReadByte() << 32); - value |= ((long) Stream.ReadByte() << 24); - value |= ((long) Stream.ReadByte() << 16); - value |= ((long) Stream.ReadByte() << 8); - value |= (uint)Stream.ReadByte(); - - return value; - } - - private byte[] ReadBinaryCompact15() - { - var size = LeadingByte.Data - 32; - var temp = new byte[size]; - - if (Stream.Read(temp, 0, size) != size) - { - throw new HessianSerializerException(); - } - - return temp; - } - - private double ReadShortDouble() - { - var value = Stream.ReadByte() << 8; - - value |= Stream.ReadByte(); - - return Convert.ToDouble((short)value); - } - - private double ReadCompactDouble() - { - var buffer = new byte[4]; - - for (var index = buffer.Length - 1; index >= 0; index--) - { - buffer[index] = (byte)Stream.ReadByte(); - } - - return BitConverter.ToSingle(buffer, 0); - } - - private double ReadUnpackedDouble() - { - var value = 0L; - - for (var count = 8; count > 0; count--) - { - value <<= 8; - value |= (uint)Stream.ReadByte(); - } - - return BitConverter.Int64BitsToDouble(value); - } - - private string ReadCompactString() - { - var length = (LeadingByte.Data - 0x30) << 8; - - length |= Stream.ReadByte(); - - return GetStringChunk(length); - } - - private string GetStringChunk(int length) - { - var buffer = new StringBuilder(); - - while (length-- > 0) - { - var ch = ReadChar(); - buffer.Append(ch); - } - - return buffer.ToString(); - } - - private int GetChunkLength() - { - var b0 = Stream.ReadByte(); - var b1 = Stream.ReadByte(); - return (b0 << 8) + b1; - } - - private char ReadChar() - { - var data = Stream.ReadByte(); - - if (data < 0x80) - { - return (char)data; - } - - if ((data & 0xE0) == 0xC0) - { - var b0 = Stream.ReadByte(); - return (char) (((data & 0x1F) << 6) + (b0 & 0x3F)); - } - - if ((data & 0xF0) == 0xE0) - { - var b0 = Stream.ReadByte(); - var b1 = Stream.ReadByte(); - return (char) (((data & 0x0F) << 12) + ((b0 & 0x3F) << 6) + (b1 & 0x3F)); - } - - throw new HessianSerializerException(); - } - - /* - * hessian implementation - * https://github.com/benjamin-bader/hessian/blob/master/Hessian/ValueReader.cs - public uint ReadUtf8Codepoint () - { - const uint replacementChar = 0xFFFD; - - byte b0, b1, b2, b3; - b0 = ReadByte (); - - if (b0 < 0x80) { - return b0; - } - - if (b0 < 0xC2) { - return replacementChar; - } - - if (b0 < 0xE0) { - b1 = ReadByte (); - - if ((b1 ^ 0x80) >= 0x40) { - return replacementChar; - } - - return (b1 & 0x3Fu) | ((b0 & 0x1Fu) << 6); - } - - if (b0 < 0xF0) { - b1 = ReadByte (); - b2 = ReadByte (); - - // Valid range: E0 A0..BF 80..BF - if (b0 == 0xE0 && (b1 ^ 0xA0) >= 0x20) { - return replacementChar; - } - - // Valid range: ED 80..9F 80..BF - if (b0 == 0xED && (b1 ^ 0x80) >= 0x20) { - return replacementChar; - } - - // Valid range: E1..EC 80..BF 80..BF - if ((b1 ^ 0x80) >= 0x40 || (b2 ^ 0x80) >= 0x40) { - return replacementChar; - } - - return (b2 & 0x3Fu) - | ((b1 & 0x3Fu) << 6) - | ((b0 & 0x0Fu) << 12); - } - - if (b0 < 0xF1) { - b1 = ReadByte(); - - if ((b1 ^ 0x90) < 0x30) { - return replacementChar; - } - - b2 = ReadByte(); - b3 = ReadByte(); - - if ((b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80) { - return replacementChar; - } - - return (b3 & 0x3Fu) - | ((b2 & 0x3Fu) << 6) - | ((b1 & 0x3Fu) << 12) - | ((b0 & 0x07u) << 18); - } - - if (b0 < 0xF4) { - b1 = ReadByte (); - b2 = ReadByte (); - b3 = ReadByte (); - - // Valid range: F1..F3 80..BF 80..BF 80..BF - if ((b1 & 0xC0) != 0x80 || (b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80) - { - return replacementChar; - } - - return (b3 & 0x3Fu) - | ((b2 & 0x3Fu) << 6) - | ((b1 & 0x3Fu) << 12) - | ((b0 & 0x07u) << 18); - } - - if (b0 < 0xF5) { - b1 = ReadByte (); - - // Valid range: F4 80..8F 80..BF 80..BF - if ((b1 ^ 0x80) >= 0x10) { - return replacementChar; - } - - b2 = ReadByte(); - b3 = ReadByte(); - - if ((b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80) - { - return replacementChar; - } - - return (b3 & 0x3Fu) - | ((b2 & 0x3Fu) << 6) - | ((b1 & 0x3Fu) << 12) - | ((b0 & 0x07u) << 18); - } - - return replacementChar; - }*/ - } -} \ No newline at end of file diff --git a/src/Hessian.NET/HessianObjectSerializerFactory.cs b/src/Hessian.NET/HessianObjectSerializerFactory.cs deleted file mode 100644 index 18f763a..0000000 --- a/src/Hessian.NET/HessianObjectSerializerFactory.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Hessian.Net -{ - internal sealed partial class HessianObjectSerializerFactory : IObjectSerializerFactory - { - //private readonly HessianSerializationContext context; - private Dictionary cache; - - //public HessianObjectSerializerFactory(HessianSerializationContext context) - //{ - // this.context = context; - //} - - public IObjectSerializer GetSerializer(Type target) - { - IObjectSerializer writer; - - EnsureCache(); - - return cache.TryGetValue(target, out writer) ? writer : null; - } - - private void EnsureCache() - { - if (null != cache) - { - return; - } - - var dict = new Dictionary - { - [typeof (bool)] = new BooleanSerializer(), - [typeof (int)] = new Int32Serializer(), - [typeof (long)] = new Int64Serializer(), - [typeof (double)] = new DoubleSerializer(), - [typeof (string)] = new StringSerializer(), - [typeof (DateTime)] = new DateTimeSerializer() - }; - - cache = dict; - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/HessianObjectWriterFactory.ObjectWriters.cs b/src/Hessian.NET/HessianObjectWriterFactory.ObjectWriters.cs deleted file mode 100644 index 424149f..0000000 --- a/src/Hessian.NET/HessianObjectWriterFactory.ObjectWriters.cs +++ /dev/null @@ -1,128 +0,0 @@ -using System; - -namespace Hessian.Net -{ - internal partial class HessianObjectSerializerFactory - { - /// - /// - /// - private class BooleanSerializer : IObjectSerializer - { - public void Serialize(HessianOutputWriter writer, object graph) - { - writer.WriteBoolean((bool) graph); - } - - public object Deserialize(HessianInputReader reader) - { - return reader.ReadBoolean(); - } - } - - /// - /// - /// - private class Int32Serializer : IObjectSerializer - { - public void Serialize(HessianOutputWriter writer, object graph) - { - writer.WriteInt32((int) graph); - } - - public object Deserialize(HessianInputReader reader) - { - return reader.ReadInt32(); - } - } - - /// - /// - /// - private class Int64Serializer : IObjectSerializer - { - public void Serialize(HessianOutputWriter writer, object graph) - { - writer.WriteInt64((long) graph); - } - - public object Deserialize(HessianInputReader reader) - { - return reader.ReadInt64(); - } - } - - /// - /// - /// - private class StringSerializer : IObjectSerializer - { - public void Serialize(HessianOutputWriter writer, object graph) - { - writer.WriteString((string) graph); - } - - public object Deserialize(HessianInputReader reader) - { - return reader.ReadString(); - } - } - - /// - /// - /// - private class DateTimeSerializer : IObjectSerializer - { - public void Serialize(HessianOutputWriter writer, object graph) - { - writer.WriteDateTime((DateTime) graph); - } - - public object Deserialize(HessianInputReader reader) - { - return reader.ReadDateTime(); - } - } - - /// - /// - /// - private class DoubleSerializer : IObjectSerializer - { - public void Serialize(HessianOutputWriter writer, object graph) - { - writer.WriteDouble((double) graph); - } - - public object Deserialize(HessianInputReader reader) - { - return reader.ReadDouble(); - } - } - -/* - /// - /// - /// - private class ClassSerializer : IObjectSerializer - { - private readonly HessianSerializationContext context; - - public ClassSerializer(HessianSerializationContext context) - { - this.context = context; - } - - public void Serialize(HessianOutputWriter writer, object graph) - { - throw new System.NotImplementedException(); - } - - public object Deserialize(HessianInputReader reader) - { - return reader. - } - } -*/ - } -} \ No newline at end of file diff --git a/src/Hessian.NET/HessianOutputWriter.cs b/src/Hessian.NET/HessianOutputWriter.cs deleted file mode 100644 index 166ca8d..0000000 --- a/src/Hessian.NET/HessianOutputWriter.cs +++ /dev/null @@ -1,321 +0,0 @@ -using System; -using System.IO; -using System.Text; -using Hessian.Net.Extension; - -namespace Hessian.Net -{ - /// - /// Class for writing Hessian-encoded data into stream. - /// - public class HessianOutputWriter : DisposableStreamHandler - { - public HessianOutputWriter(Stream stream) - : base(stream) - { - } - - /// - /// Writes NULL token into stream - /// - public virtual void WriteNull() - { - Stream.WriteByte(Marker.Null); - } - - /// - /// Writes value into output stream. - /// - /// The value. - public virtual void WriteBoolean(bool value) - { - Stream.WriteByte(value ? Marker.True : Marker.False); - } - - /// - /// Writes array of into output stream. - /// - /// The value. - public void WriteBytes(byte[] buffer) - { - if (null == buffer) - { - WriteNull(); - return; - } - - WriteBytes(buffer, 0, buffer.Length); - } - - public virtual void WriteBytes(byte[] buffer, int offset, int count) - { - if (offset < 0) - { - throw new ArgumentException("", nameof(offset)); - } - - if (null == buffer) - { - WriteNull(); - return; - } - - if (count < 0x10) - { - Stream.WriteByte((byte)(0x20 + (count & 0x0F))); - Stream.Write(buffer, offset, count); - return; - } - - const int chunkSize = 0x8000; - - while (count > chunkSize) - { - Stream.WriteByte(Marker.BinaryNonfinalChunk); - Stream.WriteByte(chunkSize >> 8); - Stream.WriteByte(chunkSize & 0xFF); - Stream.Write(buffer, offset, chunkSize); - - count -= chunkSize; - offset += chunkSize; - } - - Stream.WriteByte(Marker.BinaryFinalChunk); - Stream.WriteByte((byte)(count >> 8)); - Stream.WriteByte((byte)(count & 0xFF)); - Stream.Write(buffer, offset, count); - } - - public virtual void WriteDateTime(DateTime value) - { - if (value.Second == 0) - { - var s = value.GetTotalMinutes(); - - Stream.WriteByte(Marker.DateTimeCompact); - Stream.WriteByte((byte)(s >> 24)); - Stream.WriteByte((byte)(s >> 16)); - Stream.WriteByte((byte)(s >> 8)); - Stream.WriteByte((byte)s); - - return; - } - - var dt = value.GetTotalMilliseconds(); - - Stream.WriteByte(Marker.DateTimeLong); - Stream.WriteByte((byte)(dt >> 56)); - Stream.WriteByte((byte)(dt >> 48)); - Stream.WriteByte((byte)(dt >> 40)); - Stream.WriteByte((byte)(dt >> 32)); - Stream.WriteByte((byte)(dt >> 24)); - Stream.WriteByte((byte)(dt >> 16)); - Stream.WriteByte((byte)(dt >> 8)); - Stream.WriteByte((byte)dt); - } - - public virtual void WriteDouble(double value) - { - if (value.Equals(0.0d)) - { - Stream.WriteByte(Marker.DoubleZero); - return; - } - - if (value.Equals(1.0d)) - { - Stream.WriteByte(Marker.DoubleOne); - return; - } - - var fraction = Math.Abs(value - Math.Truncate(value)); - - if (Double.Epsilon >= fraction) - { - if (Byte.MinValue <= value && value <= Byte.MaxValue) - { - Stream.WriteByte(Marker.DoubleOctet); - Stream.WriteByte(Convert.ToByte(value)); - - return; - } - - if (Int16.MinValue <= value && value <= Int16.MaxValue) - { - var val = Convert.ToInt16(value); - - Stream.WriteByte(Marker.DoubleShort); - Stream.WriteByte((byte)(val >> 8)); - Stream.WriteByte((byte)val); - - return; - } - } - - if (Single.MinValue <= value && value <= Single.MaxValue) - { - var bytes = BitConverter.GetBytes((float) value); - - Stream.WriteByte(Marker.DoubleFloat); - - for (var index = bytes.Length - 1; index >= 0; index--) - { - Stream.WriteByte(bytes[index]); - } - - return; - } - - var temp = BitConverter.DoubleToInt64Bits(value); - - Stream.WriteByte(Marker.Double); - - for (var index = 56; index >= 0; index -= 8) - { - Stream.WriteByte((byte) (temp >> index)); - } - } - - public virtual void WriteInt32(int value) - { - if (-16 <= value && value < 48) - { - Stream.WriteByte((byte)(0x90 + value)); - } - else if (-2048 <= value && value < 2048) - { - Stream.WriteByte((byte)(0xC8 + (byte)(value >> 8))); - Stream.WriteByte((byte)value); - } - else if (-262144 <= value && value < 262144) - { - Stream.WriteByte((byte)(0xD4 + (byte)(value >> 16))); - Stream.WriteByte((byte)(value >> 8)); - Stream.WriteByte((byte)value); - } - else - { - Stream.WriteByte(Marker.UnpackedInteger); - Stream.WriteByte((byte)(value >> 24)); - Stream.WriteByte((byte)(value >> 16)); - Stream.WriteByte((byte)(value >> 8)); - Stream.WriteByte((byte)value); - } - } - - public virtual void WriteInt64(long value) - { - if (-8 <= value && value < 16) - { - Stream.WriteByte((byte)(0xE0 + value)); - } - else if (-2048 <= value && value < 2048) - { - Stream.WriteByte((byte)(0xF8 + (byte)(value >> 8))); - Stream.WriteByte((byte)value); - } - else if (-262144 <= value && value < 262144) - { - Stream.WriteByte((byte)(0x3C + (byte)(value >> 16))); - Stream.WriteByte((byte)(value >> 8)); - Stream.WriteByte((byte)value); - } - else if (Int32.MinValue <= value && value <= Int32.MaxValue) - { - Stream.WriteByte(Marker.PackedLong); - Stream.WriteByte((byte)(value >> 24)); - Stream.WriteByte((byte)(value >> 16)); - Stream.WriteByte((byte)(value >> 8)); - Stream.WriteByte((byte)value); - } - else - { - Stream.WriteByte(Marker.UnpackedLong); - Stream.WriteByte((byte)(value >> 56)); - Stream.WriteByte((byte)(value >> 48)); - Stream.WriteByte((byte)(value >> 40)); - Stream.WriteByte((byte)(value >> 32)); - Stream.WriteByte((byte)(value >> 24)); - Stream.WriteByte((byte)(value >> 16)); - Stream.WriteByte((byte)(value >> 8)); - Stream.WriteByte((byte)value); - } - } - - public virtual void WriteString(string value) - { - if (String.IsNullOrEmpty(value)) - { - Stream.WriteByte(0x00); - return; - } - - var length = value.Length; - - if (1024 > length) - { - var bytes = Encoding.UTF8.GetBytes(value.ToCharArray()); - - if (32 > length) - { - Stream.WriteByte((byte) length); - } - else - { - Stream.WriteByte((byte) (0x30 + (byte) (length >> 8))); - Stream.WriteByte((byte) length); - } - - Stream.Write(bytes, 0, bytes.Length); - - return; - } - - const int maxChunkLength = 1024; - var position = 0; - - while (position < length) - { - var count = Math.Min(length - position, maxChunkLength); - var final = length == (position + count); - var chunk = value.Substring(position, count); - var bytes = Encoding.UTF8.GetBytes(chunk.ToCharArray()); - - Stream.WriteByte(final ? Marker.StringFinalChunk : Marker.StringNonfinalChunk); - Stream.WriteByte((byte)(count >> 8)); - Stream.WriteByte((byte)count); - Stream.Write(bytes, 0, bytes.Length); - - position += count; - } - } - - public virtual void BeginClassDefinition() - { - Stream.WriteByte(Marker.ClassDefinition); - } - - public virtual void EndClassDefinition() - { - } - - public virtual void WriteObjectReference(int index) - { - if (index < 0x10) - { - Stream.WriteByte((byte)(0x60 + index)); - } - else - { - Stream.WriteByte(Marker.ClassReference); - WriteInt32(index); - } - } - - public virtual void WriteInstanceReference(int index) - { - Stream.WriteByte(Marker.InstanceReference); - WriteInt32(index); - } - } -} diff --git a/src/Hessian.NET/HessianSerializationContext.cs b/src/Hessian.NET/HessianSerializationContext.cs deleted file mode 100644 index 27db2b0..0000000 --- a/src/Hessian.NET/HessianSerializationContext.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; - -namespace Hessian.Net -{ - public class HessianSerializationContext - { - public IList Classes - { - get; - private set; - } - - public IList Instances - { - get; - private set; - } - - public HessianSerializationContext() - { - Classes = new List(); - Instances = new List(); - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/HessianSerializationScheme.cs b/src/Hessian.NET/HessianSerializationScheme.cs deleted file mode 100644 index d143e08..0000000 --- a/src/Hessian.NET/HessianSerializationScheme.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Reflection; -using System.Runtime.Serialization; - -namespace Hessian.Net -{ - internal class HessianSerializationScheme - { - public Type ObjectType - { - get; - private set; - } - - public ISerializationElement Element - { - get; - } - - private HessianSerializationScheme(Type objectType, ISerializationElement element) - { - ObjectType = objectType; - Element = element; - } - - public static HessianSerializationScheme CreateFromType(Type type, IObjectSerializerFactory factory) - { - var catalog = new Dictionary(); - var element = CreateSerializationElement(type, catalog, factory); - - return new HessianSerializationScheme(type, element); - } - - public void Serialize(HessianOutputWriter writer, object graph, HessianSerializationContext context) - { - Element.Serialize(writer, graph, context); - } - - public object Deserialize(HessianInputReader reader, HessianSerializationContext context) - { - return Element.Deserialize(reader, context); - } - - private static ISerializationElement CreateSerializationElement(Type type, IDictionary catalog, IObjectSerializerFactory factory) - { - var info = type.GetTypeInfo(); - - if (IsSimpleType(info)) - { - var serializer = factory.GetSerializer(type); - return new ValueElement(type, serializer); - } - - if (IsListType(info)) - { - return new ListElement(type, catalog, factory); - } - return BuildSerializationObject(type, catalog, factory); - } - - private static ISerializationElement BuildSerializationObject(Type type, IDictionary catalog, IObjectSerializerFactory factory) - { - if (catalog.TryGetValue(type, out var existing)) - { - return existing; - } - - var info = type.GetTypeInfo(); - var contract = info.GetCustomAttribute(); - - if (null == contract) - { - throw new Exception(); - } - - var properties = new List(); - var element = new ObjectElement(type, properties); - - catalog.Add(type, element); - - foreach (var property in info.DeclaredProperties) - { - var attribute = property.GetCustomAttribute(); - - if (null == attribute) - { - continue; - } - - if (!property.CanRead || !property.CanWrite) - { - continue; - } - - var prop = new PropertyElement(property, CreateSerializationElement(property.PropertyType, catalog, factory)); - - properties.Add(prop); - } - - properties.Sort(new ObjectPropertyComparer()); - - return element; - } - - private static bool IsSimpleType(TypeInfo typeInfo) - { - if (typeInfo.IsValueType || typeInfo.IsEnum || typeInfo.IsPrimitive) - { - return true; - } - - if (typeof (String) == typeInfo.AsType()) - { - return true; - } - - return false; - } - - private static bool IsListType(TypeInfo typeInfo) - { - return typeof(IEnumerable).IsAssignableFrom(typeInfo); - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/HessianSerializerException.cs b/src/Hessian.NET/HessianSerializerException.cs deleted file mode 100644 index 44796fe..0000000 --- a/src/Hessian.NET/HessianSerializerException.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Hessian.Net -{ - public sealed class HessianSerializerException : Exception - { - - } -} \ No newline at end of file diff --git a/src/Hessian.NET/HessianSerializerSettings.cs b/src/Hessian.NET/HessianSerializerSettings.cs deleted file mode 100644 index 39ac066..0000000 --- a/src/Hessian.NET/HessianSerializerSettings.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Hessian.Net -{ - public class HessianSerializerSettings - { - - } -} diff --git a/src/Hessian.NET/IObjectSerializer.cs b/src/Hessian.NET/IObjectSerializer.cs deleted file mode 100644 index 9b5e68a..0000000 --- a/src/Hessian.NET/IObjectSerializer.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Hessian.Net -{ - public interface IObjectSerializer - { - void Serialize(HessianOutputWriter writer, object graph); - - object Deserialize(HessianInputReader reader); - } -} \ No newline at end of file diff --git a/src/Hessian.NET/IObjectSerializerFactory.cs b/src/Hessian.NET/IObjectSerializerFactory.cs deleted file mode 100644 index 3883150..0000000 --- a/src/Hessian.NET/IObjectSerializerFactory.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Hessian.Net -{ - public interface IObjectSerializerFactory - { - IObjectSerializer GetSerializer(Type target); - } -} \ No newline at end of file diff --git a/src/Hessian.NET/ISerializationElement.cs b/src/Hessian.NET/ISerializationElement.cs deleted file mode 100644 index 07de01b..0000000 --- a/src/Hessian.NET/ISerializationElement.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace Hessian.Net -{ - public interface ISerializationElement - { - Type ObjectType - { - get; - } - - void Serialize(HessianOutputWriter writer, object graph, HessianSerializationContext context); - - object Deserialize(HessianInputReader reader, HessianSerializationContext context); - } -} \ No newline at end of file diff --git a/src/Hessian.NET/LeadingByte.cs b/src/Hessian.NET/LeadingByte.cs deleted file mode 100644 index fc9aa24..0000000 --- a/src/Hessian.NET/LeadingByte.cs +++ /dev/null @@ -1,162 +0,0 @@ -using Hessian.Net.Specification; - -namespace Hessian.Net -{ - public class LeadingByte - { - public byte Data - { - get; - private set; - } - - public bool IsNull => Check(If.Marker.Equals(Marker.Null)); - - public bool IsTrue => Check(If.Marker.Equals(Marker.True)); - - public bool IsFalse => Check(If.Marker.Equals(Marker.False)); - - public bool IsTinyInt32 => Check(If.Marker.Between(0x80).And(0xBF)); - - public bool IsShortInt32 => Check(If.Marker.Between(0xC0).And(0xCF)); - - public bool IsCompactInt32 => Check(If.Marker.Between(0xD0).And(0xD7)); - - public bool IsUnpackedInt32 => Check(If.Marker.Equals(Marker.UnpackedInteger)); - - public bool IsTinyInt64 => Check(If.Marker.Between(0xD8).And(0xEF)); - - public bool IsShortInt64 => Check(If.Marker.Between(0xF0).And(0xFF)); - - public bool IsCompactInt64 => Check(If.Marker.Between(0x38).And(0x3F)); - - public bool IsPackedInt64 => Check(If.Marker.Equals(Marker.PackedLong)); - - public bool IsUnpackedInt64 => Check(If.Marker.Equals(Marker.UnpackedLong)); - - public bool IsCompactBinary => Check(If.Marker.Between(0x20).And(0x2f)); - - public bool IsNonfinalChunkBinary => Check(If.Marker.Equals(Marker.BinaryNonfinalChunk)); - - public bool IsFinalChunkBinary => Check(If.Marker.Equals(Marker.BinaryFinalChunk)); - - public bool IsZeroDouble => Check(If.Marker.Equals(Marker.DoubleZero)); - - public bool IsOneDouble => Check(If.Marker.Equals(Marker.DoubleOne)); - - public bool IsTinyDouble => Check(If.Marker.Equals(Marker.DoubleOctet)); - - public bool IsShortDouble => Check(If.Marker.Equals(Marker.DoubleShort)); - - public bool IsCompactDouble => Check(If.Marker.Equals(Marker.DoubleFloat)); - - public bool IsUnpackedDouble => Check(If.Marker.Equals(Marker.Double)); - - public bool IsTinyString => Check(If.Marker.Between(0x00).And(0x1F)); - - public bool IsCompactString => Check(If.Marker.Between(0x30).And(0x33)); - - public bool IsNonfinalChunkString => Check(If.Marker.Equals(Marker.StringNonfinalChunk)); - - public bool IsFinalChunkString => Check(If.Marker.Equals(Marker.StringFinalChunk)); - - public bool IsCompactDateTime => Check(If.Marker.Equals(Marker.DateTimeCompact)); - - public bool IsUnpackedDateTime => Check(If.Marker.Equals(Marker.DateTimeLong)); - - public bool IsClassDefinition => Check(If.Marker.Equals(Marker.ClassDefinition)); - - public bool IsShortObjectReference => Check(If.Marker.Between(0x60).And(0x6F)); - - public bool IsLongObjectReference => Check(If.Marker.Equals(Marker.ClassReference)); - - public bool IsInstanceReference => Check(If.Marker.Equals(Marker.InstanceReference)); - - public bool IsVarList => Check(If.Marker.Equals(Marker.VarList)); - - public bool IsFixedList => Check(If.Marker.Equals(Marker.FixedList)); - public bool IsVarListUntyped => Check(If.Marker.Equals(Marker.VarListUntyped)); - public bool IsFixListUntyped => Check(If.Marker.Equals(Marker.FixListUntyped)); - public bool IsCompactFixList => Check(If.Marker.Between(Marker.CompactFixListStart).And(Marker.CompactFixListEnd)); - public bool IsCompactFixListUntyped => Check(If.Marker.Between(Marker.CompactFixListUntypedStart).And(Marker.CompactFixListUntypedEnd)); - - - - public void SetData(byte value) - { - Data = value; - } - - private bool Check(ISpecification specification) - { - return specification.IsSatisfied(Data); - } - - private static class If - { - internal static class Marker - { - public static MarkerValue Equals(byte value) - { - return new MarkerValue(value); - } - - public static MarkerMinValue Between(byte value) - { - return new MarkerMinValue(value); - } - - internal abstract class MarkerSpecification : ISpecification - { - public abstract bool IsSatisfied(byte arg); - } - - internal class MarkerValue : MarkerSpecification - { - protected readonly byte value; - - public MarkerValue(byte value) - { - this.value = value; - } - - public override bool IsSatisfied(byte arg) - { - return value == arg; - } - } - - internal class MarkerMinValue : MarkerValue - { - public MarkerMinValue(byte value) - : base(value) - { - } - - public override bool IsSatisfied(byte arg) - { - return value <= arg; - } - - public ISpecification And(byte max) - { - return this.And(new MarkerMaxValue(max)); - } - } - - private class MarkerMaxValue : MarkerValue - { - public MarkerMaxValue(byte value) - : base(value) - { - } - - public override bool IsSatisfied(byte arg) - { - return value >= arg; - } - } - } - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/ListElement.cs b/src/Hessian.NET/ListElement.cs deleted file mode 100644 index 7bfc89f..0000000 --- a/src/Hessian.NET/ListElement.cs +++ /dev/null @@ -1,179 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - - -namespace Hessian.Net -{ - public class ListElement: ISerializationElement - { - private readonly IDictionary _catalog; - private readonly IObjectSerializerFactory _factory; - public ListElement(Type listType, IDictionary catalog, IObjectSerializerFactory factory) - { - this.ObjectType = listType; - this._catalog = catalog; - this._factory = factory; - } - private readonly Lazy listTypeResolver = new Lazy(); - - public Type ObjectType { get; } - public ISerializationElement ChildSerializationElement { get; } - - public void Serialize(HessianOutputWriter writer, object graph, HessianSerializationContext context) - { - throw new NotImplementedException(); - } - - public object Deserialize(HessianInputReader reader, HessianSerializationContext context) - { - object ret=null; - var preamble = reader.BeginList(); - switch (preamble) - { - case ObjectPreamble.FixList: - ret =ReadFixList(reader, context); - break; - case ObjectPreamble.VarList: - ret = ReadVarList(reader, context); - break; - case ObjectPreamble.FixListUntyped: - ret = ReadFixListUntyped(reader, context); - break; - case ObjectPreamble.VarListUntyped: - ret = ReadVarListUntyped(reader, context); - break; - case ObjectPreamble.CompactFixList: - ret = ReadCompactFixList(reader, context); - break; - case ObjectPreamble.CompactFixListUntyped: - ret = ReadCompactFixListUntyped(reader, context); - break; - } - - reader.EndList(); - return ret; - } - - - private string ReadTypeName(HessianInputReader reader) - { - - var tag = reader.Peek(); - - // A type name is either a string, or an integer reference to a - // string already read and stored in the type-name ref map. - if ((tag >= 0x00 && tag < 0x20) - || (tag >= 0x30 && tag < 0x34) - || tag == 0x52 - || tag == 0x53) { - - var typeName = reader.ReadString(); - return typeName; - } - - reader.ReadInt32(); - return ""; - - } - - #region List - - private IList ReadVarList(HessianInputReader reader, HessianSerializationContext context) - { - var type = ReadTypeName(reader); - return ReadListCore(reader, context, type: type); - } - - private IList ReadFixList(HessianInputReader reader, HessianSerializationContext context) - { - var type = ReadTypeName(reader); - var length = reader.ReadInt32(); - return ReadListCore(reader, context, length, type); - } - - private IList ReadVarListUntyped(HessianInputReader reader, HessianSerializationContext context) - { - return ReadListCore(reader, context); - } - - private IList ReadFixListUntyped(HessianInputReader reader, HessianSerializationContext context) - { - var length = reader.ReadInt32(); - return ReadListCore(reader, context, length); - } - - private IList ReadCompactFixList(HessianInputReader reader, HessianSerializationContext context) - { - var tag = reader.LeadingByte.Data; - var length = tag - 0x70; - var type = ReadTypeName(reader); - return ReadListCore(reader, context, length, type); - } - - private IList ReadCompactFixListUntyped(HessianInputReader reader, HessianSerializationContext context) - { - var tag = reader.LeadingByte.Data; - var length = tag - 0x70; - return ReadListCore(reader, context, length); - } - - private IList ReadListCore(HessianInputReader reader, HessianSerializationContext context, int? length = null, string type = null) - { - var list = GetListInstance(type, length); - - //objectRefs.Add(list); - - if (length.HasValue) { - PopulateFixLengthList(reader, context, list, length.Value); - } else { - PopulateVarList(reader, context, list); - } - return list; - } - - private IList GetListInstance(string type, int? length = null) - { - IList list; - - if (length.HasValue) { - if (!listTypeResolver.Value.TryGetListInstance(type, length.Value, out list)) { - list = new List(length.Value); - } - } else { - if (!listTypeResolver.Value.TryGetListInstance(type, out list)) { - list = new List(); - } - } - - return list; - } - - private void PopulateFixLengthList(HessianInputReader reader, HessianSerializationContext context, IList list, int length) - { - var tag = reader.ReadByte(); //0x16 - - for (var i = 0; i < length; ++i) - { - ObjectElement objectElement = new ObjectElement(); - var scheme = HessianSerializationScheme.CreateFromType(this.GetType(), this._factory); - var obj = scheme.Deserialize(reader, context); - list.Add(obj); - } - } - - private void PopulateVarList(HessianInputReader reader, HessianSerializationContext context, IList list) - { - while (true) { - var tag = reader.ReadByte(); - if (tag == 'Z') { - reader.ReadByte(); - break; - } - list.Add(this.ChildSerializationElement.Deserialize(reader, context)); - } - } - - #endregion - } -} \ No newline at end of file diff --git a/src/Hessian.NET/ListTypeResolver.cs b/src/Hessian.NET/ListTypeResolver.cs deleted file mode 100644 index 5345603..0000000 --- a/src/Hessian.NET/ListTypeResolver.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Text; - -namespace Hessian.Net -{ - public class ListTypeResolver - { - private readonly Dictionary>> constructors = new Dictionary>>(); - private readonly Dictionary>> length_constructors = new Dictionary>>(); - private readonly Func> empty_list_ctor = () => new List(); - private readonly Func> empty_list_ctor_with_length = length => new List(length); - - public ListTypeResolver() - { - constructors.Add("System.Collections.ArrayList", empty_list_ctor); - constructors.Add("System.Collections.List", empty_list_ctor); - constructors.Add("System.Collections.IList", empty_list_ctor); - constructors.Add("System.Collections.Generic.List`1", empty_list_ctor); - constructors.Add("System.Collections.Generic.IList`1", empty_list_ctor); - constructors.Add("System.Collections.ObjectModel.Collection`1", () => new Collection()); - constructors.Add("java.util.List", empty_list_ctor); - constructors.Add("java.util.Vector", empty_list_ctor); - constructors.Add("java.util.ArrayList", empty_list_ctor); - constructors.Add("java.util.LinkedList", empty_list_ctor); - - length_constructors.Add("System.Collections.List", empty_list_ctor_with_length); - length_constructors.Add("System.Collections.IList", empty_list_ctor_with_length); - length_constructors.Add("System.Collections.Generic.List`1", empty_list_ctor_with_length); - length_constructors.Add("System.Collections.Generic.IList`1", empty_list_ctor_with_length); - length_constructors.Add("java.util.List", empty_list_ctor_with_length); - length_constructors.Add("java.util.Vector", empty_list_ctor_with_length); - length_constructors.Add("java.util.ArrayList", empty_list_ctor_with_length); - length_constructors.Add("java.util.LinkedList", empty_list_ctor_with_length); - } - - public bool TryGetListInstance(string type, out IList list) - { - list = null; - - Func> ctor; - if (!constructors.TryGetValue(type, out ctor)) - { - return false; - } - - list = ctor(); - return true; - } - - public bool TryGetListInstance(string type, int length, out IList list) - { - list = null; - - Func> ctor; - if (!length_constructors.TryGetValue(type, out ctor)) - { - return false; - } - - list = ctor(length); - return true; - } - } -} diff --git a/src/Hessian.NET/Marker.cs b/src/Hessian.NET/Marker.cs deleted file mode 100644 index cd28a8f..0000000 --- a/src/Hessian.NET/Marker.cs +++ /dev/null @@ -1,42 +0,0 @@ -namespace Hessian.Net -{ - /// - /// - /// - internal static class Marker - { - public const byte True = (byte) 'T';//0x54; - public const byte False = (byte) 'F';// 0x46; - public const byte Null = (byte) 'N';//0x4E; - public const byte BinaryNonfinalChunk = (byte) 'b';//0x41; - public const byte BinaryFinalChunk = (byte) 'B';//0x42; - public const byte ClassDefinition = (byte) 'C';//0x43; - public const byte DateTimeLong = 0x4A; - public const byte DateTimeCompact = 0x4B; - public const byte Double = 0x5A; - public const byte DoubleZero = 0x5B; - public const byte DoubleOne = 0x5C; - public const byte DoubleOctet = 0x5D; - public const byte DoubleShort = 0x5E; - public const byte DoubleFloat = 0x5F; - public const byte UnpackedInteger = (byte) 'I';// 0x49; - public const byte PackedLong = (byte) 'Y';// 0x59; - public const byte UnpackedLong = (byte) 'L';// 0x4C; - public const byte StringNonfinalChunk = 0x52; - public const byte StringFinalChunk = 0x53; - public const byte VarList = 0x55; - public const byte FixedList = 0x56; - public const byte VarListUntyped = 0x57; - public const byte FixListUntyped = 0x58; - - public const byte CompactFixListStart = 0x70; - public const byte CompactFixListEnd = 0x77; - public const byte CompactFixListUntypedStart = 0x78; - public const byte CompactFixListUntypedEnd = 0x7F; - - public const byte ClassReference = (byte) 'O';//0x4F - public const byte InstanceReference = (byte) 'Q'; //0x51; - - - } -} \ No newline at end of file diff --git a/src/Hessian.NET/ObjectElement.cs b/src/Hessian.NET/ObjectElement.cs deleted file mode 100644 index 3c51fd9..0000000 --- a/src/Hessian.NET/ObjectElement.cs +++ /dev/null @@ -1,153 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Reflection; -using System.Runtime.Serialization; -using Hessian.Net.Extension; - -namespace Hessian.Net -{ - public class ObjectElement : ISerializationElement - { - private string classname; - - public Type ObjectType - { - get; - } - - public string ClassName - { - get - { - if (String.IsNullOrEmpty(classname)) - { - classname = ObjectType - .GetTypeInfo() - .GetCustomAttribute() - .Unless(attribute => String.IsNullOrEmpty(attribute.Name)) - .Return(attribute => attribute.Name, ObjectType.FullName); - } - - return classname; - } - } - - public IList ObjectProperties - { - get; - } - public ObjectElement() - { - - } - - public ObjectElement(Type objectType, IList objectProperties) - { - ObjectType = objectType; - ObjectProperties = objectProperties; - } - - public void Serialize(HessianOutputWriter writer, object graph, HessianSerializationContext context) - { - var index = context.Instances.IndexOf(graph); - - if (index > -1) - { - writer.WriteInstanceReference(index); - return; - } - - context.Instances.Add(graph); - - index = context.Classes.IndexOf(ObjectType); - - if (index < 0) - { - writer.BeginClassDefinition(); - writer.WriteString(ClassName); - writer.WriteInt32(ObjectProperties.Count); - - foreach (var property in ObjectProperties) - { - writer.WriteString(property.PropertyName); - } - - writer.EndClassDefinition(); - - index = context.Classes.Count; - - context.Classes.Add(ObjectType); - } - - writer.WriteObjectReference(index); - - foreach (var item in ObjectProperties) - { - var value = item.Property.GetValue(graph); - item.Serialize(writer, value, context); - } - } - - public object Deserialize(HessianInputReader reader, HessianSerializationContext context) - { - reader.BeginObject(); - - if (reader.IsClassDefinition) - { - var className = reader.ReadString(); - var propertiesCount = reader.ReadInt32(); - - - - if (!String.Equals(this.ClassName, className)) - { - throw new HessianSerializerException(); - } - - if (this.ObjectProperties.Count != propertiesCount) - { - throw new HessianSerializerException(); - } - - for (var index = 0; index < propertiesCount; index++) - { - var propertyName = reader.ReadString(); - - var exists = this.ObjectProperties.Any(property => String.Equals(property.PropertyName, propertyName)); - - if (!exists) - { - throw new HessianSerializerException(); - } - } - - context.Classes.Add(this.ObjectType); - - reader.EndClassDefinition(); - } - else if (reader.IsInstanceReference) - { - var index = reader.ReadInstanceReference(); - return context.Instances[index]; - } - - var number = reader.ReadObjectReference(); - var instance = Activator.CreateInstance(this.ObjectType); - - context.Instances.Add(instance); - foreach (var item in this.ObjectProperties) - { - Console.WriteLine(item.PropertyName); - var value = item.Deserialize(reader, context); - Console.WriteLine(value); - item.Property.SetValue(instance, value); - } - - reader.EndObject(); - - return instance; - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/ObjectPreamble.cs b/src/Hessian.NET/ObjectPreamble.cs deleted file mode 100644 index 7f19c15..0000000 --- a/src/Hessian.NET/ObjectPreamble.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Hessian.Net -{ - public enum ObjectPreamble - { - None = -1, - ClassDefinition, - ObjectReference, - InstanceReference, - - VarList, - FixList, - VarListUntyped, - FixListUntyped, - CompactFixList, - CompactFixListUntyped - } -} \ No newline at end of file diff --git a/src/Hessian.NET/ObjectPropertyComparer.cs b/src/Hessian.NET/ObjectPropertyComparer.cs deleted file mode 100644 index 4344767..0000000 --- a/src/Hessian.NET/ObjectPropertyComparer.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Hessian.Net -{ - internal class ObjectPropertyComparer : IComparer - { - private readonly IComparer comparer; - - public ObjectPropertyComparer() - { - comparer = Comparer.Default; - } - - public int Compare(PropertyElement x, PropertyElement y) - { - var eq = comparer.Compare(x.PropertyOrder, y.PropertyOrder); - return 0 == eq - ? String.Compare(x.PropertyName, y.PropertyName, StringComparison.Ordinal) - : eq; - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/PropertyElement.cs b/src/Hessian.NET/PropertyElement.cs deleted file mode 100644 index 538f6c2..0000000 --- a/src/Hessian.NET/PropertyElement.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.Reflection; -using System.Runtime.Serialization; -using Hessian.Net.Extension; - -namespace Hessian.Net -{ - public class PropertyElement : ISerializationElement - { - private string propertyname; - private int? propertyOrder; - - public Type ObjectType => Property.PropertyType; - - public PropertyInfo Property - { - get; - } - - public ISerializationElement Element - { - get; - } - - public int PropertyOrder - { - get - { - if (!propertyOrder.HasValue) - { - var attribute = Property.GetCustomAttribute(); - propertyOrder = null == attribute ? 0 : attribute.Order; - } - - return propertyOrder.Value; - } - } - - public string PropertyName - { - get - { - if (String.IsNullOrEmpty(propertyname)) - { - propertyname = Property - .GetCustomAttribute() - .Unless(attribute => String.IsNullOrEmpty(attribute.Name)) - .Return(attribute => attribute.Name, Property.Name); - } - - return propertyname; - } - } - - public PropertyElement(PropertyInfo property, ISerializationElement element) - { - Property = property; - Element = element; - } - - public void Serialize(HessianOutputWriter writer, object graph, HessianSerializationContext context) - { - Element.Serialize(writer, graph, context); - } - - public object Deserialize(HessianInputReader reader, HessianSerializationContext context) - { - return Element.Deserialize(reader, context); - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Specification/AndSpecification.cs b/src/Hessian.NET/Specification/AndSpecification.cs deleted file mode 100644 index 1330ce4..0000000 --- a/src/Hessian.NET/Specification/AndSpecification.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Hessian.Net.Specification -{ - internal class AndSpecification : BinarySpecification - { - public AndSpecification(ISpecification left, ISpecification right) - : base(left, right) - { - } - - public override bool IsSatisfied(TParam arg) - { - return Left.IsSatisfied(arg) && Right.IsSatisfied(arg); - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Specification/BinarySpecification.cs b/src/Hessian.NET/Specification/BinarySpecification.cs deleted file mode 100644 index 7b3c9f2..0000000 --- a/src/Hessian.NET/Specification/BinarySpecification.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Hessian.Net.Specification -{ - public abstract class BinarySpecification : ISpecification - { - public ISpecification Left - { - get; - protected set; - } - - public ISpecification Right - { - get; - protected set; - } - - protected BinarySpecification(ISpecification left, ISpecification right) - { - Left = left; - Right = right; - } - - public abstract bool IsSatisfied(TParam arg); - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Specification/ISpecification.cs b/src/Hessian.NET/Specification/ISpecification.cs deleted file mode 100644 index a4ad7b4..0000000 --- a/src/Hessian.NET/Specification/ISpecification.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace Hessian.Net.Specification -{ - /// - /// - /// - /// - public interface ISpecification - { - /// - /// Checks if current specification can be satisfied by given - /// - /// The value to check specification - /// Flags if specification is satisfied successfuly - bool IsSatisfied(TParam arg); - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Specification/NotSpecification.cs b/src/Hessian.NET/Specification/NotSpecification.cs deleted file mode 100644 index 6d9b57a..0000000 --- a/src/Hessian.NET/Specification/NotSpecification.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Hessian.Net.Specification -{ - public class NotSpecification : ISpecification - { - private readonly ISpecification specification; - - public NotSpecification(ISpecification specification) - { - this.specification = specification; - } - - public bool IsSatisfied(TParam arg) - { - return !specification.IsSatisfied(arg); - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Specification/OrSpecification.cs b/src/Hessian.NET/Specification/OrSpecification.cs deleted file mode 100644 index 997fe6a..0000000 --- a/src/Hessian.NET/Specification/OrSpecification.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Hessian.Net.Specification -{ - /// - /// - /// - /// - public class OrSpecification : BinarySpecification - { - /// - /// - /// - /// - /// - public OrSpecification(ISpecification left, ISpecification right) - : base(left, right) - { - } - - /// - /// - /// - /// - /// - public override bool IsSatisfied(TParam arg) - { - return Left.IsSatisfied(arg) || Right.IsSatisfied(arg); - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Specification/Specification.cs b/src/Hessian.NET/Specification/Specification.cs deleted file mode 100644 index 86422eb..0000000 --- a/src/Hessian.NET/Specification/Specification.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Hessian.Net.Specification -{ - /// - /// - /// - public static class Specification - { - /// - /// - /// - /// - /// - /// - public static ISpecification Not(ISpecification specification) - { - return new NotSpecification(specification); - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Specification/SpecificationExtension.cs b/src/Hessian.NET/Specification/SpecificationExtension.cs deleted file mode 100644 index 8f1aa95..0000000 --- a/src/Hessian.NET/Specification/SpecificationExtension.cs +++ /dev/null @@ -1,32 +0,0 @@ -namespace Hessian.Net.Specification -{ - /// - /// - /// - public static class SpecificationExtension - { - /// - /// - /// - /// - /// - /// - /// - public static ISpecification And(this ISpecification left, ISpecification right) - { - return new AndSpecification(left, right); - } - - /// - /// - /// - /// - /// - /// - /// - public static ISpecification Or(this ISpecification left, ISpecification right) - { - return new OrSpecification(left, right); - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/Throw.cs b/src/Hessian.NET/Throw.cs deleted file mode 100644 index 399c172..0000000 --- a/src/Hessian.NET/Throw.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -namespace Hessian.Net -{ - internal static class Throw - { - public static void NotNull(object arg, string argname) - { - if (null == arg) - { - throw new ArgumentNullException(argname); - } - } - - public static void Validate(Func validator, string argname) - { - if (!validator()) - { - throw new ArgumentException("", argname); - } - } - } -} \ No newline at end of file diff --git a/src/Hessian.NET/TypeResolver.cs b/src/Hessian.NET/TypeResolver.cs deleted file mode 100644 index 6c3e459..0000000 --- a/src/Hessian.NET/TypeResolver.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Hessian.NET -{ - class TypeResolver - { - } -} diff --git a/src/Hessian.NET/ValueElement.cs b/src/Hessian.NET/ValueElement.cs deleted file mode 100644 index 630ed1a..0000000 --- a/src/Hessian.NET/ValueElement.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; - -namespace Hessian.Net -{ - public class ValueElement : ISerializationElement - { - public Type ObjectType - { - get; - } - - public IObjectSerializer ObjectSerializer - { - get; - } - - public ValueElement(Type objectType, IObjectSerializer objectSerializer) - { - ObjectType = objectType; - ObjectSerializer = objectSerializer; - } - - public void Serialize(HessianOutputWriter writer, object graph, HessianSerializationContext context) - { - ObjectSerializer.Serialize(writer, graph); - } - - public object Deserialize(HessianInputReader reader, HessianSerializationContext context) - { - return ObjectSerializer.Deserialize(reader); - } - } -} \ No newline at end of file diff --git a/src/Hessian/Deserializer.cs b/src/Hessian/Deserializer.cs index bdc9f60..53ee9b7 100644 --- a/src/Hessian/Deserializer.cs +++ b/src/Hessian/Deserializer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Net.NetworkInformation; using System.Text; using Hessian.Collections; @@ -47,127 +48,127 @@ namespace Hessian if (!tag.HasValue) { throw new EndOfStreamException(); } - Console.WriteLine("------------ 0x{0:x2}----------------",tag.Value); + //Console.WriteLine("------------ 0x{0:x2}----------------",tag.Value); switch (tag.Value) { case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08: case 0x09: case 0x0A: case 0x0B: case 0x0C: case 0x0D: case 0x0E: case 0x0F: case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17: case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D: case 0x1E: case 0x1F: - Console.WriteLine("ReadShortString"); + //Console.WriteLine("ReadShortString"); return ReadShortString(); case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27: case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F: - Console.WriteLine("ReadShortBinary"); + //Console.WriteLine("ReadShortBinary"); return ReadShortBinary(); case 0x30: case 0x31: case 0x32: case 0x33: - Console.WriteLine("ReadMediumString"); + //Console.WriteLine("ReadMediumString"); return ReadMediumString(); case 0x34: case 0x35: case 0x36: case 0x37: - Console.WriteLine("ReadMediumBinary"); + //Console.WriteLine("ReadMediumBinary"); return ReadMediumBinary(); case 0x38: case 0x39: case 0x3A: case 0x3B: case 0x3C: case 0x3D: case 0x3E: case 0x3F: - Console.WriteLine("ReadLongThreeBytes"); + //Console.WriteLine("ReadLongThreeBytes"); return ReadLongThreeBytes(); case 0x40: - Console.WriteLine("Reserved"); + //Console.WriteLine("Reserved"); return Reserved(); case 0x41: case 0x42: - Console.WriteLine("ReadChunkedBinary"); + //Console.WriteLine("ReadChunkedBinary"); return ReadChunkedBinary(); case 0x43: - Console.WriteLine("ReadClassDefinition"); + //Console.WriteLine("ReadClassDefinition"); return ReadClassDefinition(); case 0x44: - Console.WriteLine("ReadFullDouble"); + //Console.WriteLine("ReadFullDouble"); return ReadFullDouble(); case 0x45: - Console.WriteLine("Reserved"); + //Console.WriteLine("Reserved"); return Reserved(); case 0x46: - Console.WriteLine("ReadBoolean"); + //Console.WriteLine("ReadBoolean"); return ReadBoolean(); case 0x47: - Console.WriteLine("Reserved"); + //Console.WriteLine("Reserved"); return Reserved(); case 0x48: - Console.WriteLine("ReadUntypedMap"); + //Console.WriteLine("ReadUntypedMap"); return ReadUntypedMap(); case 0x49: - Console.WriteLine("ReadInteger"); + //Console.WriteLine("ReadInteger"); return ReadInteger(); case 0x4A: - Console.WriteLine("ReadDateInMillis"); + //Console.WriteLine("ReadDateInMillis"); return ReadDateInMillis(); case 0x4B: - Console.WriteLine("ReadDateInMinutes"); + //Console.WriteLine("ReadDateInMinutes"); return ReadDateInMinutes(); case 0x4C: - Console.WriteLine("ReadLongFull"); + //Console.WriteLine("ReadLongFull"); return ReadLongFull(); case 0x4D: - Console.WriteLine("ReadTypedMap"); + //Console.WriteLine("ReadTypedMap"); return ReadTypedMap(); case 0x4E: - Console.WriteLine("ReadNull"); + //Console.WriteLine("ReadNull"); return ReadNull(); case 0x4F: - Console.WriteLine("ReadObject"); + //Console.WriteLine("ReadObject"); return ReadObject(); case 0x50: - Console.WriteLine("Reserved"); + //Console.WriteLine("Reserved"); return Reserved(); case 0x51: - Console.WriteLine("ReadRef"); + //Console.WriteLine("ReadRef"); return ReadRef(); case 0x52: case 0x53: - Console.WriteLine("ReadChunkedString"); + //Console.WriteLine("ReadChunkedString"); return ReadChunkedString(); case 0x54: - Console.WriteLine("ReadBoolean"); + //Console.WriteLine("ReadBoolean"); return ReadBoolean(); case 0x55: - Console.WriteLine("ReadVarList"); + //Console.WriteLine("ReadVarList"); return ReadVarList(); case 0x56: - Console.WriteLine("ReadFixList"); + //Console.WriteLine("ReadFixList"); return ReadFixList(); case 0x57: - Console.WriteLine("ReadVarListUntyped"); + //Console.WriteLine("ReadVarListUntyped"); return ReadVarListUntyped(); case 0x58: - Console.WriteLine("ReadFixListUntyped"); + //Console.WriteLine("ReadFixListUntyped"); return ReadFixListUntyped(); case 0x59: - Console.WriteLine("ReadLongFourBytes"); + //Console.WriteLine("ReadLongFourBytes"); return ReadLongFourBytes(); case 0x5A: @@ -175,32 +176,32 @@ namespace Hessian throw new UnexpectedTagException(0x5A, "value"); case 0x5B: case 0x5C: - Console.WriteLine("ReadDoubleOneByte"); + //Console.WriteLine("ReadDoubleOneByte"); return ReadDoubleOneByte(); case 0x5D: - Console.WriteLine("ReadDoubleOneByte"); + //Console.WriteLine("ReadDoubleOneByte"); return ReadDoubleOneByte(); case 0x5E: - Console.WriteLine("ReadDoubleTwoBytes"); + //Console.WriteLine("ReadDoubleTwoBytes"); return ReadDoubleTwoBytes(); case 0x5F: - Console.WriteLine("ReadDoubleFourBytes"); + //Console.WriteLine("ReadDoubleFourBytes"); return ReadDoubleFourBytes(); case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67: case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6D: case 0x6E: case 0x6F: - Console.WriteLine("ReadObjectCompact"); + //Console.WriteLine("ReadObjectCompact"); return ReadObjectCompact(); case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: - Console.WriteLine("ReadCompactFixList"); + //Console.WriteLine("ReadCompactFixList"); return ReadCompactFixList(); case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F: - Console.WriteLine("ReadCompactFixListUntyped"); + //Console.WriteLine("ReadCompactFixListUntyped"); return ReadCompactFixListUntyped(); case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87: @@ -211,27 +212,27 @@ namespace Hessian case 0xA8: case 0xA9: case 0xAA: case 0xAB: case 0xAC: case 0xAD: case 0xAE: case 0xAF: case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7: case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF: - Console.WriteLine("ReadIntegerSingleByte"); + //Console.WriteLine("ReadIntegerSingleByte"); return ReadIntegerSingleByte(); case 0xC0: case 0xC1: case 0xC2: case 0xC3: case 0xC4: case 0xC5: case 0xC6: case 0xC7: case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF: - Console.WriteLine("ReadIntegerTwoBytes"); + //Console.WriteLine("ReadIntegerTwoBytes"); return ReadIntegerTwoBytes(); case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4: case 0xD5: case 0xD6: case 0xD7: - Console.WriteLine("ReadIntegerThreeBytes"); + //Console.WriteLine("ReadIntegerThreeBytes"); return ReadIntegerThreeBytes(); case 0xD8: case 0xD9: case 0xDA: case 0xDB: case 0xDC: case 0xDD: case 0xDE: case 0xDF: case 0xE0: case 0xE1: case 0xE2: case 0xE3: case 0xE4: case 0xE5: case 0xE6: case 0xE7: case 0xE8: case 0xE9: case 0xEA: case 0xEB: case 0xEC: case 0xED: case 0xEE: case 0xEF: - Console.WriteLine("ReadLongOneByte"); + //Console.WriteLine("ReadLongOneByte"); return ReadLongOneByte(); case 0xF0: case 0xF1: case 0xF2: case 0xF3: case 0xF4: case 0xF5: case 0xF6: case 0xF7: case 0xF8: case 0xF9: case 0xFA: case 0xFB: case 0xFC: case 0xFD: case 0xFE: case 0xFF: - Console.WriteLine("ReadLongTwoBytes"); + //Console.WriteLine("ReadLongTwoBytes"); return ReadLongTwoBytes(); } @@ -255,6 +256,7 @@ namespace Hessian || tag == 0x52 || tag == 0x53) { var typeName = ReadString(); + //Console.WriteLine("Type={0}",typeName); typeNameRefs.Add(typeName); return typeName; } @@ -340,9 +342,59 @@ namespace Hessian private void PopulateFixLengthList(IList list, int length) { - for (var i = 0; i < length; ++i) { - list.Add(ReadValue()); + // NOTE: 这里有BUG 第一层 如果是复杂对象 那第一个readValue的 + // 结果是 classDef,下一个才是真实的数据,但是如果数据的字段又是复杂类型 + // 下一个 就还是classDef 接着是真实数据,以此类型,但是呢,如果类型之前 + for (var i = 0; i < length; ++i) + { + var obj = ReadValue(); + switch (obj) + { + case ClassDef _: + { + var rVal = ReadValue() as HessianObject; // 真实的数据 + if (rVal == null) + { + throw new HessianException("decode error"); + } + list.Add(ReadRealObject(rVal)); + break; + } + case HessianObject vObj: + list.Add(ReadRealObject(vObj)); + break; + default: + list.Add(obj); + break; + } + } + } + + private HessianObject ReadRealObject(HessianObject hessianObject) + { + var builder = HessianObject.Builder.New(hessianObject.TypeName); + foreach (var (k,v) in hessianObject) + { + if (v is ClassDef) + { + if (!(ReadValue() is HessianObject rVal)) + { + throw new HessianException("decode error"); + } + + builder.Add(k,ReadRealObject(rVal)); + } + else if(v is HessianObject vObj) + { + builder.Add(k,ReadRealObject(vObj)); + } + else + { + builder.Add(k, v); + } } + + return builder.Create(); } private void PopulateVarList(IList list) diff --git a/src/Hessian/Serializer.cs b/src/Hessian/Serializer.cs index 5b65305..d9abfc1 100644 --- a/src/Hessian/Serializer.cs +++ b/src/Hessian/Serializer.cs @@ -112,6 +112,7 @@ namespace Hessian WriteString("["+GetItemTypeName(item.GetType())); } WriteValue(item); + index++; } } @@ -123,6 +124,11 @@ namespace Hessian return classType.Name; } + if (IsListType(type.GetTypeInfo())) + { + return "java.util.List"; + } + return type.Name; } private void WriteSimpleValue(object val)