mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 11:26:31 +00:00
7750025631
remove dummy dlls, remove resource loader from c#. TODO: Load dlls separately by Loader UI and loader client WIP
93 lines
3.0 KiB
C#
93 lines
3.0 KiB
C#
using System.Diagnostics;
|
|
using System.Text;
|
|
using Azaion.CommonSecurity.DTO;
|
|
using Azaion.CommonSecurity.DTO.Commands;
|
|
using MessagePack;
|
|
using NetMQ;
|
|
using NetMQ.Sockets;
|
|
using Serilog;
|
|
using Exception = System.Exception;
|
|
|
|
namespace Azaion.CommonSecurity.Services;
|
|
|
|
public class LoaderClient(LoaderClientConfig config, ILogger logger, CancellationToken ct = default)
|
|
{
|
|
private readonly DealerSocket _dealer = new();
|
|
private readonly Guid _clientId = Guid.NewGuid();
|
|
|
|
public void StartClient()
|
|
{
|
|
try
|
|
{
|
|
using var process = new Process();
|
|
process.StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = SecurityConstants.EXTERNAL_LOADER_PATH,
|
|
Arguments = $"--port {config.ZeroMqPort} --api {config.ApiUrl}",
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
process.OutputDataReceived += (_, e) =>
|
|
{
|
|
if (e.Data != null) Console.WriteLine(e.Data);
|
|
};
|
|
process.ErrorDataReceived += (_, e) =>
|
|
{
|
|
if (e.Data != null) Console.WriteLine(e.Data);
|
|
};
|
|
process.Start();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.Error(e.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void Connect()
|
|
{
|
|
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
|
|
_dealer.Connect($"tcp://{config.ZeroMqHost}:{config.ZeroMqPort}");
|
|
}
|
|
|
|
public void Login(ApiCredentials credentials)
|
|
{
|
|
var result = SendCommand(RemoteCommand.Create(CommandType.Login, credentials));
|
|
if (result.CommandType != CommandType.Ok)
|
|
throw new Exception(result.Message);
|
|
}
|
|
|
|
public MemoryStream LoadFile(string filename, string folder)
|
|
{
|
|
var result = SendCommand(RemoteCommand.Create(CommandType.Load, new LoadFileData(filename, folder)));
|
|
if (result.Data?.Length == 0)
|
|
throw new Exception($"Can't load {filename}. Returns 0 bytes");
|
|
return new MemoryStream(result.Data!);
|
|
}
|
|
|
|
private RemoteCommand SendCommand(RemoteCommand command, int retryCount = 15, int retryDelayMs = 400)
|
|
{
|
|
try
|
|
{
|
|
_dealer.SendFrame(MessagePackSerializer.Serialize(command));
|
|
|
|
var tryNum = 0;
|
|
while (!ct.IsCancellationRequested && tryNum++ < retryCount)
|
|
{
|
|
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(retryDelayMs), out var bytes))
|
|
continue;
|
|
var res = MessagePackSerializer.Deserialize<RemoteCommand>(bytes, cancellationToken: ct);
|
|
if (res.CommandType == CommandType.Error)
|
|
throw new Exception(res.Message);
|
|
return res;
|
|
}
|
|
|
|
throw new Exception($"Sent {command} {retryCount} times. No response from client.");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.Error(e, e.Message);
|
|
throw;
|
|
}
|
|
}
|
|
} |