Jumbo patch for NET_2_0, mscorlib is now clean
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / AsyncResult.cs
1 // 
2 // System.Runtime.Remoting.Messaging/AsyncResult.cs 
3 //
4 // Authors:
5 //   Joe Shaw (joe@ximian.com)
6 //   Martin Baulig (martin@gnome.org)
7 //   Dietmar Maurer (dietmar@ximian.com)
8 //   Duncan Mak (duncan@ximian.com)
9 //
10 // (C) 2001 Ximian, Inc.  http://www.ximian.com
11 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Threading;
35 using System.Runtime.CompilerServices;
36
37 namespace System.Runtime.Remoting.Messaging {
38
39 [System.Runtime.InteropServices.ComVisible (true)]
40 public class AsyncResult : IAsyncResult, IMessageSink {
41
42 #pragma warning disable 169, 414, 649
43         object async_state;
44         WaitHandle handle;
45         object async_delegate;
46         IntPtr data;
47         object object_data;
48         bool sync_completed;
49         bool completed;
50         bool endinvoke_called;
51         object async_callback;
52         ExecutionContext current;
53         ExecutionContext original;
54 #pragma warning restore 169, 414, 649
55
56         // not part of MonoAsyncResult...
57         MonoMethodMessage call_message;
58         IMessageCtrl message_ctrl;
59         IMessage reply_message;
60         
61         internal AsyncResult ()
62         {
63         }
64         
65         public virtual object AsyncState
66         {
67                 get {
68                         return async_state;
69                 }
70         }
71
72         public virtual WaitHandle AsyncWaitHandle {
73                 get {
74                         lock (this) {
75                                 if (handle == null)
76                                         handle = new ManualResetEvent (completed);
77
78                                 return handle;
79                         }
80                 }
81         }
82
83         public virtual bool CompletedSynchronously
84         {
85                 get {
86                         return sync_completed;
87                 }
88         }
89
90         public virtual bool IsCompleted
91         {
92                 get {
93                         return completed;
94                 }
95         }
96                 
97         public bool EndInvokeCalled
98         {
99                 get {
100                         return endinvoke_called;
101                 }
102                 set {
103                         endinvoke_called = value;
104                 }
105         }
106                 
107         public virtual object AsyncDelegate
108         {
109                 get {
110                         return async_delegate;
111                 }
112         }
113
114         public IMessageSink NextSink {
115                 get {
116                         return null;
117                 }
118         }
119
120         public virtual IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
121         {
122                 // Never called
123                 throw new NotSupportedException ();
124         }
125
126         public virtual IMessage GetReplyMessage()
127         {
128                 return reply_message;
129         }
130
131         public virtual void SetMessageCtrl (IMessageCtrl mc)
132         {
133                 message_ctrl = mc;
134         }
135
136         internal void SetCompletedSynchronously (bool completed)
137         {
138                 sync_completed = completed;
139         }
140
141         internal IMessage EndInvoke ()
142         {
143                 lock (this) {
144                         if (completed)
145                                 return reply_message;
146                 }
147
148                 AsyncWaitHandle.WaitOne ();
149                 return reply_message;
150         }
151
152         public virtual IMessage SyncProcessMessage (IMessage msg)
153         {
154                 reply_message = msg;
155
156                 lock (this) {
157                         completed = true;
158                         if (handle != null)
159                                 ((ManualResetEvent) AsyncWaitHandle).Set ();
160                 }
161                 
162                 if (async_callback != null) {
163                         AsyncCallback ac = (AsyncCallback) async_callback;
164                         ac (this);
165                 }
166
167                 return null;
168         }
169         
170         internal MonoMethodMessage CallMessage
171         {
172                 get { return call_message; }
173                 set { call_message = value; }
174         }
175 }
176 }