Skip to content

Commit

Permalink
Fix #377
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed Jul 30, 2019
1 parent b69c8cf commit 1e49251
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ To be released.
duplicated transaction ids. [[#366]]
- Fixed a bug that `NullReferenceException` occurred when serializing default
`Address`. [[#369]]

- Fixed a bug that `TurnClient` throws `KeyNotFoundException` and
`IOException` when startup. [[#377], [#378]]

[#319]: https://github.com/planetarium/libplanet/issues/319
[#343]: https://github.com/planetarium/libplanet/pull/343
Expand All @@ -59,6 +60,8 @@ To be released.
[#366]: https://github.com/planetarium/libplanet/pull/366
[#367]: https://github.com/planetarium/libplanet/pull/367
[#369]: https://github.com/planetarium/libplanet/pull/369
[#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 1e49251

Please sign in to comment.