Skip to content

Commit

Permalink
fix: improve disconnect/reconnect resiliency
Browse files Browse the repository at this point in the history
  • Loading branch information
rdavisau committed Jun 8, 2022
1 parent 239c82f commit 3254a01
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
19 changes: 7 additions & 12 deletions src/components/tbc.core/Socket/SocketServer.cs
Expand Up @@ -73,7 +73,7 @@ public async Task Run(CancellationToken ct = default)
.ContinueWith(t =>
#pragma warning restore CS4014
{
_log("Request loop terminated:");
_log("Request loop terminated: ");
_log(t.Exception?.ToString() ?? "no exception");
_finished = true;
});
Expand All @@ -87,20 +87,15 @@ private async Task RunRequestLoop(CancellationToken ct = default)
_log($"receive result: {receiveResult}");

if (receiveResult.Outcome == ReceiveResultOutcome.Disconnect)
await Terminate();
}
}

private async Task Terminate()
{
if (_onDisconnect is { } od)
await od.Invoke();
{
if (_onDisconnect is { } od)
await od.Invoke();

throw new FinishedException();
break;
}
}
}

public class FinishedException : Exception {}

private async Task<ReceiveResult> ReceiveAndHandle(CancellationToken ct = default)
{
var receiveResult = await Receive(ct);
Expand Down
5 changes: 4 additions & 1 deletion src/components/tbc.host/Extensions/HelpfulExtensions.cs
Expand Up @@ -17,7 +17,10 @@ public static class HelpfulExtensions
Task.Run(async () =>
{
while (true)
await @do();
{
try { await @do(); }
catch (Exception ex) { Console.WriteLine(ex); }
}
});

public static JsonSerializerSettings NaiveCloneSerialiserSettings = new JsonSerializerSettings
Expand Down
2 changes: 1 addition & 1 deletion src/components/tbc.target/TargetServer.cs
Expand Up @@ -29,7 +29,6 @@ public async Task Run(IReloadManager reloadManager, Action<string> log = null)
log ??= Console.WriteLine;

var listener = new TcpListener(IPAddress.Any, Configuration.ListenPort);
var handler = new AssemblyLoaderService(Configuration, reloadManager, log);

listener.Start();

Expand All @@ -41,6 +40,7 @@ public async Task Run(IReloadManager reloadManager, Action<string> log = null)
try
{
var handler = new AssemblyLoaderService(Configuration, reloadManager, log);
var socketServer = new SocketServer<ITbcProtocol>(connection, handler, "client", log);
await socketServer.Run();
}
Expand Down

0 comments on commit 3254a01

Please sign in to comment.