New test.
[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 public class AsyncResult : IAsyncResult, IMessageSink {
40
41         object async_state;
42         WaitHandle handle;
43         object async_delegate;
44         IntPtr data;
45         object object_data;
46         bool sync_completed;
47         bool completed;
48         bool endinvoke_called;
49         object async_callback;
50         ExecutionContext current;
51         ExecutionContext original;
52
53         // not part of MonoAsyncResult...
54         MonoMethodMessage call_message;
55         IMessageCtrl message_ctrl;
56         IMessage reply_message;
57         
58         internal AsyncResult ()
59         {
60         }
61         
62         public virtual object AsyncState
63         {
64                 get {
65                         return async_state;
66                 }
67         }
68
69         public virtual WaitHandle AsyncWaitHandle {
70                 get {
71                         lock (this) {
72                                 if (handle == null)
73                                         handle = new ManualResetEvent (completed);
74
75                                 return handle;
76                         }
77                 }
78         }
79
80         public virtual bool CompletedSynchronously
81         {
82                 get {
83                         return sync_completed;
84                 }
85         }
86
87         public virtual bool IsCompleted
88         {
89                 get {
90                         return completed;
91                 }
92         }
93                 
94         public bool EndInvokeCalled
95         {
96                 get {
97                         return endinvoke_called;
98                 }
99                 set {
100                         endinvoke_called = value;
101                 }
102         }
103                 
104         public virtual object AsyncDelegate
105         {
106                 get {
107                         return async_delegate;
108                 }
109         }
110
111         public IMessageSink NextSink {
112                 get {
113                         return null;
114                 }
115         }
116
117         public virtual IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
118         {
119                 // Never called
120                 throw new NotSupportedException ();
121         }
122
123         public virtual IMessage GetReplyMessage()
124         {
125                 return reply_message;
126         }
127
128         public virtual void SetMessageCtrl (IMessageCtrl mc)
129         {
130                 message_ctrl = mc;
131         }
132
133         internal void SetCompletedSynchronously (bool completed)
134         {
135                 sync_completed = completed;
136         }
137
138         internal IMessage EndInvoke ()
139         {
140                 handle.WaitOne ();
141                 return reply_message;
142         }
143
144         public virtual IMessage SyncProcessMessage (IMessage msg)
145         {
146                 reply_message = msg;
147
148                 completed = true;
149                 NativeEventCalls.SetEvent_internal (handle.Handle);
150                 
151                 if (async_callback != null)
152                 {
153                         AsyncCallback ac = (AsyncCallback) async_callback;
154                         ac (this);
155                 }
156
157                 return null;
158         }
159         
160         internal MonoMethodMessage CallMessage
161         {
162                 get { return call_message; }
163                 set { call_message = value; }
164         }
165 }
166 }