public void AddPeer(Peer peer)
		{
			peer.Disconnected += new PeerDisconnectedCallback(peer_Disconnected);
			peer.HeIsInterestedChange += new PeerStatusChangeCallback(peer_HeIsInterestedChange);
			peer.PieceCancel += new PeerPieceCallback(peer_PieceCancel);
			peer.PieceRequest += new PeerPieceCallback(peer_PieceRequest);
		}
	public PeerListViewItem(BT.Peer peer)
	{
		this.peer = peer;

		this.Text = peer.ToString();

		this.SubItems.Add("Leech");
		this.SubItems.Add("0");
		this.SubItems.Add("0");
		this.SubItems.Add("Y");
		this.SubItems.Add("N");
		this.SubItems.Add("Y");
		this.SubItems.Add("N");
		this.SubItems.Add("0");
		this.SubItems.Add(peer.Information.ClientType.ToString());
		this.SubItems.Add(peer.Information.ID.ToString());

		peer.BitfieldChange += new BitTorrent.PeerBitfieldChangeCallback(peer_BitfieldChange);
		peer.HeIsChokingChange += new BitTorrent.PeerStatusChangeCallback(peer_HeIsChokingChange);
		peer.HeIsInterestedChange += new BitTorrent.PeerStatusChangeCallback(peer_HeIsInterestedChange);
		peer.IAmChokingChange += new BitTorrent.PeerStatusChangeCallback(peer_IAmChokingChange);
		peer.IAmInterestedChange += new BitTorrent.PeerStatusChangeCallback(peer_IAmInterestedChange);

		peer.DownThrottle.RateChange += new BitTorrent.RateChangeCallback(peer_DownloadRateChange);
		peer.UpThrottle.RateChange +=new BitTorrent.RateChangeCallback(peer_UploadRateChange);
	}
Beispiel #3
0
    public PeerListViewItem(BT.Peer peer)
    {
        this.peer = peer;

        this.Text = peer.ToString();

        this.SubItems.Add("Leech");
        this.SubItems.Add("0");
        this.SubItems.Add("0");
        this.SubItems.Add("Y");
        this.SubItems.Add("N");
        this.SubItems.Add("Y");
        this.SubItems.Add("N");
        this.SubItems.Add("0");
        this.SubItems.Add(peer.Information.ClientType.ToString());
        this.SubItems.Add(peer.Information.ID.ToString());

        peer.BitfieldChange       += new BitTorrent.PeerBitfieldChangeCallback(peer_BitfieldChange);
        peer.HeIsChokingChange    += new BitTorrent.PeerStatusChangeCallback(peer_HeIsChokingChange);
        peer.HeIsInterestedChange += new BitTorrent.PeerStatusChangeCallback(peer_HeIsInterestedChange);
        peer.IAmChokingChange     += new BitTorrent.PeerStatusChangeCallback(peer_IAmChokingChange);
        peer.IAmInterestedChange  += new BitTorrent.PeerStatusChangeCallback(peer_IAmInterestedChange);

        peer.DownThrottle.RateChange += new BitTorrent.RateChangeCallback(peer_DownloadRateChange);
        peer.UpThrottle.RateChange   += new BitTorrent.RateChangeCallback(peer_UploadRateChange);
    }
Beispiel #4
0
		public PieceRequest( Peer peer, int pieceId, int begin, int length )
		{
			this.Peer = peer;
			this.PieceId = pieceId;
			this.Begin = begin;
			this.Length = length;
		}
		private void peer_Disconnected(Peer peer)
		{
			lock ( this.interestedPeers.SyncRoot )
			{
				if (this.interestedPeers.Contains(peer))
					this.interestedPeers.Remove(peer);
			}
		}
