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 (currentAsyncThreads.Count > 0)
91 if (!CancelAsync (timeout))
92 foreach (Thread asyncThread in currentAsyncThreads)
96 public virtual bool CancelAsync (TimeSpan timeout)
98 // FIXME: It should wait for the actual completion.
99 return currentAsyncResults.Count > 0;
100 //return CurrentAsyncResult == null || CurrentAsyncResult.AsyncWaitHandle.WaitOne (timeout);
103 public virtual bool TryReceiveRequest ()
105 RequestContext dummy;
106 return TryReceiveRequest (DefaultReceiveTimeout, out dummy);
109 public abstract bool TryReceiveRequest (TimeSpan timeout, out RequestContext context);
111 delegate bool TryReceiveDelegate (TimeSpan timeout, out RequestContext context);
112 TryReceiveDelegate try_recv_delegate;
114 object async_result_lock = new object ();
115 HashSet<Thread> currentAsyncThreads = new HashSet<Thread>();
116 HashSet<IAsyncResult> currentAsyncResults = new HashSet<IAsyncResult>();
118 public virtual IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
120 IAsyncResult result = null;
122 if (try_recv_delegate == null)
123 try_recv_delegate = new TryReceiveDelegate (delegate (TimeSpan tout, out RequestContext ctx) {
124 lock (async_result_lock) {
125 if (currentAsyncResults.Contains (result))
126 currentAsyncThreads.Add (Thread.CurrentThread);
129 return TryReceiveRequest (tout, out ctx);
130 } catch (XmlException ex) {
131 Console.WriteLine ("Xml Exception (Dropped Connection?):" + ex.Message);
132 //on dropped connection,
133 //whatever you do don't crash
134 //the whole app. Ignore for now
135 } catch (SocketException ex) {
136 Console.WriteLine ("Socket Exception (Dropped Connection?):" + ex.Message);
137 //on dropped connection,
138 //whatever you do don't crash
139 //the whole app. Ignore for now
140 } catch (IOException ex) {
141 Console.WriteLine ("I/O Exception (Dropped Connection?):" + ex.Message);
142 //on dropped connection,
143 //whatever you do don't crash
144 //the whole app. Ignore for now
146 lock (async_result_lock) {
147 currentAsyncResults.Remove (result);
148 currentAsyncThreads.Remove (Thread.CurrentThread);
154 RequestContext dummy;
155 lock (async_result_lock) {
156 result = try_recv_delegate.BeginInvoke (timeout, out dummy, callback, state);
157 currentAsyncResults.Add (result);
159 // Note that at this point result can be missing from currentAsyncResults here if delegate has run to completion
163 public virtual bool EndTryReceiveRequest (IAsyncResult result)
165 RequestContext dummy;
166 return EndTryReceiveRequest (result, out dummy);
169 public virtual bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
171 if (try_recv_delegate == null)
172 throw new InvalidOperationException ("BeginTryReceiveRequest operation has not started");
173 return try_recv_delegate.EndInvoke (out context, result);
176 public virtual bool WaitForRequest ()
178 return WaitForRequest (DefaultReceiveTimeout);
181 public abstract bool WaitForRequest (TimeSpan timeout);
183 Func<TimeSpan,bool> wait_delegate;
185 public virtual IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
187 if (wait_delegate == null)
188 wait_delegate = new Func<TimeSpan,bool> (WaitForRequest);
189 return wait_delegate.BeginInvoke (timeout, callback, state);
192 public virtual bool EndWaitForRequest (IAsyncResult result)
194 if (wait_delegate == null)
195 throw new InvalidOperationException ("BeginWaitForRequest operation has not started");
196 return wait_delegate.EndInvoke (result);
199 public virtual RequestContext ReceiveRequest ()
201 return ReceiveRequest (DefaultReceiveTimeout);
204 public abstract RequestContext ReceiveRequest (TimeSpan timeout);
206 public virtual IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
208 return BeginReceiveRequest (DefaultReceiveTimeout, callback, state);
211 Func<TimeSpan,RequestContext> recv_delegate;
212 public virtual IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
214 if (recv_delegate == null)
215 recv_delegate = new Func<TimeSpan,RequestContext> (ReceiveRequest);
216 return recv_delegate.BeginInvoke (timeout, callback, state);
219 public virtual RequestContext EndReceiveRequest (IAsyncResult result)
221 if (recv_delegate == null)
222 throw new InvalidOperationException ("BeginReceiveRequest operation has not started");
223 return recv_delegate.EndInvoke (result);
226 Action<TimeSpan> open_delegate, close_delegate;
228 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
229 AsyncCallback callback, object state)
231 if (open_delegate == null)
232 open_delegate = new Action<TimeSpan> (OnOpen);
233 return open_delegate.BeginInvoke (timeout, callback, state);
236 protected override void OnEndOpen (IAsyncResult result)
238 if (open_delegate == null)
239 throw new InvalidOperationException ("async open operation has not started");
240 open_delegate.EndInvoke (result);
243 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
244 AsyncCallback callback, object state)
246 if (close_delegate == null)
247 close_delegate = new Action<TimeSpan> (OnClose);
248 return close_delegate.BeginInvoke (timeout, callback, state);
251 protected override void OnEndClose (IAsyncResult result)
253 if (close_delegate == null)
254 throw new InvalidOperationException ("async close operation has not started");
255 close_delegate.EndInvoke (result);