This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
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 #158 from North-Two-Five/issue#154-Catch-PostAsync…
…-Network-exceptions-and-Retry Issue#154 catch post async network exceptions and retry
- Loading branch information
Showing
9 changed files
with
469 additions
and
21 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
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
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
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading; | ||
|
||
namespace Segment.Test | ||
{ | ||
class WebServer :IDisposable | ||
{ | ||
private HttpListener listener; | ||
private Thread runningThread; | ||
private bool runServer; | ||
private string address; | ||
|
||
public delegate void HandleRequest(HttpListenerRequest req, HttpListenerResponse res); | ||
public HandleRequest RequestHandler = null; | ||
|
||
public WebServer(string url) | ||
{ | ||
// Only string ends with '/' is acceptable for web server url | ||
address = url; | ||
if (!address.EndsWith("/")) | ||
address += "/"; | ||
} | ||
|
||
public void HandleIncomingConnections() | ||
{ | ||
while (runServer) | ||
{ | ||
try | ||
{ | ||
// Will wait here until we hear from a connection | ||
HttpListenerContext ctx = listener.GetContext(); | ||
|
||
// Peel out the requests and response objects | ||
HttpListenerRequest req = ctx.Request; | ||
HttpListenerResponse res = ctx.Response; | ||
|
||
if (RequestHandler != null) | ||
{ | ||
RequestHandler(req, res); | ||
} | ||
else | ||
{ | ||
// Write the response info | ||
string pageData = "{}"; | ||
byte[] data = Encoding.UTF8.GetBytes(pageData); | ||
res.ContentType = "application/json"; | ||
res.ContentEncoding = Encoding.UTF8; | ||
res.ContentLength64 = data.LongLength; | ||
|
||
// Write out to the response stream (asynchronously), then close it | ||
res.OutputStream.Write(data, 0, data.Length); | ||
res.Close(); | ||
} | ||
} | ||
catch (System.Exception) | ||
{ | ||
runServer = false; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public void RunAsync() | ||
{ | ||
// Stop already running server | ||
Stop(); | ||
|
||
// Create new listener | ||
listener = new HttpListener(); | ||
listener.Prefixes.Add(address); | ||
listener.Start(); | ||
|
||
// Start listening requests | ||
runServer = true; | ||
runningThread = new Thread(HandleIncomingConnections); | ||
runningThread.Start(); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
runServer = false; | ||
|
||
if (listener != null) | ||
{ | ||
listener.Close(); | ||
listener = null; | ||
} | ||
if (runningThread != null) | ||
{ | ||
runningThread.Join(); | ||
runningThread = null; | ||
} | ||
} | ||
|
||
#region IDisposable Support | ||
private bool disposedValue = false; // To detect redundant calls | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
Stop(); | ||
} | ||
|
||
disposedValue = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
} | ||
#endregion | ||
} | ||
} |
Oops, something went wrong.