wasSharpNET – Diff between revs 9 and 10
?pathlinks?
Rev 9 | Rev 10 | |||
---|---|---|---|---|
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 |
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; |
|
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 | |
- | ||
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; |
||
33 | HTTPListener.Prefixes.Add(prefix); |
35 | } |
|
34 | } |
- | ||
- | 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) |
|
38 | |
41 | { |
|
39 | // Do not start the HTTP server if it is already running. |
42 | // Do not start the HTTP server if it is already running. |
|
40 | if (HTTPListener.IsListening) |
43 | if (HTTPListener.IsListening) |
|
41 | return false; |
44 | return false; |
|
- | 45 | |
||
- | 46 | // Add all prefixes. |
||
- | 47 | foreach (var prefix in prefixes) |
||
- | 48 | { |
||
- | 49 | HTTPListener.Prefixes.Add(prefix); |
||
- | 50 | } |
||
42 | |
51 | |
|
43 | // Do not bomb if the client disconnects. |
52 | // Do not bomb if the client disconnects. |
|
44 | HTTPListener.IgnoreWriteExceptions = true; |
53 | HTTPListener.IgnoreWriteExceptions = true; |
|
45 | HTTPListener.Start(); |
54 | HTTPListener.Start(); |
|
46 | var state = new HTTPServerCallbackState(HTTPListener); |
55 | ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); |
|
47 | ThreadPool.QueueUserWorkItem(Listen, state); |
- | ||
48 | return true; |
56 | return true; |
|
49 | } |
57 | } |
|
50 | |
58 | |
|
51 | public bool Stop() |
59 | public bool Stop() |
|
52 | { |
60 | { |
|
53 | return stopEvent.Set(); |
61 | StopServerEvent.Set(); |
|
- | 62 | ServerStoppedEvent.WaitOne(); |
||
- | 63 | HTTPListener.Prefixes.Clear(); |
||
- | 64 | return true; |
||
54 | } |
65 | } |
|
55 | |
66 | |
|
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 | { |
|
62 | callbackState.Listener.BeginGetContext(ContextCallback, callbackState); |
73 | callbackState.Listener.BeginGetContext(ContextCallback, callbackState); |
|
63 | var n = WaitHandle.WaitAny(new WaitHandle[] {callbackState.ContextRetrieved, stopEvent}); |
74 | var n = WaitHandle.WaitAny(new WaitHandle[] {callbackState.ContextRetrieved, StopServerEvent}); |
|
64 | |
75 | |
|
65 | if (n.Equals(1)) |
76 | if (n.Equals(1)) |
|
66 | { |
77 | { |
|
67 | callbackState.Listener.Stop(); |
78 | callbackState.Listener.Stop(); |
|
68 | break; |
79 | break; |
|
69 | } |
80 | } |
|
70 | } |
81 | } |
|
- | 82 | |
||
- | 83 | ServerStoppedEvent.Set(); |
||
71 | } |
84 | } |
|
72 | |
85 | |
|
73 | public abstract void ProcessHTTPContext(HttpListenerContext context); |
86 | public abstract void ProcessHTTPContext(HttpListenerContext context); |
|
74 | |
87 | |
|
75 | private void ContextCallback(IAsyncResult ar) |
88 | private void ContextCallback(IAsyncResult ar) |
|
76 | { |
89 | { |
|
77 | var callbackState = (HTTPServerCallbackState) ar.AsyncState; |
90 | var callbackState = (HTTPServerCallbackState) ar.AsyncState; |
|
78 | HttpListenerContext httpContext = null; |
91 | HttpListenerContext httpContext = null; |
|
79 | |
92 | |
|
80 | Interlocked.Increment(ref processedRequests); |
93 | Interlocked.Increment(ref processedRequests); |
|
81 | Interlocked.Increment(ref activeRequests); |
94 | Interlocked.Increment(ref activeRequests); |
|
82 | |
95 | |
|
83 | try |
96 | try |
|
84 | { |
97 | { |
|
85 | httpContext = callbackState.Listener.EndGetContext(ar); |
98 | httpContext = callbackState.Listener.EndGetContext(ar); |
|
86 | } |
99 | } |
|
87 | catch (Exception) |
100 | catch (Exception) |
|
88 | { |
101 | { |
|
89 | Interlocked.Decrement(ref activeRequests); |
102 | Interlocked.Decrement(ref activeRequests); |
|
90 | return; |
103 | return; |
|
91 | } |
104 | } |
|
92 | finally |
105 | finally |
|
93 | { |
106 | { |
|
94 | callbackState.ContextRetrieved.Set(); |
107 | callbackState.ContextRetrieved.Set(); |
|
95 | } |
108 | } |
|
96 | |
109 | |
|
97 | if (httpContext == null) |
110 | if (httpContext == null) |
|
98 | return; |
111 | return; |
|
99 | |
112 | |
|
100 | try |
113 | try |
|
101 | { |
114 | { |
|
102 | ProcessHTTPContext(httpContext); |
115 | ProcessHTTPContext(httpContext); |
|
103 | } |
116 | } |
|
104 | finally |
117 | finally |
|
105 | { |
118 | { |
|
106 | Interlocked.Decrement(ref activeRequests); |
119 | Interlocked.Decrement(ref activeRequests); |
|
107 | } |
120 | } |
|
108 | } |
121 | } |
|
109 | |
122 | |
|
110 | private class HTTPServerCallbackState |
123 | private class HTTPServerCallbackState |
|
111 | { |
124 | { |
|
112 | public HTTPServerCallbackState(HttpListener listener) |
125 | public HTTPServerCallbackState(HttpListener listener) |
|
113 | { |
126 | { |
|
114 | if (listener == null) |
127 | if (listener == null) |
|
115 | throw new ArgumentNullException("listener"); |
128 | throw new ArgumentNullException("listener"); |
|
116 | Listener = listener; |
129 | Listener = listener; |
|
117 | ContextRetrieved = new AutoResetEvent(false); |
130 | ContextRetrieved = new AutoResetEvent(false); |
|
118 | } |
131 | } |
|
119 | |
132 | |
|
120 | public HttpListener Listener { get; } |
133 | public HttpListener Listener { get; } |
|
121 | |
134 | |
|
122 | public AutoResetEvent ContextRetrieved { get; } |
135 | public AutoResetEvent ContextRetrieved { get; } |
|
123 | } |
136 | } |
|
124 | } |
137 | } |
|
125 | } |
138 | } |
|
126 | |
139 | |
|
127 |
|
140 |
|
|
128 | |
141 | |
|
129 | |
142 | |
|
130 | |
143 | |