/// <summary> /// Processes events received from FreeSWITCH and dispatches them to the /// application event handlers. This is called from <see cref="SwitchApp" /> /// immediately after <see cref="SwitchApp.Main" /> returns on the /// application thread. /// </summary> internal static void EventLoop() { // Enlist in low-level global events if the application requested. var options = (switch_xml_section_enum_t)0; if (dialPlanEvent != null) { options |= switch_xml_section_enum_t.SWITCH_XML_SECTION_DIALPLAN; } if (userDirectoryEvent != null) { options |= switch_xml_section_enum_t.SWITCH_XML_SECTION_DIRECTORY; } if (options != (switch_xml_section_enum_t)0) { eventBinding = SwitchXmlSearchBinding.Bind(OnSwitchXmlSearchEvent, options); } // Decided whether we're going to actually consume NeonSwitch events based // on whether the application enlisted in the [EventReceived] event within // its [Main()] method. Note that we we'll exit the event loop (and the // application's background thread) immediately if this is the case. if (eventReceived == null) { return; } eventConsumer = new EventConsumer("all", string.Empty, 0); // Loop, waiting for the application to terminate. while (!stopPending) { try { var fsEvent = eventConsumer.pop(1, 0); var switchEvent = new SwitchEvent(fsEvent.InternalEvent); RaiseEventReceived(new SwitchEventArgs(switchEvent)); if (switchEvent.EventType == SwitchEventCode.BackgroundJob) { RaiseJobCompletedEvent(new JobCompletedEventArgs(switchEvent.Headers.Get("Job-ID", Guid.Empty))); } } catch (Exception e) { SysLog.LogException(e); } } }
public static void EventMainLoop() { EventConsumer ec = null; try { ec = new EventConsumer("CUSTOM", "sofia::register_attempt", 100); ec.bind("SHUTDOWN", String.Empty); ec.bind("HEARTBEAT", String.Empty); while (Running) { var evt = ec.pop(0, 0); if (evt == null) { continue; } var en = evt.InternalEvent.GetValueOfHeader("Event-Name"); if (en == "CUSTOM") { en = evt.InternalEvent.GetValueOfHeader("Event-SubClass"); } switch (en) { case @"sofia::register_attempt": { var iev = evt.InternalEvent; var ar = iev.GetValueOfHeader("auth-result"); // get the value of the result to see if it's the case we want var ip = iev.GetValueOfHeader("network-ip"); // and the ip address the register came from if (ar == "FORBIDDEN") { BanTracker.TrackFailure(ip); } } break; case "SHUTDOWN": Log.WriteLine(LogLevel.Critical, "FTB: Processing Shutdown event"); Running = false; break; case "HEARTBEAT": BanTracker.CleanUp(); break; default: break; } } } catch (Exception exx) { Log.WriteLine(LogLevel.Critical, "FailToBan -- Exception in event loop {0}", exx.Message); } finally { if (ec != null) { ec.Dispose(); } _eventThread = null; } }