wasSharpNET – Diff between revs 16 and 19
?pathlinks?
Rev 16 | Rev 19 | |||
---|---|---|---|---|
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 int activeRequests; |
16 | private int activeRequests; |
|
17 | |
17 | |
|
18 | private HttpListener HTTPListener = new HttpListener() |
- | ||
19 | { |
- | ||
20 | AuthenticationSchemes = AuthenticationSchemes.None |
- | ||
21 | }; |
18 | private HttpListener HTTPListener = null; |
|
22 | |
19 | |
|
23 | private int processedRequests; |
20 | private int processedRequests; |
|
24 | |
21 | |
|
25 | public AuthenticationSchemes AuthenticationSchemes |
- | ||
26 | { |
- | ||
27 | get |
- | ||
28 | { |
- | ||
29 | return HTTPListener.AuthenticationSchemes; |
- | ||
30 | } |
- | ||
31 | set |
- | ||
32 | { |
22 | public AuthenticationSchemes AuthenticationSchemes { get; set; } = |
|
33 | HTTPListener.AuthenticationSchemes = value; |
- | ||
34 | } |
- | ||
35 | } |
23 | AuthenticationSchemes.None; |
|
36 | |
24 | |
|
37 | public bool IsRunning => HTTPListener.IsListening; |
25 | public bool IsRunning => HTTPListener != null && HTTPListener.IsListening; |
|
38 | |
26 | |
|
39 | public bool Start(IEnumerable<string> prefixes) |
27 | public bool Start(IEnumerable<string> prefixes) |
|
40 | { |
28 | { |
|
41 | // Do not start the HTTP server if it is already running. |
29 | HTTPListener = new HttpListener() |
|
- | 30 | { |
||
42 | if (HTTPListener.IsListening) |
31 | AuthenticationSchemes = AuthenticationSchemes |
|
43 | return false; |
32 | }; |
|
44 | |
33 | |
|
45 | // Add all prefixes. |
34 | // Add all prefixes. |
|
46 | HTTPListener.Prefixes.Clear(); |
35 | HTTPListener.Prefixes.Clear(); |
|
47 | foreach (var prefix in prefixes) |
36 | foreach (var prefix in prefixes) |
|
48 | { |
37 | { |
|
49 | HTTPListener.Prefixes.Add(prefix); |
38 | HTTPListener.Prefixes.Add(prefix); |
|
50 | } |
39 | } |
|
51 | |
40 | |
|
52 | // Do not bomb if the client disconnects. |
41 | // Do not bomb if the client disconnects. |
|
53 | HTTPListener.IgnoreWriteExceptions = true; |
42 | HTTPListener.IgnoreWriteExceptions = true; |
|
54 | HTTPListener.Start(); |
43 | HTTPListener.Start(); |
|
55 | ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); |
44 | ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); |
|
56 | return true; |
45 | return true; |
|
57 | } |
46 | } |
|
58 | |
47 | |
|
59 | public bool Stop() |
48 | public void Stop() |
|
60 | { |
49 | { |
|
61 | HTTPListener.Stop(); |
- | ||
62 | return true; |
50 | HTTPListener.Stop(); |
|
63 | } |
51 | } |
|
64 | |
52 | |
|
65 | private void Listen(object state) |
53 | private void Listen(object state) |
|
66 | { |
54 | { |
|
67 | var callbackState = (HTTPServerCallbackState)state; |
55 | var callbackState = (HTTPServerCallbackState)state; |
|
68 | |
56 | |
|
69 | while (callbackState.Listener.IsListening) |
57 | while (callbackState.Listener.IsListening) |
|
70 | { |
58 | { |
|
71 | callbackState.Listener.BeginGetContext(ContextCallback, callbackState); |
59 | callbackState.Listener.BeginGetContext(ContextCallback, callbackState); |
|
72 | callbackState.ContextRetrieved.WaitOne(); |
60 | callbackState.ContextRetrieved.WaitOne(); |
|
73 | } |
61 | } |
|
74 | } |
62 | } |
|
75 | |
63 | |
|
76 | public abstract void ProcessHTTPContext(HttpListenerContext context); |
64 | public abstract void ProcessHTTPContext(HttpListenerContext context); |
|
77 | |
65 | |
|
78 | private void ContextCallback(IAsyncResult ar) |
66 | private void ContextCallback(IAsyncResult ar) |
|
79 | { |
67 | { |
|
80 | var callbackState = (HTTPServerCallbackState)ar.AsyncState; |
68 | var callbackState = (HTTPServerCallbackState)ar.AsyncState; |
|
81 | HttpListenerContext httpContext = null; |
69 | HttpListenerContext httpContext = null; |
|
82 | |
70 | |
|
83 | Interlocked.Increment(ref processedRequests); |
71 | Interlocked.Increment(ref processedRequests); |
|
84 | Interlocked.Increment(ref activeRequests); |
72 | Interlocked.Increment(ref activeRequests); |
|
85 | |
73 | |
|
86 | try |
74 | try |
|
87 | { |
75 | { |
|
88 | httpContext = callbackState.Listener.EndGetContext(ar); |
76 | httpContext = callbackState.Listener.EndGetContext(ar); |
|
89 | } |
77 | } |
|
90 | catch (Exception) |
78 | catch (Exception) |
|
91 | { |
79 | { |
|
92 | Interlocked.Decrement(ref activeRequests); |
80 | Interlocked.Decrement(ref activeRequests); |
|
93 | return; |
81 | return; |
|
94 | } |
82 | } |
|
95 | finally |
83 | finally |
|
96 | { |
84 | { |
|
97 | callbackState.ContextRetrieved.Set(); |
85 | callbackState.ContextRetrieved.Set(); |
|
98 | } |
86 | } |
|
99 | |
87 | |
|
100 | if (httpContext == null) |
88 | if (httpContext == null) |
|
101 | return; |
89 | return; |
|
102 | |
90 | |
|
103 | try |
91 | try |
|
104 | { |
92 | { |
|
105 | ProcessHTTPContext(httpContext); |
93 | ProcessHTTPContext(httpContext); |
|
106 | } |
94 | } |
|
107 | finally |
95 | finally |
|
108 | { |
96 | { |
|
109 | Interlocked.Decrement(ref activeRequests); |
97 | Interlocked.Decrement(ref activeRequests); |
|
110 | } |
98 | } |
|
111 | } |
99 | } |
|
112 | |
100 | |
|
113 | public void Dispose() |
101 | public void Dispose() |
|
114 | { |
102 | { |
|
115 | Stop(); |
103 | Stop(); |
|
116 | HTTPListener = null; |
104 | HTTPListener = null; |
|
117 | } |
105 | } |
|
118 | |
106 | |
|
119 | private class HTTPServerCallbackState |
107 | private class HTTPServerCallbackState |
|
120 | { |
108 | { |
|
121 | public HTTPServerCallbackState(HttpListener listener) |
109 | public HTTPServerCallbackState(HttpListener listener) |
|
122 | { |
110 | { |
|
123 | if (listener == null) |
111 | if (listener == null) |
|
124 | throw new ArgumentNullException("listener"); |
112 | throw new ArgumentNullException("listener"); |
|
125 | Listener = listener; |
113 | Listener = listener; |
|
126 | ContextRetrieved = new AutoResetEvent(false); |
114 | ContextRetrieved = new AutoResetEvent(false); |
|
127 | } |
115 | } |
|
128 | |
116 | |
|
129 | public HttpListener Listener { get; } |
117 | public HttpListener Listener { get; } |
|
130 | |
118 | |
|
131 | public AutoResetEvent ContextRetrieved { get; } |
119 | public AutoResetEvent ContextRetrieved { get; } |
|
132 | } |
120 | } |
|
133 | } |
121 | } |
|
134 | } |
122 | } |
|
135 | |
123 | |