private void HandleRecievedContext(IAsyncResult result) { if (_listener.IsListening == true) { Components.AsyncCallbackContainer container = result.AsyncState as Components.AsyncCallbackContainer; HttpListener http = container.HttpListener; HttpListenerContext context = http.EndGetContext(result); HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; try { byte[] buff = System.Text.Encoding.UTF8.GetBytes(this.GenerateMessageFunctionDelegate(new Components.StateInfo(ref http, ref context, ref request, ref response, ref container.Argument) ) ); System.IO.Stream output = response.OutputStream; output.Write(buff, 0, buff.Length); output.Flush(); output.Close(); } catch (Exception ex) { response.StatusCode = 500; response.StatusDescription = "InternalServerException"; response.ContentType = "text/javascript"; response.OutputStream.Write(System.Text.Encoding.UTF8.GetBytes(ex.ToString()), 0, System.Text.Encoding.UTF8.GetBytes(ex.ToString()).Length); response.OutputStream.Flush(); response.OutputStream.Close(); } } }
private void WaitForNextRecievedContext(object args = null) { _listener.Start(); _starting = false; do { Components.AsyncCallbackContainer container = new Components.AsyncCallbackContainer(ref _listener, ref args); IAsyncResult result = _listener.BeginGetContext(new AsyncCallback(HandleRecievedContext), container); result.AsyncWaitHandle.WaitOne(); } while (_listener.IsListening == true); }