/// <summary> /// Is needed to fetch the peer. /// </summary> /// <param name="peers">List of peers to try</param> /// <param name="retryCount">Number of retry before a timeout</param> /// <returns></returns> private async Task <PeerApi> GetInitialPeer(List <ArkPeerAddress> peers, int retryCount = 0) { try { //Picks a peer randomly from the list var peerUrl = peers[new Random().Next(peers.Count)]; LoggingApi.Info(string.Format("Getting initial peer. ip: <<{0}>>, port: <<{1}>>, retrycount: <<{2}>>", peerUrl.Ip, peerUrl.Port, retryCount)); // create a peer out of peerurl, and returns if the peer is online. // var peer = new PeerApi(this, peerUrl.Ip, peerUrl.Port); if (await peer.IsOnline().ConfigureAwait(false)) { return(peer); } LoggingApi.Warn(string.Format("Peer is not online. ip: <<{0}>>, port: <<{1}>>", peerUrl.Ip, peerUrl.Port)); // Throw an exception if all of the initial peers have been tried. // if (retryCount == peers.Count) { LoggingApi.Error(string.Format("Unable to connect to a seed peer. Retried <<{0}>> times", retryCount)); throw new Exception("Unable to connect to a seed peer"); } // redo the check and increment the retry count // return(await GetInitialPeer(peers, retryCount + 1).ConfigureAwait(false)); } catch (Exception e) { LoggingApi.Error(e.ToString()); throw e; } }
/// <summary> /// Is needed to fetch the peer. /// </summary> /// <param name="type"> /// <inheritdoc cref="NetworkType"/>Can be : /// -- DevNet (test), ask Dark (testnet coins) on the slack. /// -- MainNet (live, beware real money, financial loss possible there). /// </param> /// <param name="retryCount">Number of retry before a timeout</param> /// <returns>Returns the first <inheritdoc cref="PeerApi"/> that is online</returns> private async Task <PeerApi> GetInitialPeer(NetworkType type, int retryCount = 0) { // Pick a peer randomly in _peerSeedListMainNet // var peerUrl = _peerSeedListMainNet[new Random().Next(_peerSeedListMainNet.Count)]; // If the Network is set to DevNet, change the peer picked above by a peer from _peerSeedListDevNet // if (type == NetworkType.DevNet) { peerUrl = _peerSeedListDevNet[new Random().Next(_peerSeedListDevNet.Count)]; } // create a peer out of peerurl, and returns if the peer is online. // var peer = new PeerApi(NetworkApi, peerUrl.Item1, peerUrl.Item2); if (await peer.IsOnline().ConfigureAwait(false)) { return(peer); } // Throw an exception if all of the initial peers have been tried. // if ((type == NetworkType.DevNet && retryCount == _peerSeedListDevNet.Count) || (type == NetworkType.MainNet && retryCount == _peerSeedListMainNet.Count)) { throw new Exception("Unable to connect to a seed peer"); } // redo the check and increment the retry count // return(await GetInitialPeer(type, retryCount + 1).ConfigureAwait(false)); }
/// <summary> /// Is needed to fetch the peer. /// </summary> /// <param name="peers">List of peers to try</param> /// <param name="retryCount">Number of retry before a timeout</param> /// <returns></returns> private async Task <PeerApi> GetInitialPeer(List <ArkPeerAddress> peers, int retryCount = 0) { //Picks a peer randomly from the list var peerUrl = peers[new Random().Next(peers.Count)]; // create a peer out of peerurl, and returns if the peer is online. // var peer = new PeerApi(NetworkApi, peerUrl.Ip, peerUrl.Port); if (await peer.IsOnline().ConfigureAwait(false)) { return(peer); } // Throw an exception if all of the initial peers have been tried. // if (retryCount == peers.Count) { throw new Exception("Unable to connect to a seed peer"); } // redo the check and increment the retry count // return(await GetInitialPeer(peers, retryCount + 1).ConfigureAwait(false)); }
private async Task <PeerApi> GetInitialPeer(NetworkType type, int retryCount = 0) { var peerUrl = _peerSeedListMainNet[new Random().Next(_peerSeedListMainNet.Count)]; if (type == NetworkType.DevNet) { peerUrl = _peerSeedListDevNet[new Random().Next(_peerSeedListDevNet.Count)]; } var peer = new PeerApi(peerUrl.Item1, peerUrl.Item2); if (await peer.IsOnline()) { return(peer); } if ((type == NetworkType.DevNet && retryCount == _peerSeedListDevNet.Count) || (type == NetworkType.MainNet && retryCount == _peerSeedListMainNet.Count)) { throw new Exception("Unable to connect to a seed peer"); } return(await GetInitialPeer(type, retryCount + 1)); }