Fix Dispose()
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MethodResponse.cs
1 //
2 // System.Runtime.Remoting.Messaging.MethodResponse.cs
3 //
4 // Author:      Duncan Mak (duncan@ximian.com)
5 //              Patrik Torstensson
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
8 //
9
10 //
11 // Copyright (C) 2004 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.Collections;
35 using System.Reflection;
36 using System.Runtime.Remoting;
37 using System.Runtime.Serialization;
38
39 namespace System.Runtime.Remoting.Messaging {
40
41         [Serializable] [CLSCompliant (false)]
42         [System.Runtime.InteropServices.ComVisible (true)]
43         public class MethodResponse : IMethodReturnMessage, ISerializable, IInternalMessage, ISerializationRootObject
44         {
45                 string _methodName;
46                 string _uri;
47                 string _typeName;
48                 MethodBase _methodBase;
49
50                 object _returnValue;
51                 Exception _exception;
52                 Type [] _methodSignature;
53                 ArgInfo _inArgInfo;
54
55                 object []  _args;
56                 object []  _outArgs;
57                 IMethodCallMessage _callMsg;
58                 LogicalCallContext _callContext;
59                 Identity _targetIdentity;
60
61                 protected IDictionary ExternalProperties;
62                 protected IDictionary InternalProperties;
63
64                 public MethodResponse (Header[] h1, IMethodCallMessage mcm)
65                 {
66                         if (mcm != null)
67                         {
68                                 _methodName = mcm.MethodName;
69                                 _uri = mcm.Uri;
70                                 _typeName = mcm.TypeName;
71                                 _methodBase = mcm.MethodBase;
72                                 _methodSignature = (Type[]) mcm.MethodSignature;
73                                 _args = mcm.Args;
74                         }
75
76                         if (h1 != null)
77                         {
78                                 foreach (Header header in h1)
79                                         InitMethodProperty (header.Name, header.Value);
80                         }
81                 }
82
83                 internal MethodResponse (Exception e, IMethodCallMessage msg) {
84                         _callMsg = msg;
85
86                         if (null != msg)
87                                 _uri = msg.Uri;
88                         else
89                                 _uri = String.Empty;
90                         
91                         _exception = e;
92                         _returnValue = null;
93                         _outArgs = new object[0];       // .NET does this
94                 }
95
96                 internal MethodResponse (object returnValue, object [] outArgs, LogicalCallContext callCtx, IMethodCallMessage msg) {
97                         _callMsg = msg;
98
99                         _uri = msg.Uri;
100                         
101                         _exception = null;
102                         _returnValue = returnValue;
103                         _args = outArgs;
104                 }
105
106                 internal MethodResponse (IMethodCallMessage msg, CADMethodReturnMessage retmsg) {
107                         _callMsg = msg;
108
109                         _methodBase = msg.MethodBase;
110                         //_typeName = msg.TypeName;
111                         _uri = msg.Uri;
112                         _methodName = msg.MethodName;
113                         
114                         // Get unmarshalled arguments
115                         ArrayList args = retmsg.GetArguments ();
116
117                         _exception = retmsg.GetException (args);
118                         _returnValue = retmsg.GetReturnValue (args);
119                         _args = retmsg.GetArgs (args);
120
121                         _callContext = retmsg.GetLogicalCallContext (args);
122                         if (_callContext == null) _callContext = new LogicalCallContext ();
123                         
124                         if (retmsg.PropertiesCount > 0)
125                                 CADMessageBase.UnmarshalProperties (Properties, retmsg.PropertiesCount, args);
126                 }
127
128                 internal MethodResponse (SerializationInfo info, StreamingContext context) 
129                 {
130                         foreach (SerializationEntry entry in info)
131                                 InitMethodProperty (entry.Name, entry.Value);
132                 }
133
134                 internal void InitMethodProperty (string key, object value) 
135                 {
136                         switch (key) 
137                         {
138                                 case "__TypeName": _typeName = (string) value; break;
139                                 case "__MethodName": _methodName = (string) value; break;
140                                 case "__MethodSignature": _methodSignature = (Type[]) value; break;
141                                 case "__Uri": _uri = (string) value; break;
142                                 case "__Return": _returnValue = value; break;
143                                 case "__OutArgs": _args = (object[]) value; break;
144                                 case "__fault": _exception = (Exception) value; break;
145                                 case "__CallContext": _callContext = (LogicalCallContext) value; break;
146                                 default: Properties [key] = value; break;
147                         }
148                 }
149
150                 public int ArgCount {
151                         get { 
152                                 if (null == _args)
153                                         return 0;
154
155                                 return _args.Length;
156                         }
157                 }
158
159                 public object[] Args {
160                         get { 
161                                 return _args; 
162                         }
163                 }
164
165                 public Exception Exception {
166                         get { 
167                                 return _exception; 
168                         }
169                 }
170                 
171                 public bool HasVarArgs {
172                         get { 
173                                 return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0;
174                         }
175                 }
176                 
177                 public LogicalCallContext LogicalCallContext {
178                         get {
179                                 if (_callContext == null)
180                                         _callContext = new LogicalCallContext ();
181                                 return _callContext;
182                         }
183                 }
184                 
185                 public MethodBase MethodBase {
186                         get { 
187                                 if (null == _methodBase) {
188                                         if (_callMsg != null)
189                                                 _methodBase = _callMsg.MethodBase;
190                                         else if (MethodName != null && TypeName != null)
191                                                 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
192                                 }
193                                 return _methodBase;
194                         }
195                 }
196
197                 public string MethodName {
198                         get { 
199                                 if (null == _methodName && null != _callMsg)
200                                         _methodName = _callMsg.MethodName;
201
202                                 return _methodName;
203                         }
204                 }
205
206                 public object MethodSignature {
207                         get { 
208                                 if (null == _methodSignature && null != _callMsg)
209                                         _methodSignature = (Type []) _callMsg.MethodSignature;
210
211                                 return _methodSignature;
212                         }
213                 }
214
215                 public int OutArgCount {
216                         get { 
217                                 if (_args == null || _args.Length == 0) return 0;
218                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
219                                 return _inArgInfo.GetInOutArgCount ();
220                         }
221                 }
222
223                 public object[] OutArgs {
224                         get { 
225                                 if (_outArgs == null && _args != null) {
226                                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
227                                         _outArgs = _inArgInfo.GetInOutArgs (_args);
228                                 }                                       
229                                 return _outArgs;
230                         }
231                 }
232
233                 public virtual IDictionary Properties {
234                         get { 
235                                 if (null == ExternalProperties) {
236                                         MethodReturnDictionary properties = new MethodReturnDictionary (this);
237                                         ExternalProperties = properties;
238                                         InternalProperties = properties.GetInternalProperties();
239                                 }
240                                 
241                                 return ExternalProperties;
242                         }
243                 }
244
245                 public object ReturnValue {
246                         get { 
247                                 return _returnValue;
248                         }
249                 }
250
251                 public string TypeName {
252                         get { 
253                                 if (null == _typeName && null != _callMsg)
254                                         _typeName = _callMsg.TypeName;
255
256                                 return _typeName;
257                         }
258                 }
259
260                 public string Uri {
261                         get { 
262                                 if (null == _uri && null != _callMsg)
263                                         _uri = _callMsg.Uri;
264                                 
265                                 return _uri;
266                         }
267
268                         set { 
269                                 _uri = value;
270                         }
271                 }
272
273                 string IInternalMessage.Uri {
274                         get { return Uri; }
275                         set { Uri = value; }
276                 }
277
278                 public object GetArg (int argNum)
279                 {
280                         if (null == _args)
281                                 return null;
282
283                         return _args [argNum];
284                 }
285
286                 public string GetArgName (int index)
287                 {
288                         return MethodBase.GetParameters()[index].Name;
289                 }
290
291                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
292                 {
293                         if (_exception == null)
294                         {
295                                 info.AddValue ("__TypeName", _typeName);
296                                 info.AddValue ("__MethodName", _methodName);
297                                 info.AddValue ("__MethodSignature", _methodSignature);
298                                 info.AddValue ("__Uri", _uri);
299                                 info.AddValue ("__Return", _returnValue);
300                                 info.AddValue ("__OutArgs", _args);
301                         }
302                         else
303                                 info.AddValue ("__fault", _exception);
304
305                         info.AddValue ("__CallContext", _callContext);
306
307                         if (InternalProperties != null) {
308                                 foreach (DictionaryEntry entry in InternalProperties)
309                                         info.AddValue ((string) entry.Key, entry.Value);
310                         }
311                 } 
312
313                 public object GetOutArg (int argNum)
314                 {
315                         if (_args == null) return null;
316                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
317                         return _args[_inArgInfo.GetInOutArgIndex (argNum)];
318                 }
319
320                 public string GetOutArgName (int index)
321                 {
322                         if (null == _methodBase)
323                                 return "__method_" + index;
324
325                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
326                         return _inArgInfo.GetInOutArgName(index);
327                 }
328
329                 [MonoTODO]
330                 public virtual object HeaderHandler (Header[] h)
331                 {
332                         throw new NotImplementedException ();
333                 }
334
335                 [MonoTODO]
336                 public void RootSetObjectData (SerializationInfo info, StreamingContext ctx)
337                 {
338                         throw new NotImplementedException ();
339                 } 
340
341                 Identity IInternalMessage.TargetIdentity
342                 {
343                         get { return _targetIdentity; }
344                         set { _targetIdentity = value; }
345                 }
346
347         }
348 }