public async Task <INetworkStream> NewStream(PeerId peer, IEnumerable <ProtocolId> protocols, CancellationToken cancellationToken) { var pref = PreferredProtocol(peer, protocols); if (pref != "") { return(await NewStream(peer, pref, cancellationToken)); } var protosstrs = protocols.Select(p => p.ToString()).ToArray(); var s = await Network.NewStreamAsync(peer, cancellationToken); if (s == null) { return(null); } var selected = await MultistreamMuxer.SelectOneOfAsync(protosstrs, ((IReadWriter)s).AsSystemStream(), cancellationToken); if (selected == null) { s.Dispose(); return(null); } s.Protocol = new ProtocolId(selected); Peerstore.AddProtocols(peer, selected); //reporter return(s); }
private ProtocolId PreferredProtocol(PeerId peer, IEnumerable <ProtocolId> pids) { var pidstrs = pids.Select(p => p.ToString()).ToArray(); var supported = Peerstore.SupportsProtocols(peer, pidstrs); if (supported == null) { return(string.Empty); } return(supported.FirstOrDefault()); }
public Task Connect(PeerInfo pi, CancellationToken cancellationToken) { Peerstore.AddAddresses(pi.Id, pi.Addresses, AddressManager.TempAddrTTL); var cs = Network.ConnectionsToPeer(pi.Id); if (cs.Length > 0) { return(Task.CompletedTask); } return(DialPeer(pi.Id, cancellationToken)); }
private async Task DialPeer(PeerId peer, CancellationToken cancellationToken) { var c = await Network.DialPeerAsync(peer, cancellationToken); if (c == null) { return; } Peerstore.AddProtocols(peer); await Task.Factory.StartNew(() => _ids.IdentifyConnection(c), cancellationToken); }
private void NewConnectionHandler(INetworkConnection connection) { Peerstore.AddProtocols(connection.RemotePeer); _ids.IdentifyConnection(connection); }