This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / ReturnMessage.cs
1 //
2 // System.Runtime.Remoting.Messaging.ReturnMessage.cs
3 //
4 // Author:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
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.IO;
37
38 namespace System.Runtime.Remoting.Messaging 
39 {
40         public class ReturnMessage : IMethodReturnMessage, IMethodMessage, IMessage, IInternalMessage 
41         {
42                 object[] _outArgs;
43                 object[] _args;
44                 int _outArgsCount;
45                 LogicalCallContext _callCtx;
46                 object _returnValue;
47                 string _uri;
48                 Exception _exception;
49                 MethodBase _methodBase;
50                 string _methodName;
51                 object _methodSignature;
52                 string _typeName;
53                 MethodReturnDictionary _properties;
54                 Identity _targetIdentity;
55                 ArgInfo _inArgInfo;
56
57                 public ReturnMessage (object returnValue, object [] outArgs,
58                                int outArgCount, LogicalCallContext callCtx,
59                                IMethodCallMessage request)
60                 {
61                         // outArgCount tells how many values of outArgs are valid
62
63                         _returnValue = returnValue;
64                         _args = outArgs;
65                         _outArgsCount = outArgCount;
66                         _callCtx = callCtx;
67                         _uri = request.Uri;
68                         _methodBase = request.MethodBase;
69                         _methodName = request.MethodName;
70                         _methodSignature = request.MethodSignature;
71                         _typeName = request.TypeName;
72                         if (_args == null) _args = new object [outArgCount];
73                 }
74
75                 public ReturnMessage (Exception exc, IMethodCallMessage request)
76                 {
77                         _exception = exc;
78                         
79                         if (request != null)
80                         {
81                                 _methodBase = request.MethodBase;
82                                 _methodName = request.MethodName;
83                                 _methodSignature = request.MethodSignature;
84                                 _typeName = request.TypeName;
85                         }
86                         _args = new object[0];  // .NET does this
87                 }
88                 
89                 public int ArgCount {
90                         get {
91                                 return _args.Length;
92                         }
93                 }
94                 
95                 public object [] Args {
96                         get {
97                                 return _args;
98                         }
99                 }
100                 
101                 public bool HasVarArgs {
102                         get {
103                                 return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0;
104                         }
105                 }
106
107                 public LogicalCallContext LogicalCallContext {
108                         get {
109                                 return _callCtx;
110                         }
111                 }
112
113                 public MethodBase MethodBase {
114                         get {
115                                 return _methodBase;
116                         }
117                 }
118
119                 public string MethodName {
120                         get {
121                                 return _methodName;
122
123                         }
124                 }
125
126                 public object MethodSignature {
127                         get {
128                                 return _methodSignature;
129                         }
130                 }
131
132                 public virtual IDictionary Properties {
133                         get {
134                                 if (_properties == null) _properties = new MethodReturnDictionary (this);
135                                 return _properties;
136                         }
137                 }
138
139                 public string TypeName {
140                         get {
141                                 return _typeName;
142                         }
143                 }
144
145                 public string Uri {
146                         get {
147                                 return _uri;
148                         }
149
150                         set {
151                                 _uri = value;
152                         }
153                 }
154
155                 public object GetArg (int arg_num)
156                 {
157                         return _args [arg_num];
158                 }
159                 
160                 public string GetArgName (int arg_num)
161                 {
162                         return _methodBase.GetParameters()[arg_num].Name;
163                 }
164
165                 public Exception Exception {
166                         get {
167                                 return _exception;
168                         }
169                 }
170
171                 public int OutArgCount {
172                         get {
173                                 if (_args == null || _args.Length == 0) return 0;
174                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
175                                 return _inArgInfo.GetInOutArgCount ();
176                         }
177                 }
178
179                 public object [] OutArgs {
180                         get {
181                                 if (_outArgs == null && _args != null) {
182                                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
183                                         _outArgs = _inArgInfo.GetInOutArgs (_args);
184                                 }                                       
185                                 return _outArgs;
186                         }
187                 }
188
189                 public virtual object ReturnValue {
190                         get {
191                                 return _returnValue;
192                         }
193                 }
194
195                 public object GetOutArg (int arg_num)
196                 {
197                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
198                         return _args[_inArgInfo.GetInOutArgIndex (arg_num)];
199                 }
200
201                 public string GetOutArgName (int arg_num)
202                 {
203                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
204                         return _inArgInfo.GetInOutArgName(arg_num);
205                 }
206
207                 Identity IInternalMessage.TargetIdentity
208                 {
209                         get { return _targetIdentity; }
210                         set { _targetIdentity = value; }
211                 }
212
213                 public override string ToString ()
214                 {
215                         string s = _typeName.Split(',')[0] + "." + _methodName + " (";
216                         if (_exception != null)
217                         {
218                                 s += "Exception)\n" + _exception;
219                         }
220                         else
221                         {
222                                 for (int n=0; n<OutArgs.Length; n++)
223                                 {
224                                         if (n>0) s+= ", ";
225                                         if (OutArgs[n] != null) s += OutArgs[n].GetType().Name + " ";
226                                         s += GetOutArgName (n);
227                                         if (OutArgs[n] != null) s += " = {" + OutArgs[n] + "}";
228                                         else s+=" = {null}";
229                                 }
230                                 s += ")";
231                         }
232                         return s;
233                 }
234         }
235 }