public void FixtureSetup()
 {
     rig = TestRig.CreateMultiFile();
     id = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     for (int i = 0; i < id.BitField.Length; i += 2)
         id.BitField[i] = true;
 }
Esempio n. 2
0
        private void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            if (_engine.ConnectionManager.ShouldBanPeer(e.Peer))
            {
                e.Connection.Dispose();
                return;
            }
            var id = new PeerId(e.Peer, e.TorrentManager) {Connection = e.Connection};

            Logger.Log(id.Connection, "ListenManager - ConnectionReceived");

            if (id.Connection.IsIncoming)
            {
                var skeys = new List<InfoHash>();

                ClientEngine.MainLoop.QueueWait(delegate
                                                    {
                                                        for (var i = 0; i < _engine.Torrents.Count; i++)
                                                            skeys.Add(_engine.Torrents[i].InfoHash);
                                                    });

                EncryptorFactory.BeginCheckEncryption(id, HandshakeMessage.HandshakeLength, _endCheckEncryptionCallback,
                                                      id, skeys.ToArray());
            }
            else
            {
                ClientEngine.MainLoop.Queue(() => _engine.ConnectionManager.ProcessFreshConnection(id));
            }
        }
 internal PeerConnectionEventArgs( TorrentManager manager, PeerId id, Direction direction, String message )
     : base(manager)
 {
     _peerConnectionId = id;
     _connectionDirection = direction;
     _message = message;
 }
Esempio n. 4
0
 public virtual void Unchoke(PeerId id)
 {
     id.AmChoking = false;
     id.TorrentManager.UploadingTo++;
     id.Enqueue(new UnchokeMessage());
     id.LastUnchoked = DateTime.Now;
 }
        public PeerExchangePeersAdded(TorrentManager manager, int count, int total, PeerId id)
            : base(manager, count, total)
        {
            if (id == null)
                throw new ArgumentNullException("id");

            _id = id;
        }
Esempio n. 6
0
        internal static IAsyncResult BeginCheckEncryption(PeerId id, int bytesToReceive, AsyncCallback callback, object state, InfoHash[] sKeys)
        {
            EncryptorAsyncResult result = new EncryptorAsyncResult(id, callback, state);
            result.SKeys = sKeys;

            IConnection c = id.Connection;
            ClientEngine.MainLoop.QueueTimeout(TimeSpan.FromSeconds(10), delegate {
                if (id.Encryptor == null || id.Decryptor == null)
                    id.CloseConnection();
                return false;
            });

            try
            {
                // If the connection is incoming, receive the handshake before
                // trying to decide what encryption to use
                if (id.Connection.IsIncoming)
                {
                    result.Buffer = new byte[bytesToReceive];
                    NetworkIO.EnqueueReceive(c, result.Buffer, 0, result.Buffer.Length, null, null, null, HandshakeReceivedCallback, result);
                }
                else
                {
                    EncryptionTypes usable = CheckRC4(id);
                    bool hasPlainText = Toolbox.HasEncryption(usable, EncryptionTypes.PlainText);
                    bool hasRC4 = Toolbox.HasEncryption(usable, EncryptionTypes.RC4Full) || Toolbox.HasEncryption(usable, EncryptionTypes.RC4Header);
                    if (id.Engine.Settings.PreferEncryption)
                    {
                        if (hasRC4)
                        {
                            result.EncSocket = new PeerAEncryption(id.TorrentManager.InfoHash, usable);
                            result.EncSocket.BeginHandshake(id.Connection, CompletedEncryptedHandshakeCallback, result);
                        }
                        else
                        {
                            result.Complete();
                        }
                    }
                    else
                    {
                        if (hasPlainText)
                        {
                            result.Complete();
                        }
                        else
                        {
                            result.EncSocket = new PeerAEncryption(id.TorrentManager.InfoHash, usable);
                            result.EncSocket.BeginHandshake(id.Connection, CompletedEncryptedHandshakeCallback, result);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Complete(ex);
            }
            return result;
        }
Esempio n. 7
0
 public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
 {
     // Invert 'bitfield' and AND it with the peers bitfield
     // Any pieces which are 'true' in the bitfield will not be downloaded
     temp.From(peerBitfield).NAnd(bitfield);
     if (temp.AllFalse)
         return null;
     return base.PickPiece(id, temp, otherPeers, count, startIndex, endIndex);
 }
Esempio n. 8
0
 internal Block(Piece piece, int startOffset, int requestLength)
 {
     this.requestedOff = null;
     this.piece = piece;
     this.received = false;
     this.requested = false;
     this.requestLength = requestLength;
     this.startOffset = startOffset;
     this.written = false;
 }
Esempio n. 9
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            if (peerBitfield.AllFalse)
                return null;

            if (count > 1)
                return base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex);

            int midpoint = random.Next(startIndex, endIndex);
            return base.PickPiece(id, peerBitfield, otherPeers, count, midpoint, endIndex) ??
                   base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, midpoint);
        }
