2 // MsmqOutputChannel.cs
4 // Author: Atsushi Enomoto (atsushi@ximian.com)
6 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
29 using System.Messaging;
30 using System.ServiceModel;
31 using System.Threading;
33 namespace System.ServiceModel.Channels
35 internal class MsmqOutputChannel : OutputChannelBase
37 MsmqChannelFactory<IOutputChannel> source;
40 public MsmqOutputChannel (MsmqChannelFactory<IOutputChannel> factory,
41 EndpointAddress address, Uri via)
42 : base (factory, address, via)
44 this.source = factory;
49 public override IAsyncResult BeginSend (Message message, TimeSpan timeout, AsyncCallback callback, object state)
51 ThrowIfDisposedOrNotOpen ();
53 return new MsmqChannelOutputAsyncResult (this, message, timeout, callback, state);
56 public override void EndSend (IAsyncResult result)
59 throw new ArgumentNullException ("result");
60 MsmqChannelOutputAsyncResult r = result as MsmqChannelOutputAsyncResult;
62 throw new InvalidOperationException ("Wrong IAsyncResult");
66 public override void Send (Message message, TimeSpan timeout)
68 ThrowIfDisposedOrNotOpen ();
70 MemoryStream ms = new MemoryStream ();
71 source.MessageEncoder.WriteMessage (message, ms);
75 //throw new NotImplementedException ();
80 protected override void OnAbort ()
82 throw new NotImplementedException ();
87 protected override void OnClose (TimeSpan timeout)
94 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
96 throw new NotImplementedException ();
99 protected override void OnEndClose (IAsyncResult result)
101 throw new NotImplementedException ();
106 protected override void OnOpen (TimeSpan timeout)
108 // FIXME: is distination really like this?
109 Uri destination = Via != null ? Via : RemoteAddress.Uri;
111 queue = new MessageQueue (destination.GetLeftPart (UriPartial.Scheme));
112 // FIXME: setup queue
115 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
117 throw new NotImplementedException ();
120 protected override void OnEndOpen (IAsyncResult result)
122 throw new NotImplementedException ();
125 class MsmqChannelOutputAsyncResult : IAsyncResult
127 MsmqOutputChannel channel;
130 AsyncCallback callback;
136 public MsmqChannelOutputAsyncResult (MsmqOutputChannel channel, Message message, TimeSpan timeout, AsyncCallback callback, object state)
138 this.channel = channel;
139 this.message = message;
140 this.timeout = timeout;
141 this.callback = callback;
144 wait = new AutoResetEvent (false);
145 Thread t = new Thread (delegate () {
147 channel.Send (message, timeout);
148 if (callback != null)
150 } catch (Exception ex) {
160 public WaitHandle AsyncWaitHandle {
164 public object AsyncState {
165 get { return state; }
168 public bool CompletedSynchronously {
169 get { return done && !waiting; }
172 public bool IsCompleted {
176 public void WaitEnd ()
180 wait.WaitOne (timeout, true);