FtpAsyncResult.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // System.Net.FtpAsyncResult.cs
  3. //
  4. // Authors:
  5. // Carlos Alberto Cortez ([email protected])
  6. //
  7. // (c) Copyright 2006 Novell, Inc. (http://www.novell.com)
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Threading;
  12. using System.Net;
  13. #if NET_2_0
  14. namespace System.Net
  15. {
  16. class FtpAsyncResult : IAsyncResult
  17. {
  18. FtpWebResponse response;
  19. ManualResetEvent waitHandle;
  20. Exception exception;
  21. AsyncCallback callback;
  22. Stream stream;
  23. object state;
  24. bool completed;
  25. bool synch;
  26. object locker = new object ();
  27. public FtpAsyncResult (AsyncCallback callback, object state)
  28. {
  29. this.callback = callback;
  30. this.state = state;
  31. }
  32. public object AsyncState {
  33. get {
  34. return state;
  35. }
  36. }
  37. public WaitHandle AsyncWaitHandle {
  38. get {
  39. lock (locker) {
  40. if (waitHandle == null)
  41. waitHandle = new ManualResetEvent (false);
  42. }
  43. return waitHandle;
  44. }
  45. }
  46. public bool CompletedSynchronously {
  47. get {
  48. return synch;
  49. }
  50. }
  51. public bool IsCompleted {
  52. get {
  53. lock (locker) {
  54. return completed;
  55. }
  56. }
  57. }
  58. internal bool GotException {
  59. get {
  60. return exception != null;
  61. }
  62. }
  63. internal Exception Exception {
  64. get {
  65. return exception;
  66. }
  67. }
  68. internal FtpWebResponse Response {
  69. get {
  70. return response;
  71. }
  72. set {
  73. response = value;
  74. }
  75. }
  76. internal Stream Stream {
  77. get {
  78. return stream;
  79. }
  80. set { stream = value; }
  81. }
  82. internal void WaitUntilComplete ()
  83. {
  84. if (IsCompleted)
  85. return;
  86. AsyncWaitHandle.WaitOne ();
  87. }
  88. internal bool WaitUntilComplete (int timeout, bool exitContext)
  89. {
  90. if (IsCompleted)
  91. return true;
  92. return AsyncWaitHandle.WaitOne (timeout, exitContext);
  93. }
  94. internal void SetCompleted (bool synch, Exception exc, FtpWebResponse response)
  95. {
  96. this.synch = synch;
  97. this.exception = exc;
  98. this.response = response;
  99. lock (locker) {
  100. completed = true;
  101. if (waitHandle != null)
  102. waitHandle.Set ();
  103. }
  104. DoCallback ();
  105. }
  106. internal void SetCompleted (bool synch, FtpWebResponse response)
  107. {
  108. SetCompleted (synch, null, response);
  109. }
  110. internal void SetCompleted (bool synch, Exception exc)
  111. {
  112. SetCompleted (synch, exc, null);
  113. }
  114. internal void DoCallback ()
  115. {
  116. if (callback != null)
  117. try {
  118. callback (this);
  119. }
  120. catch (Exception) {
  121. }
  122. }
  123. // Cleanup resources
  124. internal void Reset ()
  125. {
  126. exception = null;
  127. synch = false;
  128. response = null;
  129. state = null;
  130. lock (locker) {
  131. completed = false;
  132. if (waitHandle != null)
  133. waitHandle.Reset ();
  134. }
  135. }
  136. }
  137. }
  138. #endif