-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
128 additions
and
101 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Threading; | ||
|
||
namespace Radish | ||
{ | ||
public class HttpServerEngine : IDisposable | ||
{ | ||
private readonly HttpListener _listener; | ||
private readonly Thread _listenerThread; | ||
private readonly Thread[] _workers; | ||
private readonly ManualResetEvent _stop, _ready; | ||
private readonly Queue<HttpListenerContext> _queue; | ||
|
||
public HttpServerEngine(int maxThreads) | ||
{ | ||
_workers = new Thread[maxThreads]; | ||
_queue = new Queue<HttpListenerContext>(); | ||
_stop = new ManualResetEvent(false); | ||
_ready = new ManualResetEvent(false); | ||
_listener = new HttpListener(); | ||
_listenerThread = new Thread(HandleRequests); | ||
} | ||
|
||
public void Start(int port) | ||
{ | ||
_listener.Prefixes.Add(String.Format(@"http://+:{0}/", port)); | ||
_listener.Start(); | ||
_listenerThread.Start(); | ||
|
||
for (int i = 0; i < _workers.Length; i++) | ||
{ | ||
_workers[i] = new Thread(Worker); | ||
_workers[i].Start(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ Stop(); } | ||
|
||
public void Stop() | ||
{ | ||
_stop.Set(); | ||
_listenerThread.Join(); | ||
foreach (Thread worker in _workers) | ||
worker.Join(); | ||
_listener.Stop(); | ||
} | ||
|
||
private void HandleRequests() | ||
{ | ||
while (_listener.IsListening) | ||
{ | ||
var context = _listener.BeginGetContext(ContextReady, null); | ||
|
||
if (0 == WaitHandle.WaitAny(new[] { _stop, context.AsyncWaitHandle })) | ||
return; | ||
} | ||
} | ||
|
||
private void ContextReady(IAsyncResult ar) | ||
{ | ||
try | ||
{ | ||
lock (_queue) | ||
{ | ||
_queue.Enqueue(_listener.EndGetContext(ar)); | ||
_ready.Set(); | ||
} | ||
} | ||
catch { return; } | ||
} | ||
|
||
private void Worker() | ||
{ | ||
WaitHandle[] wait = new[] { _ready, _stop }; | ||
while (0 == WaitHandle.WaitAny(wait)) | ||
{ | ||
HttpListenerContext context; | ||
lock (_queue) | ||
{ | ||
if (_queue.Count > 0) | ||
context = _queue.Dequeue(); | ||
else | ||
{ | ||
_ready.Reset(); | ||
continue; | ||
} | ||
} | ||
|
||
try { ProcessRequest(context); } | ||
catch (Exception e) { Console.Error.WriteLine(e); } | ||
} | ||
} | ||
|
||
public event Action<HttpListenerContext> ProcessRequest; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters