* RemotingProxy.cs: In Invoke, suport IMessages other than MonoMessage.
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Proxies / RemotingProxy.cs
1 //
2 // System.Runtime.Remoting.Proxies.RemotingProxy.cs
3 //
4 // Authors:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //   Lluis Sanchez Gual (lsg@ctv.es)
7 //
8 // (C) 2001 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.Reflection;
36 using System.Runtime.Remoting.Messaging;
37 using System.Runtime.Remoting.Activation;
38 using System.Runtime.Remoting.Channels;
39 using System.Runtime.Remoting.Contexts;
40 using System.Runtime.CompilerServices;
41 using System.Threading;
42
43
44 namespace System.Runtime.Remoting.Proxies
45 {
46
47         internal class RemotingProxy : RealProxy, IRemotingTypeInfo
48         {
49                 static MethodInfo _cache_GetTypeMethod = typeof(System.Object).GetMethod("GetType");
50                 static MethodInfo _cache_GetHashCodeMethod = typeof(System.Object).GetMethod("GetHashCode");
51
52                 IMessageSink _sink;
53                 bool _hasEnvoySink;
54                 ConstructionCall _ctorCall;
55                 string _targetUri;
56
57                 internal RemotingProxy (Type type, ClientIdentity identity) : base (type, identity)
58                 {
59                         _sink = identity.ChannelSink;
60                         _hasEnvoySink = false;
61                         _targetUri = identity.TargetUri;
62                 }
63
64                 internal RemotingProxy (Type type, string activationUrl, object[] activationAttributes) : base (type)
65                 {
66                         _hasEnvoySink = false;
67                         _ctorCall = ActivationServices.CreateConstructionCall (type, activationUrl, activationAttributes);
68                 }
69
70                 public override IMessage Invoke (IMessage request)
71                 {
72                         IMethodCallMessage mm = request as IMethodCallMessage;
73
74                         if (mm != null) {
75                                 if (mm.MethodBase == _cache_GetHashCodeMethod)
76                                         return new MethodResponse(ObjectIdentity.GetHashCode(), null, null, mm);
77         
78                                 if (mm.MethodBase == _cache_GetTypeMethod)
79                                         return new MethodResponse(GetProxiedType(), null, null, mm);
80                         }
81                         
82                         IInternalMessage im = request as IInternalMessage;
83                         if (im != null) {
84                                 if (im.Uri == null) im.Uri = _targetUri;
85                                 im.TargetIdentity = _objectIdentity;
86                         }
87
88                         _objectIdentity.NotifyClientDynamicSinks (true, request, true, false);
89
90                         IMessage response;
91                         IMessageSink sink;
92
93                         // Needs to go through the client context sink?
94                         if (Thread.CurrentContext.HasExitSinks && !_hasEnvoySink)
95                                 sink = Thread.CurrentContext.GetClientContextSinkChain ();
96                         else
97                                 sink = _sink;
98
99                         MonoMethodMessage mMsg = request as MonoMethodMessage;
100                         if (mMsg == null || mMsg.CallType == CallType.Sync)
101                                 response = sink.SyncProcessMessage (request);
102                         else
103                         {
104                                 AsyncResult ares = mMsg.AsyncResult;
105                                 IMessageCtrl mctrl = sink.AsyncProcessMessage (request, ares);
106                                 if (ares != null) ares.SetMessageCtrl (mctrl);
107                                 response = new ReturnMessage (null, new object[0], 0, null, mMsg);
108                         }
109
110                         _objectIdentity.NotifyClientDynamicSinks (false, request, true, false);
111
112                         return response;
113                 }
114
115                 internal void AttachIdentity (Identity identity)
116                 {
117                         _objectIdentity = identity;
118
119                         if (identity is ClientActivatedIdentity)        // It is a CBO
120                         {
121                                 ClientActivatedIdentity cai = (ClientActivatedIdentity)identity;
122                                 _targetContext = cai.Context;
123                                 AttachServer (cai.GetServerObject ());
124                                 cai.SetClientProxy ((MarshalByRefObject) GetTransparentProxy());
125                         }
126
127                         if (identity is ClientIdentity)
128                         {
129                                 ((ClientIdentity)identity).ClientProxy = (MarshalByRefObject) GetTransparentProxy();
130                                 _targetUri = ((ClientIdentity)identity).TargetUri;
131                         }
132                         else
133                                 _targetUri = identity.ObjectUri;
134
135                         if (_objectIdentity.EnvoySink != null)
136                         {
137                                 _sink = _objectIdentity.EnvoySink;
138                                 _hasEnvoySink = true;
139                         }
140                         else 
141                                 _sink = _objectIdentity.ChannelSink;
142
143                         _ctorCall = null;       // Object already constructed
144                 }
145
146                 internal IMessage ActivateRemoteObject (IMethodMessage request)
147                 {
148                         if (_ctorCall == null)  // It must be a WKO
149                                 return new ConstructionResponse (this, null, (IMethodCallMessage) request);     // Ignore constructor call for WKOs
150
151                         _ctorCall.CopyFrom (request);
152                         return ActivationServices.Activate (this, _ctorCall);
153                 }
154
155                 public string TypeName 
156                 { 
157                         get
158                         {
159                                 if (_objectIdentity is ClientIdentity) {
160                                         ObjRef oref = _objectIdentity.CreateObjRef (null);
161                                         if (oref.TypeInfo != null) return oref.TypeInfo.TypeName;
162                                 }
163                                 return GetProxiedType().AssemblyQualifiedName;
164                         }
165                         
166                         set
167                         {
168                                 throw new NotSupportedException ();
169                         }
170                 }
171                 
172                 public bool CanCastTo (Type fromType, object o)
173                 {
174                         if (_objectIdentity is ClientIdentity) {
175                                 ObjRef oref = _objectIdentity.CreateObjRef (null);
176                                 if (oref.IsReferenceToWellKnow && (fromType.IsInterface || GetProxiedType() == typeof(MarshalByRefObject))) return true;
177                                 if (oref.TypeInfo != null) return oref.TypeInfo.CanCastTo (fromType, o);
178                         }
179                         return fromType.IsAssignableFrom (GetProxiedType());
180                 }
181                 
182                 ~RemotingProxy()
183                 {
184                         if (_objectIdentity != null)
185                         {
186                                 if (!(_objectIdentity is ClientActivatedIdentity))      // Local CBO proxy?
187                                         RemotingServices.DisposeIdentity (_objectIdentity);
188                         }
189                 }
190                 
191         }
192 }