CrossContextChannel.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // System.Runtime.Remoting.Contexts.CrossContextChannel.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // (C) 2002, Lluis Sanchez Gual
  7. //
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Threading;
  32. using System.Runtime.Remoting.Messaging;
  33. using System.Runtime.Remoting.Activation;
  34. using System.Runtime.Remoting.Channels;
  35. namespace System.Runtime.Remoting.Contexts
  36. {
  37. internal class CrossContextChannel: IMessageSink
  38. {
  39. public IMessage SyncProcessMessage (IMessage msg)
  40. {
  41. ServerIdentity identity = (ServerIdentity) RemotingServices.GetMessageTargetIdentity (msg);
  42. Context oldContext = null;
  43. IMessage response;
  44. if (Threading.Thread.CurrentContext != identity.Context)
  45. oldContext = Context.SwitchToContext (identity.Context);
  46. try
  47. {
  48. Context.NotifyGlobalDynamicSinks (true, msg, false, false);
  49. Thread.CurrentContext.NotifyDynamicSinks (true, msg, false, false);
  50. response = identity.Context.GetServerContextSinkChain().SyncProcessMessage (msg);
  51. Context.NotifyGlobalDynamicSinks (false, msg, false, false);
  52. Thread.CurrentContext.NotifyDynamicSinks (false, msg, false, false);
  53. }
  54. catch (Exception ex)
  55. {
  56. response = new ReturnMessage (ex, (IMethodCallMessage)msg);
  57. }
  58. finally
  59. {
  60. if (oldContext != null)
  61. Context.SwitchToContext (oldContext);
  62. }
  63. return response;
  64. }
  65. public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
  66. {
  67. ServerIdentity identity = (ServerIdentity) RemotingServices.GetMessageTargetIdentity (msg);
  68. Context oldContext = null;
  69. if (Threading.Thread.CurrentContext != identity.Context)
  70. oldContext = Context.SwitchToContext (identity.Context);
  71. try
  72. {
  73. Context.NotifyGlobalDynamicSinks (true, msg, false, true);
  74. Thread.CurrentContext.NotifyDynamicSinks (true, msg, false, false);
  75. if (replySink != null) replySink = new ContextRestoreSink (replySink, oldContext, msg);
  76. IMessageCtrl res = identity.AsyncObjectProcessMessage (msg, replySink);
  77. if (replySink == null)
  78. {
  79. Context.NotifyGlobalDynamicSinks (false, msg, false, false);
  80. Thread.CurrentContext.NotifyDynamicSinks (false, msg, false, false);
  81. }
  82. return res;
  83. }
  84. catch (Exception ex)
  85. {
  86. if (replySink != null)
  87. replySink.SyncProcessMessage (new ReturnMessage (ex, (IMethodCallMessage)msg));
  88. return null;
  89. }
  90. finally
  91. {
  92. if (oldContext != null)
  93. Context.SwitchToContext (oldContext);
  94. }
  95. }
  96. public IMessageSink NextSink
  97. {
  98. get { return null; }
  99. }
  100. class ContextRestoreSink: IMessageSink
  101. {
  102. IMessageSink _next;
  103. Context _context;
  104. IMessage _call;
  105. public ContextRestoreSink (IMessageSink next, Context context, IMessage call)
  106. {
  107. _next = next;
  108. _context = context;
  109. _call = call;
  110. }
  111. public IMessage SyncProcessMessage (IMessage msg)
  112. {
  113. try
  114. {
  115. Context.NotifyGlobalDynamicSinks (false, msg, false, false);
  116. Thread.CurrentContext.NotifyDynamicSinks (false, msg, false, false);
  117. return _next.SyncProcessMessage (msg);
  118. }
  119. catch (Exception ex)
  120. {
  121. return new ReturnMessage (ex, (IMethodCallMessage)_call);
  122. }
  123. finally
  124. {
  125. if (_context != null)
  126. Context.SwitchToContext (_context);
  127. }
  128. }
  129. public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
  130. {
  131. throw new NotSupportedException(); // Not needed
  132. }
  133. public IMessageSink NextSink
  134. {
  135. get { return _next; }
  136. }
  137. }
  138. }
  139. }