Skip to content

Commit

Permalink
Merge pull request #378 from longfin/bugfix/377
Browse files Browse the repository at this point in the history
Fix #377
  • Loading branch information
longfin authored Aug 2, 2019
2 parents 6416996 + 6f4e42c commit 21fef6a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ To be released.
`Address`. [[#369]]
- Removed unnecessary mutex in `Swarm<T>` to avoid continuous delays in peer
registration in some situations. [[#375]]
- Fixed a bug that `TurnClient` had thrown `KeyNotFoundException` and
`IOException` on startup. [[#377], [#378]]

[#319]: https://github.com/planetarium/libplanet/issues/319
[#343]: https://github.com/planetarium/libplanet/pull/343
Expand All @@ -61,6 +63,8 @@ To be released.
[#367]: https://github.com/planetarium/libplanet/pull/367
[#369]: https://github.com/planetarium/libplanet/pull/369
[#375]: https://github.com/planetarium/libplanet/pull/375
[#377]: https://github.com/planetarium/libplanet/issues/377
[#378]: https://github.com/planetarium/libplanet/pull/378


Version 0.4.1
Expand Down
20 changes: 12 additions & 8 deletions Libplanet.Stun/Stun/TurnClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task<IPEndPoint> AllocateRequestAsync(
{
var request = new AllocateRequest((int)lifetime.TotalSeconds);
await SendMessageAsync(stream, request, cancellationToken);
response = await _responses[request.TransactionId].Task;
response = await ReceiveMessage(request.TransactionId);

if (response is AllocateErrorResponse allocError)
{
Expand Down Expand Up @@ -101,8 +101,7 @@ public async Task CreatePermissionAsync(
NetworkStream stream = _control.GetStream();
var request = new CreatePermissionRequest(peerAddress);
await SendMessageAsync(stream, request, cancellationToken);
StunMessage response =
await _responses[request.TransactionId].Task;
StunMessage response = await ReceiveMessage(request.TransactionId);

if (response is CreatePermissionErrorResponse)
{
Expand Down Expand Up @@ -154,8 +153,7 @@ public async Task<IPEndPoint> GetMappedAddressAsync(
NetworkStream stream = _control.GetStream();
var request = new BindingRequest();
await SendMessageAsync(stream, request, cancellationToken);
StunMessage response =
await _responses[request.TransactionId].Task;
StunMessage response = await ReceiveMessage(request.TransactionId);

if (response is BindingSuccessResponse success)
{
Expand All @@ -177,8 +175,7 @@ public async Task<TimeSpan> RefreshAllocationAsync(
var request = new RefreshRequest((int)lifetime.TotalSeconds);
await SendMessageAsync(stream, request, cancellationToken);

StunMessage response =
await _responses[request.TransactionId].Task;
StunMessage response = await ReceiveMessage(request.TransactionId);
if (response is RefreshSuccessResponse success)
{
return TimeSpan.FromSeconds(success.Lifetime);
Expand Down Expand Up @@ -252,7 +249,6 @@ private async Task ProcessMessage()
out TaskCompletionSource<StunMessage> tcs))
{
tcs.SetResult(message);
_responses.Remove(message.TransactionId);
}
}
catch (Exception e)
Expand All @@ -274,6 +270,14 @@ private async Task EnsureConnection()
}
}

private async Task<StunMessage> ReceiveMessage(byte[] transactionId)
{
StunMessage response = await _responses[transactionId].Task;
_responses.Remove(transactionId);

return response;
}

private class ByteArrayComparer : IEqualityComparer<byte[]>
{
public bool Equals(byte[] left, byte[] right)
Expand Down

0 comments on commit 21fef6a

Please sign in to comment.