fix subscribe event (for better code readability)

addcomment to method fireEvent
This commit is contained in:
zsender 2017-04-06 09:25:48 +03:00
parent 5e738cdd3c
commit d1bb4249b8
3 changed files with 26 additions and 17 deletions

View file

@ -138,13 +138,13 @@ Ctrl-C to exit");
manager.RegisterUserEventClass(typeof(UserAgentLoginEvent)); manager.RegisterUserEventClass(typeof(UserAgentLoginEvent));
// Add or Remove events // Add or Remove events
manager.UserEvents += dam_UserEvents; manager.UserEvents += new EventHandler<UserEvent>(dam_UserEvents);
// Dont't display this event // Dont't display this event
manager.NewExten += manager_IgnoreEvent; manager.NewExten += new EventHandler<NewExtenEvent>(manager_IgnoreEvent);
// Display all other // Display all other
manager.UnhandledEvent += dam_Events; manager.UnhandledEvent += new EventHandler<ManagerEvent>(dam_Events);
// +++ Only to debug purpose // +++ Only to debug purpose
manager.FireAllEvents = true; 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."); Console.WriteLine("Redirect Call from " + ORIGINATE_CHANNEL + " to " + ORIGINATE_EXTRA_CHANNEL + " or press ESC.");
// Wait for Dial Event from ORIGINATE_CHANNEL // Wait for Dial Event from ORIGINATE_CHANNEL
EventHandler<DialEvent> de = dam_Dial; EventHandler<DialEvent> de = dam_Dial;
manager.Dial += de; manager.Dial += new EventHandler<DialEvent>(de);
while (transferChannel == null) while (transferChannel == null)
{ {
System.Threading.Thread.Sleep(100); System.Threading.Thread.Sleep(100);
@ -323,8 +323,8 @@ Ctrl-C to exit");
// Link event used to define monitor channel // Link event used to define monitor channel
Console.WriteLine("Monitor call. Please call " + ORIGINATE_CHANNEL + " and answer or press ESC."); Console.WriteLine("Monitor call. Please call " + ORIGINATE_CHANNEL + " and answer or press ESC.");
// Wait for Link event // Wait for Link event
EventHandler<LinkEvent> le = dam_Link; EventHandler<LinkEvent> le = new EventHandler<LinkEvent>(dam_Link);
manager.Link += le; manager.Link += new EventHandler<LinkEvent>(le);
while (monitorChannel == null) while (monitorChannel == null)
{ {
System.Threading.Thread.Sleep(100); System.Threading.Thread.Sleep(100);

View file

@ -28,7 +28,7 @@ namespace AsterNET.WinForm
btnConnect.Enabled = false; btnConnect.Enabled = false;
manager = new ManagerConnection(address, port, user, password); manager = new ManagerConnection(address, port, user, password);
manager.UnhandledEvent += manager_Events; manager.UnhandledEvent += new EventHandler<ManagerEvent>(manager_Events);
try try
{ {
// Uncomment next 2 line comments to Disable timeout (debug mode) // Uncomment next 2 line comments to Disable timeout (debug mode)

View file

@ -58,7 +58,6 @@ namespace AsterNET.Manager
private int reconnectCount; private int reconnectCount;
private Dictionary<int, ConstructorInfo> registeredEventClasses; private Dictionary<int, ConstructorInfo> registeredEventClasses;
//private Dictionary<int, int> registeredEventHandlers;
private Dictionary<int, Action<ManagerEvent>> registeredEventHandlers; private Dictionary<int, Action<ManagerEvent>> registeredEventHandlers;
private event EventHandler<ManagerEvent> internalEvent; private event EventHandler<ManagerEvent> internalEvent;
private bool fireAllEvents = false; private bool fireAllEvents = false;
@ -639,7 +638,9 @@ namespace AsterNET.Manager
} }
if (fireAllEvents) if (fireAllEvents)
{
fireEvent(UnhandledEvent, e); fireEvent(UnhandledEvent, e);
}
} }
#endregion #endregion
@ -1183,7 +1184,7 @@ namespace AsterNET.Manager
enableEvents = true; enableEvents = true;
reconnected = false; reconnected = false;
disconnect(true); disconnect(true);
fireEvent(new DisconnectEvent(this)); fireInternalEvent(new DisconnectEvent(this));
} }
} }
#endregion #endregion
@ -1766,7 +1767,7 @@ namespace AsterNET.Manager
ConnectEvent ce = new ConnectEvent(this); ConnectEvent ce = new ConnectEvent(this);
ce.Reconnect = true; ce.Reconnect = true;
ce.ProtocolIdentifier = protocolIdentifier; ce.ProtocolIdentifier = protocolIdentifier;
fireEvent(ce); fireInternalEvent(ce);
} }
else if (keepAliveAfterAuthenticationFailure) else if (keepAliveAfterAuthenticationFailure)
reconnect(true); reconnect(true);
@ -1864,36 +1865,44 @@ namespace AsterNET.Manager
if (reconnected && e is DisconnectEvent) if (reconnected && e is DisconnectEvent)
{ {
((DisconnectEvent)e).Reconnect = true; ((DisconnectEvent)e).Reconnect = true;
fireEvent(e); fireInternalEvent(e);
reconnect(false); reconnect(false);
} }
else if (!reconnected && reconnectEnable && (e is DisconnectEvent || e is ReloadEvent || e is ShutdownEvent)) else if (!reconnected && reconnectEnable && (e is DisconnectEvent || e is ReloadEvent || e is ShutdownEvent))
{ {
((ConnectionStateEvent)e).Reconnect = true; ((ConnectionStateEvent)e).Reconnect = true;
fireEvent(e); fireInternalEvent(e);
reconnect(true); reconnect(true);
} }
else else
fireEvent(e); fireInternalEvent(e);
} }
private void eventComplete(IAsyncResult result) private void eventComplete(IAsyncResult result)
{ {
} }
private void fireEvent(ManagerEvent e) private void fireInternalEvent(ManagerEvent e)
{ {
if (enableEvents && internalEvent != null) if (enableEvents && internalEvent != null)
{
if (UseASyncEvents) if (UseASyncEvents)
internalEvent.BeginInvoke(this, e, new AsyncCallback(eventComplete), null); internalEvent.BeginInvoke(this, e, new AsyncCallback(eventComplete), null);
else else
internalEvent.Invoke(this, e); internalEvent.Invoke(this, e);
}
} }
private void fireEvent<T>(EventHandler<T> astEvent, ManagerEvent arg) where T : ManagerEvent /// <summary>
/// This method is called when send event to client if subscribed
/// </summary>
/// <typeparam name="T">EventHandler argument</typeparam>
/// <param name="asterEvent">Event delegate</param>
/// <param name="arg">ManagerEvent or inherited class. Argument of eventHandler.</param>
private void fireEvent<T>(EventHandler<T> asterEvent, ManagerEvent arg) where T : ManagerEvent
{ {
if (astEvent != null) if (asterEvent != null)
astEvent(this, (T)arg); asterEvent(this, (T)arg);
} }
#endregion #endregion
} }