/// <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); } }
public override void Dispose() { lock (_lock) { if (_isDisposed) { return; } _isDisposed = true; ReTransmitTimer.Dispose(); TimeOutTimer.Dispose(); EndCompletedTimer.Dispose(); SipAbstractClientTransaction tx; _table.TryRemove(this.GetId(), out tx); //removed comment. but not sure. 03.06.2013 State = TerminatedState; //is done outside of this method? } if (_stateObserver != null) { //removed comment. but not sure. 03.06.2013 _stateObserver.OnNext(CreateStateInfo(State.Name)); //is done outside of this method? _stateObserver.OnCompleted(); } }
private void Tcp_Connected(object sender, string result) { if (result == "OK") { SendReqest(ProtocolOfExchange.CheckConnection, null, m_tcpModule); TimeOutTimer.Start(); } if (result == "ERR") { ConnectToServer(); } }
public override void Dispose() { lock (_lock) { if (_isDisposed) { return; } _isDisposed = true; ReTransmitTimer.Dispose(); TimeOutTimer.Dispose(); EndCompletedTimer.Dispose(); SipAbstractClientTransaction tx; _table.TryRemove(this.GetId(), out tx); } if (_stateObserver != null) { _stateObserver.OnNext(CreateStateInfo(SipTransactionStateName.Terminated)); _stateObserver.OnCompleted(); } }
private void CloseCancelButtonPressed(object sender, EventArgs e) { if (isConnecting) { TimeOutTimer.Stop(); TimeOutTimer.Enabled = false; TimeOutTimer.Tag = Properties.Settings.Default.MaxRequestDuration; ConnectButton.Text = "Connect"; CloseCancelButton.Text = "Close"; RemoteGroupBox.Enabled = true; ConnectButton.Enabled = true; isConnecting = false; Globals.SessionHandler.CancelConnecting(); } else { if (Globals.SessionHandler != null) { Globals.SessionHandler.CloseConnection(); } Application.Exit(); } }
//оброботка входящих данных из сети private void M_TcpClient_Receive(object sender, SocketData s) { // TcpModule tcp = (TcpModule)sender; NetworkTransferObjects obj = (NetworkTransferObjects)sender; //проверка соединения if (obj.ProtocolMessage == ProtocolOfExchange.CheckConnectionOK) { m_IsConnectedToServer = true; this.Invoke((new Action(() => connectionStatusLabel.BackColor = Color.Green))); TimeOutTimer.Stop(); SendReqest(ProtocolOfExchange.AskUserInfoList, new NetworkTransferObjects()); } //получение списка пользователей для выбора при авторизации if (obj.ProtocolMessage == ProtocolOfExchange.UserInfoListOk) { m_UserInfoArray = (List <UserInfo>)obj.ListUserInfo; this.Invoke((new Action(() => UserListRecieved()))); } if (obj.ProtocolMessage == ProtocolOfExchange.AuthOk) { m_CurrentUser = obj.User; m_CurrentAuth = obj.AuthInfo; this.Invoke((new Action(() => MessageBox.Show("Вы авторизированы")))); this.Invoke((new Action(() => this.Text = m_CurrentUser.FullName))); this.Invoke((new Action(() => SyncTimer_Tick(null, null)))); this.Invoke((new Action(() => SyncTimer.Start()))); } if (obj.ProtocolMessage == ProtocolOfExchange.AuthFail) { this.Invoke((new Action(() => UserListRecieved()))); } //временно /* if (e.sendInfo.ProtocolMsg== ProtocolOfExchange.AddUserOK) * this.Invoke((new Action(() => MessageBox.Show("UserAdded"))));*/ if (obj.ProtocolMessage == ProtocolOfExchange.SyncMessages) { if (obj.AuthInfo.UserId == m_CurrentUser.Id) { if (obj.Message != null) { m_inbox.Add(obj.Message); } if (obj.Message_1 != null) { m_outbox.Add(obj.Message_1); } this.Invoke((new Action(() => SyncMessageInbox()))); } } if (obj.ProtocolMessage == ProtocolOfExchange.NewVersionClientNone) { this.Invoke((new Action(() => MessageBox.Show("Нет новых обновлений")))); } if (obj.ProtocolMessage == ProtocolOfExchange.NewVersionClientOk) { this.Invoke((new Action(() => System.Diagnostics.Process.Start("Updater.exe", m_ServerIp + " 5454")))); this.Invoke((new Action(() => this.Close()))); } }
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; } } }
private void Tcp_Receive(object sender, ReceiveEventArgs e) { if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.CheckConnectionOK) { m_IsConnectedToServer = true; this.Invoke((new Action(() => connectionStatusLabel.BackColor = Color.Green))); TimeOutTimer.Stop(); SendReqest(ProtocolOfExchange.AskDictionary, null, (TcpModule)sender); Thread.Sleep(10); SendReqest(ProtocolOfExchange.AskDbCollection, null, (TcpModule)sender); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AskDictionaryOk) { m_DbDict = (Dictionary)e.NetDataObj.Dictionary; this.Invoke((new Action(() => RefreshDictionaryTree()))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AskDbCollectionOk) { m_DbCollection = (DataBasesCollection)e.NetDataObj.DataBaseCollection; this.Invoke((new Action(() => RefreshDataBaseCollectionTree()))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AddBaseOk) { m_DbCollection = (DataBasesCollection)e.NetDataObj.DataBaseCollection; this.Invoke((new Action(() => RefreshDataBaseCollectionTree()))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.DelBaseOk) { m_DbCollection = (DataBasesCollection)e.NetDataObj.DataBaseCollection; this.Invoke((new Action(() => RefreshDataBaseCollectionTree()))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AddBaseFail) { this.Invoke((new Action(() => MessageBox.Show("База с такими параметрами уже существует")))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AddBaseOk) { m_DbCollection = (DataBasesCollection)e.NetDataObj.DataBaseCollection; this.Invoke((new Action(() => RefreshDataBaseCollectionTree()))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AddQrItemInBaseOk) { m_DbCollection = (DataBasesCollection)e.NetDataObj.DataBaseCollection; this.Invoke((new Action(() => RefreshDataBaseCollectionTree()))); if (autoModeCxb.Checked && m_AutoOperationFlags[0]) { m_AutoOperationFlags[1] = true; this.Invoke((new Action(() => AutoOperationStep2()))); } } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AddQrItemInBaseFail) { //m_DbCollection = (DataBasesCollection)e.Object; this.Invoke((new Action(() => MessageBox.Show("Ошибка добавления записи в базу")))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AddQrItemSInBaseOk) { m_DbCollection = (DataBasesCollection)e.NetDataObj.DataBaseCollection; this.Invoke((new Action(() => RefreshDataBaseCollectionTree()))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.AddQrItemSInBaseFail) { //m_DbCollection = (DataBasesCollection)e.Object; this.Invoke((new Action(() => MessageBox.Show("Ошибка добавления записей в базу")))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.DelDbItemOK || e.SendInfo.ProtocolMsg == ProtocolOfExchange.DelDbItemsOK) { m_DbCollection = (DataBasesCollection)e.NetDataObj.DataBaseCollection; this.Invoke((new Action(() => RefreshDataBaseCollectionTree()))); } if (e.SendInfo.ProtocolMsg == ProtocolOfExchange.DelDbItemsFail) { //m_DbCollection = (DataBasesCollection)e this.Invoke((new Action(() => MessageBox.Show("Ошибка удаления записей из базы")))); } }