SaveLoad Fast Resume
Version 2 (Alan McGovern, 04/16/2009 10:28 PM)
| 1 | 1 | h1. Save/Load Fast Resume |
|
|---|---|---|---|
| 2 | 1 | ||
| 3 | 1 | <pre><code> |
|
| 4 | 1 | public void SaveFastResume (List <TorrentManager> managers) |
|
| 5 | 1 | { |
|
| 6 | 1 | // Store the fast resume for each torrent in a list, |
|
| 7 | 1 | // then serialise the list to the disk. |
|
| 8 | 1 | BEncodedList list = new BEncodedList (); |
|
| 9 | 1 | foreach (TorrentManager manager in managers) { |
|
| 10 | 1 | ||
| 11 | 1 | // Get the fast resume data for the torrent |
|
| 12 | 1 | FastResume data = manager.SaveFastResume (); |
|
| 13 | 1 | ||
| 14 | 1 | // Encode the FastResume data to a BEncodedDictionary. |
|
| 15 | 1 | BEncodedDictionary fastResume = data.Encode (); |
|
| 16 | 1 | ||
| 17 | 1 | // Add the FastResume dictionary to the main dictionary using |
|
| 18 | 1 | // the torrents infohash as the key |
|
| 19 | 1 | list.Add (data); |
|
| 20 | 1 | } |
|
| 21 | 1 | ||
| 22 | 1 | // Write all the fast resume data to disk |
|
| 23 | 1 | File.WriteAllBytes (fastResumePath, list.Encode ()); |
|
| 24 | 1 | } |
|
| 25 | 1 | ||
| 26 | 1 | public void LoadFastResume (List <TorrentManager> managers) |
|
| 27 | 1 | { |
|
| 28 | 1 | // Read the main dictionary from disk and iterate through |
|
| 29 | 1 | // all the fast resume items |
|
| 30 | 2 | Alan McGovern | BEncodedList list = (BEncodedList) BEncodedValue.Decode (File.ReadAllBytes (fastResumePath)); |
| 31 | 2 | Alan McGovern | foreach (BEncodedDictionary fastResume in list) { |
| 32 | 1 | ||
| 33 | 1 | // Decode the FastResume data from the BEncodedDictionary |
|
| 34 | 1 | FastResume data = new FastResume (fastResume); |
|
| 35 | 1 | ||
| 36 | 1 | // Find the torrentmanager that the fastresume belongs to |
|
| 37 | 1 | // and then load it |
|
| 38 | 1 | foreach (TorrentManager manager in managers) |
|
| 39 | 1 | if (manager.InfoHash == data.Infohash) |
|
| 40 | 1 | manager.LoadFastResume (data); |
|
| 41 | 1 | } |
|
| 42 | 1 | } |
|
| 43 | 1 | </code></pre> |