public static USSDHttpListener Start(USSDHttpListenerConfigurationSection ussdHttpListenerConfiguration) { USSDHttpListener result = new USSDHttpListener(); result.httpListenerWaitHandle = new AutoResetEvent(false); result.httpListener = new HttpListener(); result.mobileNetwork = (MobileNetworks.MobileNetwork)Enum.Parse(typeof(MobileNetworks.MobileNetwork), ussdHttpListenerConfiguration.MobileNetwork, true); String address = ussdHttpListenerConfiguration.Address.Trim(); if (address.Substring(address.Length - 1) != "/") { address += "/"; } result.httpListener.Prefixes.Add(address); try { result.httpListener.Start(); LogManager.LogStatus("Creating {0} on {1}", ussdHttpListenerConfiguration.Name, ussdHttpListenerConfiguration.Address); IAsyncResult asyncListener; for (int listenerIndex = 1; listenerIndex <= ussdHttpListenerConfiguration.NumberOfListeners; listenerIndex++) { asyncListener = result.httpListener.BeginGetContext(new AsyncCallback(result.OnHttpRequestReceived), result.httpListener); } LogManager.LogStatus("{0} USSD Http Listener/s started for {1}", ussdHttpListenerConfiguration.NumberOfListeners, ussdHttpListenerConfiguration.Name); new Thread(new ThreadStart(delegate() { result.listenerState = ListenerStates.Running; result.httpListenerWaitHandle.WaitOne(); result.httpListener.Stop(); LogManager.LogStatus("{0} stopped", ussdHttpListenerConfiguration.Name); })).Start(); } catch (Exception ex) { LogManager.LogError(ex); result.httpListenerWaitHandle.Set(); } return(result); }
protected void StartHttpListeners() { httpListenersWaitHandle = new AutoResetEvent(false); try { USSDHttpListenerConfigurationCollection ussdHttpListenerConfigurations = USSDHttpListenerConfiguration.GetConfiguration().USSDHttpListenerConfigurations; foreach (USSDHttpListenerConfigurationSection ussdHttpListenerConfiguration in ussdHttpListenerConfigurations) { if (ussdHttpListenerConfiguration.Enabled) { USSDHttpListener httpListener = USSDHttpListener.Start(ussdHttpListenerConfiguration); listeners.Add(httpListener); } } httpListenersWaitHandle.WaitOne(); } catch (Exception) { StopHttpListeners(); throw; } }