Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af6bc86a0c | ||
|
|
fc7714198f | ||
|
|
d2b5f14e78 | ||
|
|
116cbcdc7f | ||
|
|
4e1b7d4845 | ||
|
|
71acb35df9 | ||
|
|
67cc7346dc |
@@ -14,7 +14,7 @@ jobs:
|
|||||||
- name: Setup .NET Core
|
- name: Setup .NET Core
|
||||||
uses: actions/setup-dotnet@v1
|
uses: actions/setup-dotnet@v1
|
||||||
with:
|
with:
|
||||||
dotnet-version: "8.0.200"
|
dotnet-version: "9.0.305"
|
||||||
- name: Publish
|
- name: Publish
|
||||||
run: |
|
run: |
|
||||||
version=`git describe --tags`
|
version=`git describe --tags`
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
namespace SimApi.Communications;
|
||||||
|
|
||||||
namespace SimApi.Communications;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 只有ID的请求
|
/// 只有ID的请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiIdOnlyRequest
|
public class SimApiIdOnlyRequest
|
||||||
{
|
{
|
||||||
[Required] public int Id { get; set; }
|
public required int Id { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -15,7 +13,7 @@ public class SimApiIdOnlyRequest
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiStringIdOnlyRequest
|
public class SimApiStringIdOnlyRequest
|
||||||
{
|
{
|
||||||
[Required] public string? Id { get; set; }
|
public required string Id { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -24,7 +22,7 @@ public class SimApiStringIdOnlyRequest
|
|||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class SimApiOneFieldRequest<T>
|
public class SimApiOneFieldRequest<T>
|
||||||
{
|
{
|
||||||
[Required] public T? Data { get; set; }
|
public required T Data { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -32,6 +30,6 @@ public class SimApiOneFieldRequest<T>
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiBasePageRequest
|
public class SimApiBasePageRequest
|
||||||
{
|
{
|
||||||
[Required] public int Page { get; set; }
|
public required int Page { get; set; }
|
||||||
[Required] public int Count { get; set; }
|
public required int Count { get; set; }
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,6 @@ public class SimApiLoginItem
|
|||||||
{
|
{
|
||||||
public string Id { get; set; } = null!;
|
public string Id { get; set; } = null!;
|
||||||
public string[] Type { get; set; } = ["user"];
|
public string[] Type { get; set; } = ["user"];
|
||||||
public Dictionary<string, string> Meta { get; set; } = new();
|
public Dictionary<string, string> Meta { get; set; } = [];
|
||||||
public object? Extra { get; set; }
|
public object? Extra { get; set; }
|
||||||
};
|
};
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using SimApi.Communications;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using SimApi.Communications;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
@@ -53,7 +54,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// <param name="condition">检测条件</param>
|
/// <param name="condition">检测条件</param>
|
||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述</param>
|
/// <param name="message">错误描述</param>
|
||||||
protected static void ErrorWhen(bool condition, int code = 400, string message = "")
|
protected static void ErrorWhen([DoesNotReturnIf(true)] bool condition, int code = 400, string message = "")
|
||||||
{
|
{
|
||||||
if (condition)
|
if (condition)
|
||||||
{
|
{
|
||||||
@@ -68,7 +69,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// <param name="condition">检测条件</param>
|
/// <param name="condition">检测条件</param>
|
||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述</param>
|
/// <param name="message">错误描述</param>
|
||||||
protected static void ErrorWhenTrue(bool condition, int code = 400, string message = "")
|
protected static void ErrorWhenTrue([DoesNotReturnIf(true)] bool condition, int code = 400, string message = "")
|
||||||
{
|
{
|
||||||
ErrorWhen(condition, code, message);
|
ErrorWhen(condition, code, message);
|
||||||
}
|
}
|
||||||
@@ -80,7 +81,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// <param name="condition"></param>
|
/// <param name="condition"></param>
|
||||||
/// <param name="code"></param>
|
/// <param name="code"></param>
|
||||||
/// <param name="message"></param>
|
/// <param name="message"></param>
|
||||||
protected static void ErrorWhenFalse(bool condition, int code = 400, string message = "")
|
protected static void ErrorWhenFalse([DoesNotReturnIf(false)] bool condition, int code = 400, string message = "")
|
||||||
{
|
{
|
||||||
ErrorWhen(!condition, code, message);
|
ErrorWhen(!condition, code, message);
|
||||||
}
|
}
|
||||||
@@ -91,7 +92,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// <param name="condition">检测条件</param>
|
/// <param name="condition">检测条件</param>
|
||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述</param>
|
/// <param name="message">错误描述</param>
|
||||||
protected static void ErrorWhenNull(object? condition, int code = 404, string message = "请求的资源不存在")
|
protected static void ErrorWhenNull([NotNull] object? condition, int code = 404, string message = "请求的资源不存在")
|
||||||
{
|
{
|
||||||
ErrorWhen(condition == null, code, message);
|
ErrorWhen(condition == null, code, message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,18 @@ public class SimApiAuth(IDistributedCache cache)
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新登陆信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="loginItem"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string Update(SimApiLoginItem loginItem, string token)
|
||||||
|
{
|
||||||
|
cache.SetString(token, JsonSerializer.Serialize(loginItem));
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取登陆信息
|
/// 获取登陆信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
+2
-1
@@ -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>
|
||||||
@@ -31,6 +31,7 @@
|
|||||||
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
|
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="8.1.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="8.1.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="8.1.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="8.1.1" />
|
||||||
|
<PackageReference Include="System.Text.Json" Version="9.0.10" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<MonoDevelop>
|
<MonoDevelop>
|
||||||
|
|||||||
@@ -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;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,9 +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)
|
||||||
{
|
{
|
||||||
// 检查并发情况下的线程安全问题,使用锁保证字典操作的原子性
|
// 移除锁语句
|
||||||
lock (ResponseCompletionSources)
|
|
||||||
{
|
|
||||||
string paramJson;
|
string paramJson;
|
||||||
if (param is string strParam)
|
if (param is string strParam)
|
||||||
{
|
{
|
||||||
@@ -59,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)
|
||||||
@@ -102,5 +99,4 @@ public partial class Synapse
|
|||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user