2 // System.Runtime.Remoting.Contexts.CrossContextChannel.cs
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
6 // (C) 2002, Lluis Sanchez Gual
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.Threading
;
34 using System
.Runtime
.Remoting
.Messaging
;
35 using System
.Runtime
.Remoting
.Activation
;
36 using System
.Runtime
.Remoting
.Channels
;
38 namespace System
.Runtime
.Remoting
.Contexts
40 internal class CrossContextChannel
: IMessageSink
42 public IMessage
SyncProcessMessage (IMessage msg
)
44 ServerIdentity identity
= (ServerIdentity
) RemotingServices
.GetMessageTargetIdentity (msg
);
46 Context oldContext
= null;
49 if (Threading
.Thread
.CurrentContext
!= identity
.Context
)
50 oldContext
= Context
.SwitchToContext (identity
.Context
);
54 Context
.NotifyGlobalDynamicSinks (true, msg
, false, false);
55 Thread
.CurrentContext
.NotifyDynamicSinks (true, msg
, false, false);
57 response
= identity
.Context
.GetServerContextSinkChain().SyncProcessMessage (msg
);
59 Context
.NotifyGlobalDynamicSinks (false, msg
, false, false);
60 Thread
.CurrentContext
.NotifyDynamicSinks (false, msg
, false, false);
64 response
= new ReturnMessage (ex
, (IMethodCallMessage
)msg
);
68 if (oldContext
!= null)
69 Context
.SwitchToContext (oldContext
);
75 public IMessageCtrl
AsyncProcessMessage (IMessage msg
, IMessageSink replySink
)
77 ServerIdentity identity
= (ServerIdentity
) RemotingServices
.GetMessageTargetIdentity (msg
);
79 Context oldContext
= null;
80 if (Threading
.Thread
.CurrentContext
!= identity
.Context
)
81 oldContext
= Context
.SwitchToContext (identity
.Context
);
85 Context
.NotifyGlobalDynamicSinks (true, msg
, false, true);
86 Thread
.CurrentContext
.NotifyDynamicSinks (true, msg
, false, false);
87 if (replySink
!= null) replySink
= new ContextRestoreSink (replySink
, oldContext
, msg
);
89 IMessageCtrl res
= identity
.AsyncObjectProcessMessage (msg
, replySink
);
91 if (replySink
== null)
93 Context
.NotifyGlobalDynamicSinks (false, msg
, false, false);
94 Thread
.CurrentContext
.NotifyDynamicSinks (false, msg
, false, false);
101 if (replySink
!= null)
102 replySink
.SyncProcessMessage (new ReturnMessage (ex
, (IMethodCallMessage
)msg
));
107 if (oldContext
!= null)
108 Context
.SwitchToContext (oldContext
);
112 public IMessageSink NextSink
117 class ContextRestoreSink
: IMessageSink
123 public ContextRestoreSink (IMessageSink next
, Context context
, IMessage call
)
130 public IMessage
SyncProcessMessage (IMessage msg
)
134 Context
.NotifyGlobalDynamicSinks (false, msg
, false, false);
135 Thread
.CurrentContext
.NotifyDynamicSinks (false, msg
, false, false);
136 return _next
.SyncProcessMessage (msg
);
140 return new ReturnMessage (ex
, (IMethodCallMessage
)_call
);
144 if (_context
!= null)
145 Context
.SwitchToContext (_context
);
149 public IMessageCtrl
AsyncProcessMessage (IMessage msg
, IMessageSink replySink
)
151 throw new NotSupportedException(); // Not needed
154 public IMessageSink NextSink
156 get { return _next; }