mirror of
https://github.com/RRQM/TouchSocket.git
synced 2025-12-20 10:26:43 +08:00
更新JsonRPC
This commit is contained in:
@@ -68,7 +68,7 @@ Demo:https://gitee.com/RRQM_OS/RRQMBox</Description>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RRQMSocket" Version="4.0.19" />
|
||||
<ProjectReference Include="..\RRQMSocket\RRQMSocket.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -62,6 +62,6 @@ API:https://gitee.com/RRQM_OS/RRQM/wikis/pages</Description>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RRQMSocket" Version="4.0.19" />
|
||||
<ProjectReference Include="..\RRQMSocket\RRQMSocket.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
46
RRQMSocket.RPC.JsonRpc/Control/RpcResponseContext.cs
Normal file
46
RRQMSocket.RPC.JsonRpc/Control/RpcResponseContext.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// 此代码版权归作者本人若汝棋茗所有
|
||||
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按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 System.Runtime.Serialization;
|
||||
|
||||
namespace RRQMSocket.RPC.JsonRpc
|
||||
{
|
||||
#pragma warning disable CS1591
|
||||
/// <summary>
|
||||
/// JsonRpc响应器
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
public class RpcResponseContext
|
||||
{
|
||||
[DataMember]
|
||||
public object result;
|
||||
|
||||
[DataMember]
|
||||
public error error;
|
||||
|
||||
[DataMember]
|
||||
public string id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 错误
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
public class error
|
||||
{
|
||||
[DataMember]
|
||||
public int code;
|
||||
|
||||
[DataMember]
|
||||
public string message;
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ namespace RRQMSocket.RPC.JsonRpc
|
||||
this.actionMap = new ActionMap();
|
||||
this.tcpService.CreatSocketCliect += this.OnCreatSocketCliect;
|
||||
this.tcpService.OnReceived += this.OnReceived;
|
||||
this.JsonConverter = new DataContractJsonConverter();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -85,6 +86,11 @@ namespace RRQMSocket.RPC.JsonRpc
|
||||
/// </summary>
|
||||
public ILog Logger { get { return this.tcpService.Logger; } set { this.tcpService.Logger = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// Json转换器
|
||||
/// </summary>
|
||||
public JsonConverter JsonConverter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 绑定服务
|
||||
/// </summary>
|
||||
@@ -164,8 +170,53 @@ namespace RRQMSocket.RPC.JsonRpc
|
||||
{
|
||||
ISocketClient socketClient = (ISocketClient)methodInvoker.Caller;
|
||||
|
||||
RpcResponseContext context = new RpcResponseContext();
|
||||
context.id = ((RpcRequestContext)methodInvoker.Flag).id;
|
||||
context.result = methodInvoker.ReturnParameter;
|
||||
error error = new error();
|
||||
context.error = error;
|
||||
|
||||
switch (methodInvoker.Status)
|
||||
{
|
||||
case InvokeStatus.Success:
|
||||
{
|
||||
context.error = null;
|
||||
break;
|
||||
}
|
||||
case InvokeStatus.UnFound:
|
||||
{
|
||||
error.code = -32601;
|
||||
error.message = "函数未找到";
|
||||
break;
|
||||
}
|
||||
case InvokeStatus.UnEnable:
|
||||
{
|
||||
error.code = -32601;
|
||||
error.message = "函数已被禁用";
|
||||
break;
|
||||
}
|
||||
case InvokeStatus.Abort:
|
||||
{
|
||||
error.code = -32601;
|
||||
error.message = "函数已被中断执行";
|
||||
break;
|
||||
}
|
||||
case InvokeStatus.InvocationException:
|
||||
{
|
||||
error.code = -32603;
|
||||
error.message = "函数内部异常";
|
||||
break;
|
||||
}
|
||||
case InvokeStatus.Exception:
|
||||
{
|
||||
error.code = -32602;
|
||||
error.message = methodInvoker.StatusMessage;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ByteBlock byteBlock = this.BytePool.GetByteBlock(this.BufferLength);
|
||||
this.BuildResponseByteBlock(byteBlock, methodInvoker, (RpcRequestContext)methodInvoker.Flag);
|
||||
this.BuildResponseByteBlock(byteBlock, context);
|
||||
if (socketClient.Online)
|
||||
{
|
||||
try
|
||||
@@ -222,8 +273,7 @@ namespace RRQMSocket.RPC.JsonRpc
|
||||
/// <returns></returns>
|
||||
protected virtual RpcRequestContext BuildRequestContext(ByteBlock byteBlock, out MethodInstance methodInstance)
|
||||
{
|
||||
byteBlock.Seek(0, SeekOrigin.Begin);
|
||||
RpcRequestContext context = (RpcRequestContext)ReadObject(typeof(RpcRequestContext), byteBlock);
|
||||
RpcRequestContext context = (RpcRequestContext)this.JsonConverter.Deserialize(Encoding.UTF8.GetString(byteBlock.Buffer, 0, (int)byteBlock.Length),typeof(RpcRequestContext));
|
||||
|
||||
if (this.actionMap.TryGet(context.method, out methodInstance))
|
||||
{
|
||||
@@ -240,9 +290,7 @@ namespace RRQMSocket.RPC.JsonRpc
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(s));
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
context.@params[i] = ReadObject(type, memoryStream);
|
||||
context.@params[i] = this.JsonConverter.Deserialize(s, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -258,31 +306,14 @@ namespace RRQMSocket.RPC.JsonRpc
|
||||
/// 构建响应数据
|
||||
/// </summary>
|
||||
/// <param name="responseByteBlock"></param>
|
||||
/// <param name="methodInvoker"></param>
|
||||
/// <param name="context"></param>
|
||||
protected virtual void BuildResponseByteBlock(ByteBlock responseByteBlock, MethodInvoker methodInvoker, RpcRequestContext context)
|
||||
/// <param name="responseContext"></param>
|
||||
protected virtual void BuildResponseByteBlock(ByteBlock responseByteBlock, RpcResponseContext responseContext)
|
||||
{
|
||||
if (string.IsNullOrEmpty(context.id))
|
||||
if (string.IsNullOrEmpty(responseContext.id))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (methodInvoker.ReturnParameter != null)
|
||||
{
|
||||
this.WriteObject(responseByteBlock, methodInvoker.ReturnParameter);
|
||||
}
|
||||
}
|
||||
|
||||
private object ReadObject(Type type, Stream stream)
|
||||
{
|
||||
DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(type);
|
||||
return deseralizer.ReadObject(stream);
|
||||
}
|
||||
|
||||
private void WriteObject(Stream stream, object obj)
|
||||
{
|
||||
DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(obj.GetType());
|
||||
deseralizer.WriteObject(stream, obj);
|
||||
this.JsonConverter.Serialize(responseByteBlock, responseContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -70,6 +70,6 @@
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RRQMSocket.RPC" Version="1.0.7" />
|
||||
<ProjectReference Include="..\RRQMSocket.RPC\RRQMSocket.RPC.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RRQMSocket.RPC.JsonRpc
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用DataContractJson转化器
|
||||
/// </summary>
|
||||
public class DataContractJsonConverter : JsonConverter
|
||||
{
|
||||
#pragma warning disable CS1591
|
||||
public override object Deserialize(string jsonString, Type parameterType)
|
||||
{
|
||||
DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(parameterType);
|
||||
return deseralizer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(jsonString)));
|
||||
}
|
||||
|
||||
|
||||
public override void Serialize(Stream stream, object parameter)
|
||||
{
|
||||
DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(parameter.GetType());
|
||||
deseralizer.WriteObject(stream, parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
RRQMSocket.RPC.JsonRpc/Serialization/JsonConverter.cs
Normal file
38
RRQMSocket.RPC.JsonRpc/Serialization/JsonConverter.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// 此代码版权归作者本人若汝棋茗所有
|
||||
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按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 System;
|
||||
using System.IO;
|
||||
|
||||
namespace RRQMSocket.RPC.JsonRpc
|
||||
{
|
||||
/// <summary>
|
||||
/// Json序列化转换器
|
||||
/// </summary>
|
||||
public abstract class JsonConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// </summary>
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="parameter"></param>
|
||||
/// <returns></returns>
|
||||
public abstract void Serialize(Stream stream,object parameter);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <param name="jsonString"></param>
|
||||
/// <param name="parameterType"></param>
|
||||
/// <returns></returns>
|
||||
public abstract object Deserialize(string jsonString, Type parameterType);
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ API:https://gitee.com/RRQM_OS/RRQM/wikis/pages </Description>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RRQMSocket.Http" Version="1.0.2" />
|
||||
<PackageReference Include="RRQMSocket.RPC" Version="1.0.7" />
|
||||
<ProjectReference Include="..\RRQMSocket.Http\RRQMSocket.Http.csproj" />
|
||||
<ProjectReference Include="..\RRQMSocket.RPC\RRQMSocket.RPC.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -66,7 +66,7 @@ API:https://gitee.com/RRQM_OS/RRQM/wikis/pages </Description>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="RRQMSocket.Http" Version="1.0.2" />
|
||||
<PackageReference Include="RRQMSocket.RPC" Version="1.0.7" />
|
||||
<ProjectReference Include="..\RRQMSocket.Http\RRQMSocket.Http.csproj" />
|
||||
<ProjectReference Include="..\RRQMSocket.RPC\RRQMSocket.RPC.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -13,10 +13,6 @@ using System;
|
||||
|
||||
namespace RRQMSocket.RPC.RRQMRPC
|
||||
{
|
||||
/*
|
||||
若汝棋茗
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// 序列化转换器
|
||||
/// </summary>
|
||||
|
||||
@@ -67,6 +67,8 @@ API:https://gitee.com/RRQM_OS/RRQM/wikis/pages</Description>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||
<PackageReference Include="RRQMSocket" Version="4.0.19" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RRQMSocket\RRQMSocket.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -29,6 +29,10 @@ namespace RRQMSocket
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
this.client = null;
|
||||
this.byteBlock.Dispose();
|
||||
this.byteBlock = null;
|
||||
this.endPoint = null;
|
||||
}
|
||||
|
||||
public void Recreate()
|
||||
|
||||
@@ -333,6 +333,7 @@ namespace RRQMSocket
|
||||
{
|
||||
throw new RRQMNotConnectedException("该实例已断开");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
@@ -221,7 +221,7 @@ namespace RRQMSocket
|
||||
BufferQueueGroup bufferQueueGroup = new BufferQueueGroup();
|
||||
bufferQueueGroups[i] = bufferQueueGroup;
|
||||
bufferQueueGroup.Thread = new Thread(Handle);//处理用户的消息
|
||||
bufferQueueGroup.clientBufferPool = new ObjectPool<ClientBuffer>(this.maxCount * 10);//处理用户的消息
|
||||
bufferQueueGroup.clientBufferPool = new ObjectPool<ClientBuffer>(this.maxCount);//处理用户的消息
|
||||
bufferQueueGroup.waitHandleBuffer = new AutoResetEvent(false);
|
||||
bufferQueueGroup.bufferAndClient = new BufferQueue();
|
||||
bufferQueueGroup.Thread.IsBackground = true;
|
||||
@@ -420,13 +420,13 @@ namespace RRQMSocket
|
||||
finally
|
||||
{
|
||||
queueGroup.clientBufferPool.DestroyObject(clientBuffer);
|
||||
clientBuffer.byteBlock.Dispose();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//queueGroup.isWait = true;
|
||||
//queueGroup.waitHandleBuffer.WaitOne(10);
|
||||
queueGroup.isWait = true;
|
||||
queueGroup.waitHandleBuffer.WaitOne();
|
||||
queueGroup.isWait = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,10 +212,10 @@ namespace RRQMSocket
|
||||
clientBuffer.byteBlock = byteBlock;
|
||||
queueGroup.bufferAndClient.Enqueue(clientBuffer);
|
||||
|
||||
//if (queueGroup.isWait)
|
||||
//{
|
||||
// queueGroup.waitHandleBuffer.Set();
|
||||
//}
|
||||
if (queueGroup.isWait)
|
||||
{
|
||||
queueGroup.waitHandleBuffer.Set();
|
||||
}
|
||||
try
|
||||
{
|
||||
WaitReceive();
|
||||
|
||||
Reference in New Issue
Block a user