Add support for an alternative managed DNS resolver
[mono.git] / mcs / class / System / System.Net / DnsAsyncResult.cs
1 //
2 // System.Net.DnsAsyncResult
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Threading;
33 using Mono.Dns;
34
35 namespace System.Net
36 {
37         class DnsAsyncResult : IAsyncResult
38         {
39                 static WaitCallback internal_cb = new WaitCallback (CB);
40                 ManualResetEvent handle;
41                 bool synch;
42                 bool is_completed;
43                 AsyncCallback callback;
44                 object state;
45                 IPHostEntry entry;
46                 Exception exc;
47
48                 public DnsAsyncResult (AsyncCallback cb, object state)
49                 {
50                         this.callback = cb;
51                         this.state = state;
52                 }
53
54                 public void SetCompleted (bool synch, IPHostEntry entry, Exception e)
55                 {
56                         this.synch = synch;
57                         this.entry = entry;
58                         exc = e;
59                         lock (this) {
60                                 if (is_completed)
61                                         return;
62                                 is_completed = true;
63                                 if (handle != null)
64                                         handle.Set ();
65                         }
66                         if (callback != null)
67                                 ThreadPool.QueueUserWorkItem (internal_cb, this);
68                 }
69
70                 public void SetCompleted (bool synch, Exception e)
71                 {
72                         SetCompleted (synch, null, e);
73                 }
74
75                 public void SetCompleted (bool synch, IPHostEntry entry)
76                 {
77                         SetCompleted (synch, entry, null);
78                 }
79
80                 static void CB (object _this)
81                 {
82                         DnsAsyncResult ares = (DnsAsyncResult) _this;
83                         ares.callback (ares);
84                 }
85
86                 public object AsyncState {
87                         get { return state; }
88                 }
89
90                 public WaitHandle AsyncWaitHandle {
91                         get {
92                                 lock (this) {
93                                         if (handle == null)
94                                                 handle = new ManualResetEvent (is_completed);
95                                 }
96                                 return handle;
97                         }
98                 }
99
100                 public Exception Exception {
101                         get { return exc; }
102                 }
103
104                 public IPHostEntry HostEntry {
105                         get { return entry; }
106                 }
107
108                 public bool CompletedSynchronously {
109                         get { return synch; }
110                 }
111
112                 public bool IsCompleted {
113                         get {
114                                 lock (this) {
115                                         return is_completed;
116                                 }
117                         }
118                 }
119         }
120 }
121