/// <summary>Connects the specified ip.</summary> /// <param name="ip">The ip.</param> /// <param name="port">The port.</param> /// <exception cref="System.TimeoutException">if connection took longer then 15s to establish</exception> public void Connect(string ip, int port) { TimeOutTimer timer = new TimeOutTimer(5); string error = string.Empty; timer.Start(); while (!this.client.IsConnected() && !timer.TimePassed) { try { this.client.Connect(ip, port); } catch (SocketException socketException) { Console.WriteLine(socketException); error = "Remote socket unavailable"; continue; } catch (ArgumentOutOfRangeException argumentOutOfRangeException) { Console.WriteLine(argumentOutOfRangeException); error = "Port number out of range"; continue; } catch (Exception e) { Console.WriteLine(e); error = "General Error"; } } if (timer.TimePassed && !this.client.IsConnected()) { throw new TimeoutException(error); } }
private void Tcp_Connected(object sender, string result) { if (result == "OK") { SendReqest(ProtocolOfExchange.CheckConnection, null, m_tcpModule); TimeOutTimer.Start(); } if (result == "ERR") { ConnectToServer(); } }
private void M_TcpClient_Connected(object sender, EventArgs e) { SendReqest(ProtocolOfExchange.CheckConnection, new NetworkTransferObjects()); TimeOutTimer.Start(); }
// load list of torrents from active session private async Task RefreshTorrentList() { DialogResult dialogResult; bool doContinue = true; // warn user current selection will be lost if (TorrentFileListTreeView.TotalFilesSelected != 0) { dialogResult = MessageBox.Show("Refreshing the torrent list will cause the current file selection to be lost.\r\n\r\nDo you want to continue?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.No) { doContinue = false; } } if (doContinue) { // update UI state on continue PagesTabControl.SelectedIndex = 0; SearchTorrentListTextBox.Clear(); TorrentFileListTreeView.TotalFilesSelected = 0; TorrentFileListTreeView.TotalFiles = 0; SelectedTorrentLabel.Text = "Selected torrent: None selected"; SelectedFileCountLabel.Text = "Selected files: 0 (total file count not yet known)"; TorrentTabPage.Text = "Torrents"; FilesTabPage.Text = "Files"; TorrentFileListTreeView.Nodes.Clear(); TorrentsListView.Items.Clear(); FileNamesOldNewListView.Items.Clear(); Globals.TorrentsInfo.Clear(); Globals.SelectedTorrent = null; Globals.SelectedTorrentFiles.Clear(); RenameButton.Enabled = false; RefreshTorrentListButton.Enabled = false; RefreshTorrentListButton.Text = "Waiting (10)"; TimeOutTimer.Start(); ToggleLoadingPanels(true); // retrieve torrents list List <TorrentInfo> torrentsInfo = await Task.Run(() => Globals.SessionHandler.GetTorrents()); TimeOutTimer.Stop(); TimeOutTimer.Tag = Properties.Settings.Default.MaxRequestDuration; // handle list response if (torrentsInfo != null) { if (torrentsInfo.Count == 0) { MessageBox.Show("The host returned 0 torrents.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { List <FriendlyTorrentInfo> globalTorrentsInfo = new List <FriendlyTorrentInfo>(); List <ListViewItem> torrentLVItems = new List <ListViewItem>(); TorrentsListView.BeginUpdate(); foreach (TorrentInfo torrent in torrentsInfo) { FriendlyTorrentInfo fTorrentInfo = new FriendlyTorrentInfo(torrent); globalTorrentsInfo.Add(fTorrentInfo); var torrentLVItem = new ListViewItem(new[] { fTorrentInfo.QueuePosition, fTorrentInfo.Name, fTorrentInfo.Status, fTorrentInfo.Size, fTorrentInfo.Progress }) { Tag = fTorrentInfo.Torrent }; torrentLVItems.Add(torrentLVItem); } TorrentsListView.Items.AddRange(torrentLVItems.ToArray()); TorrentsListView.EndUpdate(); Globals.TorrentsInfo = globalTorrentsInfo; } } else { MessageBox.Show("The torrent list could not be retrieved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); } allItems.Clear(); allItems.AddRange(TorrentsListView.Items.Cast <ListViewItem>()); ToggleLoadingPanels(false); RefreshTorrentListButton.Text = "Refresh Torrent List"; TorrentTabPage.Text = $"Torrents ({Globals.TorrentsInfo.Count})"; RefreshTorrentListButton.Enabled = true; SearchTorrentListTextBox.Focus(); } }
private async void ConnectButtonClicked(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(HostTextBox.Text)) { MessageBox.Show("The host value may not be empty.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } isConnecting = true; RemoteGroupBox.Enabled = false; ConnectButton.Enabled = false; ConnectButton.Text = "Waiting (20)"; CloseCancelButton.Text = "Cancel"; TimeOutTimer.Enabled = true; TimeOutTimer.Start(); Globals.RequestResult connectionResult = Globals.RequestResult.Unknown; if (AuthenticationRequiredCheckBox.Checked) { Globals.SessionHandler = new SessionHandler(HostTextBox.Text, RPCPathTextBox.Text, (int)PortUpDown.Value, UsernameTextBox.Text, PasswordTextBox.Text); } else { Globals.SessionHandler = new SessionHandler(HostTextBox.Text, RPCPathTextBox.Text, (int)PortUpDown.Value, null, null); } await Task.Run(async() => { connectionResult = await Globals.SessionHandler.TestConnection(); }); if (isConnecting) { bool revertUi = true; switch (connectionResult) { case Globals.RequestResult.Success: SaveConfig(); Hide(); SettingsForm selectTorrentFilesForm = new SettingsForm { Text = $"Transmission Renamer - {Globals.SessionHandler.SessionUrl}" }; selectTorrentFilesForm.ShowDialog(); Show(); break; case Globals.RequestResult.Timeout: MessageBox.Show("The connection to the host has timed out.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case Globals.RequestResult.InvalidResponse: MessageBox.Show("The host returned an invalid response.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case Globals.RequestResult.Error: MessageBox.Show("The specified host or RPC path is invalid.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case Globals.RequestResult.Unauthorized: MessageBox.Show("The specified port or login credentials are invalid.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case Globals.RequestResult.Cancelled: // show no message but keep UI controls and behavior intact revertUi = false; break; case Globals.RequestResult.Unknown: MessageBox.Show("An unknown error has occurred.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); break; default: MessageBox.Show("An unknown error has occurred.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } if (revertUi) { ConnectButton.Text = "Connect"; CloseCancelButton.Text = "Close"; RemoteGroupBox.Enabled = true; ConnectButton.Enabled = true; isConnecting = false; TimeOutTimer.Stop(); TimeOutTimer.Enabled = false; TimeOutTimer.Tag = Properties.Settings.Default.MaxRequestDuration; } } }