DuplexChannelBase.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // Open
  74. Action<TimeSpan> open_handler;
  75. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  76. {
  77. return open_handler.BeginInvoke (timeout, callback, state);
  78. }
  79. protected override void OnEndOpen (IAsyncResult result)
  80. {
  81. open_handler.EndInvoke (result);
  82. }
  83. // Close
  84. Action<TimeSpan> close_handler;
  85. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  86. {
  87. return close_handler.BeginInvoke (timeout, callback, state);
  88. }
  89. protected override void OnEndClose (IAsyncResult result)
  90. {
  91. close_handler.EndInvoke (result);
  92. }
  93. // Send
  94. delegate void AsyncSendHandler (Message message, TimeSpan timeout);
  95. AsyncSendHandler send_handler;
  96. public virtual IAsyncResult BeginSend (Message message, AsyncCallback callback, object state)
  97. {
  98. return BeginSend (message, DefaultSendTimeout, callback, state);
  99. }
  100. public virtual IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
  101. {
  102. return send_handler.BeginInvoke (message, timeout, callback, state);
  103. }
  104. public virtual void EndSend (IAsyncResult result)
  105. {
  106. send_handler.EndInvoke (result);
  107. }
  108. public virtual void Send (Message message)
  109. {
  110. Send (message, this.DefaultSendTimeout);
  111. }
  112. public abstract void Send (Message message, TimeSpan timeout);
  113. // Receive
  114. delegate Message AsyncReceiveHandler (TimeSpan timeout);
  115. AsyncReceiveHandler receive_handler;
  116. public virtual IAsyncResult BeginReceive (AsyncCallback callback, object state)
  117. {
  118. return BeginReceive (this.DefaultReceiveTimeout, callback, state);
  119. }
  120. public virtual IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
  121. {
  122. return receive_handler.BeginInvoke (timeout, callback, state);
  123. }
  124. public virtual Message EndReceive (IAsyncResult result)
  125. {
  126. return receive_handler.EndInvoke (result);
  127. }
  128. public virtual Message Receive ()
  129. {
  130. return Receive (this.DefaultReceiveTimeout);
  131. }
  132. public abstract Message Receive (TimeSpan timeout);
  133. // TryReceive
  134. delegate bool TryReceiveHandler (TimeSpan timeout, out Message msg);
  135. TryReceiveHandler try_receive_handler;
  136. public virtual IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state)
  137. {
  138. Message dummy;
  139. return try_receive_handler.BeginInvoke (timeout, out dummy, callback, state);
  140. }
  141. public virtual bool EndTryReceive (IAsyncResult result, out Message message)
  142. {
  143. return try_receive_handler.EndInvoke (out message, result);
  144. }
  145. public virtual bool TryReceive (TimeSpan timeout, out Message message)
  146. {
  147. try {
  148. message = Receive (timeout);
  149. return true;
  150. } catch (TimeoutException) {
  151. message = null;
  152. return false;
  153. }
  154. }
  155. // WaitForMessage
  156. delegate bool AsyncWaitForMessageHandler (TimeSpan timeout);
  157. AsyncWaitForMessageHandler wait_handler;
  158. public virtual IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
  159. {
  160. return wait_handler.BeginInvoke (timeout, callback, state);
  161. }
  162. public virtual bool EndWaitForMessage (IAsyncResult result)
  163. {
  164. return wait_handler.EndInvoke (result);
  165. }
  166. public abstract bool WaitForMessage (TimeSpan timeout);
  167. }
  168. }