/// <summary> /// Constructs manager from XmlElement /// </summary> /// <param name="xmlElement"></param> /// <returns></returns> public static TorrentManager FromXml(XmlElement xmlElement) { TorrentManager torrentManager = new TorrentManager(); foreach (XmlElement e in xmlElement["torrents"]) { Torrent t = Torrent.CreateFromXml(e); torrentManager.Add(t); //later automatic start } torrentManager.ForceIp = bool.Parse(xmlElement["forceip"].InnerText); ConnectInfo loadedConnectInfo = ConnectInfo.ParseXml(xmlElement[ConnectInfo.XmlName]); if (torrentManager.ForceIp) { torrentManager.MyConnectInfo = loadedConnectInfo; } else { torrentManager.MyConnectInfo = new ConnectInfo(GetLocalIpAddress(), loadedConnectInfo.Port); } return(torrentManager); }
/// <summary> /// Allows user to select share file and select location to download torrent and starts download /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ToolStripButtonConnect_Click(object sender, EventArgs e) { //open file dialog to select if (openFileDialogTorrent.ShowDialog() != DialogResult.OK) { return; } XmlDocument document = new XmlDocument(); Torrent torrent; if (saveFileDialogFile.ShowDialog() != DialogResult.OK) { return; } try { //loads selected share file end parses torrent and information about remote host document.Load(openFileDialogTorrent.FileName); XmlElement headerElement = document["share"][ConnectInfo.XmlName]; XmlElement torrentElement = document["share"][Torrent.XmlName]; torrent = Torrent.CreateFromXmlShare(torrentElement, saveFileDialogFile.FileName); ConnectInfo connectInfo = ConnectInfo.ParseXml(headerElement); AddRow(torrent); manager.Add(torrent); try { await manager.ConnectTorrentAsync(torrent, connectInfo); await torrent.DownloadAsync(); } catch (SocketException) { Logger.WriteLine("Unable to connect to specified host."); } catch (AggregateException ex) { Logger.WriteLine("Aggregate exception caught."); Logger.WriteLine(ex.InnerExceptions[0].Message); } catch (Exception ex) { Logger.WriteLine(ex.Message); } } catch (IOException ex) { Logger.WriteLine("Couldn't open file. " + ex); MessageBox.Show("Couldn't open file. " + ex); } catch (XmlException ex) { Logger.WriteLine("File has wrong format. " + ex); MessageBox.Show("File has wrong format. " + ex); } catch (NullReferenceException ex) { Logger.WriteLine("File has wrong format. " + ex); MessageBox.Show("File has wrong format. " + ex); } }