// ProcessHttpRequest is the async handler for new http requests // This function should do what it needs to do and then exit public static void ProcessHttpRequest(IAsyncResult result) { RequestProcessor rp = null; try // (IAsyncResult)result will be invalid if we've killed the listener { HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); // Call EndGetContext to complete the asynchronous operation. // Obtain a response object. HttpListenerResponse response = context.Response; // Create a new request processor rp = new RequestProcessor(context); rp.Run(); } catch (System.ObjectDisposedException) { // Do nothing } catch (Exception ex) { Functions.WriteLineToLogFile("Process HttpRequest Exception:"); Functions.WriteExceptionToLogFile(ex); } finally { if (rp != null) { rp.Dispose(); rp = null; } } }
protected void WebRequestCallback(IAsyncResult result) { if (myHttpListener == null) return; if (!Active) return; try { // Get out the context object HttpListenerContext context = myHttpListener.EndGetContext(result); // *** Immediately set up the next context myHttpListener.BeginGetContext(new AsyncCallback(WebRequestCallback), myHttpListener); RequestProcessor rp = new RequestProcessor(context); rp.UserAgentConnected += new EventHandler<MessageEventArgs>(rp_UserAgentConnected); rp.Run(); rp.Dispose(); rp = null; // done //RPStackSizeChanged(this, new GenericEventArgs<int>(--numberOfRequestProcessors)); } catch (Exception ex) { Functions.WriteLineToLogFile("Exception thrown processing web server request: "); Functions.WriteExceptionToLogFile(ex); } return; }