MsmqOutputChannel.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // MsmqOutputChannel.cs
  3. //
  4. // Author: Atsushi Enomoto ([email protected])
  5. //
  6. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.IO;
  29. using System.Messaging;
  30. using System.ServiceModel;
  31. using System.Threading;
  32. namespace System.ServiceModel.Channels
  33. {
  34. internal class MsmqOutputChannel : OutputChannelBase
  35. {
  36. MsmqChannelFactory<IOutputChannel> source;
  37. MessageQueue queue;
  38. public MsmqOutputChannel (MsmqChannelFactory<IOutputChannel> factory,
  39. EndpointAddress address, Uri via)
  40. : base (factory, address, via)
  41. {
  42. this.source = factory;
  43. }
  44. // Send
  45. public override IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
  46. {
  47. ThrowIfDisposedOrNotOpen ();
  48. return new MsmqChannelOutputAsyncResult (this, message, timeout, callback, state);
  49. }
  50. public override void EndSend (IAsyncResult result)
  51. {
  52. if (result == null)
  53. throw new ArgumentNullException ("result");
  54. MsmqChannelOutputAsyncResult r = result as MsmqChannelOutputAsyncResult;
  55. if (r == null)
  56. throw new InvalidOperationException ("Wrong IAsyncResult");
  57. r.WaitEnd ();
  58. }
  59. public override void Send (Message message, TimeSpan timeout)
  60. {
  61. ThrowIfDisposedOrNotOpen ();
  62. MemoryStream ms = new MemoryStream ();
  63. source.MessageEncoder.WriteMessage (message, ms);
  64. queue.Send (ms);
  65. //throw new NotImplementedException ();
  66. }
  67. // Abort
  68. protected override void OnAbort ()
  69. {
  70. throw new NotImplementedException ();
  71. }
  72. // Close
  73. protected override void OnClose (TimeSpan timeout)
  74. {
  75. if (queue != null)
  76. queue.Close ();
  77. queue = null;
  78. }
  79. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  80. {
  81. throw new NotImplementedException ();
  82. }
  83. protected override void OnEndClose (IAsyncResult result)
  84. {
  85. throw new NotImplementedException ();
  86. }
  87. // Open
  88. protected override void OnOpen (TimeSpan timeout)
  89. {
  90. // FIXME: is distination really like this?
  91. Uri destination = Via != null ? Via : RemoteAddress.Uri;
  92. queue = new MessageQueue (destination.GetLeftPart (UriPartial.Scheme));
  93. // FIXME: setup queue
  94. }
  95. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  96. {
  97. throw new NotImplementedException ();
  98. }
  99. protected override void OnEndOpen (IAsyncResult result)
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. class MsmqChannelOutputAsyncResult : IAsyncResult
  104. {
  105. MsmqOutputChannel channel;
  106. Message message;
  107. TimeSpan timeout;
  108. AsyncCallback callback;
  109. object state;
  110. AutoResetEvent wait;
  111. bool done, waiting;
  112. Exception error;
  113. public MsmqChannelOutputAsyncResult (MsmqOutputChannel channel, Message message, TimeSpan timeout, AsyncCallback callback, object state)
  114. {
  115. this.channel = channel;
  116. this.message = message;
  117. this.timeout = timeout;
  118. this.callback = callback;
  119. this.state = state;
  120. wait = new AutoResetEvent (false);
  121. Thread t = new Thread (delegate () {
  122. try {
  123. channel.Send (message, timeout);
  124. if (callback != null)
  125. callback (this);
  126. } catch (Exception ex) {
  127. error = ex;
  128. } finally {
  129. done = true;
  130. wait.Set ();
  131. }
  132. });
  133. t.Start ();
  134. }
  135. public WaitHandle AsyncWaitHandle {
  136. get { return wait; }
  137. }
  138. public object AsyncState {
  139. get { return state; }
  140. }
  141. public bool CompletedSynchronously {
  142. get { return done && !waiting; }
  143. }
  144. public bool IsCompleted {
  145. get { return done; }
  146. }
  147. public void WaitEnd ()
  148. {
  149. if (!done) {
  150. waiting = true;
  151. wait.WaitOne (timeout, true);
  152. }
  153. if (error != null)
  154. throw error;
  155. }
  156. }
  157. }
  158. }