替换RRQMSocket.RPC.WebApi代码

This commit is contained in:
若汝棋茗
2021-08-13 15:35:26 +08:00
parent a59a5f0680
commit 60295ffcc2
5 changed files with 198 additions and 11 deletions

View File

@@ -32,6 +32,22 @@ namespace RRQMSocket.RPC.WebApi
/// 所需类型<see cref="RRQMSocket.RPC.WebApi.ApiDataConverter"/>
/// </summary>
public static readonly DependencyProperty ApiDataConverterProperty =
DependencyProperty.Register("ApiDataConverter", typeof(ApiDataConverter), typeof(WebApiParserConfig), new XmlDataConverter());
DependencyProperty.Register("ApiDataConverter", typeof(ApiDataConverter), typeof(WebApiParserConfig), new JsonDataConverter());
/// <summary>
/// 最大数据包长度
/// </summary>
public int MaxPackageSize
{
get { return (int)GetValue(MaxPackageSizeProperty); }
set { SetValue(MaxPackageSizeProperty, value); }
}
/// <summary>
/// 最大数据包长度,所需类型<see cref="int"/>
/// </summary>
public static readonly DependencyProperty MaxPackageSizeProperty =
DependencyProperty.Register("MaxPackageSize", typeof(int), typeof(WebApiParserConfig), 1024);
}
}

View File

@@ -0,0 +1,128 @@
//------------------------------------------------------------------------------
// 此代码版权除特别声明或在RRQMCore.XREF命名空间的代码归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按MIT开源协议授权
// CSDN博客https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频https://space.bilibili.com/94253567
// Gitee源代码仓库https://gitee.com/RRQM_Home
// Github源代码仓库https://github.com/RRQM
// 交流QQ群234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.Helper;
using RRQMCore.XREF.Newtonsoft.Json;
using RRQMSocket.Http;
namespace RRQMSocket.RPC.WebApi
{
/// <summary>
/// Json数据转换器
/// </summary>
public class JsonDataConverter : ApiDataConverter
{
/// <summary>
/// OnPost
/// </summary>
/// <param name="httpRequest"></param>
/// <param name="methodInvoker"></param>
/// <param name="methodInstance"></param>
public override void OnPost(HttpRequest httpRequest, ref MethodInvoker methodInvoker, MethodInstance methodInstance)
{
switch (httpRequest.Content_Type)
{
case "application/x-www-form-urlencoded":
{
if (httpRequest.Params != null)
{
for (int i = 0; i < methodInstance.Parameters.Length; i++)
{
if (httpRequest.Params.TryGetValue(methodInstance.ParameterNames[i], out string value))
{
methodInvoker.Parameters[i] = value.ParseToType(methodInstance.ParameterTypes[i]);
}
else
{
methodInvoker.Parameters[i] = methodInstance.ParameterTypes[i].GetDefault();
}
}
}
break;
}
case "application/json":
{
if (methodInstance.Parameters.Length > 0)
{
for (int i = 0; i < methodInstance.Parameters.Length; i++)
{
if (i == 0)
{
methodInvoker.Parameters[i] = JsonConvert.DeserializeObject(httpRequest.Body, methodInstance.ParameterTypes[0]);
}
else
{
methodInvoker.Parameters[i] = methodInstance.ParameterTypes[i].GetDefault();
}
}
}
break;
}
}
}
/// <summary>
/// 在调用完成时转换结果
/// </summary>
/// <param name="methodInvoker"></param>
/// <param name="methodInstance"></param>
/// <returns></returns>
public override HttpResponse OnResult(MethodInvoker methodInvoker, MethodInstance methodInstance)
{
HttpRequest httpRequest = (HttpRequest)methodInvoker.Flag;
HttpResponse httpResponse = new HttpResponse();
switch (methodInvoker.Status)
{
case InvokeStatus.Success:
{
if (methodInvoker.ReturnParameter != null)
{
httpResponse.FromJson(JsonConvert.SerializeObject(methodInvoker.ReturnParameter));
break;
}
else
{
httpResponse.FromText(string.Empty);
}
break;
}
case InvokeStatus.UnFound:
{
string jsonString = JsonConvert.SerializeObject(new ActionResult() { Status = methodInvoker.Status, Message = methodInvoker.StatusMessage });
httpResponse.FromJson(jsonString, "404");
break;
}
case InvokeStatus.UnEnable:
{
string jsonString = JsonConvert.SerializeObject(new ActionResult() { Status = methodInvoker.Status, Message = methodInvoker.StatusMessage });
httpResponse.FromJson(jsonString, "405");
break;
}
case InvokeStatus.Abort:
{
string jsonString = JsonConvert.SerializeObject(new ActionResult() { Status = methodInvoker.Status, Message = methodInvoker.StatusMessage });
httpResponse.FromJson(jsonString, "403");
break;
}
case InvokeStatus.InvocationException:
case InvokeStatus.Exception:
{
string jsonString = JsonConvert.SerializeObject(new ActionResult() { Status = methodInvoker.Status, Message = methodInvoker.StatusMessage });
httpResponse.FromJson(jsonString, "422");
break;
}
}
return httpResponse;
}
}
}

