/// <inheritdoc /> public void ConnectToPeers(params EndPoint[] endPoints) { var rand = new Random(Environment.TickCount); var connectedEndPoints = _serverContext.ConnectedPeers.Keys.ToArray(); var preferredEndPoints = endPoints .Concat(_peerEndPoints) .Except(connectedEndPoints) .Distinct() .OrderBy(_ => rand.Next()) .Take(Math.Max(0, _serverContext.MaxConnectedPeers - connectedEndPoints.Length)) .ToArray(); Parallel.ForEach(preferredEndPoints, async ep => { try { if (_serverContext.ConnectedPeers.ContainsKey(ep)) { return; } var peer = await _peerFactory.ConnectTo(ep); PeerConnected(this, peer); } catch (Exception ex) { _logger.LogWarning($"Something went wrong with {ep}. Exception: {ex}"); } }); }
/// <inheritdoc /> public void ConnectToPeers(params EndPoint[] endPoints) { Parallel.ForEach(endPoints, async ep => { try { var peer = await _peerFactory.ConnectTo(ep); PeerConnected(this, peer); } catch (Exception ex) { _logger.LogWarning($"Something went wrong with {ep}. Exception: {ex}"); } }); }
private void ConnectToPeers() { // TODO: check if localhot:port in seeding list foreach (var peerEndPoint in _config.PeerEndPoints) { _peerFactory .ConnectTo(peerEndPoint) .ContinueWith(t => { if (t.IsCompletedSuccessfully) { PeerConnected(this, t.Result); } else { _logger.LogWarning($"Something went wrong with {peerEndPoint}. Exception: {t.Exception}"); } }); } }