public override void HandleConnection() { this.thread = Thread.CurrentThread; bool force_close_connection = false; // Read the data off the socket and store it in a // temporary memory buffer. Once the end-of-message // character has been read, discard remaining data // and deserialize the request. byte[] network_data = new byte [4096]; MemoryStream buffer_stream = new MemoryStream(); int bytes_read, total_bytes = 0, end_index = -1; // We use the network_data array as an object to represent this worker. Shutdown.WorkerStart(network_data, String.Format("HandleConnection ({0})", ++connection_count)); do { bytes_read = 0; try { lock (this.blocking_read_lock) this.in_blocking_read = true; lock (this.client_lock) { // The connection may have been closed within this loop. if (this.client != null) { bytes_read = this.client.GetStream().Read(network_data, 0, 4096); } } lock (this.blocking_read_lock) this.in_blocking_read = false; } catch (Exception e) { // Aborting the thread mid-read will // cause an IOException to be thorwn, // which sets the ThreadAbortException // as its InnerException.MemoryStream if (!(e is IOException || e is ThreadAbortException)) { throw; } // Reset the unsightly ThreadAbortException Thread.ResetAbort(); Logger.Log.Debug("Bailing out of HandleConnection -- shutdown requested"); this.thread = null; Server.MarkHandlerAsKilled(this); Shutdown.WorkerFinished(network_data); return; } total_bytes += bytes_read; if (bytes_read > 0) { // 0xff signifies end of message end_index = Array.IndexOf <byte> (network_data, (byte)0xff); buffer_stream.Write(network_data, 0, end_index == -1 ? bytes_read : end_index); } } while (bytes_read > 0 && end_index == -1); // Something just connected to our socket and then // hung up. The IndexHelper (among other things) does // this to check that a server is still running. It's // no big deal, so just clean up and close without // running any handlers. if (total_bytes == 0) { force_close_connection = true; goto cleanup; } buffer_stream.Seek(0, SeekOrigin.Begin); HandleConnection(buffer_stream); cleanup: buffer_stream.Close(); if (force_close_connection) { Close(); } else { SetupWatch(); } Server.MarkHandlerAsKilled(this); Shutdown.WorkerFinished(network_data); }
public override void HandleConnection() { //Logger.Log.Debug ("HTTP Server: Serving request for {0}", context.Request.Url); // Query request: read content and forward to base.HandleConnection for processing context.Response.KeepAlive = true; context.Response.ContentType = "text/txt; charset=utf-8"; context.Response.SendChunked = true; Shutdown.WorkerStart(this.context.Request.InputStream, String.Format("HandleConnection ({0})", ++connection_count)); // Read the data off the socket and store it in a // temporary memory buffer. Once the end-of-message // character has been read, discard remaining data // and deserialize the request. byte[] network_data = new byte [4096]; MemoryStream buffer_stream = new MemoryStream(); int bytes_read, total_bytes = 0, end_index = -1; do { bytes_read = 0; try { lock (this.blocking_read_lock) this.in_blocking_read = true; lock (this.client_lock) { // The connection may have been closed within this loop. if (this.context != null) { bytes_read = this.context.Request.InputStream.Read(network_data, 0, 4096); } } lock (this.blocking_read_lock) this.in_blocking_read = false; } catch (Exception e) { // Aborting the thread mid-read will // cause an IOException to be thorwn, // which sets the ThreadAbortException // as its InnerException.MemoryStream if (!(e is IOException || e is ThreadAbortException)) { throw; } // Reset the unsightly ThreadAbortException Thread.ResetAbort(); Logger.Log.Debug("Bailing out of HandleConnection -- shutdown requested"); this.thread = null; Server.MarkHandlerAsKilled(this); Shutdown.WorkerFinished(context.Request.InputStream); return; } total_bytes += bytes_read; if (bytes_read > 0) { // 0xff signifies end of message end_index = Array.IndexOf <byte> (network_data, (byte)0xff); buffer_stream.Write(network_data, 0, end_index == -1 ? bytes_read : end_index); } } while (bytes_read > 0 && end_index == -1); //Logger.Log.Debug ("HTTP Server: Handling received request message"); // The 0xff bytes from a remote beagrepd comes in a separate http request // and causes havoc by creating empty messages. HTTP streams do not behave // like a continous stream like UnixStream if (total_bytes > 0) { buffer_stream.Seek(0, SeekOrigin.Begin); base.HandleConnection(buffer_stream); } Server.MarkHandlerAsKilled(this); Shutdown.WorkerFinished(context.Request.InputStream); }