async void RequestDispatchThread() { while (_httpListener.IsListening) { try { var context = await _httpListener.GetContextAsync(); context.Response.StatusCode = (int)HttpStatusCode.OK; string queryString = context.Request.RawUrl; Stream backendContent = GetContent(queryString); switch (streamingMode) { case StreamMode.FrameworkDefault: await backendContent.CopyToAsync(context.Response.OutputStream); break; case StreamMode.FrameworkBuffer: await backendContent.CopyToAsync(context.Response.OutputStream, _readChunkSize); break; case StreamMode.MultiWrite: await Stream2StreamCopy.CopyStreamsAsync(backendContent, context.Response.OutputStream); break; } context.Response.OutputStream.Close(); } catch (HttpListenerException) { // Bail out - this happens on shutdown return; } catch (Exception e) { WriteLine("\nUnexpected exception:", ConsoleColor.Red); Console.WriteLine(e.Message + "\n"); } } }
internal Proxy(StreamMode mode, int bufferSize) { streamingMode = mode; _readChunkSize = bufferSize; Stream2StreamCopy.Init(_readChunkSize); Console.Write("\nStreaming mode: "); WriteLine(streamingMode.ToString(), ConsoleColor.Cyan); Console.Write("\nBackend Url: "); WriteLine(BackendUrl, ConsoleColor.Green); Console.Write("Listening at: "); WriteLine(LocalUrl + "\n", ConsoleColor.Green); Console.WriteLine("Buffer size:{0}\n", _readChunkSize); _httpListener.Prefixes.Add(LocalUrl); _httpListener.Start(); _requestThreads = new Thread[RequestDispatchThreadCount]; for (int i = 0; i < _requestThreads.Length; i++) { _requestThreads[i] = new Thread(RequestDispatchThread); _requestThreads[i].Start(); } }