2 // System.Runtime.Remoting.Channels.CrossAppDomainChannel.cs
4 // Author: Patrik Torstensson (totte_mono@yahoo.com)
\r
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // 2003 (C) Copyright, Ximian, Inc.
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
35 using System.Threading;
\r
36 using System.Runtime.Remoting;
\r
37 using System.Runtime.Remoting.Messaging;
\r
38 using System.Runtime.Remoting.Channels;
\r
39 using System.Runtime.Remoting.Contexts;
\r
40 using System.Runtime.Serialization;
\r
41 using System.Runtime.Serialization.Formatters.Binary;
\r
42 using System.Reflection;
\r
44 namespace System.Runtime.Remoting.Channels
\r
47 // Holds the cross appdomain channel data (used to get/create the correct sink)
49 internal class CrossAppDomainData
\r
51 // TODO: Add context support
52 private int _ContextID;
\r
53 private int _DomainID;
\r
54 private string _processGuid;
\r
56 internal CrossAppDomainData(int domainId)
\r
58 _DomainID = domainId;
\r
59 _processGuid = RemotingConfiguration.ProcessId;
\r
62 internal int DomainID
\r
64 get { return _DomainID; }
\r
67 internal string ProcessID
\r
69 get { return _processGuid; }
\r
73 // Responsible for marshalling objects between appdomains
75 internal class CrossAppDomainChannel : IChannel, IChannelSender, IChannelReceiver
\r
77 private const String _strName = "MONOCAD";
78 private const String _strBaseURI = "MONOCADURI";
80 private static Object s_lock = new Object();
82 internal static void RegisterCrossAppDomainChannel()
\r
86 // todo: make singleton
\r
87 CrossAppDomainChannel monocad = new CrossAppDomainChannel();
\r
88 ChannelServices.RegisterChannel ((IChannel) monocad);
\r
92 // IChannel implementation
\r
93 public virtual String ChannelName
\r
95 get { return _strName; }
\r
98 public virtual int ChannelPriority
\r
100 get { return 100; }
\r
103 public String Parse(String url, out String objectURI)
\r
109 // IChannelReceiver
\r
110 public virtual Object ChannelData
\r
112 get { return new CrossAppDomainData(Thread.GetDomainID()); }
\r
115 public virtual String[] GetUrlsForUri(String objectURI)
\r
117 throw new NotSupportedException("CrossAppdomain channel dont support UrlsForUri");
\r
121 public virtual void StartListening(Object data) {}
\r
122 public virtual void StopListening(Object data) {}
\r
125 public virtual IMessageSink CreateMessageSink(String url, Object data, out String uri)
\r
131 // Get the data and then get the sink
\r
132 CrossAppDomainData cadData = data as CrossAppDomainData;
\r
133 if (cadData != null && cadData.ProcessID == RemotingConfiguration.ProcessId)
\r
134 // GetSink creates a new sink if we don't have any (use contexts here later)
\r
135 return CrossAppDomainSink.GetSink(cadData.DomainID);
\r
137 if (url != null && url.StartsWith(_strName))
\r
138 throw new NotSupportedException("Can't create a named channel via crossappdomain");
\r
144 [MonoTODO("Handle domain unloading?")]
\r
145 internal class CrossAppDomainSink : IMessageSink
\r
147 private static Hashtable s_sinks = new Hashtable();
\r
149 private static MethodInfo processMessageMethod =
150 typeof (CrossAppDomainSink).GetMethod ("ProcessMessageInDomain", BindingFlags.NonPublic|BindingFlags.Static);
153 private int _domainID;
\r
155 internal CrossAppDomainSink(int domainID)
\r
157 _domainID = domainID;
\r
160 internal static CrossAppDomainSink GetSink(int domainID)
\r
162 // Check if we have a sink for the current domainID
\r
163 // note, locking is not to bad here, very few class to GetSink
\r
164 lock (s_sinks.SyncRoot)
\r
166 if (s_sinks.ContainsKey(domainID))
\r
167 return (CrossAppDomainSink) s_sinks[domainID];
\r
170 CrossAppDomainSink sink = new CrossAppDomainSink(domainID);
\r
171 s_sinks[domainID] = sink;
\r
178 internal int TargetDomainId {
\r
179 get { return _domainID; }
\r
182 private struct ProcessMessageRes {
183 public byte[] arrResponse;
184 public CADMethodReturnMessage cadMrm;
187 private static ProcessMessageRes ProcessMessageInDomain (
189 CADMethodCallMessage cadMsg)
191 ProcessMessageRes res = new ProcessMessageRes ();
195 AppDomain.CurrentDomain.ProcessMessageInDomain (arrRequest, cadMsg, out res.arrResponse, out res.cadMrm);
199 IMessage errorMsg = new MethodResponse (e, new ErrorMessage());
200 res.arrResponse = CADSerializer.SerializeMessage (errorMsg).GetBuffer();
\r
205 public virtual IMessage SyncProcessMessage(IMessage msgRequest)
\r
207 IMessage retMessage = null;
\r
211 // Time to transit into the "our" domain
\r
212 byte [] arrResponse = null;
\r
213 byte [] arrRequest = null;
\r
215 CADMethodReturnMessage cadMrm = null;
\r
216 CADMethodCallMessage cadMsg;
\r
218 cadMsg = CADMethodCallMessage.Create (msgRequest);
\r
219 if (null == cadMsg) {
\r
220 // Serialize the request message
\r
221 MemoryStream reqMsgStream = CADSerializer.SerializeMessage(msgRequest);
\r
222 arrRequest = reqMsgStream.GetBuffer();
\r
225 Context currentContext = Thread.CurrentContext;
228 // InternalInvoke can't handle out arguments, this is why
229 // we return the results in a structure
230 ProcessMessageRes res = (ProcessMessageRes)AppDomain.InvokeInDomainByID (_domainID, processMessageMethod, null, new object [] { arrRequest, cadMsg });
231 arrResponse = res.arrResponse;
234 AppDomain.InternalSetContext (currentContext);
238 if (null != arrResponse) {
\r
239 // Time to deserialize the message
\r
240 MemoryStream respMsgStream = new MemoryStream(arrResponse);
\r
242 // Deserialize the response message
\r
243 retMessage = CADSerializer.DeserializeMessage(respMsgStream, msgRequest as IMethodCallMessage);
\r
245 retMessage = new MethodResponse (msgRequest as IMethodCallMessage, cadMrm);
\r
247 catch (Exception e)
\r
251 retMessage = new ReturnMessage (e, msgRequest as IMethodCallMessage);
255 // this is just to be sure
\r
262 public virtual IMessageCtrl AsyncProcessMessage (IMessage reqMsg, IMessageSink replySink)
\r
264 AsyncRequest req = new AsyncRequest (reqMsg, replySink);
\r
265 ThreadPool.QueueUserWorkItem (new WaitCallback (SendAsyncMessage), req);
\r
269 public void SendAsyncMessage (object data)
\r
271 AsyncRequest req = (AsyncRequest)data;
\r
272 IMessage response = SyncProcessMessage (req.MsgRequest);
\r
273 req.ReplySink.SyncProcessMessage (response);
\r
276 public IMessageSink NextSink { get { return null; } }
\r
279 internal class CADSerializer
281 internal static IMessage DeserializeMessage(MemoryStream mem, IMethodCallMessage msg)
\r
283 BinaryFormatter serializer = new BinaryFormatter();
\r
285 serializer.SurrogateSelector = null;
\r
289 return (IMessage) serializer.Deserialize(mem, null);
\r
291 return (IMessage) serializer.DeserializeMethodResponse(mem, null, msg);
\r
294 internal static MemoryStream SerializeMessage(IMessage msg)
\r
296 MemoryStream mem = new MemoryStream ();
\r
297 BinaryFormatter serializer = new BinaryFormatter ();
\r
299 serializer.SurrogateSelector = new RemotingSurrogateSelector ();
\r
300 serializer.Serialize (mem, msg);
\r
307 internal static MemoryStream SerializeObject(object obj)
\r
309 MemoryStream mem = new MemoryStream ();
\r
310 BinaryFormatter serializer = new BinaryFormatter ();
\r
312 serializer.SurrogateSelector = new RemotingSurrogateSelector ();
\r
313 serializer.Serialize (mem, obj);
\r
320 internal static object DeserializeObject(MemoryStream mem)
\r
322 BinaryFormatter serializer = new BinaryFormatter();
\r
324 serializer.SurrogateSelector = null;
\r
327 return serializer.Deserialize (mem);
\r
331 internal class AsyncRequest
\r
333 internal IMessageSink ReplySink;
\r
334 internal IMessage MsgRequest;
\r
336 public AsyncRequest (IMessage msgRequest, IMessageSink replySink)
\r
338 ReplySink = replySink;
\r
339 MsgRequest = msgRequest;
\r