2 // System.Runtime.Remoting.Channels.ChannelServices.cs
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Lluis Sanchez Gual (lluis@ideary.com)
8 // 2002 (C) Copyright, Ximian, Inc.
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.Remoting;
37 using System.Runtime.Remoting.Channels;
38 using System.Runtime.Remoting.Messaging;
39 using System.Runtime.Remoting.Contexts;
41 namespace System.Runtime.Remoting
44 internal class ChannelInfo : IChannelInfo
46 object [] channelData = null;
50 channelData = ChannelServices.GetCurrentChannelInfo ();
53 public ChannelInfo (object remoteChannelData)
55 channelData = new object[] { remoteChannelData };
58 public object[] ChannelData
71 namespace System.Runtime.Remoting.Channels
73 public sealed class ChannelServices
75 private static ArrayList registeredChannels = new ArrayList ();
76 private static ArrayList delayedClientChannels = new ArrayList ();
78 private static CrossContextChannel _crossContextSink = new CrossContextChannel();
80 internal static string CrossContextUrl = "__CrossContext";
82 private ChannelServices ()
86 internal static CrossContextChannel CrossContextChannel
88 get { return _crossContextSink; }
91 internal static IMessageSink CreateClientChannelSinkChain(string url, object remoteChannelData, out string objectUri)
93 // Locate a channel that can parse the url. This channel will be used to
94 // create the sink chain.
96 object[] channelDataArray = (object[])remoteChannelData;
98 lock (registeredChannels.SyncRoot)
100 // First of all, try registered channels
101 foreach (IChannel c in registeredChannels)
103 IChannelSender sender = c as IChannelSender;
104 if (sender == null) continue;
106 IMessageSink sink = CreateClientChannelSinkChain (sender, url, channelDataArray, out objectUri);
107 if (sink != null) return sink;
110 // Not found. Try now creation delayed channels
111 RemotingConfiguration.LoadDefaultDelayedChannels ();
112 foreach (IChannelSender sender in delayedClientChannels)
114 IMessageSink sink = CreateClientChannelSinkChain (sender, url, channelDataArray, out objectUri);
116 delayedClientChannels.Remove (sender);
117 RegisterChannel (sender);
127 internal static IMessageSink CreateClientChannelSinkChain (IChannelSender sender, string url, object[] channelDataArray, out string objectUri)
130 if (channelDataArray == null) {
131 return sender.CreateMessageSink (url, null, out objectUri);
134 foreach (object data in channelDataArray) {
135 IMessageSink sink = sender.CreateMessageSink (url, data, out objectUri);
136 if (sink != null) return sink;
142 public static IChannel[] RegisteredChannels
145 lock (registeredChannels.SyncRoot)
147 ArrayList list = new ArrayList ();
149 for (int i = 0; i < registeredChannels.Count; i++) {
150 IChannel ch = (IChannel) registeredChannels[i];
151 if (ch is CrossAppDomainChannel) continue;
155 return (IChannel[]) list.ToArray (typeof(IChannel));
160 public static IServerChannelSink CreateServerChannelSinkChain (
161 IServerChannelSinkProvider provider, IChannelReceiver channel)
163 IServerChannelSinkProvider tmp = provider;
164 while (tmp.Next != null) tmp = tmp.Next;
165 tmp.Next = new ServerDispatchSinkProvider ();
167 // Every provider has to call CreateSink() of its next provider
168 return provider.CreateSink (channel);
171 public static ServerProcessing DispatchMessage (
172 IServerChannelSinkStack sinkStack,
174 out IMessage replyMsg)
176 if (msg == null) throw new ArgumentNullException ("msg");
178 // Async processing is not done here because there isn't any way
179 // to know if a message is to be dispatched sync or asynchronously.
181 replyMsg = SyncDispatchMessage (msg);
183 if (RemotingServices.IsOneWay (((IMethodMessage) msg).MethodBase))
184 return ServerProcessing.OneWay;
186 return ServerProcessing.Complete;
189 public static IChannel GetChannel (string name)
191 lock (registeredChannels.SyncRoot)
193 foreach (IChannel chnl in registeredChannels) {
194 if (chnl.ChannelName == name && !(chnl is CrossAppDomainChannel)) return chnl;
200 public static IDictionary GetChannelSinkProperties (object obj)
202 if (!RemotingServices.IsTransparentProxy (obj))
203 throw new ArgumentException ("obj must be a proxy","obj");
205 ClientIdentity ident = (ClientIdentity) RemotingServices.GetRealProxy (obj).ObjectIdentity;
206 IMessageSink sink = ident.ChannelSink;
207 ArrayList dics = new ArrayList ();
209 while (sink != null && !(sink is IClientChannelSink))
210 sink = sink.NextSink;
213 return new Hashtable ();
215 IClientChannelSink csink = sink as IClientChannelSink;
216 while (csink != null)
218 dics.Add (csink.Properties);
219 csink = csink.NextChannelSink;
222 IDictionary[] adics = (IDictionary[]) dics.ToArray (typeof(IDictionary[]));
223 return new AggregateDictionary (adics);
226 public static string[] GetUrlsForObject (MarshalByRefObject obj)
228 string uri = RemotingServices.GetObjectUri (obj);
229 if (uri == null) return new string [0];
231 ArrayList list = new ArrayList ();
233 lock (registeredChannels.SyncRoot)
235 foreach (object chnl_obj in registeredChannels) {
236 if (chnl_obj is CrossAppDomainChannel) continue;
238 IChannelReceiver chnl = chnl_obj as IChannelReceiver;
241 list.AddRange (chnl.GetUrlsForUri (uri));
245 return (string[]) list.ToArray (typeof(string));
248 public static void RegisterChannel (IChannel chnl)
250 RegisterChannel (chnl, false);
254 [MonoTODO ("Implement ensureSecurity")]
259 static void RegisterChannel (IChannel chnl, bool ensureSecurity)
262 // Put the channel in the correct place according to its priority.
263 // Since there are not many channels, a linear search is ok.
265 lock (registeredChannels.SyncRoot)
268 for (int n = 0; n < registeredChannels.Count; n++)
270 IChannel regc = (IChannel) registeredChannels[n];
272 if (regc.ChannelName == chnl.ChannelName && chnl.ChannelName != "")
273 throw new RemotingException ("Channel " + regc.ChannelName + " already registered");
275 if (regc.ChannelPriority < chnl.ChannelPriority && pos==-1)
279 if (pos != -1) registeredChannels.Insert (pos, chnl);
280 else registeredChannels.Add (chnl);
282 IChannelReceiver receiver = chnl as IChannelReceiver;
283 if (receiver != null) receiver.StartListening (null);
287 internal static void RegisterChannelConfig (ChannelData channel)
289 IServerChannelSinkProvider serverSinks = null;
290 IClientChannelSinkProvider clientSinks = null;
292 // Create server providers
293 for (int n=channel.ServerProviders.Count-1; n>=0; n--)
295 ProviderData prov = channel.ServerProviders[n] as ProviderData;
296 IServerChannelSinkProvider sinkp = (IServerChannelSinkProvider) CreateProvider (prov);
297 sinkp.Next = serverSinks;
301 // Create client providers
302 for (int n=channel.ClientProviders.Count-1; n>=0; n--)
304 ProviderData prov = channel.ClientProviders[n] as ProviderData;
305 IClientChannelSinkProvider sinkp = (IClientChannelSinkProvider) CreateProvider (prov);
306 sinkp.Next = clientSinks;
310 // Create the channel
312 Type type = Type.GetType (channel.Type);
313 if (type == null) throw new RemotingException ("Type '" + channel.Type + "' not found");
317 bool clienc = typeof (IChannelSender).IsAssignableFrom (type);
318 bool serverc = typeof (IChannelReceiver).IsAssignableFrom (type);
320 if (clienc && serverc) {
321 signature = new Type [] {typeof(IDictionary), typeof(IClientChannelSinkProvider), typeof(IServerChannelSinkProvider)};
322 parms = new Object[] {channel.CustomProperties, clientSinks, serverSinks};
325 signature = new Type [] {typeof(IDictionary), typeof(IClientChannelSinkProvider)};
326 parms = new Object[] {channel.CustomProperties, clientSinks};
329 signature = new Type [] {typeof(IDictionary), typeof(IServerChannelSinkProvider)};
330 parms = new Object[] {channel.CustomProperties, serverSinks};
333 throw new RemotingException (type + " is not a valid channel type");
335 ConstructorInfo ctor = type.GetConstructor (signature);
337 throw new RemotingException (type + " does not have a valid constructor");
342 ch = (IChannel) ctor.Invoke (parms);
344 catch (TargetInvocationException ex)
346 throw ex.InnerException;
349 lock (registeredChannels.SyncRoot)
351 if (channel.DelayLoadAsClientChannel == "true" && !(ch is IChannelReceiver))
352 delayedClientChannels.Add (ch);
354 RegisterChannel (ch);
358 static object CreateProvider (ProviderData prov)
360 Type pvtype = Type.GetType (prov.Type);
361 if (pvtype == null) throw new RemotingException ("Type '" + prov.Type + "' not found");
362 Object[] pvparms = new Object[] {prov.CustomProperties, prov.CustomData};
366 return Activator.CreateInstance (pvtype, pvparms);
370 if (ex is TargetInvocationException) ex = ((TargetInvocationException)ex).InnerException;
371 throw new RemotingException ("An instance of provider '" + pvtype + "' could not be created: " + ex.Message);
375 public static IMessage SyncDispatchMessage (IMessage msg)
377 IMessage ret = CheckIncomingMessage (msg);
378 if (ret != null) return CheckReturnMessage (msg, ret);
379 ret = _crossContextSink.SyncProcessMessage (msg);
380 return CheckReturnMessage (msg, ret);
383 public static IMessageCtrl AsyncDispatchMessage (IMessage msg, IMessageSink replySink)
385 IMessage ret = CheckIncomingMessage (msg);
387 replySink.SyncProcessMessage (CheckReturnMessage (msg, ret));
392 if (RemotingConfiguration.CustomErrorsEnabled (IsLocalCall (msg)))
393 replySink = new ExceptionFilterSink (msg, replySink);
396 return _crossContextSink.AsyncProcessMessage (msg, replySink);
399 static ReturnMessage CheckIncomingMessage (IMessage msg)
401 IMethodMessage call = (IMethodMessage)msg;
402 ServerIdentity identity = RemotingServices.GetIdentityForUri (call.Uri) as ServerIdentity;
404 if (identity == null)
405 return new ReturnMessage (new RemotingException ("No receiver for uri " + call.Uri), (IMethodCallMessage) msg);
407 RemotingServices.SetMessageTargetIdentity (msg, identity);
411 internal static IMessage CheckReturnMessage (IMessage callMsg, IMessage retMsg)
414 IMethodReturnMessage ret = retMsg as IMethodReturnMessage;
415 if (ret != null && ret.Exception != null)
417 if (RemotingConfiguration.CustomErrorsEnabled (IsLocalCall (callMsg)))
419 Exception ex = new Exception ("Server encountered an internal error. For more information, turn off customErrors in the server's .config file.");
420 retMsg = new MethodResponse (ex, (IMethodCallMessage)callMsg);
427 static bool IsLocalCall (IMessage callMsg)
431 /* How can I know if a call is local?!?
433 object isLocal = callMsg.Properties ["__isLocalCall"];
434 if (isLocal == null) return false;
435 return (bool)isLocal;
439 public static void UnregisterChannel (IChannel chnl)
442 throw new ArgumentNullException ();
444 lock (registeredChannels.SyncRoot)
446 for (int n=0; n<registeredChannels.Count; n++)
448 if (registeredChannels [n] == (object)chnl) {
449 registeredChannels.RemoveAt (n);
450 IChannelReceiver chnlReceiver = chnl as IChannelReceiver;
451 if(chnlReceiver != null)
452 chnlReceiver.StopListening(null);
457 throw new RemotingException ("Channel not registered");
462 internal static object [] GetCurrentChannelInfo ()
464 ArrayList list = new ArrayList ();
466 lock (registeredChannels.SyncRoot)
468 foreach (object chnl_obj in registeredChannels) {
469 IChannelReceiver chnl = chnl_obj as IChannelReceiver;
472 object chnl_data = chnl.ChannelData;
473 if (chnl_data != null)
474 list.Add (chnl_data);
479 return list.ToArray ();
483 internal class ExceptionFilterSink: IMessageSink
488 public ExceptionFilterSink (IMessage call, IMessageSink next)
494 public IMessage SyncProcessMessage (IMessage msg)
496 return _next.SyncProcessMessage (ChannelServices.CheckReturnMessage (_call, msg));
499 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
501 throw new InvalidOperationException();
504 public IMessageSink NextSink
506 get { return _next; }