2017-05-24 12:12:55 -07:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:31:14 -07:00
|
|
|
public object AsyncState => _state;
|
2017-05-24 12:12:55 -07:00
|
|
|
|
|
|
|
public WaitHandle AsyncWaitHandle
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
lock (_locker)
|
|
|
|
{
|
|
|
|
if (_handle == null)
|
|
|
|
_handle = new ManualResetEvent(_completed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 07:50:54 -07:00
|
|
|
public bool CompletedSynchronously => false;
|
2017-05-24 12:12:55 -07:00
|
|
|
|
|
|
|
public bool IsCompleted
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
lock (_locker)
|
|
|
|
{
|
|
|
|
return _completed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|