Beispiel #6
0
		public void AddPeer(Peer peer)
		{
			peer.Disconnected += new PeerDisconnectedCallback(peer_Disconnected);

			lock ( this )
			{
				this.peerList.Add( peer );
				this.socketList.Add( peer.Socket );
			}
		}
		private void AddPeerBitfieldToPieceCount( Peer peer )
		{
			// add to peer-piece index
			if ( peer.BitField != null )
			{
				for ( int i = 0; i < this.mPeerPieceList.Length; ++i )
				{
					if ( peer.BitField.Get( i ) )
						this.mPeerPieceList[ i ]++;
				}
			}
		}
Beispiel #8
0
	private void RemovePeer( BT.Peer peer )
	{
		foreach ( PeerListViewItem item in this.peerListView.Items )
		{
			if ( item.Peer.Equals( peer ) )
			{
				item.UnhookPeerEvents();
				this.peerListView.Items.Remove(item);
				this.peerListView.Refresh();
				break;
			}
		}
	}
Beispiel #9
0
	private void OnPeerConnected(BT.Torrent torrent, BT.Peer peer, bool connected)
	{
		if (connected)
		{
			this.peerListView.Invoke( new AddNewPeerDelegate( this.peerListView.Items.Add ), new object[] { new PeerListViewItem( peer ) } );
//			this.peerListView.Items.Add(new PeerListViewItem(peer));
		}
		else
		{
			LogDebugMessage( "Peer disconnected from mainform.cs: " + peer.ToString() );
			this.peerListView.Invoke( new RemovePeerDelegate( RemovePeer ), peer );
		}
	}
Beispiel #10
0
		private void peer_HeIsInterestedChange(Peer peer, bool newStatus)
		{
			lock ( this.interestedPeers.SyncRoot )
			{
				if (newStatus)
				{
					if (!this.interestedPeers.Contains(peer))
						this.interestedPeers.Add(peer);
				}
				else
				{
					if (this.interestedPeers.Contains(peer))
						this.interestedPeers.Remove(peer);
				}
			}
		}
Beispiel #11
0
		private void peer_Disconnected(Peer peer)
		{
			lock ( this.mPeers )
			{
				this.mPeers.Remove( peer );
			}

			peer.Disconnected -= new PeerDisconnectedCallback(peer_Disconnected);

			if (this.PeerConnected != null)
				this.PeerConnected(this, peer, false);
		}
		public void SendPieceRequestToPeer( Peer peer, int pieceId, int begin, int length )
		{
			if ( !this.mDownloadingPeers.Contains( peer ) )
				this.mDownloadingPeers.Add( peer );
			if ( this.mPeersAvailableToDownload.Contains( peer ) )
				this.mPeersAvailableToDownload.Remove( peer );

			if ( !mCurrentlyDownloadingPieces.ContainsKey( pieceId ) )
			{
				Piece piece = new Piece( this.mTorrent.Metainfo, pieceId );
				mCurrentlyDownloadingPieces.Add( pieceId, piece );
			}

			peer.SendPieceRequest( pieceId, begin, length );
		}
Beispiel #13
0
 private void peer_IAmChokingChange(BitTorrent.Peer peer, bool newStatus)
 {
     SetSubItemTextAsync(4, newStatus ? "Y" : "N");
 }
		private void peer_BitfieldChange( Peer peer, int pieceId )
		{
			if ( pieceId != -1 )
			{
				// new piece came in
				this.mPeerPieceList[ pieceId ]++;
			}
			else
			{
				// whole bitfield has changed, remove the old peer information then add the new one
				RemovePeerBitfieldFromPieceCount( peer );
				AddPeerBitfieldToPieceCount( peer );
			}
		}
		public bool IsPeerDownloading( Peer peer )
		{
			return mDownloadingPeers.Contains( peer );
		}
		private void peer_Disconnected( Peer peer )
		{
			peer.BitfieldChange -= new PeerBitfieldChangeCallback( peer_BitfieldChange );
			peer.Disconnected -= new PeerDisconnectedCallback( peer_Disconnected );
			peer.HeIsChokingChange -= new PeerStatusChangeCallback( peer_HeIsChokingChange );
			peer.PieceIncoming -= new PeerPieceCallback( peer_PieceIncoming );

			if ( this.mDownloadingPeers.Contains( peer ) )
				this.mDownloadingPeers.Remove( peer );

			if ( this.mPeersAvailableToDownload.Contains( peer ) )
				this.mPeersAvailableToDownload.Remove( peer );

			foreach ( DownloadStrategy strategy in mStrategies )
			{
				strategy.peer_Disconnected( strategy == mActiveStrategy, peer );
			}
		}
