diff --git a/CHANGES.md b/CHANGES.md index 01c3ff2d8e..866c5b39bb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -99,11 +99,13 @@ To be released. work around NAT so that `Peer`'s endpoints don't have to be multiple, `Peer.Urls` was renamed to `Peer.EndPoint` and its type also was changed from `IImmutableList` to `DnsEndPoint`. - [[#120], [#123] by Yang Chun Ung, [#126], [#127], [#165]] + [[#120], [#123] by Yang Chun Ung, [#126], [#127], [#165], [#166]] - `Swarm` became to ignore tip blocks of the same height (`Index`) that it already has and deal with only longer (higher) blocks. - Fixed a bug that occured when `Swarm` was handling multiple responses at the same time. + - Fixed a bug that the `Swarm` constructor had hanged in certain runtimes + like Unity engine. - Removed `AddressTransactionSet` which handles handle `Address` to `IEnumerable` indices, and the following methods in `IStore`: - `IStore.IterateAddresses()` @@ -141,6 +143,7 @@ To be released. [#151]: https://github.com/planetarium/libplanet/pull/151 [#159]: https://github.com/planetarium/libplanet/pull/159 [#165]: https://github.com/planetarium/libplanet/issues/165 +[#165]: https://github.com/planetarium/libplanet/pull/166 [RFC 5389]: https://tools.ietf.org/html/rfc5389 [RFC 5766]: https://tools.ietf.org/html/rfc5766 diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index 475bf68152..5e86b97e75 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -573,12 +573,26 @@ await Task.WhenAll( } [Fact] - public void CanDenyNullParams() + public void ThrowArgumentExceptionInConstructor() { Assert.Throws(() => { new Swarm(null); }); + + // Swarm needs host or iceServers. + Assert.Throws(() => + { + new Swarm(new PrivateKey()); + }); + + // Swarm needs host or iceServers. + Assert.Throws(() => + { + new Swarm( + new PrivateKey(), + iceServers: new IceServer[] { }); + }); } [Fact] diff --git a/Libplanet/Net/Swarm.cs b/Libplanet/Net/Swarm.cs index 36be6ca761..c0d8f245e0 100644 --- a/Libplanet/Net/Swarm.cs +++ b/Libplanet/Net/Swarm.cs @@ -46,12 +46,13 @@ public partial class Swarm : ICollection, IDisposable private readonly AsyncLock _receiveMutex; private readonly AsyncLock _blockSyncMutex; private readonly string _host; - private readonly TurnClient _turnClient; + private readonly IList _iceServers; private readonly ILogger _logger; private TaskCompletionSource _runningEvent; private int? _listenPort; + private TurnClient _turnClient; private NetMQQueue _replyQueue; @@ -115,15 +116,13 @@ public Swarm( EndPoint = new DnsEndPoint(_host, listenPort.Value); } - if (iceServers != null) - { - _turnClient = IceServer.CreateTurnClient(iceServers).Result; - } - - if (_host == null && _turnClient == null) + _iceServers = iceServers?.ToList(); + if (_host == null && (_iceServers == null || !_iceServers.Any())) { throw new ArgumentException( - $"Swarm needs {nameof(host)} or {iceServers}."); + $"Swarm requires either {nameof(host)} or " + + $"{nameof(iceServers)}." + ); } string loggerId = _privateKey.PublicKey.ToAddress().ToHex(); @@ -399,6 +398,11 @@ public async Task StartAsync( throw new SwarmException("Swarm is already running."); } + if (_iceServers != null) + { + _turnClient = await IceServer.CreateTurnClient(_iceServers); + } + if (_listenPort == null) { _listenPort = _router.BindRandomPort("tcp://*");