aboutsummaryrefslogtreecommitdiff
path: root/SocketHttpListener/Net/HttpStreamAsyncResult.cs
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2017-05-31 15:40:34 -0400
committerGitHub <noreply@github.com>2017-05-31 15:40:34 -0400
commit91176d9ccc1dde8155c10411c70e62a9f4b059d5 (patch)
tree21365f5a8dd09534a53d9f88d2a7a3116f3f3f98 /SocketHttpListener/Net/HttpStreamAsyncResult.cs
parentc37c9a75073b1b9caa3af2c3bc62abd837bd630e (diff)
parent4e10daf646e0788409f2bc52ef70effa2616e3f3 (diff)
Merge pull request #2677 from MediaBrowser/beta
Beta
Diffstat (limited to 'SocketHttpListener/Net/HttpStreamAsyncResult.cs')
-rw-r--r--SocketHttpListener/Net/HttpStreamAsyncResult.cs85
1 files changed, 85 insertions, 0 deletions
diff --git a/SocketHttpListener/Net/HttpStreamAsyncResult.cs b/SocketHttpListener/Net/HttpStreamAsyncResult.cs
new file mode 100644
index 000000000..e7e516c6b
--- /dev/null
+++ b/SocketHttpListener/Net/HttpStreamAsyncResult.cs
@@ -0,0 +1,85 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace SocketHttpListener.Net
+{
+ internal class HttpStreamAsyncResult : IAsyncResult
+ {
+ private object _locker = new object();
+ private ManualResetEvent _handle;
+ private bool _completed;
+
+ internal readonly object _parent;
+ internal byte[] _buffer;
+ internal int _offset;
+ internal int _count;
+ internal AsyncCallback _callback;
+ internal object _state;
+ internal int _synchRead;
+ internal Exception _error;
+ internal bool _endCalled;
+
+ internal HttpStreamAsyncResult(object parent)
+ {
+ _parent = parent;
+ }
+
+ public void Complete(Exception e)
+ {
+ _error = e;
+ Complete();
+ }
+
+ public void Complete()
+ {
+ lock (_locker)
+ {
+ if (_completed)
+ return;
+
+ _completed = true;
+ if (_handle != null)
+ _handle.Set();
+
+ if (_callback != null)
+ Task.Run(() => _callback(this));
+ }
+ }
+
+ public object AsyncState
+ {
+ get { return _state; }
+ }
+
+ public WaitHandle AsyncWaitHandle
+ {
+ get
+ {
+ lock (_locker)
+ {
+ if (_handle == null)
+ _handle = new ManualResetEvent(_completed);
+ }
+
+ return _handle;
+ }
+ }
+
+ public bool CompletedSynchronously
+ {
+ get { return (_synchRead == _count); }
+ }
+
+ public bool IsCompleted
+ {
+ get
+ {
+ lock (_locker)
+ {
+ return _completed;
+ }
+ }
+ }
+ }
+}