2 // ChannelDispatcher.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005,2009 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Reflection;
32 using System.ServiceModel.Channels;
33 using System.Threading;
34 using System.Transactions;
35 using System.ServiceModel;
36 using System.ServiceModel.Description;
38 namespace System.ServiceModel.Dispatcher
40 public class ChannelDispatcher : ChannelDispatcherBase
42 class EndpointDispatcherCollection : SynchronizedCollection<EndpointDispatcher>
44 public EndpointDispatcherCollection (ChannelDispatcher owner)
49 ChannelDispatcher owner;
51 protected override void ClearItems ()
53 foreach (var ed in this)
54 ed.ChannelDispatcher = null;
58 protected override void InsertItem (int index, EndpointDispatcher item)
60 item.ChannelDispatcher = owner;
61 base.InsertItem (index, item);
64 protected override void RemoveItem (int index)
67 this [index].ChannelDispatcher = null;
68 base.RemoveItem (index);
71 protected override void SetItem (int index, EndpointDispatcher item)
73 item.ChannelDispatcher = owner;
74 base.SetItem (index, item);
81 Collection<IErrorHandler> error_handlers
82 = new Collection<IErrorHandler> ();
83 IChannelListener listener;
84 internal IDefaultCommunicationTimeouts timeouts; // FIXME: remove internal
85 MessageVersion message_version;
86 bool receive_sync, include_exception_detail_in_faults,
87 manual_addressing, is_tx_receive;
88 int max_tx_batch_size;
89 SynchronizedCollection<IChannelInitializer> initializers
90 = new SynchronizedCollection<IChannelInitializer> ();
91 IsolationLevel tx_isolation_level;
93 ServiceThrottle throttle;
95 Guid identifier = Guid.NewGuid ();
96 ManualResetEvent async_event = new ManualResetEvent (false);
98 ListenerLoopManager loop_manager;
99 SynchronizedCollection<EndpointDispatcher> endpoints;
101 [MonoTODO ("get binding info from config")]
102 public ChannelDispatcher (IChannelListener listener)
103 : this (listener, null)
107 public ChannelDispatcher (
108 IChannelListener listener, string bindingName)
109 : this (listener, bindingName, null)
113 public ChannelDispatcher (
114 IChannelListener listener, string bindingName,
115 IDefaultCommunicationTimeouts timeouts)
117 if (listener == null)
118 throw new ArgumentNullException ("listener");
119 Init (listener, bindingName, timeouts);
122 private void Init (IChannelListener listener, string bindingName,
123 IDefaultCommunicationTimeouts timeouts)
125 this.listener = listener;
126 this.binding_name = bindingName;
127 // IChannelListener is often a ChannelListenerBase
128 // which implements IDefaultCommunicationTimeouts.
129 this.timeouts = timeouts ?? listener as IDefaultCommunicationTimeouts ?? DefaultCommunicationTimeouts.Instance;
130 endpoints = new EndpointDispatcherCollection (this);
133 internal void InitializeServiceEndpoint (Type serviceType, ServiceEndpoint se)
135 this.MessageVersion = se.Binding.MessageVersion;
136 if (this.MessageVersion == null)
137 this.MessageVersion = MessageVersion.Default;
139 //Attach one EndpointDispacher to the ChannelDispatcher
140 EndpointDispatcher ed = new EndpointDispatcher (se.Address, se.Contract.Name, se.Contract.Namespace);
141 this.Endpoints.Add (ed);
142 ed.InitializeServiceEndpoint (false, serviceType, se);
145 public string BindingName {
146 get { return binding_name; }
149 public SynchronizedCollection<IChannelInitializer> ChannelInitializers {
150 get { return initializers; }
153 protected internal override TimeSpan DefaultCloseTimeout {
154 get { return timeouts.CloseTimeout; }
157 protected internal override TimeSpan DefaultOpenTimeout {
158 get { return timeouts.OpenTimeout; }
161 public Collection<IErrorHandler> ErrorHandlers {
162 get { return error_handlers; }
165 public SynchronizedCollection<EndpointDispatcher> Endpoints {
166 get { return endpoints; }
170 public bool IsTransactedAccept {
171 get { throw new NotImplementedException (); }
174 public bool IsTransactedReceive {
175 get { return is_tx_receive; }
176 set { is_tx_receive = value; }
179 public bool ManualAddressing {
180 get { return manual_addressing; }
181 set { manual_addressing = value; }
184 public int MaxTransactedBatchSize {
185 get { return max_tx_batch_size; }
186 set { max_tx_batch_size = value; }
189 public override ServiceHostBase Host {
193 public override IChannelListener Listener {
194 get { return listener; }
197 public MessageVersion MessageVersion {
198 get { return message_version; }
199 set { message_version = value; }
202 public bool ReceiveSynchronously {
203 get { return receive_sync; }
205 ThrowIfDisposedOrImmutable ();
206 receive_sync = value;
210 public bool IncludeExceptionDetailInFaults {
211 get { return include_exception_detail_in_faults; }
212 set { include_exception_detail_in_faults = value; }
215 public ServiceThrottle ServiceThrottle {
216 get { return throttle; }
217 set { throttle = value; }
220 public IsolationLevel TransactionIsolationLevel {
221 get { return tx_isolation_level; }
222 set { tx_isolation_level = value; }
225 public TimeSpan TransactionTimeout {
226 get { return tx_timeout; }
227 set { tx_timeout = value; }
230 protected internal override void Attach (ServiceHostBase host)
235 public override void CloseInput ()
237 if (loop_manager != null)
238 loop_manager.CloseInput ();
241 protected internal override void Detach (ServiceHostBase host)
246 protected override void OnAbort ()
248 if (loop_manager != null)
249 loop_manager.Stop (TimeSpan.FromTicks (1));
252 Action<TimeSpan> open_delegate;
253 Action<TimeSpan> close_delegate;
255 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
256 AsyncCallback callback, object state)
258 if (close_delegate == null)
259 close_delegate = new Action<TimeSpan> (OnClose);
260 return close_delegate.BeginInvoke (timeout, callback, state);
263 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
264 AsyncCallback callback, object state)
266 if (open_delegate == null)
267 open_delegate = new Action<TimeSpan> (OnClose);
268 return open_delegate.BeginInvoke (timeout, callback, state);
271 protected override void OnClose (TimeSpan timeout)
273 if (loop_manager != null)
274 loop_manager.Stop (timeout);
277 protected override void OnClosed ()
280 host.ChannelDispatchers.Remove (this);
284 protected override void OnEndClose (IAsyncResult result)
286 close_delegate.EndInvoke (result);
289 protected override void OnEndOpen (IAsyncResult result)
291 open_delegate.EndInvoke (result);
294 protected override void OnOpen (TimeSpan timeout)
296 if (Host == null || MessageVersion == null)
297 throw new InvalidOperationException ("Service host is not attached to this ChannelDispatcher.");
299 loop_manager.Setup (timeout);
302 protected override void OnOpening ()
305 loop_manager = new ListenerLoopManager (this);
308 protected override void OnOpened ()
316 // FIXME: not sure if it should be filled here.
317 if (ServiceThrottle == null)
318 ServiceThrottle = new ServiceThrottle ();
320 loop_manager.Start ();
324 // isolated from ChannelDispatcher
325 class ListenerLoopManager
327 ChannelDispatcher owner;
328 AutoResetEvent throttle_wait_handle = new AutoResetEvent (false);
329 AutoResetEvent creator_handle = new AutoResetEvent (false);
330 ManualResetEvent stop_handle = new ManualResetEvent (false);
333 DateTime close_started;
334 TimeSpan close_timeout;
335 Func<IAsyncResult> channel_acceptor;
336 List<IChannel> channels = new List<IChannel> ();
337 AddressFilterMode address_filter_mode;
339 public ListenerLoopManager (ChannelDispatcher owner)
342 var sba = owner.Host != null ? owner.Host.Description.Behaviors.Find<ServiceBehaviorAttribute> () : null;
344 address_filter_mode = sba.AddressFilterMode;
347 public void Setup (TimeSpan openTimeout)
349 if (owner.Listener.State != CommunicationState.Opened)
350 owner.Listener.Open (openTimeout);
352 // It is tested at Open(), but strangely it is not instantiated at this point.
353 foreach (var ed in owner.Endpoints)
354 if (ed.DispatchRuntime.InstanceContextProvider == null && (ed.DispatchRuntime.Type == null || ed.DispatchRuntime.Type.GetConstructor (Type.EmptyTypes) == null))
355 throw new InvalidOperationException ("There is no default constructor for the service Type in the DispatchRuntime");
356 SetupChannelAcceptor ();
361 if (loop_thread == null)
362 loop_thread = new Thread (new ThreadStart (Loop));
363 loop_thread.Start ();
366 Func<IAsyncResult> CreateAcceptor<TChannel> (IChannelListener l) where TChannel : class, IChannel
368 IChannelListener<TChannel> r = l as IChannelListener<TChannel>;
371 AsyncCallback callback = delegate (IAsyncResult result) {
373 ChannelAccepted (r.EndAcceptChannel (result));
374 } catch (Exception ex) {
375 Console.WriteLine ("Exception during finishing channel acceptance.");
376 Console.WriteLine (ex);
377 creator_handle.Set ();
382 return r.BeginAcceptChannel (callback, null);
383 } catch (Exception ex) {
384 Console.WriteLine ("Exception during accepting channel.");
385 Console.WriteLine (ex);
391 void SetupChannelAcceptor ()
393 var l = owner.Listener;
395 CreateAcceptor<IReplyChannel> (l) ??
396 CreateAcceptor<IReplySessionChannel> (l) ??
397 CreateAcceptor<IInputChannel> (l) ??
398 CreateAcceptor<IInputSessionChannel> (l) ??
399 CreateAcceptor<IDuplexChannel> (l) ??
400 CreateAcceptor<IDuplexSessionChannel> (l);
401 if (channel_acceptor == null)
402 throw new InvalidOperationException (String.Format ("Unrecognized channel listener type: {0}", l.GetType ()));
405 public void Stop (TimeSpan timeout)
407 if (loop_thread == null)
410 close_started = DateTime.Now;
411 close_timeout = timeout;
413 creator_handle.Set ();
414 throttle_wait_handle.Set (); // break primary loop
415 if (stop_handle != null) {
416 stop_handle.WaitOne (timeout > TimeSpan.Zero ? timeout : TimeSpan.FromTicks (1));
417 stop_handle.Close ();
420 if (owner.Listener.State != CommunicationState.Closed) {
422 Console.WriteLine ("Channel listener '{0}' is not closed. Aborting.", owner.Listener.GetType ());
423 owner.Listener.Abort ();
425 if (loop_thread != null && loop_thread.IsAlive)
426 loop_thread.Abort ();
430 public void CloseInput ()
432 foreach (var ch in channels.ToArray ()) {
433 if (ch.State == CommunicationState.Closed)
434 channels.Remove (ch); // zonbie, if exists
437 ch.Close (close_timeout - (DateTime.Now - close_started));
438 } catch (Exception ex) {
440 Console.WriteLine (ex);
451 } catch (Exception ex) {
453 Console.WriteLine ("ChannelDispatcher caught an exception inside dispatcher loop, which is likely thrown by the channel listener {0}", owner.Listener);
454 Console.WriteLine (ex);
456 if (stop_handle != null)
465 // FIXME: use WaitForChannel() for (*only* for) transacted channel listeners.
466 // http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/3faa4a5e-8602-4dbe-a181-73b3f581835e
469 // FIXME: enable throttling and allow more than one connection to process at a time.
470 while (loop && channels.Count < 1) {
471 // while (loop && channels.Count < owner.ServiceThrottle.MaxConcurrentSessions) {
473 creator_handle.WaitOne (); // released by ChannelAccepted()
477 throttle_wait_handle.WaitOne (); // released by IChannel.Close()
480 owner.Listener.Close ();
482 // make sure to close both listener and channels.
487 void ChannelAccepted (IChannel ch)
490 if (ch == null) // could happen when it was aborted
493 var dis = ch as IDisposable;
500 ch.Opened += delegate {
501 ch.Faulted += delegate {
502 if (channels.Contains (ch))
503 channels.Remove (ch);
504 throttle_wait_handle.Set (); // release loop wait lock.
506 ch.Closed += delegate {
507 if (channels.Contains (ch))
508 channels.Remove (ch);
509 throttle_wait_handle.Set (); // release loop wait lock.
514 creator_handle.Set ();
517 ProcessRequestOrInput (ch);
520 void ProcessRequestOrInput (IChannel ch)
522 var reply = ch as IReplyChannel;
523 var input = ch as IInputChannel;
526 if (owner.ReceiveSynchronously) {
528 if (reply.TryReceiveRequest (owner.timeouts.ReceiveTimeout, out rc))
529 ProcessRequest (reply, rc);
531 reply.BeginTryReceiveRequest (owner.timeouts.ReceiveTimeout, TryReceiveRequestDone, reply);
533 } else if (input != null) {
534 if (owner.ReceiveSynchronously) {
536 if (input.TryReceive (owner.timeouts.ReceiveTimeout, out msg))
537 ProcessInput (input, msg);
539 input.BeginTryReceive (owner.timeouts.ReceiveTimeout, TryReceiveDone, input);
544 void TryReceiveRequestDone (IAsyncResult result)
547 var reply = (IReplyChannel) result.AsyncState;
548 if (reply.EndTryReceiveRequest (result, out rc))
549 ProcessRequest (reply, rc);
554 void TryReceiveDone (IAsyncResult result)
557 var input = (IInputChannel) result.AsyncState;
558 if (input.EndTryReceive (result, out msg))
559 ProcessInput (input, msg);
564 void SendEndpointNotFound (RequestContext rc, EndpointNotFoundException ex)
568 MessageVersion version = rc.RequestMessage.Version;
569 FaultCode fc = new FaultCode ("DestinationUnreachable", version.Addressing.Namespace);
570 Message res = Message.CreateMessage (version, fc, "error occured", rc.RequestMessage.Headers.Action);
572 } catch (Exception e) {
574 Console.WriteLine ("Error on sending DestinationUnreachable fault message: " + e);
578 void ProcessRequest (IReplyChannel reply, RequestContext rc)
581 EndpointDispatcher candidate = FindEndpointDispatcher (rc.RequestMessage);
582 new InputOrReplyRequestProcessor (candidate.DispatchRuntime, reply).
584 } catch (EndpointNotFoundException ex) {
585 SendEndpointNotFound (rc, ex);
586 } catch (Exception ex) {
588 Console.WriteLine (ex);
592 // unless it is closed by session/call manager, move it back to the loop to receive the next message.
593 if (loop && reply.State != CommunicationState.Closed)
594 ProcessRequestOrInput (reply);
598 void ProcessInput (IInputChannel input, Message message)
601 EndpointDispatcher candidate = null;
602 candidate = FindEndpointDispatcher (message);
603 new InputOrReplyRequestProcessor (candidate.DispatchRuntime, input).
604 ProcessInput (message);
606 catch (Exception ex) {
608 Console.WriteLine (ex);
610 // unless it is closed by session/call manager, move it back to the loop to receive the next message.
611 if (loop && input.State != CommunicationState.Closed)
612 ProcessRequestOrInput (input);
616 EndpointDispatcher FindEndpointDispatcher (Message message) {
617 EndpointDispatcher candidate = null;
618 for (int i = 0; i < owner.Endpoints.Count; i++) {
619 if (MessageMatchesEndpointDispatcher (message, owner.Endpoints [i])) {
620 var newdis = owner.Endpoints [i];
621 if (candidate == null || candidate.FilterPriority < newdis.FilterPriority)
623 else if (candidate.FilterPriority == newdis.FilterPriority)
624 throw new MultipleFilterMatchesException ();
627 if (candidate == null && owner.Host != null)
628 owner.Host.OnUnknownMessageReceived (message);
632 bool MessageMatchesEndpointDispatcher (Message req, EndpointDispatcher endpoint)
634 // FIXME: handle AddressFilterMode.Prefix too.
636 Uri to = req.Headers.To;
638 return address_filter_mode == AddressFilterMode.Any;
639 if (to.AbsoluteUri == Constants.WsaAnonymousUri)
641 return endpoint.AddressFilter.Match (req) && endpoint.ContractFilter.Match (req);