ReplyChannelBase.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // ReplyChannelBase.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 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.IO;
  31. using System.Net;
  32. using System.Net.Sockets;
  33. using System.Net.Security;
  34. using System.ServiceModel;
  35. using System.ServiceModel.Description;
  36. using System.ServiceModel.Security;
  37. using System.Threading;
  38. using System.Xml;
  39. namespace System.ServiceModel.Channels
  40. {
  41. internal abstract class InternalReplyChannelBase : ReplyChannelBase
  42. {
  43. public InternalReplyChannelBase (ChannelListenerBase listener)
  44. : base (listener)
  45. {
  46. local_address = new EndpointAddress (listener.Uri);
  47. }
  48. EndpointAddress local_address;
  49. public override EndpointAddress LocalAddress {
  50. get { return local_address; }
  51. }
  52. }
  53. internal abstract class ReplyChannelBase : ChannelBase, IReplyChannel
  54. {
  55. public ReplyChannelBase (ChannelListenerBase listener)
  56. : base (listener)
  57. {
  58. this.listener = listener;
  59. }
  60. ChannelListenerBase listener;
  61. public ChannelListenerBase Listener {
  62. get { return listener; }
  63. }
  64. public abstract EndpointAddress LocalAddress { get; }
  65. public override T GetProperty<T> ()
  66. {
  67. if (typeof (T) == typeof (MessageVersion) && listener is IHasMessageEncoder)
  68. return (T) (object) ((IHasMessageEncoder) listener).MessageEncoder.MessageVersion;
  69. if (typeof (T) == typeof (IChannelListener))
  70. return (T) (object) listener;
  71. return base.GetProperty<T> ();
  72. }
  73. // FIXME: this is wrong. Implement all of them in each channel.
  74. protected override void OnAbort ()
  75. {
  76. OnClose (TimeSpan.Zero);
  77. }
  78. protected override void OnClose (TimeSpan timeout)
  79. {
  80. if (CurrentAsyncThread != null)
  81. if (!CancelAsync (timeout))
  82. CurrentAsyncThread.Abort ();
  83. }
  84. public virtual bool CancelAsync (TimeSpan timeout)
  85. {
  86. // FIXME: It should wait for the actual completion.
  87. return CurrentAsyncResult == null;
  88. //return CurrentAsyncResult == null || CurrentAsyncResult.AsyncWaitHandle.WaitOne (timeout);
  89. }
  90. public virtual bool TryReceiveRequest ()
  91. {
  92. RequestContext dummy;
  93. return TryReceiveRequest (DefaultReceiveTimeout, out dummy);
  94. }
  95. public abstract bool TryReceiveRequest (TimeSpan timeout, out RequestContext context);
  96. delegate bool TryReceiveDelegate (TimeSpan timeout, out RequestContext context);
  97. TryReceiveDelegate try_recv_delegate;
  98. object async_result_lock = new object ();
  99. protected Thread CurrentAsyncThread { get; private set; }
  100. protected IAsyncResult CurrentAsyncResult { get; private set; }
  101. public virtual IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
  102. {
  103. if (CurrentAsyncResult != null)
  104. throw new InvalidOperationException ("Another async TryReceiveRequest operation is in progress");
  105. if (try_recv_delegate == null)
  106. try_recv_delegate = new TryReceiveDelegate (delegate (TimeSpan tout, out RequestContext ctx) {
  107. lock (async_result_lock) {
  108. if (CurrentAsyncResult != null)
  109. CurrentAsyncThread = Thread.CurrentThread;
  110. }
  111. try {
  112. return TryReceiveRequest (tout, out ctx);
  113. } catch (XmlException ex) {
  114. Console.WriteLine ("Xml Exception (Dropped Connection?):" + ex.Message);
  115. //on dropped connection,
  116. //whatever you do don't crash
  117. //the whole app. Ignore for now
  118. } catch (SocketException ex) {
  119. Console.WriteLine ("Socket Exception (Dropped Connection?):" + ex.Message);
  120. //on dropped connection,
  121. //whatever you do don't crash
  122. //the whole app. Ignore for now
  123. } catch (IOException ex) {
  124. Console.WriteLine ("I/O Exception (Dropped Connection?):" + ex.Message);
  125. //on dropped connection,
  126. //whatever you do don't crash
  127. //the whole app. Ignore for now
  128. } finally {
  129. lock (async_result_lock) {
  130. CurrentAsyncResult = null;
  131. CurrentAsyncThread = null;
  132. }
  133. }
  134. ctx = null;
  135. return false;
  136. });
  137. RequestContext dummy;
  138. IAsyncResult result;
  139. lock (async_result_lock) {
  140. result = CurrentAsyncResult = try_recv_delegate.BeginInvoke (timeout, out dummy, callback, state);
  141. }
  142. // Note that at this point CurrentAsyncResult can be null here if delegate has run to completion
  143. return result;
  144. }
  145. public virtual bool EndTryReceiveRequest (IAsyncResult result)
  146. {
  147. RequestContext dummy;
  148. return EndTryReceiveRequest (result, out dummy);
  149. }
  150. public virtual bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
  151. {
  152. if (try_recv_delegate == null)
  153. throw new InvalidOperationException ("BeginTryReceiveRequest operation has not started");
  154. return try_recv_delegate.EndInvoke (out context, result);
  155. }
  156. public virtual bool WaitForRequest ()
  157. {
  158. return WaitForRequest (DefaultReceiveTimeout);
  159. }
  160. public abstract bool WaitForRequest (TimeSpan timeout);
  161. Func<TimeSpan,bool> wait_delegate;
  162. public virtual IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
  163. {
  164. if (wait_delegate == null)
  165. wait_delegate = new Func<TimeSpan,bool> (WaitForRequest);
  166. return wait_delegate.BeginInvoke (timeout, callback, state);
  167. }
  168. public virtual bool EndWaitForRequest (IAsyncResult result)
  169. {
  170. if (wait_delegate == null)
  171. throw new InvalidOperationException ("BeginWaitForRequest operation has not started");
  172. return wait_delegate.EndInvoke (result);
  173. }
  174. public virtual RequestContext ReceiveRequest ()
  175. {
  176. return ReceiveRequest (DefaultReceiveTimeout);
  177. }
  178. public abstract RequestContext ReceiveRequest (TimeSpan timeout);
  179. public virtual IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
  180. {
  181. return BeginReceiveRequest (DefaultReceiveTimeout, callback, state);
  182. }
  183. Func<TimeSpan,RequestContext> recv_delegate;
  184. public virtual IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
  185. {
  186. if (recv_delegate == null)
  187. recv_delegate = new Func<TimeSpan,RequestContext> (ReceiveRequest);
  188. return recv_delegate.BeginInvoke (timeout, callback, state);
  189. }
  190. public virtual RequestContext EndReceiveRequest (IAsyncResult result)
  191. {
  192. if (recv_delegate == null)
  193. throw new InvalidOperationException ("BeginReceiveRequest operation has not started");
  194. return recv_delegate.EndInvoke (result);
  195. }
  196. Action<TimeSpan> open_delegate, close_delegate;
  197. protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
  198. AsyncCallback callback, object state)
  199. {
  200. if (open_delegate == null)
  201. open_delegate = new Action<TimeSpan> (OnOpen);
  202. return open_delegate.BeginInvoke (timeout, callback, state);
  203. }
  204. protected override void OnEndOpen (IAsyncResult result)
  205. {
  206. if (open_delegate == null)
  207. throw new InvalidOperationException ("async open operation has not started");
  208. open_delegate.EndInvoke (result);
  209. }
  210. protected override IAsyncResult OnBeginClose (TimeSpan timeout,
  211. AsyncCallback callback, object state)
  212. {
  213. if (close_delegate == null)
  214. close_delegate = new Action<TimeSpan> (OnClose);
  215. return close_delegate.BeginInvoke (timeout, callback, state);
  216. }
  217. protected override void OnEndClose (IAsyncResult result)
  218. {
  219. if (close_delegate == null)
  220. throw new InvalidOperationException ("async close operation has not started");
  221. close_delegate.EndInvoke (result);
  222. }
  223. }
  224. }