Fix very sticky typo in numerous places
[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 using System.Runtime.InteropServices;
37
38 namespace System.Runtime.Remoting.Messaging {
39
40 [System.Runtime.InteropServices.ComVisible (true)]
41 [StructLayout (LayoutKind.Sequential)]
42 public class AsyncResult : IAsyncResult, IMessageSink {
43
44 #pragma warning disable 169, 414, 649
45         object async_state;
46         WaitHandle handle;
47         object async_delegate;
48         IntPtr data;
49         object object_data;
50         bool sync_completed;
51         bool completed;
52         bool endinvoke_called;
53         object async_callback;
54         ExecutionContext current;
55         ExecutionContext original;
56         long add_time;
57 #pragma warning restore 169, 414, 649
58
59         // not part of MonoAsyncResult...
60         MonoMethodMessage call_message;
61 #pragma warning disable 0414
62         IMessageCtrl message_ctrl;
63 #pragma warning restore
64         IMessage reply_message;
65         
66         internal AsyncResult ()
67         {
68         }
69
70         internal AsyncResult (WaitCallback cb, object state, bool capture_context)
71         {
72                 async_state = state;
73                 async_delegate = cb;
74                 if (capture_context)
75                         current = ExecutionContext.Capture ();
76         }
77
78         public virtual object AsyncState
79         {
80                 get {
81                         return async_state;
82                 }
83         }
84
85         public virtual WaitHandle AsyncWaitHandle {
86                 get {
87                         lock (this) {
88                                 if (handle == null)
89                                         handle = new ManualResetEvent (completed);
90
91                                 return handle;
92                         }
93                 }
94         }
95
96         public virtual bool CompletedSynchronously
97         {
98                 get {
99                         return sync_completed;
100                 }
101         }
102
103         public virtual bool IsCompleted
104         {
105                 get {
106                         return completed;
107                 }
108         }
109                 
110         public bool EndInvokeCalled
111         {
112                 get {
113                         return endinvoke_called;
114                 }
115                 set {
116                         endinvoke_called = value;
117                 }
118         }
119                 
120         public virtual object AsyncDelegate
121         {
122                 get {
123                         return async_delegate;
124                 }
125         }
126
127         public IMessageSink NextSink {
128                 get {
129                         return null;
130                 }
131         }
132
133         public virtual IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
134         {
135                 // Never called
136                 throw new NotSupportedException ();
137         }
138
139         public virtual IMessage GetReplyMessage()
140         {
141                 return reply_message;
142         }
143
144         public virtual void SetMessageCtrl (IMessageCtrl mc)
145         {
146                 message_ctrl = mc;
147         }
148
149         internal void SetCompletedSynchronously (bool completed)
150         {
151                 sync_completed = completed;
152         }
153
154         internal IMessage EndInvoke ()
155         {
156                 lock (this) {
157                         if (completed)
158                                 return reply_message;
159                 }
160
161                 AsyncWaitHandle.WaitOne ();
162                 return reply_message;
163         }
164
165         public virtual IMessage SyncProcessMessage (IMessage msg)
166         {
167                 reply_message = msg;
168
169                 lock (this) {
170                         completed = true;
171                         if (handle != null)
172                                 ((ManualResetEvent) AsyncWaitHandle).Set ();
173                 }
174                 
175                 if (async_callback != null) {
176                         AsyncCallback ac = (AsyncCallback) async_callback;
177                         ac (this);
178                 }
179
180                 return null;
181         }
182         
183         internal MonoMethodMessage CallMessage
184         {
185                 get { return call_message; }
186                 set { call_message = value; }
187         }
188 }
189 }