wasSharpNET – Diff between revs 11 and 15

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