Skip to content

Commit

Permalink
DHT: minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Serg-Norseman committed Apr 19, 2018
1 parent c4d758e commit 38fc01c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions DHTSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ static int Main(string[] args)
Console.ResetColor();
};

Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e) {
e.Cancel = true;
dhtClient.StopSearch();
};

dhtClient.Run();
dhtClient.JoinNetwork();
dhtClient.SearchNodes(snkInfoHash);
Expand Down
26 changes: 18 additions & 8 deletions GKNetCore/DHT/DHTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public sealed class DHTClient

private byte[] fLocalID;
private byte[] fSearchInfoHash;
private bool fSearchRunned;

private readonly UdpClient fClient;
private readonly IPEndPoint fDefaultIP;
Expand All @@ -40,6 +41,7 @@ public DHTClient(IPAddress addr, int port, ILogger logger)
fLogger = logger;
fParser = new BencodeParser();
fRoutingTable = new DHTRoutingTable(KTableSize);
fSearchRunned = false;
fTransactions = new Dictionary<int, DHTMessage>();

const long IOC_IN = 0x80000000;
Expand Down Expand Up @@ -98,14 +100,22 @@ public void JoinNetwork()
public void SearchNodes(byte[] searchInfoHash)
{
fSearchInfoHash = searchInfoHash;
fSearchRunned = true;

while (true) {
var nodes = fRoutingTable.FindNodes(fSearchInfoHash);
foreach (var node in nodes) {
SendGetPeersQuery(node.EndPoint, fSearchInfoHash);
new Thread(() => {
while (fSearchRunned) {
var nodes = fRoutingTable.FindNodes(fSearchInfoHash);
foreach (var node in nodes) {
SendGetPeersQuery(node.EndPoint, fSearchInfoHash);
}
Thread.Sleep(1000);
}
Thread.Sleep(1000);
}
}).Start();
}

public void StopSearch()
{
fSearchRunned = false;
}

#region Receive messages and data
Expand Down Expand Up @@ -267,7 +277,7 @@ private bool ProcessValuesStr(IPEndPoint ipinfo, BList valuesList)
Console.ForegroundColor = ConsoleColor.DarkCyan;

WriteLog("send ping " + values[0].ToString(), true);
SendPingQuery(values[0], false);
SendPingQuery(values[0], true);

var newaddr = new IPEndPoint(values[0].Address, fLocalIP.Port);
WriteLog("send ping " + newaddr.ToString(), true);
Expand Down Expand Up @@ -306,7 +316,7 @@ private void OnRecvAnnouncePeerQuery(IPEndPoint ipinfo, BDictionary data)
var id = args.Get<BString>("id");
var infoHash = args.Get<BString>("info_hash");
var impliedPort = args.Get<BNumber>("implied_port");
int port = (impliedPort != null && impliedPort.Value == 1) ? PublicDHTPort : (int)args.Get<BNumber>("port").Value;
int port = (impliedPort != null && impliedPort.Value == 1) ? ipinfo.Port : (int)args.Get<BNumber>("port").Value;

Console.ForegroundColor = ConsoleColor.DarkYellow;
WriteLog("receive `announce_peer` query " + ipinfo.ToString());
Expand Down

0 comments on commit 38fc01c

Please sign in to comment.