Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MethodResponse.cs « System.Runtime.Remoting.Messaging « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ddcfbe214813bb6e17f2d11de0044ffae5f3ad14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// System.Runtime.Remoting.Messaging.MethodResponse.cs
//
// Author:	Duncan Mak (duncan@ximian.com)
//		Patrik Torstensson
//
// 2002 (C) Copyright, Ximian, Inc.
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Serialization;

namespace System.Runtime.Remoting.Messaging {

	[Serializable] [CLSCompliant (false)]
	public class MethodResponse : IMethodReturnMessage, ISerializable, IInternalMessage, ISerializationRootObject
	{
		string _methodName;
		string _uri;
		string _typeName;
		MethodBase _methodBase;

		object _returnValue;
		Exception _exception;
		Type [] _methodSignature;
		ArgInfo _inArgInfo;

		object []  _args;
		object []  _outArgs;
		IMethodCallMessage _callMsg;
		LogicalCallContext _callContext;
		Identity _targetIdentity;

		protected IDictionary ExternalProperties;
		protected IDictionary InternalProperties;

		public MethodResponse (Header[] headers, IMethodCallMessage mcm)
		{
			if (mcm != null)
			{
				_methodName = mcm.MethodName;
				_uri = mcm.Uri;
				_typeName = mcm.TypeName;
				_methodBase = mcm.MethodBase;
				_methodSignature = (Type[]) mcm.MethodSignature;
				_args = mcm.Args;
			}

			if (headers != null)
			{
				foreach (Header header in headers)
					InitMethodProperty (header.Name, header.Value);
			}
		}

		internal MethodResponse (Exception e, IMethodCallMessage msg) {
			_callMsg = msg;

			if (null != msg)
				_uri = msg.Uri;
			else
				_uri = String.Empty;
			
			_exception = e;
			_returnValue = null;
			_outArgs = new object[0];	// .NET does this
		}

		internal MethodResponse (object returnValue, object [] outArgs, LogicalCallContext callCtx, IMethodCallMessage msg) {
			_callMsg = msg;

			_uri = msg.Uri;
			
			_exception = null;
			_returnValue = returnValue;
			_args = outArgs;
		}

		internal MethodResponse (IMethodCallMessage msg, CADMethodReturnMessage retmsg) {
			_callMsg = msg;

			_methodBase = msg.MethodBase;
			//_typeName = msg.TypeName;
			_uri = msg.Uri;
			_methodName = msg.MethodName;
			
			// Get unmarshalled arguments
			ArrayList args = retmsg.GetArguments ();

			_exception = retmsg.GetException (args);
			_returnValue = retmsg.GetReturnValue (args);
			_args = retmsg.GetArgs (args);

			_callContext = retmsg.GetLogicalCallContext (args);
			if (_callContext == null) _callContext = new LogicalCallContext ();
			
			if (retmsg.PropertiesCount > 0)
				CADMessageBase.UnmarshalProperties (Properties, retmsg.PropertiesCount, args);
		}

		internal MethodResponse (SerializationInfo info, StreamingContext context) 
		{
			foreach (SerializationEntry entry in info)
				InitMethodProperty (entry.Name, entry.Value);
		}

		internal void InitMethodProperty (string key, object value) 
		{
			switch (key) 
			{
				case "__TypeName": _typeName = (string) value; break;
				case "__MethodName": _methodName = (string) value; break;
				case "__MethodSignature": _methodSignature = (Type[]) value; break;
				case "__Uri": _uri = (string) value; break;
				case "__Return": _returnValue = value; break;
				case "__OutArgs": _args = (object[]) value; break;
				case "__fault": _exception = (Exception) value; break;
				case "__CallContext": _callContext = (LogicalCallContext) value; break;
				default: Properties [key] = value; break;
			}
		}

		public int ArgCount {
			get { 
				if (null == _args)
					return 0;

				return _args.Length;
			}
		}

		public object[] Args {
			get { 
				return _args; 
			}
		}

		public Exception Exception {
			get { 
				return _exception; 
			}
		}
		
		public bool HasVarArgs {
			get { 
				return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0;
			}
		}
		