Esempio n. 10
0
        public void Choke(PeerId peer)
        {
            //Choke the supplied peer

            if (peer.AmChoking)
                //We're already choking this peer, nothing to do
                return;

            peer.AmChoking = true;
            _owningTorrent.UploadingTo--;
            RejectPendingRequests(peer);
            peer.EnqueueAt(new ChokeMessage(), 0);
            Logger.Log(peer.Connection, "Choking");
            //			Send2Log("Choking: " + PeerToChoke.Location);
        }
Esempio n. 11
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            if (peerBitfield.AllFalse)
                return null;

            if (count > 1)
                return base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex);

            GenerateRarestFirst(peerBitfield, otherPeers);

            while (rarest.Count > 0)
            {
                BitField current = rarest.Pop();
                MessageBundle bundle = base.PickPiece(id, current, otherPeers, count, startIndex, endIndex);
                spares.Push(current);

                if (bundle != null)
                    return bundle;
            }

            return null;
        }
Esempio n. 12
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            PickPieceId.Add(id);
            BitField clone = new BitField(peerBitfield.Length);
            clone.Or(peerBitfield);
            PickPieceBitfield.Add(clone);
            PickPiecePeers.Add(otherPeers);
            PickPieceStartIndex.Add(startIndex);
            PickPieceEndIndex.Add(endIndex);
            PickPieceCount.Add(count);

            for (int i = startIndex; i < endIndex; i++)
            {
                if (PickedPieces.Contains(i))
                    continue;
                PickedPieces.Add(i);
                if (ReturnNoPiece)
                    return null;
                else
                    return new MessageBundle();
            }
            return null;
        }
Esempio n. 13
0
 public RequestMessage PickPiece(PeerId peer, List<PeerId> otherPeers)
 {
     MessageBundle bundle = PickPiece(peer, otherPeers, 1);
     return bundle == null ? null : (RequestMessage)bundle.Messages[0];
 }
Esempio n. 14
0
 public override bool ValidatePiece(PeerId peer, int pieceIndex, int startOffset, int length, out Piece piece)
 {
     piece = null;
     return false;
 }
Esempio n. 15
0
 public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
 {
     return null;
 }
Esempio n. 16
0
 public override void CancelRequests(PeerId peer)
 {
 }
Esempio n. 17
0
 public override void CancelRequest(PeerId peer, int piece, int startOffset, int length)
 {
 }
Esempio n. 18
0
 public virtual RequestMessage ContinueExistingRequest(PeerId peer)
 {
     CheckOverriden();
     return picker.ContinueExistingRequest(peer);
 }
Esempio n. 19
0
 public virtual void CancelRequests(PeerId peer)
 {
     CheckOverriden();
     picker.CancelRequests(peer);
 }
Esempio n. 20
0
 public virtual void CancelRequest(PeerId peer, int piece, int startOffset, int length)
 {
     CheckOverriden();
     picker.CancelRequest(peer, piece, startOffset, length);
 }
