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