Handle multiple concurrent calls to BeginTryReceiveRequest
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / ReplyChannelBase.cs
1 //
2 // ReplyChannelBase.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Net;
32 using System.Net.Security;
33 using System.ServiceModel;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Security;
36 using System.Threading;
37
38 namespace System.ServiceModel.Channels
39 {
40         internal abstract class InternalReplyChannelBase : ReplyChannelBase
41         {
42                 public InternalReplyChannelBase (ChannelListenerBase listener)
43                         : base (listener)
44                 {
45                         local_address = new EndpointAddress (listener.Uri);
46                 }
47
48                 EndpointAddress local_address;
49
50                 public override EndpointAddress LocalAddress {
51                         get { return local_address; }
52                 }
53         }
54
55         internal abstract class ReplyChannelBase : ChannelBase, IReplyChannel
56         {
57                 public ReplyChannelBase (ChannelListenerBase listener)
58                         : base (listener)
59                 {
60                         this.listener = listener;
61                 }
62
63                 ChannelListenerBase listener;
64
65                 public ChannelListenerBase Listener {
66                         get { return listener; }
67                 }
68
69                 public abstract EndpointAddress LocalAddress { get; }
70
71                 public override T GetProperty<T> ()
72                 {
73                         if (typeof (T) == typeof (MessageVersion) && listener is IHasMessageEncoder)
74                                 return (T) (object) ((IHasMessageEncoder) listener).MessageEncoder.MessageVersion;
75                         if (typeof (T) == typeof (IChannelListener))
76                                 return (T) (object) listener;
77                         return base.GetProperty<T> ();
78                 }
79
80                 // FIXME: this is wrong. Implement all of them in each channel.
81                 protected override void OnAbort ()
82                 {
83                         OnClose (TimeSpan.Zero);
84                 }
85
86                 protected override void OnClose (TimeSpan timeout)
87                 {
88                         if (currentAsyncThreads.Count > 0)
89                                 if (!CancelAsync (timeout))
90                                         foreach (Thread asyncThread in currentAsyncThreads)
91                                                 asyncThread.Abort ();
92                 }
93
94                 public virtual bool CancelAsync (TimeSpan timeout)
95                 {
96                         // FIXME: It should wait for the actual completion.
97                         return currentAsyncResults.Count > 0;
98                         //return CurrentAsyncResult == null || CurrentAsyncResult.AsyncWaitHandle.WaitOne (timeout);
99                 }
100
101                 public virtual bool TryReceiveRequest ()
102                 {
103                         RequestContext dummy;
104                         return TryReceiveRequest (DefaultReceiveTimeout, out dummy);
105                 }
106
107                 public abstract bool TryReceiveRequest (TimeSpan timeout, out RequestContext context);
108
109                 delegate bool TryReceiveDelegate (TimeSpan timeout, out RequestContext context);
110                 TryReceiveDelegate try_recv_delegate;
111
112                 object async_result_lock = new object ();
113                 HashSet<Thread> currentAsyncThreads = new HashSet<Thread>();
114                 HashSet<IAsyncResult> currentAsyncResults = new HashSet<IAsyncResult>();
115
116                 public virtual IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
117                 {
118                         IAsyncResult result = null;
119
120                         if (try_recv_delegate == null)
121                                 try_recv_delegate = new TryReceiveDelegate (delegate (TimeSpan tout, out RequestContext ctx) {
122                                         lock (async_result_lock) {
123                                                 if (currentAsyncResults.Contains (result))
124                                                         currentAsyncThreads.Add (Thread.CurrentThread);
125                                         }
126                                         try {
127                                                 return TryReceiveRequest (tout, out ctx);
128                                         } finally {
129                                                 lock (async_result_lock) {
130                                                         currentAsyncResults.Remove (result);
131                                                         currentAsyncThreads.Remove (Thread.CurrentThread);
132                                                 }
133                                         }
134                                         });
135                         RequestContext dummy;
136                         lock (async_result_lock) {
137                                 result = try_recv_delegate.BeginInvoke (timeout, out dummy, callback, state);
138                                 currentAsyncResults.Add (result);
139                         }
140                         // Note that at this point result can be missing from currentAsyncResults here if delegate has run to completion
141                         return result;
142                 }
143
144                 public virtual bool EndTryReceiveRequest (IAsyncResult result)
145                 {
146                         RequestContext dummy;
147                         return EndTryReceiveRequest (result, out dummy);
148                 }
149
150                 public virtual bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
151                 {
152                         if (try_recv_delegate == null)
153                                 throw new InvalidOperationException ("BeginTryReceiveRequest operation has not started");
154                         return try_recv_delegate.EndInvoke (out context, result);
155                 }
156
157                 public virtual bool WaitForRequest ()
158                 {
159                         return WaitForRequest (DefaultReceiveTimeout);
160                 }
161
162                 public abstract bool WaitForRequest (TimeSpan timeout);
163
164                 Func<TimeSpan,bool> wait_delegate;
165
166                 public virtual IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
167                 {
168                         if (wait_delegate == null)
169                                 wait_delegate = new Func<TimeSpan,bool> (WaitForRequest);
170                         return wait_delegate.BeginInvoke (timeout, callback, state);
171                 }
172
173                 public virtual bool EndWaitForRequest (IAsyncResult result)
174                 {
175                         if (wait_delegate == null)
176                                 throw new InvalidOperationException ("BeginWaitForRequest operation has not started");
177                         return wait_delegate.EndInvoke (result);
178                 }
179
180                 public virtual RequestContext ReceiveRequest ()
181                 {
182                         return ReceiveRequest (DefaultReceiveTimeout);
183                 }
184
185                 public abstract RequestContext ReceiveRequest (TimeSpan timeout);
186
187                 public virtual IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
188                 {
189                         return BeginReceiveRequest (DefaultReceiveTimeout, callback, state);
190                 }
191
192                 Func<TimeSpan,RequestContext> recv_delegate;
193                 public virtual IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
194                 {
195                         if (recv_delegate == null)
196                                 recv_delegate = new Func<TimeSpan,RequestContext> (ReceiveRequest);
197                         return recv_delegate.BeginInvoke (timeout, callback, state);
198                 }
199
200                 public virtual RequestContext EndReceiveRequest (IAsyncResult result)
201                 {
202                         if (recv_delegate == null)
203                                 throw new InvalidOperationException ("BeginReceiveRequest operation has not started");
204                         return recv_delegate.EndInvoke (result);
205                 }
206
207                 Action<TimeSpan> open_delegate, close_delegate;
208
209                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
210                         AsyncCallback callback, object state)
211                 {
212                         if (open_delegate == null)
213                                 open_delegate = new Action<TimeSpan> (OnOpen);
214                         return open_delegate.BeginInvoke (timeout, callback, state);
215                 }
216
217                 protected override void OnEndOpen (IAsyncResult result)
218                 {
219                         if (open_delegate == null)
220                                 throw new InvalidOperationException ("async open operation has not started");
221                         open_delegate.EndInvoke (result);
222                 }
223
224                 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
225                         AsyncCallback callback, object state)
226                 {
227                         if (close_delegate == null)
228                                 close_delegate = new Action<TimeSpan> (OnClose);
229                         return close_delegate.BeginInvoke (timeout, callback, state);
230                 }
231
232                 protected override void OnEndClose (IAsyncResult result)
233                 {
234                         if (close_delegate == null)
235                                 throw new InvalidOperationException ("async close operation has not started");
236                         close_delegate.EndInvoke (result);
237                 }
238         }
239 }