wasSharpNET – Diff between revs 19 and 20

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 19 Rev 20
Line 13... Line 13...
13 { 13 {
14 public abstract class HTTPServer : IDisposable 14 public abstract class HTTPServer : IDisposable
15 { 15 {
16 private int activeRequests; 16 private int activeRequests;
Line 17... Line 17...
17   17  
Line 18... Line 18...
18 private HttpListener HTTPListener = null; 18 private HttpListener HTTPListener;
Line 19... Line 19...
19   19  
20 private int processedRequests; 20 private int processedRequests;
Line 21... Line 21...
21   21  
Line -... Line 22...
-   22 public AuthenticationSchemes AuthenticationSchemes { get; set; } =
-   23 AuthenticationSchemes.None;
22 public AuthenticationSchemes AuthenticationSchemes { get; set; } = 24  
23 AuthenticationSchemes.None; 25 public bool IsRunning => HTTPListener != null && HTTPListener.IsListening;
24   26  
25 public bool IsRunning => HTTPListener != null && HTTPListener.IsListening; 27 private readonly ManualResetEventSlim stopListen = new ManualResetEventSlim(false);
26   28  
27 public bool Start(IEnumerable<string> prefixes) 29 public bool Start(List<string> prefixes)
Line 28... Line 30...
28 { 30 {
Line 39... Line 41...
39 } 41 }
Line 40... Line 42...
40   42  
41 // Do not bomb if the client disconnects. 43 // Do not bomb if the client disconnects.
42 HTTPListener.IgnoreWriteExceptions = true; 44 HTTPListener.IgnoreWriteExceptions = true;
43 HTTPListener.Start(); 45 HTTPListener.Start();
44 ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); -  
45 return true; 46 return ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener));
Line 46... Line 47...
46 } 47 }
47   48  
48 public void Stop() 49 public void Stop()
49 { 50 {
Line 50... Line 51...
50 HTTPListener.Stop(); 51 stopListen.Set();
51 } 52 }
52   53  
Line 53... Line 54...
53 private void Listen(object state) 54 private void Listen(object state)
54 { 55 {
55 var callbackState = (HTTPServerCallbackState)state; 56 HTTPServerCallbackState callbackState = (HTTPServerCallbackState)state;
-   57  
-   58 while (callbackState.Listener.IsListening)
-   59 {
-   60 callbackState.Listener.BeginGetContext(ContextCallback, callbackState);
56   61  
-   62 if (WaitHandle.WaitAny(new[] { callbackState.ContextRetrieved, stopListen.WaitHandle }) != 1)
57 while (callbackState.Listener.IsListening) 63 continue;
58 { 64  
Line 59... Line 65...
59 callbackState.Listener.BeginGetContext(ContextCallback, callbackState); 65 callbackState.Listener.Stop();
Line 60... Line 66...
60 callbackState.ContextRetrieved.WaitOne(); 66 break;
61 } 67 }
62 } 68 }
63   69  
Line 64... Line 70...
64 public abstract void ProcessHTTPContext(HttpListenerContext context); 70 public abstract void ProcessHTTPContext(HttpListenerContext context);
65   71  
Line 66... Line 72...
66 private void ContextCallback(IAsyncResult ar) 72 private void ContextCallback(IAsyncResult ar)
Line 83... Line 89...
83 finally 89 finally
84 { 90 {
85 callbackState.ContextRetrieved.Set(); 91 callbackState.ContextRetrieved.Set();
86 } 92 }
Line 87... Line -...
87   -  
88 if (httpContext == null) -  
89 return; -  
90   93  
91 try 94 try
92 { 95 {
93 ProcessHTTPContext(httpContext); 96 ProcessHTTPContext(httpContext);
94 } 97 }
Line 99... Line 102...
99 } 102 }
Line 100... Line 103...
100   103  
101 public void Dispose() 104 public void Dispose()
102 { 105 {
103 Stop(); -  
104 HTTPListener = null; 106 Stop();
Line 105... Line 107...
105 } 107 }
106   108  
107 private class HTTPServerCallbackState 109 private class HTTPServerCallbackState
108 { 110 {
109 public HTTPServerCallbackState(HttpListener listener) 111 public HTTPServerCallbackState(HttpListener listener)
110 { 112 {
111 if (listener == null) 113 if (listener == null)
112 throw new ArgumentNullException("listener"); 114 throw new ArgumentNullException(nameof(listener));
113 Listener = listener; 115 Listener = listener;
Line 114... Line 116...
114 ContextRetrieved = new AutoResetEvent(false); 116 ContextRetrieved = new AutoResetEvent(false);