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