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.Security;
33 using System.ServiceModel;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Security;
36 using System.Threading;
38 namespace System.ServiceModel.Channels
40 internal abstract class InternalReplyChannelBase : ReplyChannelBase
42 public InternalReplyChannelBase (ChannelListenerBase listener)
45 local_address = new EndpointAddress (listener.Uri);
48 EndpointAddress local_address;
50 public override EndpointAddress LocalAddress {
51 get { return local_address; }
55 internal abstract class ReplyChannelBase : ChannelBase, IReplyChannel
57 public ReplyChannelBase (ChannelListenerBase listener)
60 this.listener = listener;
63 ChannelListenerBase listener;
65 public ChannelListenerBase Listener {
66 get { return listener; }
69 public abstract EndpointAddress LocalAddress { get; }
71 public override T GetProperty<T> ()
73 if (typeof (T) == typeof (IChannelListener))
74 return (T) (object) listener;
75 return base.GetProperty<T> ();
78 // FIXME: this is wrong. Implement all of them in each channel.
79 protected override void OnAbort ()
81 OnClose (TimeSpan.Zero);
84 protected override void OnClose (TimeSpan timeout)
86 if (CurrentAsyncThread != null)
87 if (!CancelAsync (timeout))
88 CurrentAsyncThread.Abort ();
91 public virtual bool CancelAsync (TimeSpan timeout)
93 // FIXME: It should wait for the actual completion.
94 return CurrentAsyncResult == null;
95 //return CurrentAsyncResult == null || CurrentAsyncResult.AsyncWaitHandle.WaitOne (timeout);
98 public virtual bool TryReceiveRequest ()
100 RequestContext dummy;
101 return TryReceiveRequest (DefaultReceiveTimeout, out dummy);
104 public abstract bool TryReceiveRequest (TimeSpan timeout, out RequestContext context);
106 delegate bool TryReceiveDelegate (TimeSpan timeout, out RequestContext context);
107 TryReceiveDelegate try_recv_delegate;
109 object async_result_lock = new object ();
110 protected Thread CurrentAsyncThread { get; private set; }
111 protected IAsyncResult CurrentAsyncResult { get; private set; }
113 public virtual IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
115 if (CurrentAsyncResult != null)
116 throw new InvalidOperationException ("Another async TryReceiveRequest operation is in progress");
117 if (try_recv_delegate == null)
118 try_recv_delegate = new TryReceiveDelegate (delegate (TimeSpan tout, out RequestContext ctx) {
119 lock (async_result_lock) {
120 if (CurrentAsyncResult != null)
121 CurrentAsyncThread = Thread.CurrentThread;
124 return TryReceiveRequest (tout, out ctx);
126 lock (async_result_lock) {
127 CurrentAsyncResult = null;
128 CurrentAsyncThread = null;
132 RequestContext dummy;
134 lock (async_result_lock) {
135 result = CurrentAsyncResult = try_recv_delegate.BeginInvoke (timeout, out dummy, callback, state);
137 // Note that at this point CurrentAsyncResult can be null here if delegate has run to completion
141 public virtual bool EndTryReceiveRequest (IAsyncResult result)
143 RequestContext dummy;
144 return EndTryReceiveRequest (result, out dummy);
147 public virtual bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
149 if (try_recv_delegate == null)
150 throw new InvalidOperationException ("BeginTryReceiveRequest operation has not started");
151 return try_recv_delegate.EndInvoke (out context, result);
154 public virtual bool WaitForRequest ()
156 return WaitForRequest (DefaultReceiveTimeout);
159 public abstract bool WaitForRequest (TimeSpan timeout);
161 Func<TimeSpan,bool> wait_delegate;
163 public virtual IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
165 if (wait_delegate == null)
166 wait_delegate = new Func<TimeSpan,bool> (WaitForRequest);
167 return wait_delegate.BeginInvoke (timeout, callback, state);
170 public virtual bool EndWaitForRequest (IAsyncResult result)
172 if (wait_delegate == null)
173 throw new InvalidOperationException ("BeginWaitForRequest operation has not started");
174 return wait_delegate.EndInvoke (result);
177 public virtual RequestContext ReceiveRequest ()
179 return ReceiveRequest (DefaultReceiveTimeout);
182 public abstract RequestContext ReceiveRequest (TimeSpan timeout);
184 public virtual IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
186 return BeginReceiveRequest (DefaultReceiveTimeout, callback, state);
189 Func<TimeSpan,RequestContext> recv_delegate;
190 public virtual IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
192 if (recv_delegate == null)
193 recv_delegate = new Func<TimeSpan,RequestContext> (ReceiveRequest);
194 return recv_delegate.BeginInvoke (timeout, callback, state);
197 public virtual RequestContext EndReceiveRequest (IAsyncResult result)
199 if (recv_delegate == null)
200 throw new InvalidOperationException ("BeginReceiveRequest operation has not started");
201 return recv_delegate.EndInvoke (result);
204 Action<TimeSpan> open_delegate, close_delegate;
206 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
207 AsyncCallback callback, object state)
209 if (open_delegate == null)
210 open_delegate = new Action<TimeSpan> (OnOpen);
211 return open_delegate.BeginInvoke (timeout, callback, state);
214 protected override void OnEndOpen (IAsyncResult result)
216 if (open_delegate == null)
217 throw new InvalidOperationException ("async open operation has not started");
218 open_delegate.EndInvoke (result);
221 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
222 AsyncCallback callback, object state)
224 if (close_delegate == null)
225 close_delegate = new Action<TimeSpan> (OnClose);
226 return close_delegate.BeginInvoke (timeout, callback, state);
229 protected override void OnEndClose (IAsyncResult result)
231 if (close_delegate == null)
232 throw new InvalidOperationException ("async close operation has not started");
233 close_delegate.EndInvoke (result);