HttpStreamAsyncResult.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #region License
  2. /*
  3. * HttpStreamAsyncResult.cs
  4. *
  5. * This code is derived from HttpStreamAsyncResult.cs (System.Net) of Mono
  6. * (http://www.mono-project.com).
  7. *
  8. * The MIT License
  9. *
  10. * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  11. * Copyright (c) 2012-2021 sta.blockhead
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #endregion
  32. #region Authors
  33. /*
  34. * Authors:
  35. * - Gonzalo Paniagua Javier <gonzalo@novell.com>
  36. */
  37. #endregion
  38. using System;
  39. using System.Threading;
  40. namespace WebSocketSharp.Net
  41. {
  42. internal class HttpStreamAsyncResult : IAsyncResult
  43. {
  44. #region Private Fields
  45. private byte[] _buffer;
  46. private AsyncCallback _callback;
  47. private bool _completed;
  48. private int _count;
  49. private Exception _exception;
  50. private int _offset;
  51. private object _state;
  52. private object _sync;
  53. private int _syncRead;
  54. private ManualResetEvent _waitHandle;
  55. #endregion
  56. #region Internal Constructors
  57. internal HttpStreamAsyncResult (AsyncCallback callback, object state)
  58. {
  59. _callback = callback;
  60. _state = state;
  61. _sync = new object ();
  62. }
  63. #endregion
  64. #region Internal Properties
  65. internal byte[] Buffer {
  66. get {
  67. return _buffer;
  68. }
  69. set {
  70. _buffer = value;
  71. }
  72. }
  73. internal int Count {
  74. get {
  75. return _count;
  76. }
  77. set {
  78. _count = value;
  79. }
  80. }
  81. internal Exception Exception {
  82. get {
  83. return _exception;
  84. }
  85. }
  86. internal bool HasException {
  87. get {
  88. return _exception != null;
  89. }
  90. }
  91. internal int Offset {
  92. get {
  93. return _offset;
  94. }
  95. set {
  96. _offset = value;
  97. }
  98. }
  99. internal int SyncRead {
  100. get {
  101. return _syncRead;
  102. }
  103. set {
  104. _syncRead = value;
  105. }
  106. }
  107. #endregion
  108. #region Public Properties
  109. public object AsyncState {
  110. get {
  111. return _state;
  112. }
  113. }
  114. public WaitHandle AsyncWaitHandle {
  115. get {
  116. lock (_sync) {
  117. if (_waitHandle == null)
  118. _waitHandle = new ManualResetEvent (_completed);
  119. return _waitHandle;
  120. }
  121. }
  122. }
  123. public bool CompletedSynchronously {
  124. get {
  125. return _syncRead == _count;
  126. }
  127. }
  128. public bool IsCompleted {
  129. get {
  130. lock (_sync)
  131. return _completed;
  132. }
  133. }
  134. #endregion
  135. #region Internal Methods
  136. internal void Complete ()
  137. {
  138. lock (_sync) {
  139. if (_completed)
  140. return;
  141. _completed = true;
  142. if (_waitHandle != null)
  143. _waitHandle.Set ();
  144. if (_callback != null)
  145. _callback.BeginInvoke (this, ar => _callback.EndInvoke (ar), null);
  146. }
  147. }
  148. internal void Complete (Exception exception)
  149. {
  150. lock (_sync) {
  151. if (_completed)
  152. return;
  153. _completed = true;
  154. _exception = exception;
  155. if (_waitHandle != null)
  156. _waitHandle.Set ();
  157. if (_callback != null)
  158. _callback.BeginInvoke (this, ar => _callback.EndInvoke (ar), null);
  159. }
  160. }
  161. #endregion
  162. }
  163. }