5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
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;
40 namespace System.ServiceModel.Channels
42 internal abstract class InternalReplyChannelBase : ReplyChannelBase
44 public InternalReplyChannelBase (ChannelListenerBase listener)
47 local_address = new EndpointAddress (listener.Uri);
50 EndpointAddress local_address;
52 public override EndpointAddress LocalAddress {
53 get { return local_address; }
57 internal abstract class ReplyChannelBase : ChannelBase, IReplyChannel
59 public ReplyChannelBase (ChannelListenerBase listener)
62 this.listener = listener;
65 ChannelListenerBase listener;
67 public ChannelListenerBase Listener {
68 get { return listener; }
71 public abstract EndpointAddress LocalAddress { get; }
73 public override T GetProperty<T> ()
75 if (typeof (T) == typeof (MessageVersion) && listener is IHasMessageEncoder)
76 return (T) (object) ((IHasMessageEncoder) listener).MessageEncoder.MessageVersion;
77 if (typeof (T) == typeof (IChannelListener))
78 return (T) (object) listener;
79 return base.GetProperty<T> ();
82 // FIXME: this is wrong. Implement all of them in each channel.
83 protected override void OnAbort ()
85 OnClose (TimeSpan.Zero);
88 protected override void OnClose (TimeSpan timeout)
90 if (CurrentAsyncThread != null)
91 if (!CancelAsync (timeout))
92 CurrentAsyncThread.Abort ();
95 public virtual bool CancelAsync (TimeSpan timeout)
97 // FIXME: It should wait for the actual completion.
98 return CurrentAsyncResult == null;
99 //return CurrentAsyncResult == null || CurrentAsyncResult.AsyncWaitHandle.WaitOne (timeout);
102 public virtual bool TryReceiveRequest ()
104 RequestContext dummy;
105 return TryReceiveRequest (DefaultReceiveTimeout, out dummy);
108 public abstract bool TryReceiveRequest (TimeSpan timeout, out RequestContext context);
110 delegate bool TryReceiveDelegate (TimeSpan timeout, out RequestContext context);
111 TryReceiveDelegate try_recv_delegate;
113 object async_result_lock = new object ();
114 protected Thread CurrentAsyncThread { get; private set; }
115 protected IAsyncResult CurrentAsyncResult { get; private set; }
117 public virtual IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
119 if (CurrentAsyncResult != null)
120 throw new InvalidOperationException ("Another async TryReceiveRequest operation is in progress");
121 if (try_recv_delegate == null)
122 try_recv_delegate = new TryReceiveDelegate (delegate (TimeSpan tout, out RequestContext ctx) {
123 lock (async_result_lock) {
124 if (CurrentAsyncResult != null)
125 CurrentAsyncThread = Thread.CurrentThread;
128 return TryReceiveRequest (tout, out ctx);
129 } catch (XmlException ex) {
130 Console.WriteLine ("Xml Exception (Dropped Connection?):" + ex.Message);
131 //on dropped connection,
132 //whatever you do don't crash
133 //the whole app. Ignore for now
134 } catch (SocketException ex) {
135 Console.WriteLine ("Socket Exception (Dropped Connection?):" + ex.Message);
136 //on dropped connection,
137 //whatever you do don't crash
138 //the whole app. Ignore for now
139 } catch (IOException ex) {
140 Console.WriteLine ("I/O Exception (Dropped Connection?):" + ex.Message);
141 //on dropped connection,
142 //whatever you do don't crash
143 //the whole app. Ignore for now
145 lock (async_result_lock) {
146 CurrentAsyncResult = null;
147 CurrentAsyncThread = null;
153 RequestContext dummy;
155 lock (async_result_lock) {
156 result = CurrentAsyncResult = try_recv_delegate.BeginInvoke (timeout, out dummy, callback, state);
158 // Note that at this point CurrentAsyncResult can be null here if delegate has run to completion
162 public virtual bool EndTryReceiveRequest (IAsyncResult result)
164 RequestContext dummy;
165 return EndTryReceiveRequest (result, out dummy);
168 public virtual bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
170 if (try_recv_delegate == null)
171 throw new InvalidOperationException ("BeginTryReceiveRequest operation has not started");
172 return try_recv_delegate.EndInvoke (out context, result);
175 public virtual bool WaitForRequest ()
177 return WaitForRequest (DefaultReceiveTimeout);
180 public abstract bool WaitForRequest (TimeSpan timeout);
182 Func<TimeSpan,bool> wait_delegate;
184 public virtual IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
186 if (wait_delegate == null)
187 wait_delegate = new Func<TimeSpan,bool> (WaitForRequest);
188 return wait_delegate.BeginInvoke (timeout, callback, state);
191 public virtual bool EndWaitForRequest (IAsyncResult result)
193 if (wait_delegate == null)
194 throw new InvalidOperationException ("BeginWaitForRequest operation has not started");
195 return wait_delegate.EndInvoke (result);
198 public virtual RequestContext ReceiveRequest ()
200 return ReceiveRequest (DefaultReceiveTimeout);
203 public abstract RequestContext ReceiveRequest (TimeSpan timeout);
205 public virtual IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
207 return BeginReceiveRequest (DefaultReceiveTimeout, callback, state);
210 Func<TimeSpan,RequestContext> recv_delegate;
211 public virtual IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
213 if (recv_delegate == null)
214 recv_delegate = new Func<TimeSpan,RequestContext> (ReceiveRequest);
215 return recv_delegate.BeginInvoke (timeout, callback, state);
218 public virtual RequestContext EndReceiveRequest (IAsyncResult result)
220 if (recv_delegate == null)
221 throw new InvalidOperationException ("BeginReceiveRequest operation has not started");
222 return recv_delegate.EndInvoke (result);
225 Action<TimeSpan> open_delegate, close_delegate;
227 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
228 AsyncCallback callback, object state)
230 if (open_delegate == null)
231 open_delegate = new Action<TimeSpan> (OnOpen);
232 return open_delegate.BeginInvoke (timeout, callback, state);
235 protected override void OnEndOpen (IAsyncResult result)
237 if (open_delegate == null)
238 throw new InvalidOperationException ("async open operation has not started");
239 open_delegate.EndInvoke (result);
242 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
243 AsyncCallback callback, object state)
245 if (close_delegate == null)
246 close_delegate = new Action<TimeSpan> (OnClose);
247 return close_delegate.BeginInvoke (timeout, callback, state);
250 protected override void OnEndClose (IAsyncResult result)
252 if (close_delegate == null)
253 throw new InvalidOperationException ("async close operation has not started");
254 close_delegate.EndInvoke (result);