-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #660 from issafram/SimpleServerFix
SimpleServer Cleanup With Added Try Catches
- Loading branch information
Showing
1 changed file
with
57 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,81 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading; | ||
|
||
namespace RestSharp.IntegrationTests.Helpers | ||
namespace RestSharp.IntegrationTests.Helpers | ||
{ | ||
using System; | ||
using System.Net; | ||
using System.Security; | ||
using System.Threading; | ||
|
||
public class SimpleServer : IDisposable | ||
{ | ||
private readonly HttpListener _listener; | ||
private readonly Action<HttpListenerContext> _handler; | ||
private Thread _processor; | ||
private readonly HttpListener listener; | ||
private readonly Action<HttpListenerContext> handler; | ||
private Thread thread; | ||
|
||
private SimpleServer(HttpListener listener, Action<HttpListenerContext> handler) | ||
{ | ||
this.listener = listener; | ||
this.handler = handler; | ||
} | ||
|
||
public static SimpleServer Create(string url, Action<HttpListenerContext> handler, | ||
public static SimpleServer Create( | ||
string url, | ||
Action<HttpListenerContext> handler, | ||
AuthenticationSchemes authenticationSchemes = AuthenticationSchemes.Anonymous) | ||
{ | ||
var listener = new HttpListener | ||
{ | ||
Prefixes = {url}, | ||
AuthenticationSchemes = authenticationSchemes | ||
}; | ||
var listener = new HttpListener { Prefixes = { url }, AuthenticationSchemes = authenticationSchemes }; | ||
var server = new SimpleServer(listener, handler); | ||
|
||
server.Start(); | ||
|
||
return server; | ||
} | ||
|
||
private SimpleServer(HttpListener listener, Action<HttpListenerContext> handler) | ||
{ | ||
_listener = listener; | ||
_handler = handler; | ||
} | ||
|
||
public void Start() | ||
{ | ||
if (!_listener.IsListening) | ||
if (this.listener.IsListening) | ||
{ | ||
_listener.Start(); | ||
return; | ||
} | ||
|
||
_processor = new Thread(() => | ||
{ | ||
var context = _listener.GetContext(); | ||
_handler(context); | ||
context.Response.Close(); | ||
}) {Name = "WebServer"}; | ||
this.listener.Start(); | ||
|
||
_processor.Start(); | ||
} | ||
this.thread = new Thread(() => | ||
{ | ||
var context = this.listener.GetContext(); | ||
this.handler(context); | ||
context.Response.Close(); | ||
}) { Name = "WebServer" }; | ||
|
||
this.thread.Start(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_processor.Abort(); | ||
_listener.Stop(); | ||
_listener.Close(); | ||
try | ||
{ | ||
this.thread.Abort(); | ||
} | ||
catch (ThreadStateException threadStateException) | ||
{ | ||
Console.WriteLine("Issue aborting thread - {0}.", threadStateException.Message); | ||
} | ||
catch (SecurityException securityException) | ||
{ | ||
Console.WriteLine("Issue aborting thread - {0}.", securityException.Message); | ||
} | ||
|
||
if (this.listener.IsListening) | ||
{ | ||
try | ||
{ | ||
this.listener.Stop(); | ||
} | ||
catch (ObjectDisposedException objectDisposedException) | ||
{ | ||
Console.WriteLine("Issue stopping listener - {0}", objectDisposedException.Message); | ||
} | ||
} | ||
|
||
this.listener.Close(); | ||
} | ||
} | ||
} |