Esempio n. 21
0
        public void Setup()
        {
            bitfield = new BitField(40).SetAll(true)
                                       .Set(4, false)
                                       .Set(6, false)
                                       .Set(24, false)
                                       .Set(36, false);
            picker = new EndGamePicker();
            pieces = new List<Piece>(new Piece[] {
                new Piece(4, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(6, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(24, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(36, rig.Torrent.PieceLength, rig.Torrent.Size)
            });

            id = new PeerId(new Peer("peerid", new Uri("tcp://weburl.com")), rig.Manager);
            id.IsChoking = false;
            id.BitField.SetAll(false);

            other = new PeerId(new Peer("other", new Uri("tcp://other.com")), rig.Manager);
            other.IsChoking = false;
            other.BitField.SetAll(false);
        }
Esempio n. 22
0
        public void Setup()
        {
            requestedUrl.Clear();
            partialData = false;
            int i;
            for (i = 0; i < 1000; i++)
            {
                try
                {
                    listener = new HttpListener();
                    listener.Prefixes.Add(string.Format(listenerURL, i));
                    listener.Start();
                    break;
                }
                catch
                {

                }
            }
            listener.BeginGetContext(GotContext, null);
            rig = TestRig.CreateMultiFile();
            connection = new HttpConnection(new Uri(string.Format(listenerURL, i)));
            connection.Manager = rig.Manager;

            id = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager);
            id.Connection = connection;
            id.IsChoking = false;
            id.AmInterested = true;
            id.BitField.SetAll(true);
            id.MaxPendingRequests = numberOfPieces;

            requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), numberOfPieces);
        }
Esempio n. 23
0
 public void FixtureSetup()
 {
     rig = TestRig.CreateMultiFile();
     id = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     id.BitField.SetAll(true);
 }
Esempio n. 24
0
        public void SingleFileTorrent()
        {
            rig.Dispose();
            rig = TestRig.CreateSingleFile();
            string url = rig.Torrent.GetRightHttpSeeds[0];
            connection = new HttpConnection(new Uri (url));
            connection.Manager = rig.Manager;

            id = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager);
            id.Connection = connection;
            id.IsChoking = false;
            id.AmInterested = true;
            id.BitField.SetAll(true);
            id.MaxPendingRequests = numberOfPieces;

            requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), numberOfPieces);
            RecieveFirst();
            Assert.AreEqual(url, requestedUrl[0]);
        }
Esempio n. 25
0
 public virtual void Choke(PeerId id)
 {
     id.AmChoking = true;
     id.TorrentManager.UploadingTo--;
     id.Enqueue(new ChokeMessage());
 }
Esempio n. 26
0
 public MessageBundle PickPiece(PeerId peer, List<PeerId> otherPeers, int count)
 {
     return PickPiece(peer, peer.BitField, otherPeers, count, 0, peer.BitField.Length);
 }
Esempio n. 27
0
        public void Setup(bool metadataMode, string metadataPath)
        {
            pair = new ConnectionPair(55432);
            rig = TestRig.CreateSingleFile(1024 * 1024 * 1024, 32768, metadataMode);
            rig.MetadataPath = metadataPath;
            rig.RecreateManager();

            rig.Manager.HashChecked = true;
            rig.Manager.Start();
            rig.AddConnection(pair.Outgoing);

            var connection = pair.Incoming;
            PeerId id = new PeerId(new Peer("", connection.Uri), rig.Manager);
            id.Connection = connection;
            byte[] data;

            EncryptorFactory.EndCheckEncryption(EncryptorFactory.BeginCheckEncryption(id, 68, null, null, new InfoHash[] { id.TorrentManager.InfoHash }), out data);
            decryptor = id.Decryptor;
            encryptor = id.Encryptor;
        }
Esempio n. 28
0
 public virtual MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex)
 {
     CheckOverriden();
     return picker.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex);
 }
Esempio n. 29
0
 public virtual void Setup()
 {
     // Yes, this is horrible. Deal with it.
     rig = TestRig.CreateMultiFile();
     peers = new List<PeerId>();
     picker = new IgnoringPicker(rig.Manager.Bitfield, new StandardPicker());
     picker.Initialise(rig.Manager.Bitfield, rig.Manager.Torrent.Files, new List<Piece>());
     peer = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     for (int i = 0; i < 20; i++)
     {
         PeerId p = new PeerId(new Peer(new string(i.ToString()[0], 20), new Uri("tcp://" + i)), rig.Manager);
         p.SupportsFastPeer = true;
         peers.Add(p);
     }
 }
Esempio n. 30
0
 public virtual bool ValidatePiece(PeerId peer, int pieceIndex, int startOffset, int length, out Piece piece)
 {
     CheckOverriden();
     return picker.ValidatePiece(peer, pieceIndex, startOffset, length, out piece);
 }