yavsc/src/Yavsc.Server/Services/LiveProcessor.cs

202 lines
9.4 KiB
C#
Raw Normal View History

2019-06-27 10:30:28 +01:00
using System.Collections.Concurrent;
using System.Net.WebSockets;
using Yavsc.Models;
using Yavsc.ViewModels.Streaming;
2019-06-28 00:49:20 +01:00
using Newtonsoft.Json;
2025-02-15 12:45:27 +00:00
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
2019-06-27 10:30:28 +01:00
namespace Yavsc.Services
{
2020-09-12 01:11:30 +01:00
public class LiveProcessor : ILiveProcessor
{
private readonly ILogger _logger;
2019-06-27 10:30:28 +01:00
2020-09-12 01:11:30 +01:00
public ConcurrentDictionary<string, LiveCastHandler> Casters { get; } = new ConcurrentDictionary<string, LiveCastHandler>();
2019-06-27 10:30:28 +01:00
2021-03-10 01:41:31 +00:00
public LiveProcessor(ILoggerFactory loggerFactory)
2019-06-27 10:30:28 +01:00
{
_logger = loggerFactory.CreateLogger<LiveProcessor>();
}
2020-10-09 19:35:39 +01:00
public async Task<bool> AcceptStream(HttpContext context, ApplicationUser user, string destDir, string fileName)
2019-06-27 10:30:28 +01:00
{
// TODO defer request handling
2020-10-09 19:35:39 +01:00
string uname = user.UserName;
LiveCastHandler liveHandler = null;
2019-06-27 10:30:28 +01:00
if (Casters.ContainsKey(uname))
{
2019-06-28 00:49:20 +01:00
_logger.LogWarning($"Casters.ContainsKey({uname})");
liveHandler = Casters[uname];
2020-09-12 01:11:30 +01:00
if (liveHandler.Socket.State == WebSocketState.Open || liveHandler.Socket.State == WebSocketState.Connecting)
2019-06-27 10:30:28 +01:00
{
2020-09-12 01:11:30 +01:00
_logger.LogWarning($"Closing cx");
2019-06-27 10:30:28 +01:00
// FIXME loosed connexion should be detected & disposed else where
2020-09-12 01:11:30 +01:00
await liveHandler.Socket.CloseAsync(WebSocketCloseStatus.EndpointUnavailable, "one by user", CancellationToken.None);
2019-06-27 10:30:28 +01:00
}
2020-09-12 01:11:30 +01:00
if (!liveHandler.TokenSource.IsCancellationRequested)
{
liveHandler.TokenSource.Cancel();
2019-06-27 10:30:28 +01:00
}
liveHandler.Socket.Dispose();
liveHandler.Socket = await context.WebSockets.AcceptWebSocketAsync();
liveHandler.TokenSource = new CancellationTokenSource();
2019-06-27 10:30:28 +01:00
}
else
{
2019-06-28 00:49:20 +01:00
_logger.LogInformation($"new caster");
2019-06-27 10:30:28 +01:00
// Accept the socket
liveHandler = new LiveCastHandler { Socket = await context.WebSockets.AcceptWebSocketAsync() };
2019-06-27 10:30:28 +01:00
}
_logger.LogInformation("Accepted web socket");
// Dispatch the flow
2020-09-12 01:11:30 +01:00
2019-06-27 10:30:28 +01:00
try
{
if (liveHandler.Socket != null && liveHandler.Socket.State == WebSocketState.Open)
2019-06-27 10:30:28 +01:00
{
Casters[uname] = liveHandler;
2019-06-27 10:30:28 +01:00
// TODO: Handle the socket here.
// Find receivers: others in the chat room
// send them the flow
2026-05-30 19:34:22 +01:00
var buffer = new byte[YavscConstants.WebSocketsMaxBufLen];
2019-06-27 10:30:28 +01:00
var sBuffer = new ArraySegment<byte>(buffer);
_logger.LogInformation("Receiving bytes...");
WebSocketReceiveResult received = await liveHandler.Socket.ReceiveAsync(sBuffer, liveHandler.TokenSource.Token);
2020-09-12 01:11:30 +01:00
_logger.LogInformation($"Received bytes : {received.Count}");
2019-06-27 10:30:28 +01:00
_logger.LogInformation($"Is the end : {received.EndOfMessage}");
2020-10-09 19:35:39 +01:00
2020-09-12 01:11:30 +01:00
2020-10-09 19:35:39 +01:00
2019-06-28 00:49:20 +01:00
var fsInputQueue = new Queue<ArraySegment<byte>>();
2020-09-12 01:11:30 +01:00
bool endOfInput = false;
2020-10-18 23:00:17 +01:00
sBuffer = new ArraySegment<byte>(buffer,0,received.Count);
fsInputQueue.Enqueue(sBuffer);
2020-10-09 19:35:39 +01:00
var taskWritingToFs = liveHandler.ReceiveUserFile(user, _logger, destDir, fsInputQueue, fileName, () => endOfInput);
2019-06-27 10:30:28 +01:00
Stack<string> ToClose = new Stack<string>();
try
{
2020-09-12 01:11:30 +01:00
do
{
_logger.LogInformation($"Echoing {received.Count} bytes received in a {received.MessageType} message; Fin={received.EndOfMessage}");
// Echo anything we receive
// and send to all listner found
2020-09-12 01:11:30 +01:00
_logger.LogInformation($"{liveHandler.Listeners.Count} listeners");
foreach (var cliItem in liveHandler.Listeners)
2019-06-27 10:30:28 +01:00
{
var listenningSocket = cliItem.Value;
2020-09-12 01:11:30 +01:00
if (listenningSocket.State == WebSocketState.Open)
{
_logger.LogInformation(cliItem.Key);
2019-06-27 10:30:28 +01:00
await listenningSocket.SendAsync(
sBuffer, received.MessageType, received.EndOfMessage, liveHandler.TokenSource.Token);
2019-06-27 10:30:28 +01:00
}
else if (listenningSocket.State == WebSocketState.CloseReceived || listenningSocket.State == WebSocketState.CloseSent)
2019-06-27 10:30:28 +01:00
{
ToClose.Push(cliItem.Key);
}
}
2020-09-12 01:11:30 +01:00
if (!received.CloseStatus.HasValue)
{
_logger.LogInformation("try and receive new bytes");
2026-05-30 19:34:22 +01:00
buffer = new byte[YavscConstants.WebSocketsMaxBufLen];
received = await liveHandler.Socket.ReceiveAsync(sBuffer, liveHandler.TokenSource.Token);
2020-09-12 01:11:30 +01:00
_logger.LogInformation($"Received bytes : {received.Count}");
2020-10-18 23:00:17 +01:00
sBuffer = new ArraySegment<byte>(buffer,0,received.Count);
_logger.LogInformation($"segment : offset: {sBuffer.Offset} count: {sBuffer.Count}");
2019-06-27 10:30:28 +01:00
_logger.LogInformation($"Is the end : {received.EndOfMessage}");
2020-10-18 23:00:17 +01:00
2020-09-12 01:11:30 +01:00
if (received.CloseStatus.HasValue)
{
endOfInput=true;
_logger.LogInformation($"received a close status: {received.CloseStatus.Value}: {received.CloseStatusDescription}");
}
2020-10-18 23:00:17 +01:00
else fsInputQueue.Enqueue(sBuffer);
}
2020-09-12 01:11:30 +01:00
else endOfInput=true;
while (ToClose.Count > 0)
2019-06-27 10:30:28 +01:00
{
string no = ToClose.Pop();
_logger.LogInformation("Closing follower connection");
WebSocket listenningSocket;
2020-09-12 01:11:30 +01:00
if (liveHandler.Listeners.TryRemove(no, out listenningSocket))
{
2019-06-28 00:49:20 +01:00
await listenningSocket.CloseAsync(WebSocketCloseStatus.EndpointUnavailable,
"State != WebSocketState.Open", CancellationToken.None);
listenningSocket.Dispose();
}
2019-06-27 10:30:28 +01:00
}
}
2020-09-12 01:11:30 +01:00
while (liveHandler.Socket.State == WebSocketState.Open);
2019-06-27 10:30:28 +01:00
_logger.LogInformation("Closing connection");
2019-06-28 00:49:20 +01:00
taskWritingToFs.Wait();
2020-09-12 01:11:30 +01:00
await liveHandler.Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, received.CloseStatusDescription, liveHandler.TokenSource.Token);
liveHandler.TokenSource.Cancel();
liveHandler.Dispose();
2020-09-12 01:11:30 +01:00
_logger.LogInformation("Resulting file : " + JsonConvert.SerializeObject(taskWritingToFs.Result));
2019-06-27 10:30:28 +01:00
}
catch (Exception ex)
{
_logger.LogError($"Exception occured : {ex.Message}");
_logger.LogError(ex.StackTrace);
liveHandler.TokenSource.Cancel();
throw;
2019-06-27 10:30:28 +01:00
}
2019-06-28 00:49:20 +01:00
taskWritingToFs.Dispose();
2019-06-27 10:30:28 +01:00
}
else
2019-06-28 00:49:20 +01:00
{
// Socket was not accepted open ...
2020-09-12 01:11:30 +01:00
// not (meta.Socket != null && meta.Socket.State == WebSocketState.Open)
if (liveHandler.Socket != null)
2019-06-27 10:30:28 +01:00
{
2020-09-12 01:11:30 +01:00
_logger.LogError($"meta.Socket.State not Open: {liveHandler.Socket.State} ");
liveHandler.Socket.Dispose();
2019-06-27 10:30:28 +01:00
}
else
_logger.LogError("socket object is null");
}
2020-09-12 01:11:30 +01:00
2019-06-28 00:49:20 +01:00
RemoveLiveInfo(uname);
2019-06-27 10:30:28 +01:00
}
catch (IOException ex)
{
if (ex.Message == "Unexpected end of stream")
{
_logger.LogError($"Unexpected end of stream");
}
else
{
_logger.LogError($"Really unexpected end of stream");
await liveHandler.Socket?.CloseAsync(WebSocketCloseStatus.EndpointUnavailable, ex.Message, CancellationToken.None);
}
liveHandler.Socket?.Dispose();
2019-06-28 00:49:20 +01:00
RemoveLiveInfo(uname);
2019-06-27 10:30:28 +01:00
}
return true;
}
2019-06-28 00:49:20 +01:00
void RemoveLiveInfo(string userName)
{
LiveCastHandler caster;
2020-09-12 01:11:30 +01:00
if (Casters.TryRemove(userName, out caster))
2019-06-28 00:49:20 +01:00
_logger.LogInformation("removed live info");
2020-09-12 01:11:30 +01:00
else
2019-06-28 00:49:20 +01:00
_logger.LogError("could not remove live info");
}
2019-06-27 10:30:28 +01:00
}
2020-09-12 01:11:30 +01:00
}