Skip to content
Permalink
Browse files

Ensure mutex for EnsureConnection

  • Loading branch information...
longfin committed Jul 29, 2019
1 parent 21fef6a commit 2a127c15a99d83d10e01782ac55348e4e5122891
Showing with 8 additions and 3 deletions.
  1. +8 −3 Libplanet.Stun/Stun/TurnClient.cs
@@ -26,6 +26,7 @@ public class TurnClient : IStunContext, IDisposable

private readonly AsyncProducerConsumerQueue<ConnectionAttempt> _connectionAttempts;
private readonly TcpClient _control;
private readonly AsyncLock _connMutex;

private Task _messageProcessor;

@@ -47,6 +48,7 @@ public class TurnClient : IStunContext, IDisposable
_responses =
new Dictionary<byte[], TaskCompletionSource<StunMessage>>(
new ByteArrayComparer());
_connMutex = new AsyncLock();
}

public string Username { get; }
@@ -263,10 +265,13 @@ private async Task ProcessMessage()

private async Task EnsureConnection()
{
if (!_control.Connected)
using (await _connMutex.LockAsync())
{
await _control.ConnectAsync(_host, _port);
_messageProcessor = ProcessMessage();
if (!_control.Connected)
{
await _control.ConnectAsync(_host, _port);
_messageProcessor = ProcessMessage();
}
}
}

0 comments on commit 2a127c1

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