Beispiel #17
0
 public static void SetInterestedOnPeerIfNecessary(DownloadStrategyManager manager, Peer peer)
 {
     SetInterestedOnPeerIfNecessary(manager, peer, -1);
 }
Beispiel #18
0
 public void peer_Disconnected(bool isActive, Peer peer)
 {
 }
		private void RemovePeerBitfieldFromPieceCount( Peer peer )
		{
			// remove from peer-piece index
			if ( peer.BitField != null )
			{
				for ( int i = 0; i < this.mPeerPieceList.Length; ++i )
				{
					if ( peer.BitField.Get( i ) )
						this.mPeerPieceList[ i ]--;
				}
			}
		}
		private void peer_HeIsChokingChange( Peer peer, bool choked )
		{
			// remove from pieces downloading if downloading
			if ( choked )
			{
				if ( this.mDownloadingPeers.Contains( peer ) )
					this.mDownloadingPeers.Remove( peer );
				if ( this.mPeersAvailableToDownload.Contains( peer ) )
					this.mPeersAvailableToDownload.Remove( peer );
			}
			else
			{
				if ( !this.mPeersAvailableToDownload.Contains( peer ) )
					this.mPeersAvailableToDownload.Add( peer );
			}

			foreach ( DownloadStrategy strategy in mStrategies )
			{
				strategy.peer_HeIsChokingChange( strategy == mActiveStrategy, peer, choked );
			}
		}
		public void HookPeerEvents( Peer peer )
		{
			peer.BitfieldChange += new PeerBitfieldChangeCallback( peer_BitfieldChange );
			peer.Disconnected += new PeerDisconnectedCallback( peer_Disconnected );
		}
		public void SendPieceCancelToPeer( Peer peer, int pieceId, int begin, int length )
		{
			if ( this.mDownloadingPeers.Contains( peer ) )
				this.mDownloadingPeers.Remove( peer );
			if ( !this.mPeersAvailableToDownload.Contains( peer ) )
				this.mPeersAvailableToDownload.Add( peer );

			peer.SendPieceCancel( pieceId, begin, length );
		}
Beispiel #23
0
		private void peer_PieceRequest(Peer peer, int pieceId, int begin, int length, byte[] data)
		{
			// if i am choking, ignore
			if (!peer.AmIChoking)
			{
				this.incomingRequests.Add( new PieceRequest(peer, pieceId, begin, length) );

				// TODO: check if we can serve the request
				// TODO: make more efficient than loading the whole piece each time
				IO.MemoryStream stream = new IO.MemoryStream(this.infofile.GetPieceLength(pieceId));
				this.downloadFile.LoadFromFile(pieceId, stream);
				stream.Seek(begin, IO.SeekOrigin.Begin);
				peer.SendPiece(pieceId, begin, length, stream, new PeerPieceCallback(PieceSendFinished));
			}
		}
Beispiel #24
0
		private void PieceSendFinished(Peer peer, int pieceId, int begin, int length, byte[] data)
		{
			foreach (PieceRequest pieceRequest in this.incomingRequests)
			{
				if (pieceRequest.Peer.Equals(peer) && pieceRequest.PieceId == pieceId && pieceRequest.Begin == begin && pieceRequest.Length == length)
				{
					this.incomingRequests.Remove(pieceRequest);
					break;
				}
			}

			if (this.PieceSectionFinished != null)
				this.PieceSectionFinished(pieceId, begin, length);
		}
