Skip to content

Commit

Permalink
Clumsily handle progress updates in (mostly) real time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koi-3088 committed Jun 4, 2022
1 parent 0b6ae8c commit 531e36b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Etumrep.Server/Etumrep.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.Z3" Version="4.8.14" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NLog" Version="4.7.13" />
<PackageReference Include="PKHeX.Core" Version="22.5.8" />
<PackageReference Include="PKHeX.Core" Version="22.6.1" />
</ItemGroup>

<ItemGroup>
Expand Down
56 changes: 42 additions & 14 deletions Etumrep.Server/ServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class ServerConnection
private bool IsStopped { get; set; }
private List<string> HostWhitelist { get; }
private List<string> UserBlacklist { get; }
private string CurrentSeedChecker { get; set; } = "Waiting for users...";
private int Progress { get; set; }
private bool InQueue { get; set; }
private static IProgress<(string, int, bool)> UserProgress { get; set; } = default!;
private ConcurrentQueue<RemoteUser> UserQueue { get; set; } = new();

Expand All @@ -26,7 +29,7 @@ public ServerConnection(HostSettings settings, IProgress<(string, int, bool)> pr
HostWhitelist = settings.HostWhitelist;
UserBlacklist = settings.UserBlacklist;
UserProgress = progress;
Listener = new(IPAddress.Loopback, Port);
Listener = new(IPAddress.Any, Port);
}

internal class RemoteUser
Expand Down Expand Up @@ -60,14 +63,22 @@ internal class UserAuth

public async Task Stop()
{
while (!IsStopped)
if (!IsStopped)
{
LogUtil.Log("Stopping the TCP Listener...", "[TCP Listener]");
Listener.Stop();
IsStopped = true;
await Task.Delay(0_050).ConfigureAwait(false);
}
}

public async Task MainAsync(CancellationToken token)
{
Listener.Start(100);
IsStopped = false;

_ = Task.Run(async () => await RemoteUserQueue(token).ConfigureAwait(false), token);
_ = Task.Run(async () => await ReportUserProgress(token).ConfigureAwait(false), token);
LogUtil.Log("Server initialized, waiting for connections...", "[TCP Listener]");

while (!token.IsCancellationRequested)
Expand Down Expand Up @@ -99,21 +110,18 @@ public async Task MainAsync(CancellationToken token)
LogUtil.Log($"{user.Name} was successfully authenticated, enqueueing...", "[TCP Listener]");
UserQueue.Enqueue(user);
}

Listener.Stop();
IsStopped = true;
}

private async Task RemoteUserQueue(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
var ready = UserQueue.TryDequeue(out var user);
if (ready && user is not null)
UserQueue.TryDequeue(out var user);
if (user is not null)
{
var c = UserQueue.Count;
var waiting = $" | {c} {(c is 1 ? "user" : "users")} waiting.";
RemoteUser.Report(user.SeedCheckerName + waiting, 10, true);
CurrentSeedChecker = user.SeedCheckerName;
Progress = 10;
InQueue = true;

LogUtil.Log($"{user.Name}: Attempting to read PKM data from {user.SeedCheckerName}.", "[UserQueue]");
if (!user.Stream.CanRead)
Expand All @@ -133,16 +141,16 @@ private async Task RemoteUserQueue(CancellationToken token)
continue;
}

RemoteUser.Report(user.SeedCheckerName + waiting, 30, true);
Progress = 30;
LogUtil.Log($"{user.Name}: Beginning seed calculation for {user.SeedCheckerName}...", "[UserQueue]");

var sw = new Stopwatch();
sw.Start();
var seed = EtumrepUtil.CalculateSeed(user.Buffer, count);
sw.Stop();

Progress = 80;
LogUtil.Log($"{user.Name}: Seed ({seed}) calculation for {user.SeedCheckerName} complete ({sw.Elapsed}). Attempting to send the result...", "[UserQueue]");
RemoteUser.Report(user.SeedCheckerName + waiting, 80, true);

if (!user.Stream.CanWrite)
{
Expand All @@ -154,12 +162,17 @@ private async Task RemoteUserQueue(CancellationToken token)
var bytes = BitConverter.GetBytes(seed);
await user.Stream.WriteAsync(bytes, token).ConfigureAwait(false);

RemoteUser.Report(user.SeedCheckerName + waiting, 100, true);
LogUtil.Log($"{user.Name}: Sent results to {user.Name}, removing from queue.", "[UserQueue]");

DisposeStream(user);
Progress = 100;
await Task.Delay(0_250, token).ConfigureAwait(false);
}
else
{
InQueue = false;
await Task.Delay(0_250, token).ConfigureAwait(false);
}
else await Task.Delay(0_250, token).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -226,6 +239,21 @@ private static async Task<RemoteUser> AuthenticateConnection(TcpClient client)
return authObj;
}

private async Task ReportUserProgress(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
await Task.Delay(0_100, token).ConfigureAwait(false);
if (InQueue)
{
var c = UserQueue.Count;
var msg = $"{CurrentSeedChecker} | {c} {(c is 1 ? "user" : "users")} waiting.";
RemoteUser.Report(msg, Progress, true);
}
else CurrentSeedChecker = "Waiting for users...";
}
}

private static void DisposeStream(RemoteUser user)
{
user.Client.Dispose();
Expand Down
9 changes: 5 additions & 4 deletions Etumrep.WinForms/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions Etumrep.WinForms/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ private void Stop()
{
SaveSettings();
Source.Cancel();
Source = new();
WasStarted = false;

async Task WaitUntilDone() { await Connection.Stop().ConfigureAwait(false); }
async Task WaitUntilDone()
{
await Connection.Stop().ConfigureAwait(false);
Source = new();
WasStarted = false;
}
Task.WhenAny(WaitUntilDone(), Task.Delay(1_000)).ConfigureAwait(true).GetAwaiter().GetResult();
LogUtil.Log("Server has been shut down.", "[Stop Button Event]");
}

private void PostLog(string message, string identity)
Expand Down

0 comments on commit 531e36b

Please sign in to comment.