		public LogicalCallContext LogicalCallContext {
			get {
				if (_callContext == null)
					_callContext = new LogicalCallContext ();
				return _callContext;
			}
		}
		
		public MethodBase MethodBase {
			get { 
				if (null == _methodBase) {
					if (_callMsg != null)
						_methodBase = _callMsg.MethodBase;
					else if (MethodName != null && TypeName != null)
						_methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
				}
				return _methodBase;
			}
		}

		public string MethodName {
			get { 
				if (null == _methodName && null != _callMsg)
					_methodName = _callMsg.MethodName;

				return _methodName;
			}
		}

		public object MethodSignature {
			get { 
				if (null == _methodSignature && null != _callMsg)
					_methodSignature = (Type []) _callMsg.MethodSignature;

				return _methodSignature;
			}
		}

		public int OutArgCount {
			get { 
				if (_args == null || _args.Length == 0) return 0;
				if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
				return _inArgInfo.GetInOutArgCount ();
			}
		}

		public object[] OutArgs {
			get { 
				if (_outArgs == null && _args != null) {
					if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
					_outArgs = _inArgInfo.GetInOutArgs (_args);
				}					
				return _outArgs;
			}
		}

		public virtual IDictionary Properties {
			get { 
				if (null == ExternalProperties) {
					MethodReturnDictionary properties = new MethodReturnDictionary (this);
					ExternalProperties = properties;
					InternalProperties = properties.GetInternalProperties();
				}
				
				return ExternalProperties;
			}
		}

		public object ReturnValue {
			get { 
				return _returnValue;
			}
		}

		public string TypeName {
			get { 
				if (null == _typeName && null != _callMsg)
					_typeName = _callMsg.TypeName;

				return _typeName;
			}
		}

		public string Uri {
			get { 
				if (null == _uri && null != _callMsg)
					_uri = _callMsg.Uri;
				
				return _uri;
			}

			set { 
				_uri = value;
			}
		}

		public object GetArg (int argNum)
		{
			if (null == _args)
				return null;

			return _args [argNum];
		}

		public string GetArgName (int index)
		{
			return MethodBase.GetParameters()[index].Name;
		}

		public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
		{
			if (_exception == null)
			{
				info.AddValue ("__TypeName", _typeName);
				info.AddValue ("__MethodName", _methodName);
				info.AddValue ("__MethodSignature", _methodSignature);
				info.AddValue ("__Uri", _uri);
				info.AddValue ("__Return", _returnValue);
				info.AddValue ("__OutArgs", _args);
			}
			else
				info.AddValue ("__fault", _exception);

			info.AddValue ("__CallContext", _callContext);

			if (InternalProperties != null) {
				foreach (DictionaryEntry entry in InternalProperties)
					info.AddValue ((string) entry.Key, entry.Value);
			}
		} 

		public object GetOutArg (int argNum)
		{
			if (_args == null) return null;
			if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
			return _args[_inArgInfo.GetInOutArgIndex (argNum)];
		}

		public string GetOutArgName (int index)
		{
			if (null == _methodBase)
				return "__method_" + index;

			if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
			return _inArgInfo.GetInOutArgName(index);
		}

		[MonoTODO]
		public virtual object HeaderHandler (Header[] h)
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public void RootSetObjectData (SerializationInfo info, StreamingContext context)
		{
			throw new NotImplementedException ();
		} 

		Identity IInternalMessage.TargetIdentity
		{
			get { return _targetIdentity; }
			set { _targetIdentity = value; }
		}

		public override string ToString ()
		{
			string s = _typeName.Split(',')[0] + "." + _methodName + " (";
			if (_exception != null)
			{
				s += "Exception\n)" + _exception;
			}
			else
			{
				if (_args != null)
				{
					for (int n=0; n<_args.Length; n++)
					{
						if (n>0) s+= ", ";
						if (_args[n] != null) s += _args[n].GetType().Name + " ";
						s += GetOutArgName (n);
						if (_args[n] != null) s += " = {" + _args[n] + "}";
						else s+=" = {null}";
					}
				}
				s += ")";
			}
			return s;
		}

	}
}