Beispiel #25
0
 public static void SetInterestedOnPeerIfNecessary(DownloadStrategyManager manager, Peer peer, int pieceId)
 {
     if (pieceId >= 0)
     {
         // new piece came in
         if (!manager.Torrent.DownloadFile.Bitfield.Get(pieceId))
         {
             peer.AmIInterested = true;
         }
     }
     else
     {
         // whole bitfield has changed
         if (DoesPeerHavePieceWeDont(manager, peer))
         {
             peer.AmIInterested = true;
         }
     }
 }
		public void HookPeerEvents( Peer peer )
		{
			peer.BitfieldChange += new PeerBitfieldChangeCallback( peer_BitfieldChange );
			peer.Disconnected += new PeerDisconnectedCallback( peer_Disconnected );
			peer.HeIsChokingChange += new PeerStatusChangeCallback( peer_HeIsChokingChange );
			peer.PieceIncoming += new PeerPieceCallback( peer_PieceIncoming );

			mPieceCounter.HookPeerEvents( peer );
		}
		private void peer_Disconnected( Peer peer )
		{
			RemovePeerBitfieldFromPieceCount( peer );

			peer.BitfieldChange -= new PeerBitfieldChangeCallback( peer_BitfieldChange );
			peer.Disconnected -= new PeerDisconnectedCallback( peer_Disconnected );
		}
Beispiel #28
0
		private void peer_PieceCancel(Peer peer, int pieceId, int begin, int length, byte[] data)
		{
			for (int i=0; i<this.incomingRequests.Count; ++i)
			{
				PieceRequest request = (PieceRequest)this.incomingRequests[i];
				if (request.Peer.Equals(peer) && request.PieceId == pieceId && request.Begin == begin && request.Length == length)
				{
					this.incomingRequests.RemoveAt(i);
					break;
				}
			}
		}
Beispiel #29
0
		private void peer_Disconnected(Peer peer)
		{
			if (this.peerList.Contains(peer))
				this.peerList.Remove(peer);
			if (this.socketList.Contains(peer.Socket))
				this.socketList.Remove(peer.Socket);
		}
        private void AddPeer(Peer peer)
        {
            peer.BlockRequested += HandleBlockRequested;
            peer.BlockCancelled += HandleBlockCancelled;
            peer.BlockReceived += HandleBlockReceived;
            peer.Disconnected += HandlePeerDisconnected;
            peer.StateChanged += HandlePeerStateChanged;

            peer.Connect();

            if (!Peers.TryAdd(peer.Key, peer))
                peer.Disconnect();
        }
		private void peer_BitfieldChange( Peer peer, int pieceId )
		{
			foreach ( DownloadStrategy strategy in mStrategies )
			{
				strategy.peer_BitfieldChange( strategy == mActiveStrategy, peer, pieceId );
			}
		}
Beispiel #32
0
 private void peer_BitfieldChange(BitTorrent.Peer peer, int pieceId)
 {
     SetSubItemTextAsync(1, peer.IsSeed ? "Seed" : "Leech");
     SetSubItemTextAsync(8, peer.BitField.PercentageTrue.ToString("0.0"));
 }
