wasSharpNET – Diff between revs 9 and 10

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 9 Rev 10
Line 12... Line 12...
12 namespace wasSharpNET.Network.HTTP 12 namespace wasSharpNET.Network.HTTP
13 { 13 {
14 public abstract class HTTPServer 14 public abstract class HTTPServer
15 { 15 {
16 private int activeRequests; 16 private int activeRequests;
17 private readonly HttpListener HTTPListener = new HttpListener(); 17 private readonly HttpListener HTTPListener = new HttpListener()
-   18 {
-   19 AuthenticationSchemes = AuthenticationSchemes.None
-   20 };
18 private int processedRequests; 21 private int processedRequests;
Line 19... Line 22...
19   22  
20 private readonly ManualResetEvent stopEvent = new ManualResetEvent(false); -  
21   23 private readonly AutoResetEvent StopServerEvent = new AutoResetEvent(false);
22 public HashSet<string> Prefixes { get; set; } = new HashSet<string>(); -  
23   -  
24 public AuthenticationSchemes AuthenticationSchemes { get; set; } = AuthenticationSchemes.None; -  
25   -  
Line 26... Line 24...
26 public bool IsRunning => HTTPListener.IsListening; 24 private readonly AutoResetEvent ServerStoppedEvent = new AutoResetEvent(false);
27   25  
-   26 public AuthenticationSchemes AuthenticationSchemes
-   27 {
28 public bool Start() 28 get
-   29 {
-   30 return HTTPListener.AuthenticationSchemes;
29 { 31 }
30 foreach (var prefix in Prefixes) 32 set
31 { -  
32 if (!HTTPListener.Prefixes.Contains(prefix)) 33 {
-   34 HTTPListener.AuthenticationSchemes = value;
Line 33... Line 35...
33 HTTPListener.Prefixes.Add(prefix); 35 }
34 } -  
Line -... Line 36...
-   36 }
-   37  
35   38 public bool IsRunning => HTTPListener.IsListening;
36 // Set up HTTP listener. 39  
37 HTTPListener.AuthenticationSchemes = AuthenticationSchemes; 40 public bool Start(IEnumerable<string> prefixes)
Line -... Line 41...
-   41 {
-   42 // Do not start the HTTP server if it is already running.
-   43 if (HTTPListener.IsListening)
-   44 return false;
-   45  
-   46 // Add all prefixes.
38   47 foreach (var prefix in prefixes)
39 // Do not start the HTTP server if it is already running. 48 {
40 if (HTTPListener.IsListening) 49 HTTPListener.Prefixes.Add(prefix);
41 return false; 50 }
42   -  
43 // Do not bomb if the client disconnects. 51  
44 HTTPListener.IgnoreWriteExceptions = true; 52 // Do not bomb if the client disconnects.
Line 45... Line 53...
45 HTTPListener.Start(); 53 HTTPListener.IgnoreWriteExceptions = true;
46 var state = new HTTPServerCallbackState(HTTPListener); 54 HTTPListener.Start();
47 ThreadPool.QueueUserWorkItem(Listen, state); 55 ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener));
-   56 return true;
-   57 }
-   58  
48 return true; 59 public bool Stop()
Line 49... Line 60...
49 } 60 {
50   61 StopServerEvent.Set();
51 public bool Stop() 62 ServerStoppedEvent.WaitOne();
Line 52... Line 63...
52 { 63 HTTPListener.Prefixes.Clear();
53 return stopEvent.Set(); 64 return true;
54 } 65 }
55   66  
Line 56... Line 67...
56 private void Listen(object state) 67 private void Listen(object state)
57 { 68 {
58 var callbackState = (HTTPServerCallbackState) state; 69 var callbackState = (HTTPServerCallbackState) state;
59   70  
60 while (callbackState.Listener.IsListening) 71 while (callbackState.Listener.IsListening)
61 { 72 {
-   73 callbackState.Listener.BeginGetContext(ContextCallback, callbackState);
-   74 var n = WaitHandle.WaitAny(new WaitHandle[] {callbackState.ContextRetrieved, StopServerEvent});
62 callbackState.Listener.BeginGetContext(ContextCallback, callbackState); 75  
Line 63... Line 76...
63 var n = WaitHandle.WaitAny(new WaitHandle[] {callbackState.ContextRetrieved, stopEvent}); 76 if (n.Equals(1))
Line 64... Line 77...
64   77 {