2006-11-27 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / System.Net / FtpAsyncResult.cs
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
14 #if NET_2_0
15
16 namespace System.Net 
17 {
18         class FtpAsyncResult : IAsyncResult
19         {
20                 FtpWebResponse response;
21                 ManualResetEvent waitHandle;
22                 Exception exception;
23                 AsyncCallback callback;
24                 Stream stream;
25                 object state;
26                 bool completed;
27                 bool synch;
28                 object locker = new object ();
29
30                 public FtpAsyncResult (AsyncCallback callback, object state)
31                 {
32                         this.callback = callback;
33                         this.state = state;
34                 }
35                 
36                 public object AsyncState {
37                         get {
38                                 return state;
39                         }
40                 }
41
42                 public WaitHandle AsyncWaitHandle {
43                         get {
44                                 lock (locker) {
45                                         if (waitHandle == null)
46                                                 waitHandle = new ManualResetEvent (false);
47                                 }
48                                 
49                                 return waitHandle;
50                         }
51                 }
52
53                 public bool CompletedSynchronously {
54                         get {
55                                 return synch;
56                         }
57                 }
58
59                 public bool IsCompleted {
60                         get {
61                                 lock (locker) {
62                                         return completed;
63                                 }
64                         }
65                 }
66
67                 internal bool GotException {
68                         get {
69                                 return exception != null;
70                         }
71                 }
72
73                 internal Exception Exception {
74                         get {
75                                 return exception;
76                         }
77                 }
78
79                 internal FtpWebResponse Response {
80                         get {
81                                 return response;
82                         }
83                         set {
84                                 response = value;
85                         }
86                 }
87
88                 internal Stream Stream {
89                         get {
90                                 return stream;
91                         }
92                 }
93
94                 internal void WaitUntilComplete ()
95                 {
96                         if (IsCompleted)
97                                 return;
98
99                         AsyncWaitHandle.WaitOne ();
100                 }
101
102                 internal bool WaitUntilComplete (int timeout, bool exitContext)
103                 {
104                         if (IsCompleted)
105                                 return true;
106                         
107                         return AsyncWaitHandle.WaitOne (timeout, exitContext);
108                 }
109
110                 internal void SetCompleted (bool synch, Exception exc, FtpWebResponse response)
111                 {
112                         this.synch = synch;
113                         this.exception = exc;
114                         this.response = response;
115                         lock (locker) {
116                                 completed = true;
117                                 if (waitHandle != null)
118                                         waitHandle.Set ();
119                         }
120                 }
121
122                 internal void SetCompleted (bool synch)
123                 {
124                         SetCompleted (synch, null, null);
125                 }
126
127                 internal void SetCompleted (bool synch, FtpWebResponse response)
128                 {
129                         SetCompleted (synch, null, response);
130                 }
131
132                 internal void SetCompleted (bool synch, Exception exc)
133                 {
134                         SetCompleted (synch, exc, null);
135                 }
136
137                 internal void SetCompleted (bool synch, Stream stream)
138                 {
139                         this.synch = synch;
140                         this.stream = stream;
141                         lock (locker) {
142                                 completed = true;
143                                 if (waitHandle != null)
144                                         waitHandle.Set ();
145                         }
146                 }
147
148                 internal void DoCallback ()
149                 {
150                         callback (this);
151                 }
152
153                 // Cleanup resources
154                 internal void Reset () 
155                 {
156                         exception = null;
157                         synch = false;
158                         response = null;
159                         state = null;
160                         
161                         lock (locker) {
162                                 completed = false;
163                                 if (waitHandle != null)
164                                         waitHandle.Reset ();
165                         }
166                 }
167                 
168         }
169 }
170
171 #endif
172