merge -r 60439:60440
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MethodCall.cs
1 //
2 // System.Runtime.Remoting.Messaging.MethodCall.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //         Lluis Sanchez Gual (lluis@ideary.com)
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.Serialization;
37
38 namespace System.Runtime.Remoting.Messaging {
39
40         [Serializable] [CLSCompliant (false)]
41         public class MethodCall : IMethodCallMessage, IMethodMessage, IMessage, ISerializable, IInternalMessage, ISerializationRootObject
42         {
43                 string _uri;
44                 string _typeName;
45                 string _methodName;
46                 object[] _args;
47                 Type[] _methodSignature;
48                 MethodBase _methodBase;
49                 LogicalCallContext _callContext;
50                 ArgInfo _inArgInfo;
51                 Identity _targetIdentity;
52
53                 protected IDictionary ExternalProperties;
54                 protected IDictionary InternalProperties;
55
56                 public MethodCall (Header [] headers)
57                 {
58                         Init();
59
60                         if (headers == null || headers.Length == 0) return;
61
62                         foreach (Header header in headers)
63                                 InitMethodProperty (header.Name, header.Value);
64
65                         ResolveMethod ();
66                 }
67
68                 internal MethodCall (SerializationInfo info, StreamingContext context)
69                 {
70                         Init();
71
72                         foreach (SerializationEntry entry in info)
73                                 InitMethodProperty ((string)entry.Name, entry.Value);
74                 }
75
76                 internal MethodCall (CADMethodCallMessage msg) 
77                 {
78                         _uri = string.Copy (msg.Uri);
79                         
80                         // Get unmarshalled arguments
81                         ArrayList args = msg.GetArguments ();
82
83                         _args = msg.GetArgs (args);
84                         _callContext = msg.GetLogicalCallContext (args);
85                         if (_callContext == null)
86                                 _callContext = new LogicalCallContext ();
87         
88                         _methodBase = MethodBase.GetMethodFromHandle (msg.MethodHandle);
89                         Init();
90
91                         if (msg.PropertiesCount > 0)
92                                 CADMessageBase.UnmarshalProperties (Properties, msg.PropertiesCount, args);
93                 }
94
95                 public MethodCall (IMessage msg)
96                 {
97                         if (msg is IMethodMessage)
98                                 CopyFrom ((IMethodMessage) msg);
99                         else
100                         {
101                                 foreach (DictionaryEntry entry in msg.Properties)
102                                         InitMethodProperty ((String) entry.Key, entry.Value);
103                                 Init();
104                 }
105                 }
106
107                 internal MethodCall (string uri, string typeName, string methodName, object[] args)
108                 {
109                         _uri = uri;
110                         _typeName = typeName;
111                         _methodName = methodName;
112                         _args = args;
113
114                         Init();
115                         ResolveMethod();
116                 }
117
118                 internal MethodCall ()
119                 {
120                 }
121                 
122                 internal void CopyFrom (IMethodMessage call)
123                 {
124                         _uri = call.Uri;
125                         _typeName = call.TypeName;
126                         _methodName = call.MethodName;
127                         _args = call.Args;
128                         _methodSignature = (Type[]) call.MethodSignature;
129                         _methodBase = call.MethodBase;
130                         _callContext = call.LogicalCallContext;
131
132                         Init();
133                 }
134                 
135                 internal virtual void InitMethodProperty(string key, object value)
136                 {
137                         switch (key)
138                         {
139                                 case "__TypeName" : _typeName = (string) value; return;
140                                 case "__MethodName" : _methodName = (string) value; return;
141                                 case "__MethodSignature" : _methodSignature = (Type[]) value; return;
142                                 case "__Args" : _args = (object[]) value; return;
143                                 case "__CallContext" : _callContext = (LogicalCallContext) value; return;
144                                 case "__Uri" : _uri = (string) value; return;
145                                 default: Properties[key] = value; return;
146                         }
147                 }
148
149                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
150                 {
151                         info.AddValue ("__TypeName", _typeName);
152                         info.AddValue ("__MethodName", _methodName);
153                         info.AddValue ("__MethodSignature", _methodSignature);
154                         info.AddValue ("__Args", _args);
155                         info.AddValue ("__CallContext", _callContext);
156                         info.AddValue ("__Uri", _uri);
157
158                         if (InternalProperties != null) {
159                                 foreach (DictionaryEntry entry in InternalProperties)
160                                         info.AddValue ((string) entry.Key, entry.Value);
161                         }
162                 } 
163
164                 public int ArgCount {
165                         get { return _args.Length; }
166                 }
167
168                 public object[] Args {
169                         get { return _args; }
170                 }
171                 
172                 public bool HasVarArgs {
173                         get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
174                 }
175
176                 public int InArgCount 
177                 {
178                         get 
179                         { 
180                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
181                                 return _inArgInfo.GetInOutArgCount();
182                         }
183                 }
184
185                 public object[] InArgs 
186                 {
187                         get 
188                         { 
189                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
190                                 return _inArgInfo.GetInOutArgs (_args);
191                         }
192                 }
193                 
194                 public LogicalCallContext LogicalCallContext {
195                         get {
196                                 if (_callContext == null)
197                                         _callContext = new LogicalCallContext ();
198                                 return _callContext;
199                         }
200                 }
201                 
202                 public MethodBase MethodBase {
203                         get {
204                                 if (_methodBase == null)
205                                         ResolveMethod ();
206                                         
207                                 return _methodBase;
208                         }
209                 }
210
211                 public string MethodName {
212                         get {
213                                 // lazily fill in _methodName from _methodBase
214                                 if (_methodName == null)
215                                         _methodName = _methodBase.Name;
216                                 return _methodName;
217                         }
218                 }
219
220                 public object MethodSignature {
221                         get { 
222                                 if (_methodSignature == null && _methodBase != null)
223                                 {
224                                         ParameterInfo[] parameters = _methodBase.GetParameters();
225                                         _methodSignature = new Type[parameters.Length];
226                                         for (int n=0; n<parameters.Length; n++)
227                                                 _methodSignature[n] = parameters[n].ParameterType;
228                                 }
229                                 return _methodSignature;
230                         }
231                 }
232
233                 public virtual IDictionary Properties {
234                         get 
235                         { 
236                                 if (ExternalProperties == null) InitDictionary ();
237                                 return ExternalProperties; 
238                         }
239                 }
240
241                 internal virtual void InitDictionary()
242                 {
243                         MethodCallDictionary props = new MethodCallDictionary (this);
244                         ExternalProperties = props;
245                         InternalProperties = props.GetInternalProperties();
246                 }
247
248                 public string TypeName 
249                 {
250                         get {
251                                 // lazily fill in _typeName from _methodBase
252                                 if (_typeName == null)
253                                         _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
254                                 return _typeName;
255                         }
256                 }
257
258                 public string Uri {
259                         get { return _uri; }
260                         set { _uri = value; }
261                 }
262
263                 public object GetArg (int argNum)
264                 {
265                         return _args[argNum];
266                 }
267
268                 public string GetArgName (int index)
269                 {
270                         return _methodBase.GetParameters()[index].Name;
271                 }
272
273                 public object GetInArg (int argNum)
274                 {
275                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
276                         return _args[_inArgInfo.GetInOutArgIndex (argNum)];
277                 }
278
279                 public string GetInArgName (int index)
280                 {
281                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
282                         return _inArgInfo.GetInOutArgName(index);
283                 }
284
285                 [MonoTODO]
286                 public virtual object HeaderHandler (Header[] h)
287                 {
288                         throw new NotImplementedException ();
289                 }
290
291                 public virtual void Init ()
292                 {
293                 }
294
295                 public void ResolveMethod ()
296                 {
297                         if (_uri != null)
298                         {
299                                 Type type = RemotingServices.GetServerTypeForUri (_uri);
300                                 if (type == null) {
301                                         string sname = _typeName != null ? " (" + _typeName + ")" : "";
302                                         throw new RemotingException ("Requested service not found" + sname + ". No receiver for uri " + _uri);
303                                 }
304
305                                 if (CastTo (_typeName, type) != null) {
306                                         _methodBase = RemotingServices.GetMethodBaseFromName (type, _methodName, _methodSignature);
307                                         if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + type);
308                                         return;
309                                 }
310                                 else
311                                         throw new RemotingException ("Cannot cast from client type '" + _typeName + "' to server type '" + type.FullName + "'");
312                         }
313                         _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
314                         if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + TypeName);
315                 }
316
317                 Type CastTo (string clientType, Type serverType)
318                 {
319                         int i = clientType.IndexOf(',');
320                         if (i != -1) clientType = clientType.Substring (0,i).Trim();
321
322                         if (clientType == serverType.FullName) return serverType;
323
324                         // base class hierarchy
325
326                         Type baseType = serverType.BaseType;
327                         while (baseType != null) {
328                         if (clientType == baseType.FullName) return baseType;
329                         baseType = baseType.BaseType;
330                 }
331
332                         // Implemented interfaces
333
334                         Type[] interfaces = serverType.GetInterfaces();
335                         foreach (Type itype in interfaces)
336                                 if (clientType == itype.FullName) return itype;
337      
338                 return null;
339                 }
340                 
341                 [MonoTODO]
342                 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
343                 {
344                         throw new NotImplementedException ();
345                 }
346
347                 Identity IInternalMessage.TargetIdentity
348                 {
349                         get { return _targetIdentity; }
350                         set { _targetIdentity = value; }
351                 }
352
353                 public override string ToString ()
354                 {
355                         string s = _typeName.Split(',')[0] + "." + _methodName + " (";
356                         if (_args != null)
357                         {
358                                 for (int n=0; n<_args.Length; n++)
359                                 {
360                                         if (n>0) s+= ", ";
361                                         if (_args[n] != null) s += _args[n].GetType().Name + " ";
362                                         s += GetArgName (n);
363                                         if (_args[n] != null) s += " = {" + _args[n] + "}";
364                                         else s+=" = {null}";
365                                 }
366                         }
367                         s += ")";
368                         return s;
369                 }
370         }
371 }