MonoTouch specific initialization for TimeZone (devices). Fix bug #1790
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MonoMethodMessage.cs
1 //
2 // System.Runtime.Remoting.Messaging.MonoMethodMessage.cs
3 //
4 // Author:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //   Patrik Torstensson
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Collections;
36 using System.Reflection;
37 using System.Runtime.CompilerServices;
38
39 namespace System.Runtime.Remoting.Messaging {
40         
41         [Serializable]
42         internal class MonoMethodMessage : IMethodCallMessage, IMethodReturnMessage, IInternalMessage {
43
44 #pragma warning disable 649
45                 #region keep in sync with MonoMessage in object-internals.h
46                 MonoMethod method;
47                 object []  args;
48                 string []  names;
49                 byte [] arg_types; /* 1 == IN; 2 == OUT; 3 == INOUT; 4 == COPY OUT */
50                 public LogicalCallContext ctx;
51                 public object rval;
52                 public Exception exc;
53                 AsyncResult asyncResult;
54                 CallType call_type;
55                 #endregion
56 #pragma warning restore 649
57
58                 string uri;
59
60                 MethodCallDictionary properties;
61
62                 Type[] methodSignature;
63
64                 Identity identity;
65
66
67                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68                 internal extern void InitMessage (MonoMethod method, object [] out_args);
69
70                 public MonoMethodMessage (MethodBase method, object [] out_args)
71                 {
72                         if (method != null)
73                                 InitMessage ((MonoMethod)method, out_args);
74                         else
75                                 args = null;
76                 }
77
78                 public MonoMethodMessage (Type type, string method_name, object [] in_args)
79                 {
80                         // fixme: consider arg types
81                         MethodInfo minfo = type.GetMethod (method_name);
82                         
83                         InitMessage ((MonoMethod)minfo, null);
84
85                         int len = in_args.Length;
86                         for (int i = 0; i < len; i++) {
87                                 args [i] = in_args [i];
88                         }
89                 }
90                 
91                 public IDictionary Properties {
92                         get {
93                                 if (properties == null) properties = new MethodCallDictionary (this);
94                                 return properties;
95                         }
96                 }
97
98                 public int ArgCount {
99                         get {
100                                 if (CallType == CallType.EndInvoke)
101                                         return -1;
102                                         
103                                 if (null == args)
104                                         return 0;
105
106                                 return args.Length;
107                         }
108                 }
109                 
110                 public object [] Args {
111                         get {
112                                 return args;
113                         }
114                 }
115                 
116                 public bool HasVarArgs {
117                         get {
118                                 return false;
119                         }
120                 }
121
122                 public LogicalCallContext LogicalCallContext {
123                         get {
124                                 return ctx;
125                         }
126
127                         set {
128                                 ctx = value;
129                         }
130                 }
131
132                 public MethodBase MethodBase {
133                         get {
134                                 return method;
135                         }
136                 }
137
138                 public string MethodName {
139                         get {
140                                 if (null == method)
141                                         return String.Empty;
142
143                                 return method.Name;
144                         }
145                 }
146
147                 public object MethodSignature {
148                         get {
149                                 if (methodSignature == null) {
150                                         ParameterInfo[] parameters = method.GetParameters();
151                                         methodSignature = new Type[parameters.Length];
152                                         for (int n=0; n<parameters.Length; n++)
153                                                 methodSignature[n] = parameters[n].ParameterType;
154                                 }
155                                 return methodSignature;
156                         }
157                 }
158
159                 public string TypeName {
160                         get {
161                                 if (null == method)
162                                         return String.Empty;
163
164                                 return method.DeclaringType.AssemblyQualifiedName;
165                         }
166                 }
167
168                 public string Uri {
169                         get {
170                                 return uri;
171                         }
172
173                         set {
174                                 uri = value;
175                         }
176                 }
177
178                 public object GetArg (int arg_num)
179                 {
180                         if (null == args)
181                                 return null;
182
183                         return args [arg_num];
184                 }
185                 
186                 public string GetArgName (int arg_num)
187                 {
188                         if (null == args)
189                                 return String.Empty;
190
191                         return names [arg_num];
192                 }
193
194                 public int InArgCount {
195                         get {
196                                 if (CallType == CallType.EndInvoke)
197                                         return -1;
198
199                                 if (null == args)
200                                         return 0;
201
202                                 int count = 0;
203
204                                 foreach (byte t in arg_types) {
205                                         if ((t & 1) != 0) count++;
206                                                 
207                                 }
208                                 return count;
209                         }
210                 }
211                 
212                 public object [] InArgs {
213                         get {                
214                                 int i, j, count = InArgCount;
215                                 object [] inargs = new object [count];
216
217                                 i = j = 0;
218                                 foreach (byte t in arg_types) {
219                                         if ((t & 1) != 0)
220                                                 inargs [j++] = args [i];
221                                         i++;
222                                 }
223                                 
224                                 return inargs;
225                         }
226                 }
227                 
228                 public object GetInArg (int arg_num)
229                 {
230                         int i = 0, j = 0;
231                         foreach (byte t in arg_types) {
232                                 if ((t & 1) != 0) {
233                                         if (j++ == arg_num)
234                                                 return args [i]; 
235                                 }
236                                 i++;
237                         }
238                         return null;
239                 }
240                 
241                 public string GetInArgName (int arg_num)
242                 {
243                         int i = 0, j = 0;
244                         foreach (byte t in arg_types) {
245                                 if ((t & 1) != 0) {
246                                         if (j++ == arg_num)
247                                                 return names [i]; 
248                                 }
249                                 i++;
250                         }
251                         return null;
252                 }
253
254                 public Exception Exception {
255                         get {
256                                 return exc;
257                         }
258                 }
259                 
260                 public int OutArgCount {
261                         get {
262                                 if (null == args)
263                                         return 0;
264                                 
265                                 int count = 0;
266
267                                 foreach (byte t in arg_types) {
268                                         if ((t & 2) != 0) count++;
269                                                 
270                                 }
271                                 return count;
272                         }
273                 }
274                 
275                 public object [] OutArgs {
276                         get {
277                                 if (null == args)
278                                         return null;
279
280                                 int i, j, count = OutArgCount;
281                                 object [] outargs = new object [count];
282
283                                 i = j = 0;
284                                 foreach (byte t in arg_types) {
285                                         if ((t & 2) != 0)
286                                                 outargs [j++] = args [i];
287                                         i++;
288                                 }
289                                 
290                                 return outargs;
291                         }
292                 }
293                 
294                 public object ReturnValue {
295                         get {
296                                 return rval;
297                         }
298                 }
299
300                 public object GetOutArg (int arg_num)
301                 {
302                         int i = 0, j = 0;
303                         foreach (byte t in arg_types) {
304                                 if ((t & 2) != 0) {
305                                         if (j++ == arg_num)
306                                                 return args [i]; 
307                                 }
308                                 i++;
309                         }
310                         return null;
311                 }
312                 
313                 public string GetOutArgName (int arg_num)
314                 {
315                         int i = 0, j = 0;
316                         foreach (byte t in arg_types) {
317                                 if ((t & 2) != 0) {
318                                         if (j++ == arg_num)
319                                                 return names [i]; 
320                                 }
321                                 i++;
322                         }
323                         return null;
324                 }
325
326                 Identity IInternalMessage.TargetIdentity
327                 {
328                         get { return identity; }
329                         set { identity = value; }
330                 }
331
332                 public bool IsAsync
333                 {
334                         get { return asyncResult != null; }
335                 }
336
337                 public AsyncResult AsyncResult
338                 {
339                         get { return asyncResult; }
340                 }
341
342                 internal CallType CallType
343                 {
344                         get
345                         {
346                                 // FIXME: ideally, the OneWay type would be set by the runtime
347                                 
348                                 if (call_type == CallType.Sync && RemotingServices.IsOneWay (method))
349                                         call_type = CallType.OneWay;
350                                 return call_type;
351                         }
352                 }
353                 
354                 public bool NeedsOutProcessing (out int outCount) {
355                         bool res = false;
356                         outCount = 0;
357                         foreach (byte t in arg_types) {
358                                 if ((t & 2) != 0)
359                                         outCount++;
360                                 else if ((t & 4) != 0)
361                                         res = true;
362                         }
363                         return outCount > 0 || res;
364                 }
365                 
366         }
367
368         internal enum CallType: int
369         {
370                 Sync = 0,
371                 BeginInvoke = 1,
372                 EndInvoke = 2,
373                 OneWay = 3
374         }
375 }
376