LayeredChannelFactory.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Runtime;
  7. using System.ServiceModel;
  8. abstract class LayeredChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
  9. {
  10. IChannelFactory innerChannelFactory;
  11. public LayeredChannelFactory(IDefaultCommunicationTimeouts timeouts, IChannelFactory innerChannelFactory)
  12. : base(timeouts)
  13. {
  14. this.innerChannelFactory = innerChannelFactory;
  15. }
  16. protected IChannelFactory InnerChannelFactory
  17. {
  18. get { return this.innerChannelFactory; }
  19. }
  20. public override T GetProperty<T>()
  21. {
  22. if (typeof(T) == typeof(IChannelFactory<TChannel>))
  23. {
  24. return (T)(object)this;
  25. }
  26. T baseProperty = base.GetProperty<T>();
  27. if (baseProperty != null)
  28. {
  29. return baseProperty;
  30. }
  31. return this.innerChannelFactory.GetProperty<T>();
  32. }
  33. protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  34. {
  35. return this.innerChannelFactory.BeginOpen(timeout, callback, state);
  36. }
  37. protected override void OnEndOpen(IAsyncResult result)
  38. {
  39. this.innerChannelFactory.EndOpen(result);
  40. }
  41. protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  42. {
  43. return new ChainedCloseAsyncResult(timeout, callback, state, base.OnBeginClose, base.OnEndClose, this.innerChannelFactory);
  44. }
  45. protected override void OnEndClose(IAsyncResult result)
  46. {
  47. ChainedCloseAsyncResult.End(result);
  48. }
  49. protected override void OnClose(TimeSpan timeout)
  50. {
  51. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  52. base.OnClose(timeoutHelper.RemainingTime());
  53. this.innerChannelFactory.Close(timeoutHelper.RemainingTime());
  54. }
  55. protected override void OnOpen(TimeSpan timeout)
  56. {
  57. this.innerChannelFactory.Open(timeout);
  58. }
  59. protected override void OnAbort()
  60. {
  61. base.OnAbort();
  62. this.innerChannelFactory.Abort();
  63. }
  64. }
  65. class LayeredInputChannel : LayeredChannel<IInputChannel>, IInputChannel
  66. {
  67. public LayeredInputChannel(ChannelManagerBase channelManager, IInputChannel innerChannel)
  68. : base(channelManager, innerChannel)
  69. {
  70. }
  71. public virtual EndpointAddress LocalAddress
  72. {
  73. get { return InnerChannel.LocalAddress; }
  74. }
  75. void InternalOnReceive(Message message)
  76. {
  77. if (message != null)
  78. {
  79. this.OnReceive(message);
  80. }
  81. }
  82. protected virtual void OnReceive(Message message)
  83. {
  84. }
  85. public Message Receive()
  86. {
  87. Message message = InnerChannel.Receive();
  88. this.InternalOnReceive(message);
  89. return message;
  90. }
  91. public Message Receive(TimeSpan timeout)
  92. {
  93. Message message = InnerChannel.Receive(timeout);
  94. this.InternalOnReceive(message);
  95. return message;
  96. }
  97. public IAsyncResult BeginReceive(AsyncCallback callback, object state)
  98. {
  99. return InnerChannel.BeginReceive(callback, state);
  100. }
  101. public IAsyncResult BeginReceive(TimeSpan timeout, AsyncCallback callback, object state)
  102. {
  103. return InnerChannel.BeginReceive(timeout, callback, state);
  104. }
  105. public Message EndReceive(IAsyncResult result)
  106. {
  107. Message message = InnerChannel.EndReceive(result);
  108. this.InternalOnReceive(message);
  109. return message;
  110. }
  111. public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
  112. {
  113. return InnerChannel.BeginTryReceive(timeout, callback, state);
  114. }
  115. public bool EndTryReceive(IAsyncResult result, out Message message)
  116. {
  117. bool retVal = InnerChannel.EndTryReceive(result, out message);
  118. this.InternalOnReceive(message);
  119. return retVal;
  120. }
  121. public bool TryReceive(TimeSpan timeout, out Message message)
  122. {
  123. bool retVal = InnerChannel.TryReceive(timeout, out message);
  124. this.InternalOnReceive(message);
  125. return retVal;
  126. }
  127. public bool WaitForMessage(TimeSpan timeout)
  128. {
  129. return InnerChannel.WaitForMessage(timeout);
  130. }
  131. public IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback, object state)
  132. {
  133. return InnerChannel.BeginWaitForMessage(timeout, callback, state);
  134. }
  135. public bool EndWaitForMessage(IAsyncResult result)
  136. {
  137. return InnerChannel.EndWaitForMessage(result);
  138. }
  139. }
  140. class LayeredDuplexChannel : LayeredInputChannel, IDuplexChannel
  141. {
  142. IOutputChannel innerOutputChannel;
  143. EndpointAddress localAddress;
  144. EventHandler onInnerOutputChannelFaulted;
  145. public LayeredDuplexChannel(ChannelManagerBase channelManager, IInputChannel innerInputChannel, EndpointAddress localAddress, IOutputChannel innerOutputChannel)
  146. : base(channelManager, innerInputChannel)
  147. {
  148. this.localAddress = localAddress;
  149. this.innerOutputChannel = innerOutputChannel;
  150. this.onInnerOutputChannelFaulted = new EventHandler(OnInnerOutputChannelFaulted);
  151. this.innerOutputChannel.Faulted += this.onInnerOutputChannelFaulted;
  152. }
  153. public override EndpointAddress LocalAddress
  154. {
  155. get { return this.localAddress; }
  156. }
  157. public EndpointAddress RemoteAddress
  158. {
  159. get { return this.innerOutputChannel.RemoteAddress; }
  160. }
  161. public Uri Via
  162. {
  163. get { return innerOutputChannel.Via; }
  164. }
  165. protected override void OnClosing()
  166. {
  167. this.innerOutputChannel.Faulted -= this.onInnerOutputChannelFaulted;
  168. base.OnClosing();
  169. }
  170. protected override void OnAbort()
  171. {
  172. this.innerOutputChannel.Abort();
  173. base.OnAbort();
  174. }
  175. protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  176. {
  177. return new ChainedCloseAsyncResult(timeout, callback, state, base.OnBeginClose, base.OnEndClose, this.innerOutputChannel);
  178. }
  179. protected override void OnEndClose(IAsyncResult result)
  180. {
  181. ChainedCloseAsyncResult.End(result);
  182. }
  183. protected override void OnClose(TimeSpan timeout)
  184. {
  185. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  186. this.innerOutputChannel.Close(timeoutHelper.RemainingTime());
  187. base.OnClose(timeoutHelper.RemainingTime());
  188. }
  189. protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  190. {
  191. return new ChainedOpenAsyncResult(timeout, callback, state, base.OnBeginOpen, base.OnEndOpen, this.innerOutputChannel);
  192. }
  193. protected override void OnEndOpen(IAsyncResult result)
  194. {
  195. ChainedOpenAsyncResult.End(result);
  196. }
  197. protected override void OnOpen(TimeSpan timeout)
  198. {
  199. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  200. base.OnOpen(timeoutHelper.RemainingTime());
  201. innerOutputChannel.Open(timeoutHelper.RemainingTime());
  202. }
  203. public void Send(Message message)
  204. {
  205. this.Send(message, this.DefaultSendTimeout);
  206. }
  207. public void Send(Message message, TimeSpan timeout)
  208. {
  209. this.innerOutputChannel.Send(message, timeout);
  210. }
  211. public IAsyncResult BeginSend(Message message, AsyncCallback callback, object state)
  212. {
  213. return this.BeginSend(message, this.DefaultSendTimeout, callback, state);
  214. }
  215. public IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state)
  216. {
  217. return this.innerOutputChannel.BeginSend(message, timeout, callback, state);
  218. }
  219. public void EndSend(IAsyncResult result)
  220. {
  221. this.innerOutputChannel.EndSend(result);
  222. }
  223. void OnInnerOutputChannelFaulted(object sender, EventArgs e)
  224. {
  225. this.Fault();
  226. }
  227. }
  228. }