This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                         MonoMethodMessage mMsg = (MonoMethodMessage) request;
73
74                         if (mMsg.MethodBase == _cache_GetHashCodeMethod)
75                                 return new MethodResponse(ObjectIdentity.GetHashCode(), null, null, request as IMethodCallMessage);
76
77                         if (mMsg.MethodBase == _cache_GetTypeMethod)
78                                 return new MethodResponse(GetProxiedType(), null, null, request as IMethodCallMessage);
79
80                         mMsg.Uri = _targetUri;
81                         ((IInternalMessage)mMsg).TargetIdentity = _objectIdentity;
82
83                         _objectIdentity.NotifyClientDynamicSinks (true, request, true, false);
84
85                         IMessage response;
86                         IMessageSink sink;
87
88                         // Needs to go through the client context sink?
89                         if (Thread.CurrentContext.HasExitSinks && !_hasEnvoySink)
90                                 sink = Thread.CurrentContext.GetClientContextSinkChain ();
91                         else
92                                 sink = _sink;
93
94                         if (mMsg.CallType == CallType.Sync)
95                                 response = sink.SyncProcessMessage (request);
96                         else
97                         {
98                                 AsyncResult ares = mMsg.AsyncResult;
99                                 IMessageCtrl mctrl = sink.AsyncProcessMessage (request, ares);
100                                 if (ares != null) ares.SetMessageCtrl (mctrl);
101                                 response = new ReturnMessage (null, new object[0], 0, null, mMsg);
102                         }
103
104                         _objectIdentity.NotifyClientDynamicSinks (false, request, true, false);
105
106                         return response;
107                 }
108
109                 internal void AttachIdentity (Identity identity)
110                 {
111                         _objectIdentity = identity;
112
113                         if (identity is ClientActivatedIdentity)        // It is a CBO
114                         {
115                                 ClientActivatedIdentity cai = (ClientActivatedIdentity)identity;
116                                 _targetContext = cai.Context;
117                                 AttachServer (cai.GetServerObject ());
118                         }
119
120                         if (identity is ClientIdentity)
121                         {
122                                 ((ClientIdentity)identity).ClientProxy = (MarshalByRefObject) GetTransparentProxy();
123                                 _targetUri = ((ClientIdentity)identity).TargetUri;
124                         }
125                         else
126                                 _targetUri = identity.ObjectUri;
127
128                         if (_objectIdentity.EnvoySink != null)
129                         {
130                                 _sink = _objectIdentity.EnvoySink;
131                                 _hasEnvoySink = true;
132                         }
133                         else 
134                                 _sink = _objectIdentity.ChannelSink;
135
136                         _ctorCall = null;       // Object already constructed
137                 }
138
139                 internal IMessage ActivateRemoteObject (IMethodMessage request)
140                 {
141                         if (_ctorCall == null)  // It must be a WKO
142                                 return new ConstructionResponse (this, null, (IMethodCallMessage) request);     // Ignore constructor call for WKOs
143
144                         _ctorCall.CopyFrom (request);
145                         return ActivationServices.Activate (this, _ctorCall);
146                 }
147
148                 public string TypeName 
149                 { 
150                         get
151                         {
152                                 if (_objectIdentity is ClientIdentity) {
153                                         ObjRef oref = _objectIdentity.CreateObjRef (null);
154                                         if (oref.TypeInfo != null) return oref.TypeInfo.TypeName;
155                                 }
156                                 return GetProxiedType().AssemblyQualifiedName;
157                         }
158                         
159                         set
160                         {
161                                 throw new NotSupportedException ();
162                         }
163                 }
164                 
165                 public bool CanCastTo (Type fromType, object o)
166                 {
167                         if (_objectIdentity is ClientIdentity) {
168                                 ObjRef oref = _objectIdentity.CreateObjRef (null);
169                                 if (oref.IsReferenceToWellKnow && (fromType.IsInterface || GetProxiedType() == typeof(MarshalByRefObject))) return true;
170                                 if (oref.TypeInfo != null) return oref.TypeInfo.CanCastTo (fromType, o);
171                         }
172                         return fromType.IsAssignableFrom (GetProxiedType());
173                 }
174                 
175                 ~RemotingProxy()
176                 {
177                         if (_objectIdentity != null)
178                         {
179                                 if (!(_objectIdentity is ClientActivatedIdentity))      // Local CBO proxy?
180                                         RemotingServices.DisposeIdentity (_objectIdentity);
181                         }
182                 }
183                 
184         }
185 }