From 15adf9717483dfbb166a5f2922581e1d58ea2b82 Mon Sep 17 00:00:00 2001 From: David Sparer Date: Thu, 15 Nov 2018 15:31:38 -0600 Subject: [PATCH] refactored some protocol base events to standardize how disconnect/error events are raised --- mRemoteV1/Connection/ConnectionInitiator.cs | 43 ++++++++++--------- .../Connection/Protocol/ICA/IcaProtocol.cs | 4 +- mRemoteV1/Connection/Protocol/ProtocolBase.cs | 18 ++++---- .../Connection/Protocol/RDP/RdpErrorCodes.cs | 22 +++++----- .../Connection/Protocol/RDP/RdpProtocol.cs | 7 +-- .../Protocol/VNC/Connection.Protocol.VNC.cs | 2 +- .../Resources/Language/Language.Designer.cs | 6 +-- mRemoteV1/Resources/Language/Language.resx | 6 +-- 8 files changed, 54 insertions(+), 54 deletions(-) diff --git a/mRemoteV1/Connection/ConnectionInitiator.cs b/mRemoteV1/Connection/ConnectionInitiator.cs index 86ae3243..a973c27b 100644 --- a/mRemoteV1/Connection/ConnectionInitiator.cs +++ b/mRemoteV1/Connection/ConnectionInitiator.cs @@ -1,6 +1,5 @@ using mRemoteNG.App; using mRemoteNG.Connection.Protocol; -using mRemoteNG.Connection.Protocol.RDP; using mRemoteNG.Container; using mRemoteNG.Messages; using mRemoteNG.UI.Forms; @@ -204,25 +203,27 @@ namespace mRemoteNG.Connection #endregion #region Event handlers - private static void Prot_Event_Disconnected(object sender, string disconnectedMessage) + private static void Prot_Event_Disconnected(object sender, string disconnectedMessage, int? reasonCode) { try { - if (sender is VncSharp.RemoteDesktop) + var prot = (ProtocolBase)sender; + var msgClass = MessageClass.InformationMsg; + + if (prot.InterfaceControl.Info.Protocol == ProtocolType.RDP) { - Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.strProtocolEventDisconnected, @"VncSharp Disconnected."), true); - return; + if (reasonCode > 3) + { + msgClass = MessageClass.WarningMsg; + } } - Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.strProtocolEventDisconnected, disconnectedMessage), true); - - var prot = (ProtocolBase)sender; - if (prot.InterfaceControl.Info.Protocol != ProtocolType.RDP) return; - var reasonCode = disconnectedMessage.Split("\r\n".ToCharArray())[0]; - var desc = disconnectedMessage.Replace("\r\n", " "); - - if (Convert.ToInt32(reasonCode) > 3) - Runtime.MessageCollector.AddMessage(MessageClass.WarningMsg, Language.strRdpDisconnected + Environment.NewLine + desc); + Runtime.MessageCollector.AddMessage(msgClass, + string.Format( + Language.strProtocolEventDisconnected, + disconnectedMessage, + prot.InterfaceControl.Info.Hostname, + prot.InterfaceControl.Info.Protocol.ToString())); } catch (Exception ex) { @@ -266,16 +267,18 @@ namespace mRemoteNG.Connection Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.strConnectionEventConnectedDetail, prot.InterfaceControl.Info.Hostname, prot.InterfaceControl.Info.Protocol, Environment.UserName, prot.InterfaceControl.Info.Description, prot.InterfaceControl.Info.UserField)); } - private static void Prot_Event_ErrorOccured(object sender, string errorMessage) + private static void Prot_Event_ErrorOccured(object sender, string errorMessage, int? errorCode) { try { - Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, Language.strConnectionEventErrorOccured, true); - var prot = (ProtocolBase)sender; + var prot = (ProtocolBase) sender; - if (prot.InterfaceControl.Info.Protocol != ProtocolType.RDP) return; - if (Convert.ToInt32(errorMessage) > -1) - Runtime.MessageCollector.AddMessage(MessageClass.WarningMsg, string.Format(Language.strConnectionRdpErrorDetail, errorMessage, RdpErrorCodes.GetError(errorMessage))); + var msg = string.Format( + Language.strConnectionEventErrorOccured, + errorMessage, + prot.InterfaceControl.Info.Hostname, + errorCode?.ToString() ?? "-"); + Runtime.MessageCollector.AddMessage(MessageClass.WarningMsg, msg); } catch (Exception ex) { diff --git a/mRemoteV1/Connection/Protocol/ICA/IcaProtocol.cs b/mRemoteV1/Connection/Protocol/ICA/IcaProtocol.cs index e28c2114..795e6494 100644 --- a/mRemoteV1/Connection/Protocol/ICA/IcaProtocol.cs +++ b/mRemoteV1/Connection/Protocol/ICA/IcaProtocol.cs @@ -285,12 +285,12 @@ namespace mRemoteNG.Connection.Protocol.ICA private void ICAEvent_OnConnectFailed(object sender, EventArgs e) { - Event_ErrorOccured(this, e.ToString()); + Event_ErrorOccured(this, e.ToString(), null); } private void ICAEvent_OnDisconnect(object sender, EventArgs e) { - Event_Disconnected(this, e.ToString()); + Event_Disconnected(this, e.ToString(), null); if (Settings.Default.ReconnectOnDisconnect) { diff --git a/mRemoteV1/Connection/Protocol/ProtocolBase.cs b/mRemoteV1/Connection/Protocol/ProtocolBase.cs index b9008b03..15bb49bf 100644 --- a/mRemoteV1/Connection/Protocol/ProtocolBase.cs +++ b/mRemoteV1/Connection/Protocol/ProtocolBase.cs @@ -1,13 +1,13 @@ +using mRemoteNG.App; +using mRemoteNG.Tools; using System; using System.Threading; using System.Windows.Forms; -using mRemoteNG.App; -using mRemoteNG.Tools; namespace mRemoteNG.Connection.Protocol { - public abstract class ProtocolBase + public abstract class ProtocolBase { #region Private Variables @@ -239,14 +239,14 @@ namespace mRemoteNG.Connection.Protocol remove { ConnectedEvent = (ConnectedEventHandler) Delegate.Remove(ConnectedEvent, value); } } - public delegate void DisconnectedEventHandler(object sender, string DisconnectedMessage); + public delegate void DisconnectedEventHandler(object sender, string disconnectedMessage, int? reasonCode); public event DisconnectedEventHandler Disconnected { add { DisconnectedEvent = (DisconnectedEventHandler) Delegate.Combine(DisconnectedEvent, value); } remove { DisconnectedEvent = (DisconnectedEventHandler) Delegate.Remove(DisconnectedEvent, value); } } - public delegate void ErrorOccuredEventHandler(object sender, string ErrorMessage); + public delegate void ErrorOccuredEventHandler(object sender, string errorMessage, int? errorCode); public event ErrorOccuredEventHandler ErrorOccured { add { ErrorOccuredEvent = (ErrorOccuredEventHandler) Delegate.Combine(ErrorOccuredEvent, value); } @@ -288,14 +288,14 @@ namespace mRemoteNG.Connection.Protocol ConnectedEvent?.Invoke(sender); } - protected void Event_Disconnected(object sender, string DisconnectedMessage) + protected void Event_Disconnected(object sender, string disconnectedMessage, int? reasonCode) { - DisconnectedEvent?.Invoke(sender, DisconnectedMessage); + DisconnectedEvent?.Invoke(sender, disconnectedMessage, reasonCode); } - protected void Event_ErrorOccured(object sender, string ErrorMsg) + protected void Event_ErrorOccured(object sender, string errorMsg, int? errorCode) { - ErrorOccuredEvent?.Invoke(sender, ErrorMsg); + ErrorOccuredEvent?.Invoke(sender, errorMsg, errorCode); } protected void Event_ReconnectGroupCloseClicked() diff --git a/mRemoteV1/Connection/Protocol/RDP/RdpErrorCodes.cs b/mRemoteV1/Connection/Protocol/RDP/RdpErrorCodes.cs index fa98147b..aa963ffd 100644 --- a/mRemoteV1/Connection/Protocol/RDP/RdpErrorCodes.cs +++ b/mRemoteV1/Connection/Protocol/RDP/RdpErrorCodes.cs @@ -1,6 +1,6 @@ +using mRemoteNG.App; using System; using System.Collections; -using mRemoteNG.App; namespace mRemoteNG.Connection.Protocol.RDP { @@ -12,19 +12,19 @@ namespace mRemoteNG.Connection.Protocol.RDP { _description = new Hashtable { - {"0", "Language.strRdpErrorUnknown"}, - {"1", "Language.strRdpErrorCode1"}, - {"2", "Language.strRdpErrorOutOfMemory"}, - {"3", "Language.strRdpErrorWindowCreation"}, - {"4", "Language.strRdpErrorCode2"}, - {"5", "Language.strRdpErrorCode3"}, - {"6", "Language.strRdpErrorCode4"}, - {"7", "Language.strRdpErrorConnection"}, - {"100", "Language.strRdpErrorWinsock"} + { 0, "Language.strRdpErrorUnknown"}, + { 1, "Language.strRdpErrorCode1"}, + { 2, "Language.strRdpErrorOutOfMemory"}, + { 3, "Language.strRdpErrorWindowCreation"}, + { 4, "Language.strRdpErrorCode2"}, + { 5, "Language.strRdpErrorCode3"}, + { 6, "Language.strRdpErrorCode4"}, + { 7, "Language.strRdpErrorConnection"}, + { 100, "Language.strRdpErrorWinsock"} }; } - public static string GetError(string id) + public static string GetError(int id) { try { diff --git a/mRemoteV1/Connection/Protocol/RDP/RdpProtocol.cs b/mRemoteV1/Connection/Protocol/RDP/RdpProtocol.cs index 0feb886c..40317306 100644 --- a/mRemoteV1/Connection/Protocol/RDP/RdpProtocol.cs +++ b/mRemoteV1/Connection/Protocol/RDP/RdpProtocol.cs @@ -690,8 +690,9 @@ namespace mRemoteNG.Connection.Protocol.RDP private void RDPEvent_OnFatalError(int errorCode) - { - Event_ErrorOccured(this, Convert.ToString(errorCode)); + { + var errorMsg = RdpErrorCodes.GetError(errorCode); + Event_ErrorOccured(this, errorMsg, errorCode); } private void RDPEvent_OnDisconnected(int discReason) @@ -700,7 +701,7 @@ namespace mRemoteNG.Connection.Protocol.RDP if (discReason != UI_ERR_NORMAL_DISCONNECT) { var reason = _rdpClient.GetErrorDescription((uint)discReason, (uint) _rdpClient.ExtendedDisconnectReason); - Event_Disconnected(this, discReason + "\r\n" + reason); + Event_Disconnected(this, reason, discReason); } if (Settings.Default.ReconnectOnDisconnect) diff --git a/mRemoteV1/Connection/Protocol/VNC/Connection.Protocol.VNC.cs b/mRemoteV1/Connection/Protocol/VNC/Connection.Protocol.VNC.cs index a405a576..63db3a92 100644 --- a/mRemoteV1/Connection/Protocol/VNC/Connection.Protocol.VNC.cs +++ b/mRemoteV1/Connection/Protocol/VNC/Connection.Protocol.VNC.cs @@ -186,7 +186,7 @@ namespace mRemoteNG.Connection.Protocol.VNC private void VNCEvent_Disconnected(object sender, EventArgs e) { FrmMain.ClipboardChanged -= VNCEvent_ClipboardChanged; - Event_Disconnected(sender, e.ToString()); + Event_Disconnected(sender, @"VncSharp Disconnected.", null); Close(); } diff --git a/mRemoteV1/Resources/Language/Language.Designer.cs b/mRemoteV1/Resources/Language/Language.Designer.cs index 1a9d0d0b..da008ab3 100644 --- a/mRemoteV1/Resources/Language/Language.Designer.cs +++ b/mRemoteV1/Resources/Language/Language.Designer.cs @@ -1473,7 +1473,7 @@ namespace mRemoteNG { } /// - /// Looks up a localized string similar to Protocol Event ErrorOccured. + /// Looks up a localized string similar to A connection protocol error occurred. Host: "{1}"; Error code: "{2}"; Error Description: "{0}". /// internal static string strConnectionEventErrorOccured { get { @@ -5551,9 +5551,7 @@ namespace mRemoteNG { } /// - /// Looks up a localized string similar to Protocol Event Disconnected. - ///Message: - ///{0}. + /// Looks up a localized string similar to Protocol Event Disconnected. Host: "{1}"; Protocol: "{2}" Message: "{0}". /// internal static string strProtocolEventDisconnected { get { diff --git a/mRemoteV1/Resources/Language/Language.resx b/mRemoteV1/Resources/Language/Language.resx index 40e61522..c124008a 100644 --- a/mRemoteV1/Resources/Language/Language.resx +++ b/mRemoteV1/Resources/Language/Language.resx @@ -508,7 +508,7 @@ VncSharp Control Version {0} Connection failed! - Protocol Event ErrorOccured + A connection protocol error occurred. Host: "{1}"; Error code: "{2}"; Error Description: "{0}" Opening connection failed! @@ -1683,9 +1683,7 @@ If you run into such an error, please create a new connection file! Proxy Username - Protocol Event Disconnected. -Message: -{0} + Protocol Event Disconnected. Host: "{1}"; Protocol: "{2}" Message: "{0}" Protocol Event Disconnected failed.