From d1bb4249b88e3bbe3b1e897bf335071d5335887a Mon Sep 17 00:00:00 2001 From: zsender Date: Thu, 6 Apr 2017 09:25:48 +0300 Subject: [PATCH] fix subscribe event (for better code readability) addcomment to method fireEvent --- .../Asterisk.NET.Test/Program.cs | 12 ++++---- .../Asterisk.NET.WinForm/FormMain.cs | 2 +- .../Asterisk.NET/Manager/ManagerConnection.cs | 29 ++++++++++++------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Asterisk.2013/Asterisk.NET.Test/Asterisk.NET.Test/Program.cs b/Asterisk.2013/Asterisk.NET.Test/Asterisk.NET.Test/Program.cs index d39c86e..5d2eeeb 100644 --- a/Asterisk.2013/Asterisk.NET.Test/Asterisk.NET.Test/Program.cs +++ b/Asterisk.2013/Asterisk.NET.Test/Asterisk.NET.Test/Program.cs @@ -138,13 +138,13 @@ Ctrl-C to exit"); manager.RegisterUserEventClass(typeof(UserAgentLoginEvent)); // Add or Remove events - manager.UserEvents += dam_UserEvents; + manager.UserEvents += new EventHandler(dam_UserEvents); // Dont't display this event - manager.NewExten += manager_IgnoreEvent; + manager.NewExten += new EventHandler(manager_IgnoreEvent); // Display all other - manager.UnhandledEvent += dam_Events; + manager.UnhandledEvent += new EventHandler(dam_Events); // +++ Only to debug purpose manager.FireAllEvents = true; @@ -289,7 +289,7 @@ Ctrl-C to exit"); Console.WriteLine("Redirect Call from " + ORIGINATE_CHANNEL + " to " + ORIGINATE_EXTRA_CHANNEL + " or press ESC."); // Wait for Dial Event from ORIGINATE_CHANNEL EventHandler de = dam_Dial; - manager.Dial += de; + manager.Dial += new EventHandler(de); while (transferChannel == null) { System.Threading.Thread.Sleep(100); @@ -323,8 +323,8 @@ Ctrl-C to exit"); // Link event used to define monitor channel Console.WriteLine("Monitor call. Please call " + ORIGINATE_CHANNEL + " and answer or press ESC."); // Wait for Link event - EventHandler le = dam_Link; - manager.Link += le; + EventHandler le = new EventHandler(dam_Link); + manager.Link += new EventHandler(le); while (monitorChannel == null) { System.Threading.Thread.Sleep(100); diff --git a/Asterisk.2013/Asterisk.NET.WinForm/FormMain.cs b/Asterisk.2013/Asterisk.NET.WinForm/FormMain.cs index 89dcec4..6ac81b2 100644 --- a/Asterisk.2013/Asterisk.NET.WinForm/FormMain.cs +++ b/Asterisk.2013/Asterisk.NET.WinForm/FormMain.cs @@ -28,7 +28,7 @@ namespace AsterNET.WinForm btnConnect.Enabled = false; manager = new ManagerConnection(address, port, user, password); - manager.UnhandledEvent += manager_Events; + manager.UnhandledEvent += new EventHandler(manager_Events); try { // Uncomment next 2 line comments to Disable timeout (debug mode) diff --git a/Asterisk.2013/Asterisk.NET/Manager/ManagerConnection.cs b/Asterisk.2013/Asterisk.NET/Manager/ManagerConnection.cs index d3dd4be..943c8bb 100644 --- a/Asterisk.2013/Asterisk.NET/Manager/ManagerConnection.cs +++ b/Asterisk.2013/Asterisk.NET/Manager/ManagerConnection.cs @@ -58,7 +58,6 @@ namespace AsterNET.Manager private int reconnectCount; private Dictionary registeredEventClasses; - //private Dictionary registeredEventHandlers; private Dictionary> registeredEventHandlers; private event EventHandler internalEvent; private bool fireAllEvents = false; @@ -639,7 +638,9 @@ namespace AsterNET.Manager } if (fireAllEvents) + { fireEvent(UnhandledEvent, e); + } } #endregion @@ -1183,7 +1184,7 @@ namespace AsterNET.Manager enableEvents = true; reconnected = false; disconnect(true); - fireEvent(new DisconnectEvent(this)); + fireInternalEvent(new DisconnectEvent(this)); } } #endregion @@ -1766,7 +1767,7 @@ namespace AsterNET.Manager ConnectEvent ce = new ConnectEvent(this); ce.Reconnect = true; ce.ProtocolIdentifier = protocolIdentifier; - fireEvent(ce); + fireInternalEvent(ce); } else if (keepAliveAfterAuthenticationFailure) reconnect(true); @@ -1864,36 +1865,44 @@ namespace AsterNET.Manager if (reconnected && e is DisconnectEvent) { ((DisconnectEvent)e).Reconnect = true; - fireEvent(e); + fireInternalEvent(e); reconnect(false); } else if (!reconnected && reconnectEnable && (e is DisconnectEvent || e is ReloadEvent || e is ShutdownEvent)) { ((ConnectionStateEvent)e).Reconnect = true; - fireEvent(e); + fireInternalEvent(e); reconnect(true); } else - fireEvent(e); + fireInternalEvent(e); } private void eventComplete(IAsyncResult result) { } - private void fireEvent(ManagerEvent e) + private void fireInternalEvent(ManagerEvent e) { if (enableEvents && internalEvent != null) + { if (UseASyncEvents) internalEvent.BeginInvoke(this, e, new AsyncCallback(eventComplete), null); else internalEvent.Invoke(this, e); + } } - private void fireEvent(EventHandler astEvent, ManagerEvent arg) where T : ManagerEvent + /// + /// This method is called when send event to client if subscribed + /// + /// EventHandler argument + /// Event delegate + /// ManagerEvent or inherited class. Argument of eventHandler. + private void fireEvent(EventHandler asterEvent, ManagerEvent arg) where T : ManagerEvent { - if (astEvent != null) - astEvent(this, (T)arg); + if (asterEvent != null) + asterEvent(this, (T)arg); } #endregion }