Beispiel #33
0
		// Called by the Session class for peers connecting through the server and in StartPeerConnectionThread() method for peers
		// connected to directly
		private void AddPeerPrivate(Sockets.Socket socket, Sockets.NetworkStream netStream, PeerInformation peerInformation)
		{
			try
			{
				if ( Config.ActiveConfig.MaxPeersConnected < this.mPeers.Count )
				{
					socket.Close();
					return;
				}

				bool connect = true;

				if (Config.ActiveConfig.OnlyOneConnectionFromEachIP)
				{
					foreach ( Peer connectedPeer in this.mPeers )
					{
						if (connectedPeer.Information.IP.Equals(peerInformation.IP))
						{
							connect = false;
							break;
						}
					}
				}

				if (!connect)
				{
					socket.Close();
					return;
				}

				if (!this.downloadFile.Bitfield.AllFalse)
					PeerProtocol.SendBitfieldMessage(netStream, this.downloadFile);

				Peer peer = new Peer(this.infofile, this.downloadFile, socket, netStream, peerInformation);
				peer.Disconnected += new PeerDisconnectedCallback(peer_Disconnected);

				Config.LogDebugMessage("Connection accepted from: " + peer.ToString());

				// add to download and upload manager
				this.mDownloadStrategy.HookPeerEvents(peer);
				this.uploadManager.AddPeer(peer);
				this.peerManager.AddPeer(peer);

				if (this.PeerConnected != null)
					this.PeerConnected(this, peer, true);

				this.mPeers.Add( peer );
			}
			catch ( System.Exception e )
			{
				Config.LogException( e );
			}
		}
Beispiel #34
0
 private void peer_IAmInterestedChange(BitTorrent.Peer peer, bool newStatus)
 {
     SetSubItemTextAsync(5, newStatus ? "Y" : "N");
 }
		private void peer_PieceIncoming( Peer peer, int pieceId, int begin, int length, byte[] data )
		{
			// save the piece data then inform the strategies
			if ( !mCurrentlyDownloadingPieces.ContainsKey( pieceId ) )
			{
				Config.LogDebugMessage( "INVALID PIECE INCOMING: " + pieceId );
				return;
			}

			Piece piece = mCurrentlyDownloadingPieces[ pieceId ];
			piece.Write( data, 0, length, begin );

			if ( !peer.HeIsChoking && !this.mPeersAvailableToDownload.Contains( peer ) )
				this.mPeersAvailableToDownload.Add( peer );

			if ( mDownloadingPeers.Contains( peer ) )
				mDownloadingPeers.Remove( peer );

			if ( piece.FullyDownloaded )
			{
				IO.MemoryStream pstream = new IO.MemoryStream( this.mTorrent.Metainfo.GetPieceLength( pieceId ) );
				piece.Read( pstream, this.mTorrent.Metainfo.GetPieceLength( pieceId ), 0 );
				pstream.Seek( 0, IO.SeekOrigin.Begin );

				if ( mCurrentlyDownloadingPieces.ContainsKey( pieceId ) )
					mCurrentlyDownloadingPieces.Remove( pieceId );

				if ( this.mTorrent.DownloadFile.SaveToFile( pieceId, pstream ) )
				{
					// Piece successfully downloaded, inform strategies and update other peers
					foreach ( DownloadStrategy strategy in mStrategies )
					{
						strategy.peer_PieceFinished( strategy == mActiveStrategy, peer, pieceId );
					}

					if ( this.mTorrent.DownloadFile.Bitfield.AllTrue )
					{
						foreach ( DownloadStrategy strategy in mStrategies )
						{
							strategy.OnDownloadFinished( strategy == mActiveStrategy );
						}
					}

					// piece just finished, announce to everyone we have it
					List<Peer> disconnectPeers = new List<Peer>();
					foreach ( Peer ipeer in this.mTorrent.Peers )
					{
						try
						{
							ipeer.SendHaveMessage( pieceId );
						}
						catch ( System.Exception e )
						{
							Config.LogException( e );
							disconnectPeers.Add( ipeer );
						}
					}
					foreach ( Peer ipeer in disconnectPeers )
					{
						ipeer.Disconnect();
					}

					this.mBytesDownloaded += this.mTorrent.Metainfo.GetPieceLength( pieceId );
				}
				else
				{ // bad data has been sent by a peer TODO: ban?
				}
			}

			foreach ( DownloadStrategy strategy in mStrategies )
			{
				strategy.peer_PieceSectionFinished( strategy == mActiveStrategy, peer, pieceId, begin, length );
			}
		}