wasSharpNET – Diff between revs 10 and 11

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