more uploads fixes

This commit is contained in:
Stanley Dimant
2022-07-01 13:47:30 +02:00
parent 35b12720c6
commit fa9be47d1d
4 changed files with 30 additions and 20 deletions

View File

@@ -53,19 +53,22 @@ public class DownloadUi : Window, IDisposable
if (_apiController.CurrentUploads.Any())
{
var currentUploads = _apiController.CurrentUploads.ToList();
var doneUploads = currentUploads.Count(c => c.IsTransferred);
var totalUploads = currentUploads.Count;
var doneUploads = currentUploads.Count(c => c.IsTransferred);
var totalUploaded = currentUploads.Sum(c => c.Transferred);
var totalToUpload = currentUploads.Sum(c => c.Total);
UiShared.DrawOutlinedFont(drawList, "▲",
new Vector2(basePosition.X + 0, basePosition.Y + (int)(yDistance * 0.5)),
UiShared.Color(255, 255, 255, 255), UiShared.Color(0, 0, 0, 255), 2);
UiShared.DrawOutlinedFont(drawList, $"Uploading {doneUploads}/{totalUploads}",
UiShared.DrawOutlinedFont(drawList, $"Compressing+Uploading {doneUploads}/{totalUploads}",
new Vector2(basePosition.X + xDistance, basePosition.Y + yDistance * 0),
UiShared.Color(255, 255, 255, 255), UiShared.Color(0, 0, 0, 255), 2);
UiShared.DrawOutlinedFont(drawList, $"{UiShared.ByteToString(totalUploaded)}/{UiShared.ByteToString(totalToUpload)}",
new Vector2(basePosition.X + xDistance, basePosition.Y + yDistance * 1),
UiShared.Color(255, 255, 255, 255), UiShared.Color(0, 0, 0, 255), 2);
}
if (_apiController.CurrentDownloads.Any())

View File

@@ -3,10 +3,9 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Dalamud.Logging;
using LZ4;
using MareSynchronos.API;
using MareSynchronos.FileCacheDB;
@@ -162,6 +161,7 @@ namespace MareSynchronos.WebAPI
}
}
var totalSize = CurrentUploads.Sum(c => c.Total);
Logger.Verbose("Compressing and uploading files");
foreach (var file in CurrentUploads.Where(f => f.CanBeTransferred && !f.IsTransferred))
{
@@ -175,6 +175,12 @@ namespace MareSynchronos.WebAPI
break;
}
if (CurrentUploads.Any())
{
var compressedSize = CurrentUploads.Sum(c => c.Total);
Logger.Debug($"Compressed {totalSize} to {compressedSize} ({(compressedSize / (double)totalSize):P2})");
}
Logger.Verbose("Upload tasks complete, waiting for server to confirm");
var anyUploadsOpen = await _fileHub!.InvokeAsync<bool>("IsUploadFinished", uploadToken);
Logger.Verbose("Uploads open: " + anyUploadsOpen);
@@ -213,21 +219,21 @@ namespace MareSynchronos.WebAPI
{
if (uploadToken.IsCancellationRequested) return;
async IAsyncEnumerable<byte[]> AsyncFileData()
async IAsyncEnumerable<byte[]> AsyncFileData([EnumeratorCancellation] CancellationToken token)
{
var chunkSize = 1024 * 512; // 512kb
using var ms = new MemoryStream(compressedFile);
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = await ms.ReadAsync(buffer, 0, chunkSize, uploadToken)) > 0)
while ((bytesRead = await ms.ReadAsync(buffer, 0, chunkSize, token)) > 0 && !token.IsCancellationRequested)
{
CurrentUploads.Single(f => f.Hash == fileHash).Transferred += bytesRead;
uploadToken.ThrowIfCancellationRequested();
token.ThrowIfCancellationRequested();
yield return bytesRead == chunkSize ? buffer.ToArray() : buffer.Take(bytesRead).ToArray();
}
}
await _fileHub!.SendAsync("UploadFileStreamAsync", fileHash, AsyncFileData(), uploadToken);
await _fileHub!.SendAsync("UploadFileStreamAsync", fileHash, AsyncFileData(uploadToken), uploadToken);
}
}

View File

@@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MareSynchronos.API;
using MareSynchronos.FileCacheDB;
using MareSynchronos.Utils;
using MareSynchronos.WebAPI.Utils;
using Microsoft.AspNetCore.SignalR.Client;
namespace MareSynchronos.WebAPI

View File

@@ -130,6 +130,7 @@ namespace MareSynchronos.WebAPI
try
{
if (_dalamudUtil.PlayerCharacter == null) throw new ArgumentException("Player not initialized");
Logger.Debug("Building connection");
_heartbeatHub = BuildHubConnection("heartbeat");
_userHub = BuildHubConnection("user");
@@ -184,7 +185,7 @@ namespace MareSynchronos.WebAPI
catch (Exception ex)
{
Logger.Warn(ex.Message);
Logger.Warn(ex.StackTrace);
Logger.Warn(ex.StackTrace ?? string.Empty);
Logger.Debug("Failed to establish connection, retrying");
await Task.Delay(TimeSpan.FromSeconds(5), token);
}
@@ -227,6 +228,9 @@ namespace MareSynchronos.WebAPI
private Task HeartbeatHubOnClosed(Exception? arg)
{
CurrentUploads.Clear();
CurrentDownloads.Clear();
_uploadCancellationTokenSource?.Cancel();
Logger.Debug("Connection closed");
Disconnected?.Invoke(null, EventArgs.Empty);
return Task.CompletedTask;
@@ -243,14 +247,17 @@ namespace MareSynchronos.WebAPI
private Task HeartbeatHubOnReconnecting(Exception? arg)
{
Logger.Debug("Connection closed... Reconnecting…");
CurrentUploads.Clear();
CurrentDownloads.Clear();
_uploadCancellationTokenSource?.Cancel();
Logger.Debug("Connection closed... Reconnecting");
Disconnected?.Invoke(null, EventArgs.Empty);
return Task.CompletedTask;
}
private async Task StopAllConnections(CancellationToken token)
{
if (_heartbeatHub is { State: HubConnectionState.Connected or HubConnectionState.Connecting or HubConnectionState.Reconnecting })
if (_heartbeatHub is not null)
{
await _heartbeatHub.StopAsync(token);
_heartbeatHub.Closed -= HeartbeatHubOnClosed;
@@ -260,21 +267,21 @@ namespace MareSynchronos.WebAPI
_heartbeatHub = null;
}
if (_fileHub is { State: HubConnectionState.Connected or HubConnectionState.Connecting or HubConnectionState.Reconnecting })
if (_fileHub is not null)
{
await _fileHub.StopAsync(token);
await _fileHub.DisposeAsync();
_fileHub = null;
}
if (_userHub is { State: HubConnectionState.Connected or HubConnectionState.Connecting or HubConnectionState.Reconnecting })
if (_userHub is not null)
{
await _userHub.StopAsync(token);
await _userHub.DisposeAsync();
_userHub = null;
}
if (_adminHub is { State: HubConnectionState.Connected or HubConnectionState.Connecting or HubConnectionState.Reconnecting })
if (_adminHub is not null)
{
await _adminHub.StopAsync(token);
await _adminHub.DisposeAsync();