add task.run
This commit is contained in:
+31
-28
@@ -19,39 +19,42 @@ public partial class Synapse
|
|||||||
{
|
{
|
||||||
Client!.ApplicationMessageReceivedAsync += async e =>
|
Client!.ApplicationMessageReceivedAsync += async e =>
|
||||||
{
|
{
|
||||||
if (!e.ApplicationMessage.Topic.StartsWith(EventServerTopicPrefix)) return;
|
await Task.Run(async () =>
|
||||||
var reqBody = e.ApplicationMessage.ConvertPayloadToString();
|
{
|
||||||
var eventName = e.ApplicationMessage.Topic.Replace(EventServerTopicPrefix, string.Empty);
|
if (!e.ApplicationMessage.Topic.StartsWith(EventServerTopicPrefix)) return;
|
||||||
logger.LogDebug("Synapse Event Receive: {EventName}\n{Body}", eventName, reqBody);
|
var reqBody = e.ApplicationMessage.ConvertPayloadToString();
|
||||||
var methods = EventRegistry
|
var eventName = e.ApplicationMessage.Topic.Replace(EventServerTopicPrefix, string.Empty);
|
||||||
.Where(x => Regex.IsMatch(eventName,
|
logger.LogDebug("Synapse Event Receive: {EventName}\n{Body}", eventName, reqBody);
|
||||||
"^" + Regex.Escape(x.Key!).Replace("\\+", "[^/]+").Replace("\\#", ".*") + "$"))
|
var methods = EventRegistry
|
||||||
.ToArray();
|
.Where(x => Regex.IsMatch(eventName,
|
||||||
var tasks = methods.Select(method => Task.Run(() =>
|
"^" + Regex.Escape(x.Key!).Replace("\\+", "[^/]+").Replace("\\#", ".*") + "$"))
|
||||||
{
|
.ToArray();
|
||||||
var callClass = sp.CreateScope().ServiceProvider.GetRequiredService(method.Class!);
|
var tasks = methods.Select(method => Task.Run(() =>
|
||||||
var mt = callClass.GetType().GetMethod(method.Method!);
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (mt!.GetParameters().Length == 2)
|
var callClass = sp.CreateScope().ServiceProvider.GetRequiredService(method.Class!);
|
||||||
|
var mt = callClass.GetType().GetMethod(method.Method!);
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var pt = mt.GetParameters()[1].ParameterType;
|
if (mt!.GetParameters().Length == 2)
|
||||||
mt.Invoke(callClass, pt == typeof(string)
|
{
|
||||||
? new object?[] { eventName, reqBody }
|
var pt = mt.GetParameters()[1].ParameterType;
|
||||||
: new object?[] { eventName, JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption) });
|
mt.Invoke(callClass, pt == typeof(string)
|
||||||
|
? [eventName, reqBody]
|
||||||
|
: [eventName, JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption)]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mt.Invoke(callClass, new object[] { eventName });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
mt.Invoke(callClass, new object[] { eventName });
|
logger.LogError("Synapse Event Processor Error: {Err}\n{Stack}", ex.Message, ex.StackTrace);
|
||||||
}
|
}
|
||||||
}
|
}))
|
||||||
catch (Exception ex)
|
.ToList();
|
||||||
{
|
await Task.WhenAll(tasks);
|
||||||
logger.LogError("Synapse Event Processor Error: {Err}\n{Stack}", ex.Message, ex.StackTrace);
|
});
|
||||||
}
|
|
||||||
}))
|
|
||||||
.ToList();
|
|
||||||
await Task.WhenAll(tasks);
|
|
||||||
};
|
};
|
||||||
SubEventServerTopic();
|
SubEventServerTopic();
|
||||||
}
|
}
|
||||||
|
|||||||
+76
-71
@@ -22,88 +22,93 @@ public partial class Synapse
|
|||||||
{
|
{
|
||||||
Client!.ApplicationMessageReceivedAsync += e =>
|
Client!.ApplicationMessageReceivedAsync += e =>
|
||||||
{
|
{
|
||||||
if (!e.ApplicationMessage.Topic.StartsWith(RpcServerTopicPrefix)) return Task.CompletedTask;
|
Task.Run(() =>
|
||||||
var reqBody = e.ApplicationMessage.ConvertPayloadToString();
|
|
||||||
var action = e.ApplicationMessage.Topic.Replace(RpcServerTopicPrefix, string.Empty);
|
|
||||||
var appInfo = e.ApplicationMessage.ResponseTopic.Split(",");
|
|
||||||
logger.LogDebug(
|
|
||||||
"Synapse RPC Server Receive: ({BasicPropertiesMessageId}) {BasicPropertiesReplyTo} -> {BasicPropertiesType}@{OptionsAppName}\n{S}",
|
|
||||||
appInfo[2], appInfo[0], action, Options.AppName, reqBody);
|
|
||||||
SimApiBaseResponse res;
|
|
||||||
var method = RpcRegistry.FirstOrDefault(x => x.Key == action);
|
|
||||||
if (method == null)
|
|
||||||
{
|
{
|
||||||
res = new SimApiBaseResponse(404, "method not found");
|
if (!e.ApplicationMessage.Topic.StartsWith(RpcServerTopicPrefix)) return;
|
||||||
}
|
var reqBody = e.ApplicationMessage.ConvertPayloadToString();
|
||||||
else
|
var action = e.ApplicationMessage.Topic.Replace(RpcServerTopicPrefix, string.Empty);
|
||||||
{
|
var appInfo = e.ApplicationMessage.ResponseTopic.Split(",");
|
||||||
var callClass = sp.CreateScope().ServiceProvider.GetRequiredService(method.Class!);
|
logger.LogDebug(
|
||||||
var mt = callClass.GetType().GetMethod(method.Method!);
|
"Synapse RPC Server Receive: ({BasicPropertiesMessageId}) {BasicPropertiesReplyTo} -> {BasicPropertiesType}@{OptionsAppName}\n{S}",
|
||||||
try
|
appInfo[2], appInfo[0], action, Options.AppName, reqBody);
|
||||||
|
SimApiBaseResponse res;
|
||||||
|
var method = RpcRegistry.FirstOrDefault(x => x.Key == action);
|
||||||
|
if (method == null)
|
||||||
{
|
{
|
||||||
var methodParams = mt!.GetParameters();
|
res = new SimApiBaseResponse(404, "method not found");
|
||||||
object? ret;
|
|
||||||
switch (methodParams.Length)
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
var pt = mt.GetParameters()[0].ParameterType;
|
|
||||||
var param = pt == typeof(string)
|
|
||||||
? [reqBody]
|
|
||||||
: new[] { JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption) };
|
|
||||||
ret = mt.Invoke(callClass, param);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
var headerData =
|
|
||||||
e.ApplicationMessage.UserProperties.ToDictionary(x => x.Name, x => x.Value);
|
|
||||||
var pt2 = mt.GetParameters()[0].ParameterType;
|
|
||||||
var param2 = pt2 == typeof(string)
|
|
||||||
? [reqBody]
|
|
||||||
: new[] { JsonSerializer.Deserialize(reqBody, pt2, SimApiUtil.JsonOption), headerData };
|
|
||||||
ret = mt.Invoke(callClass, param2);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ret = mt.Invoke(callClass, []);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = new SimApiBaseResponse<object?>
|
|
||||||
{
|
|
||||||
Data = ret
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
catch (TargetInvocationException ex)
|
else
|
||||||
{
|
{
|
||||||
if (ex.InnerException is SimApiException ie)
|
var callClass = sp.CreateScope().ServiceProvider.GetRequiredService(method.Class!);
|
||||||
|
var mt = callClass.GetType().GetMethod(method.Method!);
|
||||||
|
try
|
||||||
{
|
{
|
||||||
logger.LogDebug("Synapse RPC调用错误: {Err}", ie.Message);
|
var methodParams = mt!.GetParameters();
|
||||||
res = new SimApiBaseResponse(ie.Code, ie.Message);
|
object? ret;
|
||||||
|
switch (methodParams.Length)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
var pt = mt.GetParameters()[0].ParameterType;
|
||||||
|
var param = pt == typeof(string)
|
||||||
|
? [reqBody]
|
||||||
|
: new[] { JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption) };
|
||||||
|
ret = mt.Invoke(callClass, param);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
var headerData =
|
||||||
|
e.ApplicationMessage.UserProperties.ToDictionary(x => x.Name, x => x.Value);
|
||||||
|
var pt2 = mt.GetParameters()[0].ParameterType;
|
||||||
|
var param2 = pt2 == typeof(string)
|
||||||
|
? [reqBody]
|
||||||
|
: new[]
|
||||||
|
{
|
||||||
|
JsonSerializer.Deserialize(reqBody, pt2, SimApiUtil.JsonOption), headerData
|
||||||
|
};
|
||||||
|
ret = mt.Invoke(callClass, param2);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ret = mt.Invoke(callClass, []);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
res = new SimApiBaseResponse<object?>
|
||||||
|
{
|
||||||
|
Data = ret
|
||||||
|
};
|
||||||
}
|
}
|
||||||
else
|
catch (TargetInvocationException ex)
|
||||||
{
|
{
|
||||||
logger.LogError("Synapse RPC 方法异常: {Err}\n{Stack}", ex.Message, ex.StackTrace);
|
if (ex.InnerException is SimApiException ie)
|
||||||
|
{
|
||||||
|
logger.LogDebug("Synapse RPC调用错误: {Err}", ie.Message);
|
||||||
|
res = new SimApiBaseResponse(ie.Code, ie.Message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogError("Synapse RPC 方法异常: {Err}\n{Stack}", ex.Message, ex.StackTrace);
|
||||||
|
res = new SimApiBaseResponse(500, ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogDebug("Synapse RPC调用失败: {Err}", ex.Message);
|
||||||
res = new SimApiBaseResponse(500, ex.Message);
|
res = new SimApiBaseResponse(500, ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.LogDebug("Synapse RPC调用失败: {Err}", ex.Message);
|
|
||||||
res = new SimApiBaseResponse(500, ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var returnJson = JsonSerializer.Serialize((object)res, SimApiUtil.JsonOption);
|
|
||||||
var reply = $"{Options.SysName}/{appInfo[0]}/rpc/client/{appInfo[1]}/{appInfo[2]}";
|
|
||||||
var message = new MqttApplicationMessageBuilder()
|
|
||||||
.WithTopic(reply)
|
|
||||||
.WithPayload(returnJson)
|
|
||||||
.WithRetainFlag(false)
|
|
||||||
.Build();
|
|
||||||
if (!Client.IsConnected) return Task.CompletedTask;
|
|
||||||
Client.PublishAsync(message, CancellationToken.None).Wait();
|
|
||||||
logger.LogDebug(
|
|
||||||
"Synapse Rpc Server Return: ({BasicPropertiesMessageId}) {BasicPropertiesType}@{OptionsAppName} -> {BasicPropertiesReplyTo}\n{ReturnJson}",
|
|
||||||
appInfo[2], action, Options.AppName, appInfo[0], returnJson);
|
|
||||||
|
|
||||||
|
var returnJson = JsonSerializer.Serialize((object)res, SimApiUtil.JsonOption);
|
||||||
|
var reply = $"{Options.SysName}/{appInfo[0]}/rpc/client/{appInfo[1]}/{appInfo[2]}";
|
||||||
|
var message = new MqttApplicationMessageBuilder()
|
||||||
|
.WithTopic(reply)
|
||||||
|
.WithPayload(returnJson)
|
||||||
|
.WithRetainFlag(false)
|
||||||
|
.Build();
|
||||||
|
if (!Client.IsConnected) return;
|
||||||
|
Client.PublishAsync(message, CancellationToken.None).Wait();
|
||||||
|
logger.LogDebug(
|
||||||
|
"Synapse Rpc Server Return: ({BasicPropertiesMessageId}) {BasicPropertiesType}@{OptionsAppName} -> {BasicPropertiesReplyTo}\n{ReturnJson}",
|
||||||
|
appInfo[2], action, Options.AppName, appInfo[0], returnJson);
|
||||||
|
});
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
};
|
};
|
||||||
SubRpcServerTopic();
|
SubRpcServerTopic();
|
||||||
|
|||||||
Reference in New Issue
Block a user