This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented timeouts on reading from and writing to a TCP connection. Bug: #5
- Loading branch information
Showing
5 changed files
with
170 additions
and
4 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,134 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
|
||
namespace NHttp | ||
{ | ||
internal class HttpTimeoutManager : IDisposable | ||
{ | ||
private Thread _thread; | ||
private ManualResetEvent _closeEvent = new ManualResetEvent(false); | ||
|
||
public TimeoutQueue ReadQueue { get; private set; } | ||
public TimeoutQueue WriteQueue { get; private set; } | ||
|
||
public HttpTimeoutManager(HttpServer server) | ||
{ | ||
if (server == null) | ||
throw new ArgumentNullException(nameof(server)); | ||
|
||
ReadQueue = new TimeoutQueue(server.ReadTimeout); | ||
WriteQueue = new TimeoutQueue(server.WriteTimeout); | ||
|
||
_thread = new Thread(ThreadProc); | ||
_thread.Start(); | ||
} | ||
|
||
private void ThreadProc() | ||
{ | ||
while (!_closeEvent.WaitOne(TimeSpan.FromSeconds(1))) | ||
{ | ||
ProcessQueue(ReadQueue); | ||
ProcessQueue(WriteQueue); | ||
} | ||
} | ||
|
||
private void ProcessQueue(TimeoutQueue queue) | ||
{ | ||
while (true) | ||
{ | ||
var item = queue.DequeueExpired(); | ||
if (item == null) | ||
return; | ||
|
||
if (!item.AsyncResult.IsCompleted) | ||
{ | ||
try | ||
{ | ||
item.Disposable.Dispose(); | ||
} | ||
catch | ||
{ | ||
// Ignore exceptions. | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_thread != null) | ||
{ | ||
_closeEvent.Set(); | ||
_thread.Join(); | ||
_thread = null; | ||
} | ||
if (_closeEvent != null) | ||
{ | ||
_closeEvent.Close(); | ||
_closeEvent = null; | ||
} | ||
} | ||
|
||
public class TimeoutQueue | ||
{ | ||
private readonly object _syncRoot = new object(); | ||
private readonly Stopwatch _stopwatch = Stopwatch.StartNew(); | ||
private readonly long _timeout; | ||
private readonly Queue<TimeoutItem> _items = new Queue<TimeoutItem>(); | ||
|
||
public TimeoutQueue(TimeSpan timeout) | ||
{ | ||
_timeout = (long)(timeout.TotalSeconds * Stopwatch.Frequency); | ||
} | ||
|
||
public void Add(IAsyncResult asyncResult, IDisposable disposable) | ||
{ | ||
if (asyncResult == null) | ||
throw new ArgumentNullException(nameof(asyncResult)); | ||
if (disposable == null) | ||
throw new ArgumentNullException(nameof(disposable)); | ||
|
||
lock (_syncRoot) | ||
{ | ||
_items.Enqueue(new TimeoutItem(_stopwatch.ElapsedTicks + _timeout, asyncResult, disposable)); | ||
} | ||
} | ||
|
||
public TimeoutItem DequeueExpired() | ||
{ | ||
lock (_syncRoot) | ||
{ | ||
if (_items.Count == 0) | ||
return null; | ||
|
||
var item = _items.Peek(); | ||
if (item.Expires < _stopwatch.ElapsedTicks) | ||
return _items.Dequeue(); | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
|
||
public class TimeoutItem | ||
{ | ||
public long Expires { get; private set; } | ||
public IAsyncResult AsyncResult { get; private set; } | ||
public IDisposable Disposable { get; private set; } | ||
|
||
public TimeoutItem(long expires, IAsyncResult asyncResult, IDisposable disposable) | ||
{ | ||
if (asyncResult == null) | ||
throw new ArgumentNullException(nameof(asyncResult)); | ||
|
||
Expires = expires; | ||
AsyncResult = asyncResult; | ||
Disposable = disposable; | ||
} | ||
} | ||
} | ||
} |
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