|
| 1 | +using Castle.DynamicProxy; |
| 2 | +using DotNetCoreRpc.Core; |
| 3 | +using System; |
| 4 | +using System.Collections.Concurrent; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq.Expressions; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Net.Http.Headers; |
| 10 | +using System.Reflection; |
| 11 | +using System.Text; |
| 12 | +using System.Text.Json; |
| 13 | +using System.Threading; |
| 14 | +using System.Threading.Tasks; |
| 15 | + |
| 16 | +namespace DotNetCoreRpc.Client |
| 17 | +{ |
| 18 | + public class RequestHandler |
| 19 | + { |
| 20 | + private static readonly ConcurrentDictionary<TypeInfo, Func<MethodInfo, object[], object>> _taskFuncCache = new ConcurrentDictionary<TypeInfo, Func<MethodInfo, object[], object>>(); |
| 21 | + private static readonly ConcurrentDictionary<TypeInfo, Func<MethodInfo, object[], object>> _valueTaskFuncCache = new ConcurrentDictionary<TypeInfo, Func<MethodInfo, object[], object>>(); |
| 22 | + |
| 23 | + private readonly HttpClient _httpClient; |
| 24 | + public RequestHandler(HttpClient httpClient) |
| 25 | + { |
| 26 | + _httpClient = httpClient; |
| 27 | + } |
| 28 | + |
| 29 | + public object SyncResultHandle(MethodInfo methodInfo, params object[] arguments) |
| 30 | + { |
| 31 | + return TaskResultHandle<object>(methodInfo, arguments).GetAwaiter().GetResult(); |
| 32 | + } |
| 33 | + |
| 34 | + public Func<MethodInfo, object[], object> GetTaskResultHandleFunc(TypeInfo methodReturnType) |
| 35 | + { |
| 36 | + return _taskFuncCache.GetOrAdd(methodReturnType, type => { |
| 37 | + return GetHandleFunc(nameof(TaskResultHandle), type); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + public Func<MethodInfo, object[], object> GetValueResultHandleFunc(TypeInfo methodReturnType) |
| 42 | + { |
| 43 | + return _valueTaskFuncCache.GetOrAdd(methodReturnType, type => { |
| 44 | + return GetHandleFunc(nameof(ValueResultHandle), type); |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + private Func<MethodInfo, object[], object> GetHandleFunc(string handleMethodName, TypeInfo methodReturnType) |
| 49 | + { |
| 50 | + var returnType = methodReturnType.GetGenericArguments()[0]; |
| 51 | + var resultMethod = GetType().GetMethod(handleMethodName, BindingFlags.NonPublic | BindingFlags.Instance)!.MakeGenericMethod(returnType); |
| 52 | + ParameterExpression methodInfoSource = Expression.Parameter(typeof(MethodInfo), "methodInfo"); |
| 53 | + ParameterExpression argumentsSource = Expression.Parameter(typeof(object[]), "arguments"); |
| 54 | + var instance = Expression.Constant(this); |
| 55 | + var callExpr = Expression.Call(instance, resultMethod, methodInfoSource, argumentsSource); |
| 56 | + var convertBody = Expression.Convert(callExpr, typeof(object)); |
| 57 | + var expr = Expression.Lambda<Func<MethodInfo, object[], object>>(convertBody, methodInfoSource, argumentsSource).Compile(); |
| 58 | + return expr; |
| 59 | + } |
| 60 | + |
| 61 | + private async Task<T> TaskResultHandle<T>(MethodInfo methodInfo, params object[] arguments) |
| 62 | + { |
| 63 | + var result = await SendRequest(methodInfo, arguments); |
| 64 | + if (result != null && result.Length != 0) |
| 65 | + { |
| 66 | + ResponseModel responseModel = result.FromJson<ResponseModel>(); |
| 67 | + if (responseModel.Code != (int)HttpStatusCode.OK) |
| 68 | + { |
| 69 | + throw new Exception($"请求出错,返回内容:{Encoding.UTF8.GetString(result)}"); |
| 70 | + } |
| 71 | + |
| 72 | + TypeInfo methodReturnType = methodInfo.ReturnType.GetTypeInfo(); |
| 73 | + if (methodReturnType.IsAsync()) |
| 74 | + { |
| 75 | + methodReturnType = methodReturnType.GetGenericArguments()[0].GetTypeInfo(); |
| 76 | + } |
| 77 | + |
| 78 | + if (responseModel.Data is JsonElement jsonElement) |
| 79 | + { |
| 80 | + var returnValue = jsonElement.FromJson(methodReturnType); |
| 81 | + return (T)returnValue; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return default; |
| 86 | + } |
| 87 | + |
| 88 | + private ValueTask<T> ValueResultHandle<T>(MethodInfo methodInfo, params object[] arguments) |
| 89 | + { |
| 90 | + var taskResult = TaskResultHandle<T>(methodInfo, arguments); |
| 91 | + return new ValueTask<T>(taskResult); |
| 92 | + } |
| 93 | + |
| 94 | + private async Task<byte[]> SendRequest(MethodInfo methodInfo, params object[] arguments) |
| 95 | + { |
| 96 | + var requestModel = new RequestModel |
| 97 | + { |
| 98 | + TypeFullName = methodInfo.DeclaringType.FullName, |
| 99 | + MethodName = methodInfo.Name, |
| 100 | + Paramters = arguments |
| 101 | + }; |
| 102 | + |
| 103 | + HttpContent httpContent = new StringContent(requestModel.ToJson()); |
| 104 | + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
| 105 | + var responseMessage = await _httpClient.PostAsync("/DotNetCoreRpc/ServerRequest", httpContent); |
| 106 | + byte[] result = await responseMessage.Content.ReadAsByteArrayAsync(); |
| 107 | + if (responseMessage.StatusCode == HttpStatusCode.OK) |
| 108 | + { |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + throw new Exception($"请求异常,StatusCode:{responseMessage.StatusCode}"); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments