ChannelFactoryBase.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // ChannelFactoryBase.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Dispatcher;
  33. namespace System.ServiceModel.Channels
  34. {
  35. internal interface IHasMessageEncoder
  36. {
  37. MessageEncoder MessageEncoder { get; }
  38. }
  39. internal abstract class TransportChannelFactoryBase<TChannel> : ChannelFactoryBase<TChannel>, IHasMessageEncoder
  40. {
  41. protected TransportChannelFactoryBase (TransportBindingElement source, BindingContext ctx)
  42. {
  43. Transport = source;
  44. }
  45. public TransportBindingElement Transport { get; private set; }
  46. public MessageEncoder MessageEncoder { get; internal set; }
  47. Action<TimeSpan> open_delegate;
  48. protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
  49. AsyncCallback callback, object state)
  50. {
  51. if (open_delegate == null)
  52. open_delegate = new Action<TimeSpan> (OnOpen);
  53. return open_delegate.BeginInvoke (timeout, callback, state);
  54. }
  55. protected override void OnEndOpen (IAsyncResult result)
  56. {
  57. if (open_delegate == null)
  58. throw new InvalidOperationException ("Async open operation has not started");
  59. open_delegate.EndInvoke (result);
  60. }
  61. protected override void OnOpen (TimeSpan timeout)
  62. {
  63. }
  64. /* commented out as it is in doubt.
  65. public override T GetProperty<T> ()
  66. {
  67. if (typeof (T) == typeof (MessageVersion))
  68. return (T) (object) MessageEncoder.MessageVersion;
  69. return base.GetProperty<T> ();
  70. }
  71. */
  72. }
  73. public abstract class ChannelFactoryBase<TChannel>
  74. : ChannelFactoryBase, IChannelFactory<TChannel>
  75. {
  76. List<TChannel> channels = new List<TChannel> ();
  77. protected ChannelFactoryBase ()
  78. : this (DefaultCommunicationTimeouts.Instance)
  79. {
  80. }
  81. protected ChannelFactoryBase (
  82. IDefaultCommunicationTimeouts timeouts)
  83. : base (timeouts)
  84. {
  85. }
  86. public TChannel CreateChannel (
  87. EndpointAddress remoteAddress)
  88. {
  89. return CreateChannel (remoteAddress, null);
  90. }
  91. public TChannel CreateChannel (
  92. EndpointAddress remoteAddress, Uri via)
  93. {
  94. if (remoteAddress == null)
  95. throw new ArgumentNullException ("remoteAddress");
  96. ValidateCreateChannel ();
  97. var ch = OnCreateChannel (remoteAddress, via);
  98. channels.Add (ch);
  99. return ch;
  100. }
  101. protected abstract TChannel OnCreateChannel (
  102. EndpointAddress remoteAddress, Uri via);
  103. protected override void OnAbort ()
  104. {
  105. // this implicitly premises: TChannel is IChannel
  106. foreach (IChannel ch in channels)
  107. ch.Abort ();
  108. base.OnAbort ();
  109. }
  110. protected override void OnClose (TimeSpan timeout)
  111. {
  112. DateTime start = DateTime.Now;
  113. // this implicitly premises: TChannel is IChannel
  114. foreach (IChannel ch in channels)
  115. ch.Close (timeout - (DateTime.Now - start));
  116. base.OnClose (timeout - (DateTime.Now - start));
  117. }
  118. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  119. {
  120. // base impl. will call this.OnClose()
  121. // FIXME: use async BeginClose/EndClose on the channels.
  122. return base.OnBeginClose (timeout, callback, state);
  123. }
  124. protected override void OnEndClose (IAsyncResult result)
  125. {
  126. // base impl. will call this.OnClose()
  127. base.OnEndClose (result);
  128. }
  129. protected void ValidateCreateChannel ()
  130. {
  131. ThrowIfDisposedOrNotOpen ();
  132. if (State == CommunicationState.Faulted)
  133. throw new CommunicationObjectFaultedException ();
  134. }
  135. }
  136. public abstract class ChannelFactoryBase
  137. : ChannelManagerBase, IChannelFactory, ICommunicationObject
  138. {
  139. TimeSpan open_timeout, close_timeout, receive_timeout, send_timeout;
  140. protected ChannelFactoryBase ()
  141. : this (DefaultCommunicationTimeouts.Instance)
  142. {
  143. }
  144. protected ChannelFactoryBase (
  145. IDefaultCommunicationTimeouts timeouts)
  146. {
  147. open_timeout = timeouts.OpenTimeout;
  148. close_timeout = timeouts.CloseTimeout;
  149. send_timeout = timeouts.SendTimeout;
  150. receive_timeout = timeouts.ReceiveTimeout;
  151. }
  152. protected internal override TimeSpan DefaultCloseTimeout {
  153. get { return close_timeout; }
  154. }
  155. protected internal override TimeSpan DefaultOpenTimeout {
  156. get { return open_timeout; }
  157. }
  158. protected internal override TimeSpan DefaultReceiveTimeout {
  159. get { return receive_timeout; }
  160. }
  161. protected internal override TimeSpan DefaultSendTimeout {
  162. get { return send_timeout; }
  163. }
  164. public virtual T GetProperty<T> () where T : class
  165. {
  166. return null;
  167. }
  168. protected override void OnAbort ()
  169. {
  170. // what should we do here?
  171. }
  172. Action<TimeSpan> close_delegate;
  173. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  174. {
  175. if (close_delegate == null)
  176. close_delegate = new Action<TimeSpan> (OnClose);
  177. return close_delegate.BeginInvoke (timeout, callback, state);
  178. }
  179. protected override void OnEndClose (IAsyncResult result)
  180. {
  181. if (close_delegate == null)
  182. throw new InvalidOperationException ("Async close operation has not started");
  183. close_delegate.EndInvoke (result);
  184. }
  185. protected override void OnClose (TimeSpan timeout)
  186. {
  187. // what should we do here?
  188. }
  189. }
  190. }