Skip to content

Commit

Permalink
Merge pull request #1424 from longfin/bugfix/turn-reconnection
Browse files Browse the repository at this point in the history
Release 0.13.2 & Fix TURN reconnection
  • Loading branch information
longfin authored Aug 5, 2021
2 parents 5d631fd + ef47369 commit dd22711
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
9 changes: 6 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ Libplanet changelog
Version 0.13.2
--------------

To be released.
Released on Aug 5, 2021.

- When a reorg happens, `Swarm<T>` now broadcasts a reorged chain tip first
before rendering. [[#1385], [#1415]]
- When a reorg happens, `Swarm<T>` now broadcasts a reorged chain tip first
before rendering. [[#1385], [#1415]]
- Fixed a bug where `TurnClient` hadn't been recovered when TURN connection
had been disconnected. [[#1424]]

[#1385]: https://github.com/planetarium/libplanet/issues/1385
[#1415]: https://github.com/planetarium/libplanet/pull/1415
[#1424]: https://github.com/planetarium/libplanet/pull/1424


Version 0.13.1
Expand Down
42 changes: 24 additions & 18 deletions Libplanet.Stun/Stun/TurnClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public TurnClient(
public async Task InitializeTurnAsync(CancellationToken cancellationToken)
{
_control = new TcpClient();
#pragma warning disable PC001 // API not supported on all platforms
_control.Connect(_host, _port);
#pragma warning restore PC001 // API not supported on all platforms
await _control.ConnectAsync(_host, _port);
_processMessage = ProcessMessage(_turnTaskCts.Token);

BehindNAT = await IsBehindNAT(cancellationToken);
Expand Down Expand Up @@ -119,7 +117,19 @@ public async Task ReconnectTurn(int listenPort, CancellationToken cancellationTo
ClearSession();
_turnTaskCts = new CancellationTokenSource();

await InitializeTurnAsync(cancellationToken);
try
{
await InitializeTurnAsync(cancellationToken);
}
catch (Exception e)
{
_logger.Error(
e,
"Failed to initialize due to error ({Exception}); retry...",
e
);
await Task.Delay(1000, cancellationToken);
}
}
}
}
Expand Down Expand Up @@ -384,24 +394,14 @@ await stream.WriteAsync(

private async Task ProcessMessage(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested || _control.Connected)
while (!cancellationToken.IsCancellationRequested && _control.Connected)
{
NetworkStream stream = _control.GetStream();

try
{
NetworkStream stream = _control.GetStream();
StunMessage message;
try
{
message = await StunMessage.ParseAsync(stream, cancellationToken);
_logger.Debug("Stun Message is: {message}", message);
}
catch (TurnClientException e)
{
_logger.Error(e, "Failed to parse StunMessage. {e}", e);
ClearResponses();
break;
}
message = await StunMessage.ParseAsync(stream, cancellationToken);
_logger.Debug("Parsed " + nameof(StunMessage) + ": {Message}", message);

if (message is ConnectionAttempt attempt)
{
Expand All @@ -415,6 +415,12 @@ private async Task ProcessMessage(CancellationToken cancellationToken)
tcs.TrySetResult(message);
}
}
catch (TurnClientException e)
{
_logger.Error(e, "Failed to parse " + nameof(StunMessage) + ": {Exception}", e);
ClearResponses();
break;
}
catch (Exception e)
{
_logger.Error(
Expand Down

0 comments on commit dd22711

Please sign in to comment.