wasSharpNET – Diff between revs 15 and 16

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