2009-08-17 Atsushi Enomoto <atsushi@ximian.com>
[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                 }
61
62                 ChannelListenerBase listener;
63
64                 public abstract EndpointAddress LocalAddress { get; }
65
66                 public override T GetProperty<T> ()
67                 {
68                         if (typeof (T) == typeof (IChannelListener))
69                                 return (T) (object) listener;
70                         return base.GetProperty<T> ();
71                 }
72
73                 protected override void OnAbort ()
74                 {
75                         OnClose (TimeSpan.Zero);
76                 }
77
78                 protected override void OnClose (TimeSpan timeout)
79                 {
80                         if (CurrentAsyncThread != null)
81                                 if (!CancelAsync (timeout))
82                                         CurrentAsyncThread.Abort ();
83                 }
84
85                 public virtual bool CancelAsync (TimeSpan timeout)
86                 {
87                         // FIXME: It should wait for the actual completion.
88                         return CurrentAsyncResult == null;
89                         //return CurrentAsyncResult == null || CurrentAsyncResult.AsyncWaitHandle.WaitOne (timeout);
90                 }
91
92                 public virtual bool TryReceiveRequest ()
93                 {
94                         RequestContext dummy;
95                         return TryReceiveRequest (DefaultReceiveTimeout, out dummy);
96                 }
97
98                 public abstract bool TryReceiveRequest (TimeSpan timeout, out RequestContext context);
99
100                 delegate bool TryReceiveDelegate (TimeSpan timeout, out RequestContext context);
101                 TryReceiveDelegate try_recv_delegate;
102
103                 protected Thread CurrentAsyncThread { get; private set; }
104                 protected IAsyncResult CurrentAsyncResult { get; private set; }
105
106                 public virtual IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
107                 {
108                         if (CurrentAsyncResult != null)
109                                 throw new InvalidOperationException ("Another async TryReceiveRequest operation is in progress");
110                         if (try_recv_delegate == null)
111                                 try_recv_delegate = new TryReceiveDelegate (delegate (TimeSpan tout, out RequestContext ctx) {
112                                         if (CurrentAsyncResult != null)
113                                                 CurrentAsyncThread = Thread.CurrentThread;
114                                         try {
115                                                 return TryReceiveRequest (tout, out ctx);
116                                         } finally {
117                                                 CurrentAsyncResult = null;
118                                                 CurrentAsyncThread = null;
119                                         }
120                                         });
121                         RequestContext dummy;
122                         CurrentAsyncResult = try_recv_delegate.BeginInvoke (timeout, out dummy, callback, state);
123                         return CurrentAsyncResult;
124                 }
125
126                 public virtual bool EndTryReceiveRequest (IAsyncResult result)
127                 {
128                         RequestContext dummy;
129                         return EndTryReceiveRequest (result, out dummy);
130                 }
131
132                 public virtual bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
133                 {
134                         if (try_recv_delegate == null)
135                                 throw new InvalidOperationException ("BeginTryReceiveRequest operation has not started");
136                         return try_recv_delegate.EndInvoke (out context, result);
137                 }
138
139                 public virtual bool WaitForRequest ()
140                 {
141                         return WaitForRequest (DefaultReceiveTimeout);
142                 }
143
144                 public abstract bool WaitForRequest (TimeSpan timeout);
145
146                 Func<TimeSpan,bool> wait_delegate;
147
148                 public virtual IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
149                 {
150                         if (wait_delegate == null)
151                                 wait_delegate = new Func<TimeSpan,bool> (WaitForRequest);
152                         return wait_delegate.BeginInvoke (timeout, callback, state);
153                 }
154
155                 public virtual bool EndWaitForRequest (IAsyncResult result)
156                 {
157                         if (wait_delegate == null)
158                                 throw new InvalidOperationException ("BeginWaitForRequest operation has not started");
159                         return wait_delegate.EndInvoke (result);
160                 }
161
162                 public virtual RequestContext ReceiveRequest ()
163                 {
164                         return ReceiveRequest (DefaultReceiveTimeout);
165                 }
166
167                 public abstract RequestContext ReceiveRequest (TimeSpan timeout);
168
169                 public virtual IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
170                 {
171                         return BeginReceiveRequest (DefaultReceiveTimeout, callback, state);
172                 }
173
174                 Func<TimeSpan,RequestContext> recv_delegate;
175                 public virtual IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
176                 {
177                         if (recv_delegate == null)
178                                 recv_delegate = new Func<TimeSpan,RequestContext> (ReceiveRequest);
179                         return recv_delegate.BeginInvoke (timeout, callback, state);
180                 }
181
182                 public virtual RequestContext EndReceiveRequest (IAsyncResult result)
183                 {
184                         if (recv_delegate == null)
185                                 throw new InvalidOperationException ("BeginReceiveRequest operation has not started");
186                         return recv_delegate.EndInvoke (result);
187                 }
188
189                 Action<TimeSpan> open_delegate, close_delegate;
190
191                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
192                         AsyncCallback callback, object state)
193                 {
194                         if (open_delegate == null)
195                                 open_delegate = new Action<TimeSpan> (OnOpen);
196                         return open_delegate.BeginInvoke (timeout, callback, state);
197                 }
198
199                 protected override void OnEndOpen (IAsyncResult result)
200                 {
201                         if (open_delegate == null)
202                                 throw new InvalidOperationException ("async open operation has not started");
203                         open_delegate.EndInvoke (result);
204                 }
205
206                 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
207                         AsyncCallback callback, object state)
208                 {
209                         if (close_delegate == null)
210                                 close_delegate = new Action<TimeSpan> (OnClose);
211                         return close_delegate.BeginInvoke (timeout, callback, state);
212                 }
213
214                 protected override void OnEndClose (IAsyncResult result)
215                 {
216                         if (close_delegate == null)
217                                 throw new InvalidOperationException ("async close operation has not started");
218                         close_delegate.EndInvoke (result);
219                 }
220         }
221 }