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