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));
// Add or Remove events
manager.UserEvents += dam_UserEvents;
manager.UserEvents += new EventHandler<UserEvent>(dam_UserEvents);
// Dont't display this event
manager.NewExten += manager_IgnoreEvent;
manager.NewExten += new EventHandler<NewExtenEvent>(manager_IgnoreEvent);
// Display all other
manager.UnhandledEvent += dam_Events;
manager.UnhandledEvent += new EventHandler<ManagerEvent>(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<DialEvent> de = dam_Dial;
manager.Dial += de;
manager.Dial += new EventHandler<DialEvent>(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<LinkEvent> le = dam_Link;
manager.Link += le;
EventHandler<LinkEvent> le = new EventHandler<LinkEvent>(dam_Link);
manager.Link += new EventHandler<LinkEvent>(le);
while (monitorChannel == null)
{
System.Threading.Thread.Sleep(100);

View file

@ -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<ManagerEvent>(manager_Events);
try
{
// Uncomment next 2 line comments to Disable timeout (debug mode)

View file

@ -58,7 +58,6 @@ namespace AsterNET.Manager
private int reconnectCount;
private Dictionary<int, ConstructorInfo> registeredEventClasses;
//private Dictionary<int, int> registeredEventHandlers;
private Dictionary<int, Action<ManagerEvent>> registeredEventHandlers;
private event EventHandler<ManagerEvent> internalEvent;
private bool fireAllEvents = false;
@ -639,8 +638,10 @@ namespace AsterNET.Manager
}
if (fireAllEvents)
{
fireEvent(UnhandledEvent, e);
}
}
#endregion
#region FireAllEvents
@ -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<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)
astEvent(this, (T)arg);
if (asterEvent != null)
asterEvent(this, (T)arg);
}
#endregion
}