View File

@@ -63,6 +63,10 @@ APIhttps://gitee.com/RRQM_OS/RRQM/wikis/pages </Description>
<PackagePath></PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RRQMSocket.Http\RRQMSocket.Http.csproj" />
<ProjectReference Include="..\RRQMSocket.RPC\RRQMSocket.RPC.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="RRQMCore">

View File

@@ -22,7 +22,7 @@ namespace RRQMSocket.RPC.WebApi
/// <summary>
/// WebApi解析器
/// </summary>
public class WebApiParser : TcpService<SimpleSocketClient>, IRPCParser
public class WebApiParser : TcpService<WebApiSocketClient>, IRPCParser
{
private RouteMap routeMap;
@@ -39,6 +39,16 @@ namespace RRQMSocket.RPC.WebApi
/// </summary>
public ApiDataConverter ApiDataConverter { get; private set; }
private int maxPackageSize;
/// <summary>
/// 最大数据包长度
/// </summary>
public int MaxPackageSize
{
get { return maxPackageSize; }
}
/// <summary>
/// 函数映射
/// </summary>
@@ -95,7 +105,7 @@ namespace RRQMSocket.RPC.WebApi
/// </summary>
/// <param name="provider"></param>
/// <param name="methodInstances"></param>
public void OnRegisterServer(ServerProvider provider, MethodInstance[] methodInstances)
public void OnRegisterServer(IServerProvider provider, MethodInstance[] methodInstances)
{
foreach (var methodInstance in methodInstances)
{
@@ -150,9 +160,8 @@ namespace RRQMSocket.RPC.WebApi
/// </summary>
/// <param name="provider"></param>
/// <param name="methodInstances"></param>
public void OnUnregisterServer(ServerProvider provider, MethodInstance[] methodInstances)
public void OnUnregisterServer(IServerProvider provider, MethodInstance[] methodInstances)
{
}
/// <summary>
@@ -185,11 +194,12 @@ namespace RRQMSocket.RPC.WebApi
/// <summary>
/// 载入配置
/// </summary>
/// <param name="serverConfig"></param>
protected override void LoadConfig(ServiceConfig serverConfig)
/// <param name="serviceConfig"></param>
protected override void LoadConfig(ServiceConfig serviceConfig)
{
base.LoadConfig(serverConfig);
this.ApiDataConverter = (ApiDataConverter)serverConfig.GetValue(WebApiParserConfig.ApiDataConverterProperty);
base.LoadConfig(serviceConfig);
this.ApiDataConverter = (ApiDataConverter)serviceConfig.GetValue(WebApiParserConfig.ApiDataConverterProperty);
this.maxPackageSize = (int)serviceConfig.GetValue(WebApiParserConfig.MaxPackageSizeProperty);
}
/// <summary>
@@ -197,13 +207,13 @@ namespace RRQMSocket.RPC.WebApi
/// </summary>
/// <param name="socketClient"></param>
/// <param name="createOption"></param>
protected override void OnCreateSocketCliect(SimpleSocketClient socketClient, CreateOption createOption)
protected override void OnCreateSocketCliect(WebApiSocketClient socketClient, CreateOption createOption)
{
if (createOption.NewCreate)
{
socketClient.OnReceived = this.OnReceived;
}
socketClient.SetDataHandlingAdapter(new Http.HttpDataHandlingAdapter(this.BufferLength, HttpType.Server));
socketClient.SetAdapter(new HttpDataHandlingAdapter(this.maxPackageSize, HttpType.Server));
}
private void OnReceived(SimpleSocketClient socketClient, ByteBlock byteBlock, object obj)

View File

@@ -0,0 +1,29 @@
using RRQMCore.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RRQMSocket.RPC.WebApi
{
/// <summary>
/// WebApiSocket辅助类
/// </summary>
public class WebApiSocketClient : SimpleSocketClient
{
/// <summary>
/// 禁用适配器赋值
/// </summary>
/// <param name="adapter"></param>
public sealed override void SetDataHandlingAdapter(DataHandlingAdapter adapter)
{
throw new RRQMException($"{nameof(WebApiSocketClient)}不允许设置适配器。");
}
internal void SetAdapter(DataHandlingAdapter adapter)
{
base.SetDataHandlingAdapter(adapter);
}
}
}