wasSharpNET – Blame information for rev 19

Subversion Repositories:
Rev:
Rev Author Line No. Line
9 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.Collections.Generic;
9 using System.Net;
10 using System.Threading;
11  
12 namespace wasSharpNET.Network.HTTP
13 {
15 office 14 public abstract class HTTPServer : IDisposable
9 office 15 {
16 private int activeRequests;
11 office 17  
19 office 18 private HttpListener HTTPListener = null;
11 office 19  
9 office 20 private int processedRequests;
21  
19 office 22 public AuthenticationSchemes AuthenticationSchemes { get; set; } =
23 AuthenticationSchemes.None;
9 office 24  
19 office 25 public bool IsRunning => HTTPListener != null && HTTPListener.IsListening;
9 office 26  
10 office 27 public bool Start(IEnumerable<string> prefixes)
28 {
19 office 29 HTTPListener = new HttpListener()
30 {
31 AuthenticationSchemes = AuthenticationSchemes
32 };
9 office 33  
10 office 34 // Add all prefixes.
16 office 35 HTTPListener.Prefixes.Clear();
10 office 36 foreach (var prefix in prefixes)
37 {
38 HTTPListener.Prefixes.Add(prefix);
39 }
40  
9 office 41 // Do not bomb if the client disconnects.
42 HTTPListener.IgnoreWriteExceptions = true;
43 HTTPListener.Start();
10 office 44 ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener));
9 office 45 return true;
46 }
47  
19 office 48 public void Stop()
9 office 49 {
16 office 50 HTTPListener.Stop();
9 office 51 }
52  
53 private void Listen(object state)
54 {
11 office 55 var callbackState = (HTTPServerCallbackState)state;
9 office 56  
57 while (callbackState.Listener.IsListening)
58 {
59 callbackState.Listener.BeginGetContext(ContextCallback, callbackState);
16 office 60 callbackState.ContextRetrieved.WaitOne();
9 office 61 }
62 }
63  
64 public abstract void ProcessHTTPContext(HttpListenerContext context);
65  
66 private void ContextCallback(IAsyncResult ar)
67 {
11 office 68 var callbackState = (HTTPServerCallbackState)ar.AsyncState;
9 office 69 HttpListenerContext httpContext = null;
70  
71 Interlocked.Increment(ref processedRequests);
72 Interlocked.Increment(ref activeRequests);
73  
74 try
75 {
76 httpContext = callbackState.Listener.EndGetContext(ar);
77 }
78 catch (Exception)
79 {
80 Interlocked.Decrement(ref activeRequests);
81 return;
82 }
83 finally
84 {
85 callbackState.ContextRetrieved.Set();
86 }
87  
88 if (httpContext == null)
89 return;
90  
91 try
92 {
93 ProcessHTTPContext(httpContext);
94 }
95 finally
96 {
97 Interlocked.Decrement(ref activeRequests);
98 }
99 }
100  
15 office 101 public void Dispose()
102 {
103 Stop();
104 HTTPListener = null;
105 }
106  
9 office 107 private class HTTPServerCallbackState
108 {
109 public HTTPServerCallbackState(HttpListener listener)
110 {
111 if (listener == null)
112 throw new ArgumentNullException("listener");
113 Listener = listener;
114 ContextRetrieved = new AutoResetEvent(false);
115 }
116  
117 public HttpListener Listener { get; }
118  
119 public AutoResetEvent ContextRetrieved { get; }
120 }
121 }
11 office 122 }