Skip to content

Commit

Permalink
slightly expanded demo app that connects to a peer
Browse files Browse the repository at this point in the history
  • Loading branch information
bizzehdee committed Oct 15, 2023
1 parent 279a2fe commit 0616822
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<StartupObject>Demo.Program</StartupObject>
</PropertyGroup>

Expand All @@ -18,6 +18,9 @@
<None Update="TestTorrents\ubuntu-20.04.3-desktop-amd64.iso.torrent">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestTorrents\ubuntu-23.10-live-server-amd64.iso.torrent">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
135 changes: 130 additions & 5 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Collections.Generic;
using System.Net;
using bzTorrent.ProtocolExtensions;

namespace Demo
{
Expand All @@ -20,6 +21,7 @@ class Program
static string downloadDirectory;
static IMetadata downloadMetadata;
static readonly List<IPEndPoint> knownPeers = new();
static bool choked = false;

static void Main(string[] args)
{
Expand Down Expand Up @@ -120,7 +122,7 @@ static void Main(string[] args)
}
}

var socket = new PeerWireuTPConnection
var socket = new PeerWireTCPConnection
{
Timeout = 5
};
Expand All @@ -130,29 +132,152 @@ static void Main(string[] args)
KeepConnectionAlive = true
};

var fastExt = new FastExtensions();
fastExt.AllowedFast += FastExt_AllowedFast;
fastExt.HaveAll += FastExt_HaveAll;
fastExt.HaveNone += FastExt_HaveNone;
fastExt.SuggestPiece += FastExt_SuggestPiece;

client.RegisterBTExtension(fastExt);

client.NoData += Client_NoData;
client.BitField += Client_BitField;
client.Cancel += Client_Cancel;
client.Piece += Client_Piece;
client.Choke += Client_Choke;
client.UnChoke += Client_UnChoke;
client.DroppedConnection += Client_DroppedConnection;
client.HandshakeComplete += Client_HandshakeComplete;
client.Have += Client_Have;
client.Interested += Client_Interested;
client.NotInterested += Client_NotInterested;
client.Request += Client_Request;

foreach (var peer in knownPeers)
{
try
{
Console.WriteLine("Attempting to connect to utp://{0}:{1}", peer.Address.ToString(), peer.Port);
Console.WriteLine("Attempting to connect to {0}:{1}", peer.Address.ToString(), peer.Port);
client.Connect(peer);
}
catch (Exception ex)
{
Console.WriteLine("Failed to connect: {0}", ex.Message);
continue;
//continue;
}

Console.WriteLine("Connected");
client.Handshake(downloadMetadata.HashString, peerId);

Thread.Sleep(200);
int x = 0, i=0;
while (client.Process())
{
Thread.Sleep(100);
if (choked == false)
{

}
else
{

}

if(x++ > 10)
{
x = 0;
client.SendKeepAlive();
}

Thread.Sleep(200);
}
}
}

private static void FastExt_SuggestPiece(IPeerWireClient arg1, int arg2)
{
Console.WriteLine("SuggestPiece");
}

private static void FastExt_HaveNone(IPeerWireClient obj)
{
Console.WriteLine("HaveNone");
}

private static void FastExt_HaveAll(IPeerWireClient obj)
{
Console.WriteLine("HaveAll");
//obj.SendRequest(1, 0, (uint)downloadMetadata.PieceSize);
}

private static void FastExt_AllowedFast(IPeerWireClient arg1, int arg2)
{
Console.WriteLine("AllowedFast");
}

private static void Client_Request(IPeerWireClient arg1, int arg2, int arg3, int arg4)
{
Console.WriteLine("Request");
}

private static void Client_NotInterested(IPeerWireClient obj)
{
Console.WriteLine("NotInterested");
}

private static void Client_Interested(IPeerWireClient obj)
{
Console.WriteLine("Interested");
}

private static void Client_Have(IPeerWireClient arg1, int arg2)
{
Console.WriteLine("Have");
}

private static void Client_HandshakeComplete(IPeerWireClient obj)
{
Console.WriteLine("HandshakeComplete");
}

private static void Client_DroppedConnection(IPeerWireClient obj)
{
Console.WriteLine("DroppedConnection");
}

private static void Client_UnChoke(IPeerWireClient obj)
{
Console.WriteLine("UnChoke");
choked = false;
}

private static void Client_Choke(IPeerWireClient obj)
{
Console.WriteLine("Choke");
choked = true;
}

private static void Client_Piece(IPeerWireClient arg1, int index, int start, byte[] buffer)
{
Console.WriteLine("Piece");

}

private static void Client_Cancel(IPeerWireClient arg1, int arg2, int arg3, int arg4)
{
Console.WriteLine("Cancel");

}

private static void Client_BitField(IPeerWireClient arg1, int bitFieldLength, bool[] bitField)
{
Console.WriteLine("BitField");

}

private static void Client_NoData(IPeerWireClient obj)
{
Console.WriteLine("NoData");
}

private static void GeneratePeerId()
{
var sb = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion Demo/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Demo": {
"commandName": "Project",
"commandLineArgs": "-file \"TestTorrents/ubuntu-20.04.3-desktop-amd64.iso.torrent\" -output ."
"commandLineArgs": "-file \"TestTorrents/ubuntu-23.10-live-server-amd64.iso.torrent\" -output ."
}
}
}
Binary file not shown.

0 comments on commit 0616822

Please sign in to comment.