HttpListenerAsyncResult.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #region License
  2. /*
  3. * HttpListenerAsyncResult.cs
  4. *
  5. * This code is derived from ListenerAsyncResult.cs (System.Net) of Mono
  6. * (http://www.mono-project.com).
  7. *
  8. * The MIT License
  9. *
  10. * Copyright (c) 2005 Ximian, Inc. (http://www.ximian.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@ximian.com>
  36. */
  37. #endregion
  38. #region Contributors
  39. /*
  40. * Contributors:
  41. * - Nicholas Devenish
  42. */
  43. #endregion
  44. using System;
  45. using System.Threading;
  46. namespace WebSocketSharp.Net
  47. {
  48. internal class HttpListenerAsyncResult : IAsyncResult
  49. {
  50. #region Private Fields
  51. private AsyncCallback _callback;
  52. private bool _completed;
  53. private bool _completedSynchronously;
  54. private HttpListenerContext _context;
  55. private bool _endCalled;
  56. private Exception _exception;
  57. private object _state;
  58. private object _sync;
  59. private ManualResetEvent _waitHandle;
  60. #endregion
  61. #region Internal Constructors
  62. internal HttpListenerAsyncResult (AsyncCallback callback, object state)
  63. {
  64. _callback = callback;
  65. _state = state;
  66. _sync = new object ();
  67. }
  68. #endregion
  69. #region Internal Properties
  70. internal HttpListenerContext Context
  71. {
  72. get {
  73. if (_exception != null)
  74. throw _exception;
  75. return _context;
  76. }
  77. }
  78. internal bool EndCalled {
  79. get {
  80. return _endCalled;
  81. }
  82. set {
  83. _endCalled = value;
  84. }
  85. }
  86. internal object SyncRoot {
  87. get {
  88. return _sync;
  89. }
  90. }
  91. #endregion
  92. #region Public Properties
  93. public object AsyncState {
  94. get {
  95. return _state;
  96. }
  97. }
  98. public WaitHandle AsyncWaitHandle {
  99. get {
  100. lock (_sync) {
  101. if (_waitHandle == null)
  102. _waitHandle = new ManualResetEvent (_completed);
  103. return _waitHandle;
  104. }
  105. }
  106. }
  107. public bool CompletedSynchronously {
  108. get {
  109. return _completedSynchronously;
  110. }
  111. }
  112. public bool IsCompleted {
  113. get {
  114. lock (_sync)
  115. return _completed;
  116. }
  117. }
  118. #endregion
  119. #region Private Methods
  120. private void complete ()
  121. {
  122. lock (_sync) {
  123. _completed = true;
  124. if (_waitHandle != null)
  125. _waitHandle.Set ();
  126. }
  127. if (_callback == null)
  128. return;
  129. ThreadPool.QueueUserWorkItem (
  130. state => {
  131. try {
  132. _callback (this);
  133. }
  134. catch {
  135. }
  136. },
  137. null
  138. );
  139. }
  140. #endregion
  141. #region Internal Methods
  142. internal void Complete (Exception exception)
  143. {
  144. _exception = exception;
  145. complete ();
  146. }
  147. internal void Complete (
  148. HttpListenerContext context, bool completedSynchronously
  149. )
  150. {
  151. _context = context;
  152. _completedSynchronously = completedSynchronously;
  153. complete ();
  154. }
  155. #endregion
  156. }
  157. }