HttpStreamAsyncResult.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 => _state;
  43. public WaitHandle AsyncWaitHandle
  44. {
  45. get
  46. {
  47. lock (_locker)
  48. {
  49. if (_handle == null)
  50. _handle = new ManualResetEvent(_completed);
  51. }
  52. return _handle;
  53. }
  54. }
  55. public bool CompletedSynchronously => false;
  56. public bool IsCompleted
  57. {
  58. get
  59. {
  60. lock (_locker)
  61. {
  62. return _completed;
  63. }
  64. }
  65. }
  66. }
  67. }