2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2009,2010 Novell, Inc (http://www.novell.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
29 using System.ServiceModel;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Dispatcher;
34 namespace System.ServiceModel.Discovery
36 //* Strange, but this causes compiler error at DiscoveryClientBindingElement.
38 internal class DiscoveryChannel<TChannel> : DiscoveryChannelBase, IRequestSessionChannel, IDuplexSessionChannel
40 DiscoveryChannelFactory<TChannel> factory;
43 public DiscoveryChannel (DiscoveryChannelFactory<TChannel> factory, EndpointAddress address, Uri via)
46 this.factory = factory;
47 RemoteAddress = address;
51 public EndpointAddress RemoteAddress { get; private set; }
52 public Uri Via { get; private set; }
53 public EndpointAddress LocalAddress {
54 get { return ((IDuplexSessionChannel) inner).LocalAddress; }
57 IDuplexSession ISessionChannel<IDuplexSession>.Session {
58 get { return ((IDuplexSessionChannel) inner).Session; }
61 IOutputSession ISessionChannel<IOutputSession>.Session {
62 get { return ((IOutputSessionChannel) inner).Session; }
65 Action<TimeSpan> open_delegate, close_delegate;
67 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
69 if (close_delegate == null)
70 close_delegate = new Action<TimeSpan> (OnClose);
71 return close_delegate.BeginInvoke (timeout, callback, state);
74 protected override void OnEndClose (IAsyncResult result)
76 close_delegate.EndInvoke (result);
79 protected override void OnAbort ()
82 ((IChannel) inner).Abort ();
83 inner = default (TChannel);
87 protected override void OnClose (TimeSpan timeout)
90 ((IChannel) inner).Close (timeout);
91 inner = default (TChannel);
95 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
97 if (open_delegate == null)
98 open_delegate = new Action<TimeSpan> (OnOpen);
99 return open_delegate.BeginInvoke (timeout, callback, state);
102 protected override void OnEndOpen (IAsyncResult result)
104 open_delegate.EndInvoke (result);
107 protected override void OnOpen (TimeSpan timeout)
109 // FIXME: use timeout
110 DateTime start = DateTime.Now;
111 inner = CreateDiscoveryInnerChannel<TChannel> (factory);
112 ((IChannel) inner).Open (timeout - (DateTime.Now - start));
115 public Message Request (Message msg)
117 return Request (msg, DefaultSendTimeout + DefaultReceiveTimeout);
120 public Message Request (Message msg, TimeSpan timeout)
122 return ((IRequestChannel) inner).Request (msg, timeout);
125 public IAsyncResult BeginRequest (Message msg, AsyncCallback callback, object state)
127 return BeginRequest (msg, DefaultSendTimeout + DefaultReceiveTimeout, callback, state);
130 public IAsyncResult BeginRequest (Message msg, TimeSpan timeout, AsyncCallback callback, object state)
132 return ((IRequestChannel) inner).BeginRequest (msg, timeout, callback, state);
135 public Message EndRequest (IAsyncResult result)
137 return ((IRequestChannel) inner).EndRequest (result);
140 public Message Receive ()
142 return Receive (DefaultReceiveTimeout);
145 public Message Receive (TimeSpan timeout)
147 return ((IInputChannel) inner).Receive (timeout);
150 public IAsyncResult BeginReceive (AsyncCallback callback, object state)
152 return BeginReceive (DefaultReceiveTimeout, callback, state);
155 public IAsyncResult BeginReceive (TimeSpan timeout, AsyncCallback callback, object state)
157 return ((IInputChannel) inner).BeginReceive (timeout, callback, state);
160 public Message EndReceive (IAsyncResult result)
162 return ((IInputChannel) inner).EndReceive (result);
165 public bool TryReceive (out Message msg)
167 return TryReceive (DefaultReceiveTimeout, out msg);
170 public bool TryReceive (TimeSpan timeout, out Message msg)
172 return ((IInputChannel) inner).TryReceive (timeout, out msg);
175 public IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state)
177 return ((IInputChannel) inner).BeginTryReceive (timeout, callback, state);
180 public bool EndTryReceive (IAsyncResult result, out Message msg)
182 return ((IInputChannel) inner).EndTryReceive (result, out msg);
185 public bool WaitForMessage (TimeSpan timeout)
187 return ((IInputChannel) inner).WaitForMessage (timeout);
190 public IAsyncResult BeginWaitForMessage (TimeSpan timeout, AsyncCallback callback, object state)
192 return ((IInputChannel) inner).BeginWaitForMessage (timeout, callback, state);
195 public bool EndWaitForMessage (IAsyncResult result)
197 return ((IInputChannel) inner).EndWaitForMessage (result);
200 public void Send (Message msg)
202 Send (msg, DefaultSendTimeout);
205 public void Send (Message msg, TimeSpan timeout)
207 ((IOutputChannel) inner).Send (msg, timeout);
210 public IAsyncResult BeginSend (Message msg, AsyncCallback callback, object state)
212 return BeginSend (msg, DefaultSendTimeout, callback, state);
215 public IAsyncResult BeginSend (Message msg, TimeSpan timeout, AsyncCallback callback, object state)
217 return ((IOutputChannel) inner).BeginSend (msg, timeout, callback, state);
220 public void EndSend (IAsyncResult result)
222 ((IOutputChannel) inner).EndSend (result);
227 internal class DiscoveryRequestChannel : RequestChannelBase
229 public DiscoveryRequestChannel (DiscoveryChannelFactory<IRequestChannel> factory, EndpointAddress address, Uri via)
230 : base (factory, address, via)
232 this.factory = factory;
235 DiscoveryChannelFactory<IRequestChannel> factory;
236 IRequestChannel inner;
237 DiscoveryClient client;
239 protected override void OnOpen (TimeSpan timeout)
241 inner = CreateDiscoveryInnerChannel<IRequestChannel> (factory);
244 protected override void OnClose (TimeSpan timeout)
247 inner.Close (timeout);
252 protected override void OnAbort ()
260 public override Message Request (Message input, TimeSpan timeout)
262 ThrowIfDisposedOrNotOpen ();
263 return inner.Request (input, timeout);