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