wasSharpNET – Diff between revs 27 and 28

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