2023-10-20 12:33:18 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.Json;
|
2024-07-26 05:03:28 +08:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-10-20 12:33:18 +08:00
|
|
|
using Microsoft.Extensions.Logging;
|
2024-07-26 05:03:28 +08:00
|
|
|
using MQTTnet;
|
2023-10-20 12:33:18 +08:00
|
|
|
using SimApi.Communications;
|
|
|
|
|
using SimApi.Helpers;
|
|
|
|
|
|
|
|
|
|
namespace SimApi;
|
|
|
|
|
|
|
|
|
|
public partial class Synapse
|
|
|
|
|
{
|
2024-07-26 05:20:27 +08:00
|
|
|
private Dictionary<string, TaskCompletionSource<string>> ResponseCompletionSources { get; } = new();
|
2023-10-20 12:33:18 +08:00
|
|
|
|
|
|
|
|
private void RunRpcClient()
|
|
|
|
|
{
|
2024-07-26 05:03:28 +08:00
|
|
|
var rcTopic = $"{Options.SysName}/{Options.AppName}/rpc/client/{Options.AppId}/";
|
|
|
|
|
var rcSubOpts = MqttFactory.CreateSubscribeOptionsBuilder()
|
|
|
|
|
.WithTopicFilter(o => o.WithTopic($"{rcTopic}+")).Build();
|
|
|
|
|
Client.ApplicationMessageReceivedAsync += e =>
|
2023-10-20 12:33:18 +08:00
|
|
|
{
|
2024-07-26 05:03:28 +08:00
|
|
|
if (!e.ApplicationMessage.Topic.StartsWith(rcTopic)) return Task.CompletedTask;
|
|
|
|
|
var reqBody = e.ApplicationMessage.ConvertPayloadToString();
|
|
|
|
|
var messageId = e.ApplicationMessage.Topic.Replace(rcTopic, string.Empty);
|
2024-07-26 05:20:27 +08:00
|
|
|
if (!ResponseCompletionSources.TryGetValue(messageId, out var tcs)) return Task.CompletedTask;
|
|
|
|
|
tcs.SetResult(reqBody);
|
|
|
|
|
ResponseCompletionSources.Remove(messageId);
|
2024-07-26 05:03:28 +08:00
|
|
|
return Task.CompletedTask;
|
2023-10-20 12:33:18 +08:00
|
|
|
};
|
2024-07-26 05:03:28 +08:00
|
|
|
Client.SubscribeAsync(rcSubOpts).Wait();
|
2023-10-20 12:33:18 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-30 03:45:15 +08:00
|
|
|
private string FireRpc(string app, string action, object param)
|
2023-10-20 12:33:18 +08:00
|
|
|
{
|
2023-10-23 17:39:27 +08:00
|
|
|
var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
2024-07-26 05:03:28 +08:00
|
|
|
var topic = $"{Options.SysName}/{app}/rpc/server/{action}";
|
|
|
|
|
var messageId = Guid.NewGuid().ToString();
|
2024-07-26 05:20:27 +08:00
|
|
|
var tcs = new TaskCompletionSource<string>();
|
|
|
|
|
ResponseCompletionSources.Add(messageId, tcs);
|
2024-07-26 05:03:28 +08:00
|
|
|
var message = new MqttApplicationMessageBuilder()
|
|
|
|
|
.WithTopic(topic)
|
|
|
|
|
.WithPayload(paramJson)
|
|
|
|
|
.WithResponseTopic($"{Options.AppName},{Options.AppId}")
|
|
|
|
|
.WithContentType(messageId)
|
|
|
|
|
.WithRetainFlag(false)
|
|
|
|
|
.Build();
|
|
|
|
|
if (!Client!.IsConnected) return null;
|
|
|
|
|
Client!.PublishAsync(message, CancellationToken.None).Wait();
|
|
|
|
|
logger.LogDebug(
|
2024-07-26 05:20:27 +08:00
|
|
|
"Synapse RPC Client Request: ({PropsMessageId}) {OptionsAppName} -> {Action}@{App}\n{ParamJson}", messageId,
|
|
|
|
|
Options.AppName, action, app, paramJson);
|
|
|
|
|
|
|
|
|
|
string response;
|
|
|
|
|
try
|
2023-10-20 12:33:18 +08:00
|
|
|
{
|
2024-07-26 05:20:27 +08:00
|
|
|
if (tcs.Task.Wait(Options.RpcTimeout * 1000))
|
|
|
|
|
{
|
|
|
|
|
response = tcs.Task.Result;
|
|
|
|
|
logger.LogDebug(
|
|
|
|
|
"Synapse RPC Client Response: ({BasicPropertiesCorrelationId}) {BasicPropertiesType}@{BasicPropertiesReplyTo} -> {OptionsAppName}\n{S}",
|
|
|
|
|
messageId, action, app, Options.AppName, response);
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-10-20 12:33:18 +08:00
|
|
|
{
|
2023-10-30 03:45:15 +08:00
|
|
|
response = JsonSerializer.Serialize(new SimApiBaseResponse(502, "timeout"), SimApiUtil.JsonOption);
|
2023-10-20 12:33:18 +08:00
|
|
|
}
|
2024-07-26 05:20:27 +08:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
response = JsonSerializer.Serialize(new SimApiBaseResponse(500, "Synapse RPC Client Error"),
|
|
|
|
|
SimApiUtil.JsonOption);
|
2023-10-20 12:33:18 +08:00
|
|
|
}
|
2024-07-26 05:03:28 +08:00
|
|
|
|
2023-10-20 12:33:18 +08:00
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|