Esempio n. 1
0
 /// <summary>
 ///   Advertise that we can provide the CID to the X closest peers
 ///   of the CID.
 /// </summary>
 /// <param name="cid">
 ///   The CID to advertise.
 /// </param>
 /// <remarks>
 ///   This starts a background process to send the AddProvider message
 ///   to the 4 closest peers to the <paramref name="cid"/>.
 /// </remarks>
 public void Advertise(Cid cid)
 {
     _ = Task.Run(async() =>
     {
         int advertsNeeded = 4;
         var message       = new DhtMessage
         {
             Type          = MessageType.AddProvider,
             Key           = cid.Hash.ToArray(),
             ProviderPeers = new DhtPeerMessage[]
             {
                 new DhtPeerMessage
                 {
                     Id        = Swarm.LocalPeer.Id.ToArray(),
                     Addresses = Swarm.LocalPeer.Addresses
                                 .Select(a => a.WithoutPeerId().ToArray())
                                 .ToArray()
                 }
             }
         };
         var peers = RoutingTable
                     .NearestPeers(cid.Hash)
                     .Where(p => p != Swarm.LocalPeer);
         foreach (var peer in peers)
         {
             try
             {
                 using (var stream = await Swarm.DialAsync(peer, this.ToString()))
                 {
                     ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, message, PrefixStyle.Base128);
                     await stream.FlushAsync();
                 }
                 if (--advertsNeeded == 0)
                 {
                     break;
                 }
             }
             catch (Exception)
             {
                 // eat it.  This is fire and forget.
             }
         }
     });
 }
Esempio n. 2
0
        async Task SendWantListAsync(Peer peer, IEnumerable <WantedBlock> wants, bool full)
        {
            log.Debug($"sending want list to {peer}");

            // Send the want list to the peer on any bitswap protocol
            // that it supports.
            foreach (var protocol in protocols)
            {
                try
                {
                    using (var stream = await Swarm.DialAsync(peer, protocol.ToString()))
                    {
                        await protocol.SendWantsAsync(stream, wants, full : full);
                    }
                    return;
                }
                catch (Exception e)
                {
                    log.Debug($"{peer} refused {protocol}", e);
                }
            }

            log.Warn($"{peer} does not support any bitswap protocol");
        }