From b019362f5f22f1c75c68f9cfb26a90fb3f2cca84 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Thu, 29 Oct 2020 14:46:39 +0800 Subject: [PATCH] refactor: Check STUN Server setting --- Netch/Forms/SettingForm.cs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 4df19f7e..aaf4e3d7 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -297,6 +297,8 @@ namespace Netch.Forms Utils.Utils.ComponentIterator(this, component => ChangeControlForeColor(component, Color.Black)); + #region Check + var flag = true; foreach (var pair in _checkActions.Where(pair => !pair.Value.Invoke(pair.Key.Text))) { @@ -309,31 +311,32 @@ namespace Netch.Forms return; } - #region Check - #region Behavior + #region CheckSTUN - // STUN - string stunServer; - int stunServerPort; - try + var stunFlag = true; + var stunServer = string.Empty; + ushort stunServerPort = 3478; + + var stun = STUN_ServerComboBox.Text.Split(':'); + + if (stun.Any()) { - var stun = STUN_ServerComboBox.Text.Split(':'); stunServer = stun[0]; - - stunServerPort = 3478; if (stun.Length > 1) - stunServerPort = ushort.Parse(stun[1]); - - if (stunServerPort <= 0) - { - throw new FormatException(); - } + if (!ushort.TryParse(stun[1], out stunServerPort)) + { + stunFlag = false; + } } - catch (FormatException) + else { - MessageBoxX.Show(i18N.Translate("STUN_ServerPort value illegal. Try again.")); + stunFlag = false; + } + if (!stunFlag) + { + ChangeControlForeColor(STUN_ServerComboBox, Color.Red); return; }