Skip to content
Permalink
Browse files

Fix #377

  • Loading branch information...
longfin committed Jul 30, 2019
1 parent 046c15f commit d04822cb973d250252f585ab18d0f9c33731309f
Showing with 16 additions and 9 deletions.
  1. +4 −1 CHANGES.md
  2. +12 −8 Libplanet.Stun/Stun/TurnClient.cs
@@ -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
@@ -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
@@ -70,7 +70,7 @@ public class TurnClient : IStunContext, IDisposable
{
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)
{
@@ -101,8 +101,7 @@ public class TurnClient : IStunContext, IDisposable
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)
{
@@ -154,8 +153,7 @@ public class TurnClient : IStunContext, IDisposable
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)
{
@@ -177,8 +175,7 @@ public class TurnClient : IStunContext, IDisposable
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);
@@ -252,7 +249,6 @@ private async Task ProcessMessage()
out TaskCompletionSource<StunMessage> tcs))
{
tcs.SetResult(message);
_responses.Remove(message.TransactionId);
}
}
catch (Exception e)
@@ -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)

0 comments on commit d04822c

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