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