DuplexChannelBase.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // DuplexSessionChannelBase.cs
  3. //
  4. // Author:
  5. // Marcos Cobena ([email protected])
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
  9. //
  10. // Copyright (C) 2009 Novell, Inc. http://www.novell.com
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using System;
  32. using System.ServiceModel.Channels;
  33. namespace System.ServiceModel.Channels
  34. {
  35. internal abstract class DuplexChannelBase : ChannelBase, IDuplexChannel
  36. {
  37. ChannelFactoryBase channel_factory_base;
  38. ChannelListenerBase channel_listener_base;
  39. EndpointAddress local_address;
  40. EndpointAddress remote_address;
  41. Uri via;
  42. public DuplexChannelBase (ChannelFactoryBase factory, EndpointAddress remoteAddress, Uri via) : base (factory)
  43. {
  44. channel_factory_base = factory;
  45. remote_address = remoteAddress;
  46. this.via = via;
  47. SetupDelegates ();
  48. }
  49. public DuplexChannelBase (ChannelListenerBase listener) : base (listener)
  50. {
  51. channel_listener_base = listener;
  52. local_address = new EndpointAddress (listener.Uri);
  53. SetupDelegates ();
  54. }
  55. public virtual EndpointAddress LocalAddress {
  56. get { return local_address; }
  57. }
  58. public virtual EndpointAddress RemoteAddress {
  59. get { return remote_address; }
  60. }
  61. public Uri Via {
  62. get { return via ?? RemoteAddress.Uri; }
  63. }
  64. void SetupDelegates ()
  65. {
  66. open_handler = new Action<TimeSpan> (Open);
  67. close_handler = new Action<TimeSpan> (Close);
  68. send_handler = new AsyncSendHandler (Send);
  69. receive_handler = new AsyncReceiveHandler (Receive);
  70. wait_handler = new AsyncWaitForMessageHandler (WaitForMessage);
  71. try_receive_handler = new TryReceiveHandler (TryReceive);
  72. }
  73. public override T GetProperty<T> ()
  74. {
  75. if (typeof (T) == typeof (IChannelFactory))
  76. return (T) (object) channel_factory_base;
  77. if (typeof (T) == typeof (IChannelListener))
  78. return (T) (object) channel_listener_base;
  79. return base.GetProperty<T> ();
  80. }
  81. // Open
  82. Action<TimeSpan> open_handler;
  83. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  84. {
  85. return open_handler.BeginInvoke (timeout, callback, state);
  86. }
  87. protected override void OnEndOpen (IAsyncResult result)
  88. {
  89. open_handler.EndInvoke (result);
  90. }
  91. // Close
  92. Action<TimeSpan> close_handler;
  93. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  94. {
  95. return close_handler.BeginInvoke (timeout, callback, state);
  96. }
  97. protected override void OnEndClose (IAsyncResult result)
  98. {
  99. close_handler.EndInvoke (result);
  100. }
  101. // Send
  102. delegate void AsyncSendHandler (Message message, TimeSpan timeout);
  103. AsyncSendHandler send_handler;
  104. public virtual IAsyncResult BeginSend (Message message, AsyncCallback callback, object state)
  105. {
  106. return BeginSend (message, DefaultSendTimeout, callback, state);
  107. }
  108. public virtual IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
  109. {
  110. return send_handler.BeginInvoke (message, timeout, callback, state);
  111. }
  112. public virtual void EndSend (IAsyncResult result)
  113. {
  114. send_handler.EndInvoke (result);
  115. }
  116. public virtual void Send (Message message)
  117. {
  118. Send (message, this.DefaultSendTimeout);
  119. }
  120. public abstract void Send (Message message, TimeSpan timeout);
  121. // Receive
  122. delegate Message AsyncReceiveHandler (TimeSpan timeout);
  123. AsyncReceiveHandler receive_handler;
  124. public virtual IAsyncResult BeginReceive (AsyncCallback callback, object state)
  125. {
  126. return BeginReceive (this.DefaultReceiveTimeout, callback, state);
  127. }
  128. public virtual IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
  129. {
  130. return receive_handler.BeginInvoke (timeout, callback, state);
  131. }
  132. public virtual Message EndReceive (IAsyncResult result)
  133. {
  134. return receive_handler.EndInvoke (result);
  135. }
  136. public virtual Message Receive ()
  137. {
  138. return Receive (this.DefaultReceiveTimeout);
  139. }
  140. public virtual Message Receive (TimeSpan timeout)
  141. {
  142. Message msg;
  143. if (!TryReceive (timeout, out msg))
  144. throw new TimeoutException ();
  145. return msg;
  146. }
  147. // TryReceive
  148. delegate bool TryReceiveHandler (TimeSpan timeout, out Message msg);
  149. TryReceiveHandler try_receive_handler;
  150. public virtual IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state)
  151. {
  152. Message dummy;
  153. return try_receive_handler.BeginInvoke (timeout, out dummy, callback, state);
  154. }
  155. public virtual bool EndTryReceive (IAsyncResult result, out Message message)
  156. {
  157. return try_receive_handler.EndInvoke (out message, result);
  158. }
  159. public abstract bool TryReceive (TimeSpan timeout, out Message message);
  160. // WaitForMessage
  161. delegate bool AsyncWaitForMessageHandler (TimeSpan timeout);
  162. AsyncWaitForMessageHandler wait_handler;
  163. public virtual IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
  164. {
  165. return wait_handler.BeginInvoke (timeout, callback, state);
  166. }
  167. public virtual bool EndWaitForMessage (IAsyncResult result)
  168. {
  169. return wait_handler.EndInvoke (result);
  170. }
  171. public abstract bool WaitForMessage (TimeSpan timeout);
  172. }
  173. }