2 // HttpStandaloneChannelListener.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 interface IChannelDispatcherBoundListener
45 ChannelDispatcher ChannelDispatcher { get; set; }
48 internal class HttpStandaloneChannelListener<TChannel> : InternalChannelListenerBase<TChannel>, IChannelDispatcherBoundListener
49 where TChannel : class, IChannel
51 HttpListenerManager listener_manager;
53 public HttpStandaloneChannelListener (HttpTransportBindingElement source, BindingContext context)
56 if (ServiceHostBase.CurrentServiceHostHack != null)
57 DispatcherBuilder.ChannelDispatcherSetter = delegate (ChannelDispatcher cd) { this.ChannelDispatcher = cd; };
60 // The null Uri check looks weird, but it seems the listener can be built without it.
61 // See HttpTransportBindingElementTest.BuildChannelListenerWithoutListenUri().
62 if (Uri != null && source.Scheme != Uri.Scheme)
63 throw new ArgumentException (String.Format ("Requested listen uri scheme must be {0}, but was {1}.", source.Scheme, Uri.Scheme));
65 foreach (BindingElement be in context.Binding.Elements) {
66 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
68 MessageEncoder = CreateEncoder<TChannel> (mbe);
72 if (MessageEncoder == null)
73 MessageEncoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
75 if (context.BindingParameters.Contains (typeof (ServiceCredentials)))
76 SecurityTokenManager = new ServiceCredentialsSecurityTokenManager ((ServiceCredentials) context.BindingParameters [typeof (ServiceCredentials)]);
79 public ChannelDispatcher ChannelDispatcher { get; set; }
81 public HttpTransportBindingElement Source { get; private set; }
83 public HttpListenerManager ListenerManager {
84 get { return listener_manager; }
87 public ServiceCredentialsSecurityTokenManager SecurityTokenManager { get; private set; }
89 ManualResetEvent accept_channel_handle = new ManualResetEvent (true);
91 protected override TChannel OnAcceptChannel (TimeSpan timeout)
93 // HTTP channel could be accepted while there is no incoming request yet. The reply channel waits for the actual request.
94 // HTTP channel listeners do not accept more than one channel at a time.
95 DateTime start = DateTime.Now;
96 accept_channel_handle.WaitOne (timeout - (DateTime.Now - start));
97 accept_channel_handle.Reset ();
98 TChannel ch = CreateChannel (timeout - (DateTime.Now - 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 HttpStandaloneReplyChannel ((HttpStandaloneChannelListener<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);
131 protected override void OnOpen (TimeSpan timeout)
133 listener_manager = GetOrCreateListenerManager ();
134 Properties.Add (listener_manager);
135 listener_manager.RegisterListener (ChannelDispatcher, 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) {