2 // HttpChannelListener.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2010 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;
33 using System.Net.Security;
34 using System.ServiceModel;
35 using System.ServiceModel.Description;
36 using System.ServiceModel.Dispatcher;
37 using System.ServiceModel.Security;
39 using System.Threading;
41 namespace System.ServiceModel.Channels.Http
43 internal class HttpChannelListener<TChannel> : InternalChannelListenerBase<TChannel>, IChannelDispatcherBoundListener
44 where TChannel : class, IChannel
46 HttpListenerManager listener_manager;
48 public HttpChannelListener (HttpTransportBindingElement source, BindingContext context)
51 if (ServiceHostBase.CurrentServiceHostHack != null)
52 DispatcherBuilder.ChannelDispatcherSetter = delegate (ChannelDispatcher cd) { this.ChannelDispatcher = cd; };
55 // The null Uri check looks weird, but it seems the listener can be built without it.
56 // See HttpTransportBindingElementTest.BuildChannelListenerWithoutListenUri().
57 if (Uri != null && source.Scheme != Uri.Scheme)
58 throw new ArgumentException (String.Format ("Requested listen uri scheme must be {0}, but was {1}.", source.Scheme, Uri.Scheme));
60 foreach (BindingElement be in context.Binding.Elements) {
61 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
63 MessageEncoder = CreateEncoder<TChannel> (mbe);
67 if (MessageEncoder == null)
68 MessageEncoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
70 if (context.BindingParameters.Contains (typeof (ServiceCredentials)))
71 SecurityTokenManager = new ServiceCredentialsSecurityTokenManager ((ServiceCredentials) context.BindingParameters [typeof (ServiceCredentials)]);
74 public ChannelDispatcher ChannelDispatcher { get; set; }
76 public HttpTransportBindingElement Source { get; private set; }
78 public HttpListenerManager ListenerManager {
79 get { return listener_manager; }
82 public ServiceCredentialsSecurityTokenManager SecurityTokenManager { get; private set; }
84 ManualResetEvent accept_channel_handle = new ManualResetEvent (true);
86 protected override TChannel OnAcceptChannel (TimeSpan timeout)
88 // HTTP channel could be accepted while there is no incoming request yet. The reply channel waits for the actual request.
89 // HTTP channel listeners do not accept more than one channel at a time.
90 DateTime start = DateTime.UtcNow;
92 if (timeout == TimeSpan.MaxValue)
93 waitTimeout = TimeSpan.FromMilliseconds(int.MaxValue);
95 waitTimeout = timeout - (DateTime.UtcNow - start);
96 accept_channel_handle.WaitOne (waitTimeout);
97 accept_channel_handle.Reset ();
98 TChannel ch = CreateChannel (timeout - (DateTime.UtcNow - start));
99 ch.Closed += delegate {
100 accept_channel_handle.Set ();
105 protected TChannel CreateChannel (TimeSpan timeout)
108 return CreateChannelCore (timeout);
112 TChannel CreateChannelCore (TimeSpan timeout)
114 if (typeof (TChannel) == typeof (IReplyChannel))
115 return (TChannel) (object) new HttpReplyChannel ((HttpChannelListener<IReplyChannel>) (object) this);
117 throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
120 protected override bool OnWaitForChannel (TimeSpan timeout)
122 throw new NotImplementedException ();
125 protected HttpListenerManager GetOrCreateListenerManager ()
127 var table = HttpListenerManagerTable.GetOrCreate (ChannelDispatcher != null ? ChannelDispatcher.Host : null);
128 return table.GetOrCreateManager (Uri, Source);
131 protected override void OnOpen (TimeSpan timeout)
133 listener_manager = GetOrCreateListenerManager ();
134 Properties.Add (listener_manager);
135 listener_manager.RegisterListener (ChannelDispatcher, Source, timeout);
138 protected override void OnAbort ()
140 listener_manager.UnregisterListener (ChannelDispatcher, TimeSpan.Zero);
143 protected override void OnClose (TimeSpan timeout)
145 if (State == CommunicationState.Closed)
147 base.OnClose (timeout);
148 // The channels are kept open when the creator channel listener is closed.
149 // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
150 listener_manager.UnregisterListener (ChannelDispatcher, timeout);
153 // immediately stop accepting channel.
154 public override bool CancelAsync (TimeSpan timeout)
157 CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.Zero);
158 } catch (TimeoutException) {