Skip to content

Instantly share code, notes, and snippets.

@patriksvensson
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save patriksvensson/3b0a41459d5eebb1f6be to your computer and use it in GitHub Desktop.

Select an option

Save patriksvensson/3b0a41459d5eebb1f6be to your computer and use it in GitHub Desktop.
// Ragnar is available on NuGet: http://www.nuget.org/packages/ragnar
// The source code is available on GitHub: https://github.com/libragnar/ragnar
using System;
using System.IO;
using System.Linq;
using System.Threading;
namespace Ragnar.SampleClient
{
public class Program
{
public static void Main(string[] args)
{
Console.Title = "Ragnar demo";
Console.Clear();
Console.CursorVisible = false;
// Create a new session.
using (var session = new Session())
{
// Create the torrent settings that will keep
// all there is to know about the torrent we will be downloading.
var torrentSettings = new AddTorrentParams
{
SavePath = "C:\\Downloads",
TorrentInfo = new TorrentInfo(args[0])
};
// Load session state.
// The session state contains DHT state and such,
// which will make the application faster to start next time.
LoadSessionState(session);
// Load torrent state.
// The torrent state keeps track of what pieces that
// are already downloaded. We do not need to do this,
// but it will keep Ragnar from having to calculate hashes.
LoadTorrentState(torrentSettings);
// Start the session by listening on ports
// in the range 6881-6889.
session.ListenOn(6881, 6889);
// Get the torrent handle by adding the torrent to the session.
var torrentHandle = session.AddTorrent(torrentSettings);
// Wait for Ctrl+C to be pressed.
WaitForKeyPress(torrentHandle);
// Pause the session.
session.Pause();
// Save torrent state.
SaveTorrentState(torrentHandle, session);
// Save session state
SaveSessionState(session);
}
}
private static void WaitForKeyPress(TorrentHandle torrentHandle)
{
var waitHandle = new ManualResetEvent(false);
Console.CancelKeyPress += (sender, args) => waitHandle.Set();
long tick = 0;
while (!waitHandle.WaitOne(0))
{
// Query the status until we're not downloading anymore.
var status = torrentHandle.QueryStatus();
if (status.IsSeeding)
{
break;
}
// Output some information to the console.
Console.SetCursorPosition(0, 1);
Console.Write(" ");
Console.SetCursorPosition(0, 1);
Console.Write("Status: {0} {1}", status.State.ToString(), tick%2 == 0 ? "-" : "|");
Console.SetCursorPosition(0, 2);
Console.WriteLine("Progress: {0}%", status.Progress*100);
// Wait 300 ms.
waitHandle.WaitOne(300);
tick++;
}
}
private static void LoadSessionState(Session session)
{
if (File.Exists(".ses_state"))
{
Console.WriteLine("Loading state");
session.LoadState(File.ReadAllBytes(".ses_state"));
}
}
private static void SaveSessionState(Session session)
{
Console.WriteLine("Saving state.");
File.WriteAllBytes(".ses_state", session.SaveState());
}
private static void LoadTorrentState(AddTorrentParams p)
{
if (File.Exists(Path.Combine(".resume", p.TorrentInfo.Name + ".fastresume")))
{
Console.WriteLine("Loading fast resume data");
p.ResumeData = File.ReadAllBytes(Path.Combine(".resume", p.TorrentInfo.Name + ".fastresume"));
}
}
private static void SaveTorrentState(TorrentHandle torrentHandle, Session session)
{
if (torrentHandle.NeedSaveResumeData())
{
Console.WriteLine("Saving resume data");
torrentHandle.SaveResumeData();
var savedFastResume = false;
while (!savedFastResume)
{
var alerts = session.Alerts.PopAll();
if (alerts == null || !alerts.Any()) continue;
foreach (var alert in alerts)
{
if (alert is SaveResumeDataAlert)
{
var saveResumeAlert = (SaveResumeDataAlert) alert;
if (!Directory.Exists(".resume")) Directory.CreateDirectory(".resume");
var status = saveResumeAlert.Handle.QueryStatus();
File.WriteAllBytes(
Path.Combine(".resume", status.Name + ".fastresume"),
saveResumeAlert.ResumeData);
Console.WriteLine("Resume data saved");
savedFastResume = true;
break;
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment