Skip to content

Commit

Permalink
Reorganising methods and removing unnecessary references
Browse files Browse the repository at this point in the history
  • Loading branch information
seanoflynn committed May 27, 2016
1 parent e60d2c5 commit f022d1b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
64 changes: 32 additions & 32 deletions BitTorrent/Client.cs
Expand Up @@ -289,6 +289,26 @@ private void ProcessPeers()

#region Uploads

private void HandleBlockRequested(object sender, DataRequest block)
{
OutgoingBlocks.Enqueue(block);

ProcessUploads();
}

private void HandleBlockCancelled(object sender, DataRequest block)
{
foreach (var item in OutgoingBlocks)
{
if (item.Peer != block.Peer || item.Piece != block.Piece || item.Begin != block.Begin || item.Length != block.Length)
continue;

item.IsCancelled = true;
}

ProcessUploads();
}

private void ProcessUploads()
{
if (Interlocked.Exchange(ref isProcessUploads, 1) == 1)
Expand All @@ -315,30 +335,28 @@ private void ProcessUploads()
Interlocked.Exchange(ref isProcessUploads, 0);
}

private void HandleBlockRequested(object sender, DataRequest block)
{
OutgoingBlocks.Enqueue(block);
#endregion

ProcessUploads();
}
#region Downloads

private void HandleBlockCancelled(object sender, DataRequest block)
private void HandleBlockReceived(object sender, DataPackage args)
{
foreach (var item in OutgoingBlocks)
IncomingBlocks.Enqueue(args);

args.Peer.IsBlockRequested[args.Piece][args.Block] = false;

foreach(var peer in Peers)
{
if (item.Peer != block.Peer || item.Piece != block.Piece || item.Begin != block.Begin || item.Length != block.Length)
if (!peer.Value.IsBlockRequested[args.Piece][args.Block])
continue;

item.IsCancelled = true;
peer.Value.SendCancel(args.Piece, args.Block * Torrent.BlockSize, Torrent.BlockSize);
peer.Value.IsBlockRequested[args.Piece][args.Block] = false;
}

ProcessUploads();
ProcessDownloads();
}

#endregion

#region Downloads

private void ProcessDownloads()
{
if (Interlocked.Exchange(ref isProcessDownloads, 1) == 1)
Expand Down Expand Up @@ -394,24 +412,6 @@ private void ProcessDownloads()
Interlocked.Exchange(ref isProcessDownloads, 0);
}

private void HandleBlockReceived(object sender, DataPackage args)
{
IncomingBlocks.Enqueue(args);

args.Peer.IsBlockRequested[args.Piece][args.Block] = false;

foreach(var peer in Peers)
{
if (!peer.Value.IsBlockRequested[args.Piece][args.Block])
continue;

peer.Value.SendCancel(args.Piece, args.Block * Torrent.BlockSize, Torrent.BlockSize);
peer.Value.IsBlockRequested[args.Piece][args.Block] = false;
}

ProcessDownloads();
}

#endregion

#region Ranking
Expand Down
8 changes: 4 additions & 4 deletions BitTorrent/Peer.cs
Expand Up @@ -228,6 +228,10 @@ private int GetMessageLength(List<byte> data)
return EndianBitConverter.Big.ToInt32(data.ToArray(), 0) + 4;
}

#endregion

#region Incoming Messages

private MessageType GetMessageType(byte[] bytes)
{
if (!IsHandshakeReceived)
Expand Down Expand Up @@ -348,10 +352,6 @@ private void HandleMessage(byte[] bytes)
Disconnect();
}

#endregion

#region Incoming Messages

private void HandleHandshake(byte[] hash, string id)
{
Log.WriteLine(this, "<- handshake");
Expand Down
4 changes: 0 additions & 4 deletions Client/Program.cs
@@ -1,9 +1,5 @@
using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using Mono.Unix;
using Mono.Unix.Native;
using BitTorrent;
Expand Down

0 comments on commit f022d1b

Please sign in to comment.