Merge pull request #1971 from angeloc/master
[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 (CurrentAsyncThread != null)
91                                 if (!CancelAsync (timeout))
92                                         CurrentAsyncThread.Abort ();
93                 }
94
95                 public virtual bool CancelAsync (TimeSpan timeout)
96                 {
97                         // FIXME: It should wait for the actual completion.
98                         return CurrentAsyncResult == null;
99                         //return CurrentAsyncResult == null || CurrentAsyncResult.AsyncWaitHandle.WaitOne (timeout);
100                 }
101
102                 public virtual bool TryReceiveRequest ()
103                 {
104                         RequestContext dummy;
105                         return TryReceiveRequest (DefaultReceiveTimeout, out dummy);
106                 }
107
108                 public abstract bool TryReceiveRequest (TimeSpan timeout, out RequestContext context);
109
110                 delegate bool TryReceiveDelegate (TimeSpan timeout, out RequestContext context);
111                 TryReceiveDelegate try_recv_delegate;
112
113                 object async_result_lock = new object ();
114                 protected Thread CurrentAsyncThread { get; private set; }
115                 protected IAsyncResult CurrentAsyncResult { get; private set; }
116
117                 public virtual IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
118                 {
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;
126                                         }
127                                         try {
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
144                                         } finally {
145                                                 lock (async_result_lock) {
146                                                         CurrentAsyncResult = null;
147                                                         CurrentAsyncThread = null;
148                                                 }
149                                         }
150                                         ctx = null;
151                                         return false;
152                                         });
153                         RequestContext dummy;
154                         IAsyncResult result;
155                         lock (async_result_lock) {
156                                 result = CurrentAsyncResult = try_recv_delegate.BeginInvoke (timeout, out dummy, callback, state);
157                         }
158                         // Note that at this point CurrentAsyncResult can be null here if delegate has run to completion
159                         return result;
160                 }
161
162                 public virtual bool EndTryReceiveRequest (IAsyncResult result)
163                 {
164                         RequestContext dummy;
165                         return EndTryReceiveRequest (result, out dummy);
166                 }
167
168                 public virtual bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
169                 {
170                         if (try_recv_delegate == null)
171                                 throw new InvalidOperationException ("BeginTryReceiveRequest operation has not started");
172                         return try_recv_delegate.EndInvoke (out context, result);
173                 }
174
175                 public virtual bool WaitForRequest ()
176                 {
177                         return WaitForRequest (DefaultReceiveTimeout);
178                 }
179
180                 public abstract bool WaitForRequest (TimeSpan timeout);
181
182                 Func<TimeSpan,bool> wait_delegate;
183
184                 public virtual IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
185                 {
186                         if (wait_delegate == null)
187                                 wait_delegate = new Func<TimeSpan,bool> (WaitForRequest);
188                         return wait_delegate.BeginInvoke (timeout, callback, state);
189                 }
190
191                 public virtual bool EndWaitForRequest (IAsyncResult result)
192                 {
193                         if (wait_delegate == null)
194                                 throw new InvalidOperationException ("BeginWaitForRequest operation has not started");
195                         return wait_delegate.EndInvoke (result);
196                 }
197
198                 public virtual RequestContext ReceiveRequest ()
199                 {
200                         return ReceiveRequest (DefaultReceiveTimeout);
201                 }
202
203                 public abstract RequestContext ReceiveRequest (TimeSpan timeout);
204
205                 public virtual IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
206                 {
207                         return BeginReceiveRequest (DefaultReceiveTimeout, callback, state);
208                 }
209
210                 Func<TimeSpan,RequestContext> recv_delegate;
211                 public virtual IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
212                 {
213                         if (recv_delegate == null)
214                                 recv_delegate = new Func<TimeSpan,RequestContext> (ReceiveRequest);
215                         return recv_delegate.BeginInvoke (timeout, callback, state);
216                 }
217
218                 public virtual RequestContext EndReceiveRequest (IAsyncResult result)
219                 {
220                         if (recv_delegate == null)
221                                 throw new InvalidOperationException ("BeginReceiveRequest operation has not started");
222                         return recv_delegate.EndInvoke (result);
223                 }
224
225                 Action<TimeSpan> open_delegate, close_delegate;
226
227                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
228                         AsyncCallback callback, object state)
229                 {
230                         if (open_delegate == null)
231                                 open_delegate = new Action<TimeSpan> (OnOpen);
232                         return open_delegate.BeginInvoke (timeout, callback, state);
233                 }
234
235                 protected override void OnEndOpen (IAsyncResult result)
236                 {
237                         if (open_delegate == null)
238                                 throw new InvalidOperationException ("async open operation has not started");
239                         open_delegate.EndInvoke (result);
240                 }
241
242                 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
243                         AsyncCallback callback, object state)
244                 {
245                         if (close_delegate == null)
246                                 close_delegate = new Action<TimeSpan> (OnClose);
247                         return close_delegate.BeginInvoke (timeout, callback, state);
248                 }
249
250                 protected override void OnEndClose (IAsyncResult result)
251                 {
252                         if (close_delegate == null)
253                                 throw new InvalidOperationException ("async close operation has not started");
254                         close_delegate.EndInvoke (result);
255                 }
256         }
257 }