Compare commits

..
5 Commits
Author SHA1 Message Date
xrain 4e1b7d4845 update to 9 2025-10-14 02:20:37 +08:00
xrain 71acb35df9 cancle json refrence 2025-08-13 17:28:24 +08:00
xrain 67cc7346dc use concurrentDic 2025-05-17 21:17:43 +08:00
xrain 8627ae61fa use lock 2025-05-17 21:11:27 +08:00
xrain 02523028db add task.run 2025-05-17 20:40:48 +08:00
5 changed files with 10 additions and 13 deletions
+2 -2
View File
@@ -24,10 +24,10 @@ public static class SimApiUtil
/// </summary> /// </summary>
public static JsonSerializerOptions JsonOption => new() public static JsonSerializerOptions JsonOption => new()
{ {
ReferenceHandler = ReferenceHandler.Preserve, // ReferenceHandler = ReferenceHandler.Preserve,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull // DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
}; };
/// <summary> /// <summary>
+1 -1
View File
@@ -14,8 +14,8 @@
<SynchReleaseVersion>false</SynchReleaseVersion> <SynchReleaseVersion>false</SynchReleaseVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>5.0.2</PackageVersion> <PackageVersion>5.0.2</PackageVersion>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
-2
View File
@@ -3,7 +3,6 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
using Hangfire; using Hangfire;
using Hangfire.Console; using Hangfire.Console;
using Hangfire.Redis.StackExchange; using Hangfire.Redis.StackExchange;
@@ -251,7 +250,6 @@ public static class SimApiExtensions
.AddJsonOptions(opt => .AddJsonOptions(opt =>
{ {
opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
opt.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
}); });
} }
+5 -5
View File
@@ -1,20 +1,19 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet; using MQTTnet;
using MQTTnet.Packets;
using SimApi.Communications; using SimApi.Communications;
using SimApi.Helpers; using SimApi.Helpers;
using System.Collections.Concurrent;
namespace SimApi; namespace SimApi;
public partial class Synapse public partial class Synapse
{ {
private Dictionary<string, TaskCompletionSource<string>> ResponseCompletionSources { get; } = new(); private ConcurrentDictionary<string, TaskCompletionSource<string>> ResponseCompletionSources { get; } = new();
private string EventClientTopicPrefix => $"{Options.SysName}/{Options.AppName}/rpc/client/{Options.AppId}/"; private string EventClientTopicPrefix => $"{Options.SysName}/{Options.AppName}/rpc/client/{Options.AppId}/";
@@ -27,7 +26,7 @@ public partial class Synapse
var messageId = e.ApplicationMessage.Topic.Replace(EventClientTopicPrefix, string.Empty); var messageId = e.ApplicationMessage.Topic.Replace(EventClientTopicPrefix, string.Empty);
if (!ResponseCompletionSources.TryGetValue(messageId, out var tcs)) return Task.CompletedTask; if (!ResponseCompletionSources.TryGetValue(messageId, out var tcs)) return Task.CompletedTask;
tcs.SetResult(reqBody); tcs.SetResult(reqBody);
ResponseCompletionSources.Remove(messageId); ResponseCompletionSources.Remove(messageId, out _);
return Task.CompletedTask; return Task.CompletedTask;
}; };
SubRpcClientTopic(); SubRpcClientTopic();
@@ -43,6 +42,7 @@ public partial class Synapse
private string? FireRpc(string app, string action, object? param, Dictionary<string, string>? headers = null, private string? FireRpc(string app, string action, object? param, Dictionary<string, string>? headers = null,
int? timeout = null) int? timeout = null)
{ {
// 移除锁语句
string paramJson; string paramJson;
if (param is string strParam) if (param is string strParam)
{ {
@@ -56,7 +56,7 @@ public partial class Synapse
var topic = $"{Options.SysName}/{app}/rpc/server/{action}"; var topic = $"{Options.SysName}/{app}/rpc/server/{action}";
var messageId = Guid.NewGuid().ToString(); var messageId = Guid.NewGuid().ToString();
var tcs = new TaskCompletionSource<string>(); var tcs = new TaskCompletionSource<string>();
ResponseCompletionSources.Add(messageId, tcs); ResponseCompletionSources.TryAdd(messageId, tcs);
var messageBuilder = new MqttApplicationMessageBuilder() var messageBuilder = new MqttApplicationMessageBuilder()
.WithTopic(topic) .WithTopic(topic)
.WithPayload(paramJson) .WithPayload(paramJson)
+2 -3
View File
@@ -20,9 +20,9 @@ public partial class Synapse
private void RunRpcServer() private void RunRpcServer()
{ {
Client!.ApplicationMessageReceivedAsync += e => Client!.ApplicationMessageReceivedAsync += async e =>
{ {
Task.Run(() => await Task.Run(() =>
{ {
if (!e.ApplicationMessage.Topic.StartsWith(RpcServerTopicPrefix)) return; if (!e.ApplicationMessage.Topic.StartsWith(RpcServerTopicPrefix)) return;
var reqBody = e.ApplicationMessage.ConvertPayloadToString(); var reqBody = e.ApplicationMessage.ConvertPayloadToString();
@@ -109,7 +109,6 @@ public partial class Synapse
"Synapse Rpc Server Return: ({BasicPropertiesMessageId}) {BasicPropertiesType}@{OptionsAppName} -> {BasicPropertiesReplyTo}\n{ReturnJson}", "Synapse Rpc Server Return: ({BasicPropertiesMessageId}) {BasicPropertiesType}@{OptionsAppName} -> {BasicPropertiesReplyTo}\n{ReturnJson}",
appInfo[2], action, Options.AppName, appInfo[0], returnJson); appInfo[2], action, Options.AppName, appInfo[0], returnJson);
}); });
return Task.CompletedTask;
}; };
SubRpcServerTopic(); SubRpcServerTopic();
} }