wasSharpNET – Diff between revs 22 and 27

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 22 Rev 27
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
6   6  
7 using System; 7 using System;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
9 using System.Net; 9 using System.Net;
10 using System.Threading; 10 using System.Threading;
11   11  
12 namespace wasSharpNET.Network.HTTP 12 namespace wasSharpNET.Network.HTTP
13 { 13 {
14 public abstract class HTTPServer : IDisposable 14 public abstract class HTTPServer : IDisposable
15 { 15 {
-   16 private readonly ManualResetEventSlim listenStop = new ManualResetEventSlim(false);
-   17  
-   18 private readonly ManualResetEventSlim stopListen = new ManualResetEventSlim(false);
16 private int activeRequests; 19 private int activeRequests;
17   20  
18 private HttpListener HTTPListener; 21 private HttpListener HTTPListener;
19   22  
20 private int processedRequests; 23 private int processedRequests;
21   24  
22 public AuthenticationSchemes AuthenticationSchemes { get; set; } = 25 public AuthenticationSchemes AuthenticationSchemes { get; set; } =
23 AuthenticationSchemes.None; 26 AuthenticationSchemes.None;
24   27  
25 public bool IsRunning => HTTPListener != null && HTTPListener.IsListening; 28 public bool IsRunning => HTTPListener != null && HTTPListener.IsListening;
26   29  
-   30 public void Dispose()
27 private readonly ManualResetEventSlim stopListen = new ManualResetEventSlim(false); 31 {
-   32 stopListen.Set();
28 private readonly ManualResetEventSlim listenStop = new ManualResetEventSlim(false); 33 }
29   34  
30 public bool Start(List<string> prefixes) 35 public bool Start(List<string> prefixes)
31 { 36 {
32 HTTPListener = new HttpListener 37 HTTPListener = new HttpListener
33 { 38 {
34 AuthenticationSchemes = AuthenticationSchemes 39 AuthenticationSchemes = AuthenticationSchemes
35 }; 40 };
36   41  
37 // Add all prefixes. 42 // Add all prefixes.
38 HTTPListener.Prefixes.Clear(); 43 HTTPListener.Prefixes.Clear();
39 foreach (var prefix in prefixes) 44 foreach (var prefix in prefixes)
40 { -  
41 HTTPListener.Prefixes.Add(prefix); 45 HTTPListener.Prefixes.Add(prefix);
42 } -  
43   46  
44 // Do not bomb if the client disconnects. 47 // Do not bomb if the client disconnects.
45 HTTPListener.IgnoreWriteExceptions = true; 48 HTTPListener.IgnoreWriteExceptions = true;
46 HTTPListener.Start(); 49 HTTPListener.Start();
47 return ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); 50 return ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener));
48 } 51 }
49   52  
50 public bool Stop(int timeout) 53 public bool Stop(int timeout)
51 { 54 {
52 stopListen.Set(); 55 stopListen.Set();
53 return listenStop.Wait(timeout); 56 return listenStop.Wait(timeout);
54 } 57 }
55   58  
56 private void Listen(object state) 59 private void Listen(object state)
57 { 60 {
58 HTTPServerCallbackState callbackState = (HTTPServerCallbackState)state; 61 var callbackState = (HTTPServerCallbackState) state;
59   62  
60 while (callbackState.Listener.IsListening) 63 while (callbackState.Listener.IsListening)
61 { 64 {
62 callbackState.Listener?.BeginGetContext(ContextCallback, callbackState); 65 callbackState.Listener?.BeginGetContext(ContextCallback, callbackState);
63   66  
64 if (WaitHandle.WaitAny(new[] { callbackState.ContextRetrieved, stopListen.WaitHandle }) != 1) 67 if (WaitHandle.WaitAny(new[] {callbackState.ContextRetrieved, stopListen.WaitHandle}) != 1)
65 continue; 68 continue;
66   69  
67 try 70 try
68 { 71 {
69 callbackState.Listener.Stop(); 72 callbackState.Listener.Stop();
70 callbackState.Listener.Close(); 73 callbackState.Listener.Close();
71 } 74 }
72 finally 75 finally
73 { 76 {
74 listenStop.Set(); 77 listenStop.Set();
75 } 78 }
76 break; 79 break;
77 } 80 }
78 } 81 }
79   82  
80 public abstract void ProcessHTTPContext(HttpListenerContext context); 83 public abstract void ProcessHTTPContext(HttpListenerContext context);
81   84  
82 private void ContextCallback(IAsyncResult ar) 85 private void ContextCallback(IAsyncResult ar)
83 { 86 {
84 var callbackState = (HTTPServerCallbackState)ar.AsyncState; 87 var callbackState = (HTTPServerCallbackState) ar.AsyncState;
85 HttpListenerContext httpContext; 88 HttpListenerContext httpContext;
86   89  
87 Interlocked.Increment(ref processedRequests); 90 Interlocked.Increment(ref processedRequests);
88 Interlocked.Increment(ref activeRequests); 91 Interlocked.Increment(ref activeRequests);
89   92  
90 try 93 try
91 { 94 {
92 httpContext = callbackState.Listener.EndGetContext(ar); 95 httpContext = callbackState.Listener.EndGetContext(ar);
93 } 96 }
94 catch (Exception) 97 catch (Exception)
95 { 98 {
96 Interlocked.Decrement(ref activeRequests); 99 Interlocked.Decrement(ref activeRequests);
97 return; 100 return;
98 } 101 }
99 finally 102 finally
100 { 103 {
101 callbackState.ContextRetrieved.Set(); 104 callbackState.ContextRetrieved.Set();
102 } 105 }
103   106  
104 try 107 try
105 { 108 {
106 ProcessHTTPContext(httpContext); 109 ProcessHTTPContext(httpContext);
107 } 110 }
108 finally 111 finally
109 { 112 {
110 Interlocked.Decrement(ref activeRequests); 113 Interlocked.Decrement(ref activeRequests);
111 } 114 }
112 } 115 }
113   -  
114 public void Dispose() -  
115 { -  
116 stopListen.Set(); -  
117 } -  
118   116  
119 private class HTTPServerCallbackState 117 private class HTTPServerCallbackState
120 { 118 {
121 public HTTPServerCallbackState(HttpListener listener) 119 public HTTPServerCallbackState(HttpListener listener)
122 { 120 {
123 if (listener == null) 121 if (listener == null)
124 throw new ArgumentNullException(nameof(listener)); 122 throw new ArgumentNullException(nameof(listener));
125 Listener = listener; 123 Listener = listener;
126 ContextRetrieved = new AutoResetEvent(false); 124 ContextRetrieved = new AutoResetEvent(false);
127 } 125 }
128   126  
129 public HttpListener Listener { get; } 127 public HttpListener Listener { get; }
130   128  
131 public AutoResetEvent ContextRetrieved { get; } 129 public AutoResetEvent ContextRetrieved { get; }
132 } 130 }
133 } 131 }
134 } 132 }
135   133  
-   134
Generated by GNU Enscript 1.6.5.90.
-   135  
-   136  
-   137