SaveLoad Fast Resume

Version 1 (Alan McGovern, 04/13/2009 01:10 PM) → Version 2/4 (Alan McGovern, 04/16/2009 10:28 PM)


h1. Save/Load Fast Resume

<pre><code>
public void SaveFastResume (List <TorrentManager> managers)
{
// Store the fast resume for each torrent in a list,
// then serialise the list to the disk.
BEncodedList list = new BEncodedList ();
foreach (TorrentManager manager in managers) {

// Get the fast resume data for the torrent
FastResume data = manager.SaveFastResume ();

// Encode the FastResume data to a BEncodedDictionary.
BEncodedDictionary fastResume = data.Encode ();

// Add the FastResume dictionary to the main dictionary using
// the torrents infohash as the key
list.Add (data);
}

// Write all the fast resume data to disk
File.WriteAllBytes (fastResumePath, list.Encode ());
}

public void LoadFastResume (List <TorrentManager> managers)
{
// Read the main dictionary from disk and iterate through
// all the fast resume items
BEncodedList list dictionary = (BEncodedList) BEncodedValue.Decode (File.ReadAllBytes (fastResumePath));
foreach (BEncodedDictionary fastResume in list) dictionary) {

// Decode the FastResume data from the BEncodedDictionary
FastResume data = new FastResume (fastResume);

// Find the torrentmanager that the fastresume belongs to
// and then load it
foreach (TorrentManager manager in managers)
if (manager.InfoHash == data.Infohash)
manager.LoadFastResume (data);
}
}
</code></pre>