Merge pull request #463 from strawd/concurrent-requests
[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.Sockets;
33 using System.Net.Security;
34 using System.ServiceModel;
35 using System.ServiceModel.Description;
36 using System.ServiceModel.Security;
37 using System.Threading;
38 using System.Xml;
39
40 namespace System.ServiceModel.Channels
41 {
42         internal abstract class InternalReplyChannelBase : ReplyChannelBase
43         {
44                 public InternalReplyChannelBase (ChannelListenerBase listener)
45                         : base (listener)
46                 {
47                         local_address = new EndpointAddress (listener.Uri);
48                 }
49
50                 EndpointAddress local_address;
51
52                 public override EndpointAddress LocalAddress {
53                         get { return local_address; }
54                 }
55         }
56
57         internal abstract class ReplyChannelBase : ChannelBase, IReplyChannel
58         {
59                 public ReplyChannelBase (ChannelListenerBase listener)
60                         : base (listener)
61                 {
62                         this.listener = listener;
63                 }
64
65                 ChannelListenerBase listener;
66
67                 public ChannelListenerBase Listener {
68                         get { return listener; }
69                 }
70
71                 public abstract EndpointAddress LocalAddress { get; }
72
73                 public override T GetProperty<T> ()
74                 {
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> ();
80                 }
81
82                 // FIXME: this is wrong. Implement all of them in each channel.
83                 protected override void OnAbort ()
84                 {
85                         OnClose (TimeSpan.Zero);
86                 }
87
88                 protected override void OnClose (TimeSpan timeout)
89                 {
90                         if (currentAsyncThreads.Count > 0)
91                                 if (!CancelAsync (timeout))
92                                         foreach (Thread asyncThread in currentAsyncThreads)
93                                                 asyncThread.Abort ();
94                 }
95
96                 public virtual bool CancelAsync (TimeSpan timeout)
97                 {
98                         // FIXME: It should wait for the actual completion.
99                         return currentAsyncResults.Count > 0;
100                         //return CurrentAsyncResult == null || CurrentAsyncResult.AsyncWaitHandle.WaitOne (timeout);
101                 }
102
103                 public virtual bool TryReceiveRequest ()
104                 {
105                         RequestContext dummy;
106                         return TryReceiveRequest (DefaultReceiveTimeout, out dummy);
107                 }
108
109                 public abstract bool TryReceiveRequest (TimeSpan timeout, out RequestContext context);
110
111                 delegate bool TryReceiveDelegate (TimeSpan timeout, out RequestContext context);
112                 TryReceiveDelegate try_recv_delegate;
113
114                 object async_result_lock = new object ();
115                 HashSet<Thread> currentAsyncThreads = new HashSet<Thread>();
116                 HashSet<IAsyncResult> currentAsyncResults = new HashSet<IAsyncResult>();
117
118                 public virtual IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
119                 {
120                         IAsyncResult result = null;
121
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);
127                                         }
128                                         try {
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
145                                         } finally {
146                                                 lock (async_result_lock) {
147                                                         currentAsyncResults.Remove (result);
148                                                         currentAsyncThreads.Remove (Thread.CurrentThread);
149                                                 }
150                                         }
151                                         ctx = null;
152                                         return false;
153                                         });
154                         RequestContext dummy;
155                         lock (async_result_lock) {
156                                 result = try_recv_delegate.BeginInvoke (timeout, out dummy, callback, state);
157                                 currentAsyncResults.Add (result);
158                         }
159                         // Note that at this point result can be missing from currentAsyncResults here if delegate has run to completion
160                         return result;
161                 }
162
163                 public virtual bool EndTryReceiveRequest (IAsyncResult result)
164                 {
165                         RequestContext dummy;
166                         return EndTryReceiveRequest (result, out dummy);
167                 }
168
169                 public virtual bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
170                 {
171                         if (try_recv_delegate == null)
172                                 throw new InvalidOperationException ("BeginTryReceiveRequest operation has not started");
173                         return try_recv_delegate.EndInvoke (out context, result);
174                 }
175
176                 public virtual bool WaitForRequest ()
177                 {
178                         return WaitForRequest (DefaultReceiveTimeout);
179                 }
180
181                 public abstract bool WaitForRequest (TimeSpan timeout);
182
183                 Func<TimeSpan,bool> wait_delegate;
184
185                 public virtual IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
186                 {
187                         if (wait_delegate == null)
188                                 wait_delegate = new Func<TimeSpan,bool> (WaitForRequest);
189                         return wait_delegate.BeginInvoke (timeout, callback, state);
190                 }
191
192                 public virtual bool EndWaitForRequest (IAsyncResult result)
193                 {
194                         if (wait_delegate == null)
195                                 throw new InvalidOperationException ("BeginWaitForRequest operation has not started");
196                         return wait_delegate.EndInvoke (result);
197                 }
198
199                 public virtual RequestContext ReceiveRequest ()
200                 {
201                         return ReceiveRequest (DefaultReceiveTimeout);
202                 }
203
204                 public abstract RequestContext ReceiveRequest (TimeSpan timeout);
205
206                 public virtual IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
207                 {
208                         return BeginReceiveRequest (DefaultReceiveTimeout, callback, state);
209                 }
210
211                 Func<TimeSpan,RequestContext> recv_delegate;
212                 public virtual IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
213                 {
214                         if (recv_delegate == null)
215                                 recv_delegate = new Func<TimeSpan,RequestContext> (ReceiveRequest);
216                         return recv_delegate.BeginInvoke (timeout, callback, state);
217                 }
218
219                 public virtual RequestContext EndReceiveRequest (IAsyncResult result)
220                 {
221                         if (recv_delegate == null)
222                                 throw new InvalidOperationException ("BeginReceiveRequest operation has not started");
223                         return recv_delegate.EndInvoke (result);
224                 }
225
226                 Action<TimeSpan> open_delegate, close_delegate;
227
228                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
229                         AsyncCallback callback, object state)
230                 {
231                         if (open_delegate == null)
232                                 open_delegate = new Action<TimeSpan> (OnOpen);
233                         return open_delegate.BeginInvoke (timeout, callback, state);
234                 }
235
236                 protected override void OnEndOpen (IAsyncResult result)
237                 {
238                         if (open_delegate == null)
239                                 throw new InvalidOperationException ("async open operation has not started");
240                         open_delegate.EndInvoke (result);
241                 }
242
243                 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
244                         AsyncCallback callback, object state)
245                 {
246                         if (close_delegate == null)
247                                 close_delegate = new Action<TimeSpan> (OnClose);
248                         return close_delegate.BeginInvoke (timeout, callback, state);
249                 }
250
251                 protected override void OnEndClose (IAsyncResult result)
252                 {
253                         if (close_delegate == null)
254                                 throw new InvalidOperationException ("async close operation has not started");
255                         close_delegate.EndInvoke (result);
256                 }
257         }
258 }