Esempio n. 1
0
        public TorrentDownloader(IEnumerable <Peer> peers, TorrentFileInfo info,
                                 ConcurrentQueue <RequestItem> items)
        {
            var channel = Channel.CreateUnbounded <Piece>();

            _writer = channel.Writer;
            _reader = channel.Reader;
            _peers  = peers;
            _info   = info;
            _items  = items;
        }
Esempio n. 2
0
        static (long, long) CalculateBounds(int index, TorrentFileInfo info)
        {
            var begin = index * info.PieceSize;
            var end   = begin + info.PieceSize;

            if (end > info.Size)
            {
                end = info.Size;
            }

            return(begin, end);
        }
Esempio n. 3
0
        static async Task Main(string[] args)
        {
            var torrentFileInfo = new TorrentFileInfo(@"D:\debian.torrent");
            var tracker         = new TorrentTracker(torrentFileInfo.Announce);
            var endpoints       =
                await tracker.RetrievePeersAsync(torrentFileInfo.InfoHash, PeerId.CreateNew(), torrentFileInfo.Size);

            var peers = endpoints.Select(Peer.Create);

            var queue = new ConcurrentQueue <RequestItem>();

            torrentFileInfo.PieceHashes.Select((hash, index)
                                               => new RequestItem(index, hash, torrentFileInfo.PieceSize, torrentFileInfo.Size)).ToList()
            .ForEach(r => queue.Enqueue(r));


            var torrentDownloader = new TorrentDownloader(peers, torrentFileInfo, queue);
            await torrentDownloader.Download();

            Console.WriteLine("Downloaded!");
        }
Esempio n. 4
0
File: Peer.cs Progetto: Denisa8/SOB
        private void HandleRead(IAsyncResult ar)
        {
            int bytes = 0;

            try
            {
                bytes = stream.EndRead(ar);
                if (bytes > 0 && Settings.availablePeer)
                {
                    Console.WriteLine("bytes: " + bytes);
                    byte[] b;
                    int    id          = EndianBitConverter.Big.ToInt32(buffer, 0);
                    int    length      = EndianBitConverter.Big.ToInt32(buffer, 4);
                    int    torrentHash = EndianBitConverter.Big.ToInt32(buffer, 8);
                    int    type        = buffer[12];
                    var    idS         = buffer.Skip(13).Take(16).ToArray();
                    Guid   guid        = new Guid(idS);
                    Console.WriteLine(Settings.ID);
                    if (length != 0)
                    {
                        b = buffer.Skip(29).Take(length).ToArray();
                    }
                    else
                    {
                        b = buffer.Skip(29).Take(length).ToArray();
                    }

                    buffer = new byte[TorrentFileInfo.PiecesLength + Program.messageMetadataSize];
                    Console.WriteLine("odczytano typ: " + type + " id: " + id + " l " + length);
                    counter++;
                    Console.WriteLine("bytes: " + bytes);


                    var p = Program.Peers.ToList().Where(x => x.Value.GUID == guid).ToList();
                    if (p.Count() > 0)
                    {
                        p[0].Value.LastActive = DateTime.Now;
                    }


                    if (type == 1)
                    {
                        if (torrentHash != TorrentFileInfo.TorrentHash)
                        {
                            Console.WriteLine("Otrzymano hash: " + torrentHash + " ----- " + "Oczekiwany hash: " + TorrentFileInfo.TorrentHash);
                            Console.WriteLine("Niewłaściwy hash pliku.");

                            var peers = Program.Peers.ToList();
                            foreach (KeyValuePair <int, Peer> keyValuePair in peers)
                            {
                                if (keyValuePair.Value.stream == stream)
                                {
                                    keyValuePair.Value.IncreaseErrorCounter(keyValuePair.Value.GUID);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("PieceIndex = " + id);
                            if (id == 205)
                            {
                                Console.WriteLine("test");
                            }
                            var result = TorrentFileInfo.CheckReceivedPiece(b, id, b.Length);
                            //TorrentFileInfo.CheckPieceHash(b, id);
                            if (!result)
                            {
                                Console.WriteLine("Odebrano błędny fragment.");

                                var peers = Program.Peers.ToList();
                                foreach (KeyValuePair <int, Peer> keyValuePair in peers)
                                {
                                    if (keyValuePair.Value.stream == stream)
                                    {
                                        keyValuePair.Value.IncreaseErrorCounter(keyValuePair.Value.GUID);
                                    }
                                }
                            }

                            else
                            {
                                Program.Incoming.Add(new PendingMessage
                                {
                                    PieceIndex     = id,
                                    Stream         = stream,
                                    EncodedMessage = b,
                                    Type           = type,
                                    Guid           = guid
                                });
                                //if (Settings.ReadPieces != null && id < Settings.ReadPieces.Length)
                                // Settings.ReadPieces[id] = true;
                            }
                        }
                    }
                    else if (type == 0)
                    {
                        Program.Incoming.Add(new PendingMessage
                        {
                            PieceIndex = id,
                            Stream     = stream,
                            Type       = type,
                            Guid       = guid
                        });
                    }
                    else if (type == 2)
                    {
                        Program.Incoming.Add(new PendingMessage
                        {
                            PieceIndex = id,
                            Stream     = stream,
                            Type       = type,
                            Guid       = guid,
                            IndexPeer  = torrentHash
                        });
                    }
                    else if (type == 3)
                    {
                        Program.Incoming.Add(new PendingMessage
                        {
                            PieceIndex = id,
                            Stream     = stream,
                            Type       = type,
                            Guid       = guid,
                            IndexPeer  = torrentHash
                        });
                    }
                    else
                    {
                        Console.WriteLine("Niepoprawny typ wiadomosci");

                        var peers = Program.Peers.ToList();
                        foreach (KeyValuePair <int, Peer> keyValuePair in peers)
                        {
                            if (keyValuePair.Value.stream == stream)
                            {
                                keyValuePair.Value.IncreaseErrorCounter(keyValuePair.Value.GUID);
                            }
                        }
                    }
                }
                //stream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Disconnect();
                return;
            }

            try
            {
                if (client.Connected)
                {
                    stream = client.GetStream();
                    stream.BeginRead(buffer, 0, TorrentFileInfo.PiecesLength + Program.messageMetadataSize, new AsyncCallback(HandleRead), null);
                }
            }
            catch (Exception e)
            {
                Disconnect();
            }
        }