Skip to content
Permalink
Browse files

Merge pull request #198 from longfin/bugfix/disconnect-turn

Fix disconnecting on TURN relay session
  • Loading branch information...
longfin committed Apr 15, 2019
2 parents d138388 + a84e576 commit 920c7ea8cb6b2f9bbdee63027d59fa63f7964b35
Showing with 41 additions and 12 deletions.
  1. +5 −0 CHANGES.md
  2. +17 −10 Libplanet/Blockchain/BlockChain.cs
  3. +1 −1 Libplanet/Net/NetworkStreamProxy.cs
  4. +18 −1 Libplanet/Net/Swarm.cs
@@ -24,6 +24,11 @@ To be released.
and its all implementations. [[#189]]
- The signature of `IStore.PutBlock<T>(Block<T>)` method was changed to
`PutBlock<T>(Block<T>, Address)`. [[#189]]
- Improved the read throughput of `BlockChain<T>.Append()`.
- Fixed a bug that `Swarm` had attempted to use TURN relay even if the `host` argument was
given.
- Fixed a bug that TURN relay had been disconnected when being connected for longer than 5
minutes.

[#185]: https://github.com/planetarium/libplanet/pull/185
[#187]: https://github.com/planetarium/libplanet/issues/187
@@ -227,10 +227,9 @@ bool IsPossiblyIn(BitArray addr, BitArray maskPattern)

public void Append(Block<T> block, DateTimeOffset currentTime)
{
_rwlock.EnterUpgradeableReadLock();
try
{
_rwlock.EnterWriteLock();

HashDigest<SHA256>? tip =
Store.IndexBlockHash(Id.ToString(), -1);

@@ -239,19 +238,27 @@ public void Append(Block<T> block, DateTimeOffset currentTime)
a => GetStates(new[] { a }, tip).GetValueOrDefault(a));
Validate(Enumerable.Append(this, block), currentTime);

Blocks[block.Hash] = block;
EvaluateActions(block);
_rwlock.EnterWriteLock();
try
{
Blocks[block.Hash] = block;
EvaluateActions(block);

Store.AppendIndex(Id.ToString(), block.Hash);
ISet<TxId> txIds = block.Transactions
.Select(t => t.Id)
.ToImmutableHashSet();
Store.AppendIndex(Id.ToString(), block.Hash);
ISet<TxId> txIds = block.Transactions
.Select(t => t.Id)
.ToImmutableHashSet();

Store.UnstageTransactionIds(txIds);
Store.UnstageTransactionIds(txIds);
}
finally
{
_rwlock.ExitWriteLock();
}
}
finally
{
_rwlock.ExitWriteLock();
_rwlock.ExitUpgradeableReadLock();
}
}

@@ -43,7 +43,7 @@ public void Dispose()

private async Task Proxy(NetworkStream source, NetworkStream target)
{
while (true)
while (source.CanRead && target.CanWrite)
{
var buf = new byte[_bufferSize];
int read = await source.ReadAsync(buf, 0, buf.Length);
@@ -33,6 +33,11 @@ public partial class Swarm : ICollection<Peer>, IDisposable
private static readonly TimeSpan TurnAllocationLifetime =
TimeSpan.FromSeconds(777);

// TURN Permission lifetime was defined in RFC 5766
// see also https://tools.ietf.org/html/rfc5766#section-8
private static readonly TimeSpan TurnPermissionLifetime =
TimeSpan.FromMinutes(5);

private readonly IDictionary<Peer, DateTimeOffset> _peers;
private readonly IDictionary<Peer, DateTimeOffset> _removedPeers;

@@ -436,7 +441,7 @@ IEnumerator IEnumerable.GetEnumerator()
throw new SwarmException("Swarm is already running.");
}

if (_iceServers != null)
if (_host is null && !(_iceServers is null))
{
_turnClient = await IceServer.CreateTurnClient(_iceServers);
}
@@ -494,6 +499,7 @@ IEnumerator IEnumerable.GetEnumerator()
{
tasks.Add(BindingProxies());
tasks.Add(RefreshAllocate());
tasks.Add(RefreshPermissions());
}

await Task.WhenAll(tasks);
@@ -801,6 +807,17 @@ private async Task RefreshAllocate()
}
}

private async Task RefreshPermissions()
{
TimeSpan lifetime = TurnPermissionLifetime;
while (Running)
{
await Task.Delay(lifetime - TimeSpan.FromMinutes(1));
await Task.WhenAll(
_peers.Keys.Select(CreatePermission));
}
}

private async Task ReceiveMessageAsync<T>(
BlockChain<T> blockChain, CancellationToken cancellationToken)
where T : IAction, new()

0 comments on commit 920c7ea

Please sign in to comment.
You can’t perform that action at this time.