2023-11-17 17:12:41 +01:00
|
|
|
|
using Insight.Domain.Enums;
|
|
|
|
|
|
using Insight.Domain.Interfaces;
|
|
|
|
|
|
using Insight.Domain.Network;
|
|
|
|
|
|
using Insight.Domain.Network.Remote.Messages;
|
|
|
|
|
|
using Vaitr.Bus;
|
|
|
|
|
|
using Vaitr.Network;
|
|
|
|
|
|
using static Insight.Web.Messages.RemoteMessages;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Insight.Web.Network.Remote;
|
|
|
|
|
|
|
2023-12-18 16:31:00 +01:00
|
|
|
|
public class RemoteSession(
|
|
|
|
|
|
Bus bus,
|
|
|
|
|
|
IEnumerable<IMessageHandler<RemoteSession>> handlers,
|
|
|
|
|
|
ISerializer<IMessage> serializer,
|
|
|
|
|
|
ILogger<RemoteSession> logger) : TcpSession<IMessage>(serializer, logger)
|
2023-11-17 17:12:41 +01:00
|
|
|
|
{
|
2023-12-18 16:31:00 +01:00
|
|
|
|
public string Id { get; } = GenerateRandomId();
|
2023-11-17 17:12:41 +01:00
|
|
|
|
public RemoteControlMode Mode { get; set; }
|
|
|
|
|
|
|
2023-12-18 16:31:00 +01:00
|
|
|
|
private readonly Bus _bus = bus;
|
|
|
|
|
|
private readonly IEnumerable<IMessageHandler<RemoteSession>> _handlers = handlers;
|
2023-11-17 17:12:41 +01:00
|
|
|
|
|
|
|
|
|
|
protected override async ValueTask OnConnectedAsync(CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("Remote ({ep?}) connected", RemoteEndPoint);
|
|
|
|
|
|
|
|
|
|
|
|
await _bus.PublishAsync(new RemoteDisconnected(this), default);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async ValueTask OnDisconnectedAsync(CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("Remote ({ep?}) disconnected", RemoteEndPoint);
|
|
|
|
|
|
|
|
|
|
|
|
await _bus.PublishAsync(new RemoteDisconnected(this), default);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 16:31:00 +01:00
|
|
|
|
protected override ValueTask OnSentAsync(IPacketContext<IMessage> context, CancellationToken cancellationToken)
|
2023-11-17 17:12:41 +01:00
|
|
|
|
{
|
|
|
|
|
|
//await base.OnSentAsync(context, cancellationToken);
|
2023-12-18 16:31:00 +01:00
|
|
|
|
return default;
|
2023-11-17 17:12:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async ValueTask OnReceivedAsync(IPacketContext<IMessage> context, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
//await base.OnReceivedAsync(context, cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var handler in _handlers)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await handler.HandleAsync(this, context.Packet, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning("Remote ({ep?}) {ex}", RemoteEndPoint, ex.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 16:31:00 +01:00
|
|
|
|
protected override ValueTask OnHeartbeatAsync(CancellationToken cancellationToken)
|
2023-11-17 17:12:41 +01:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("Remote ({ep?}) Heartbeat", RemoteEndPoint);
|
2023-12-18 16:31:00 +01:00
|
|
|
|
return default;
|
2023-11-17 17:12:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task ScreenDataAckAsync(CastScreen screenData, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync(new CastScreenReceived(screenData), cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string GenerateRandomId()
|
|
|
|
|
|
{
|
|
|
|
|
|
var random = new Random();
|
|
|
|
|
|
string? sessionId = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 3; i++) sessionId += random.Next(0, 999).ToString().PadLeft(3, '0');
|
|
|
|
|
|
return sessionId;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|