HttpStreamAsyncResult.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace SocketHttpListener.Net
  5. {
  6. internal class HttpStreamAsyncResult : IAsyncResult
  7. {
  8. private object _locker = new object();
  9. private ManualResetEvent _handle;
  10. private bool _completed;
  11. internal readonly object _parent;
  12. internal byte[] _buffer;
  13. internal int _offset;
  14. internal int _count;
  15. internal AsyncCallback _callback;
  16. internal object _state;
  17. internal int _synchRead;
  18. internal Exception _error;
  19. internal bool _endCalled;
  20. internal HttpStreamAsyncResult(object parent)
  21. {
  22. _parent = parent;
  23. }
  24. public void Complete(Exception e)
  25. {
  26. _error = e;
  27. Complete();
  28. }
  29. public void Complete()
  30. {
  31. lock (_locker)
  32. {
  33. if (_completed)
  34. return;
  35. _completed = true;
  36. if (_handle != null)
  37. _handle.Set();
  38. if (_callback != null)
  39. Task.Run(() => _callback(this));
  40. }
  41. }
  42. public object AsyncState
  43. {
  44. get { return _state; }
  45. }
  46. public WaitHandle AsyncWaitHandle
  47. {
  48. get
  49. {
  50. lock (_locker)
  51. {
  52. if (_handle == null)
  53. _handle = new ManualResetEvent(_completed);
  54. }
  55. return _handle;
  56. }
  57. }
  58. public bool CompletedSynchronously => false;
  59. public bool IsCompleted
  60. {
  61. get
  62. {
  63. lock (_locker)
  64. {
  65. return _completed;
  66. }
  67. }
  68. }
  69. }
  70. }