2 // HttpChannelListener.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 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.Security;
39 namespace System.ServiceModel.Channels
41 internal class HttpSimpleChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
42 where TChannel : class, IChannel
44 HttpListenerManager<TChannel> httpChannelManager;
46 public HttpSimpleChannelListener (HttpTransportBindingElement source,
47 BindingContext context)
48 : base (source, context)
52 public HttpListener Http {
53 get { return httpChannelManager.HttpListener; }
56 object creator_lock = new object ();
58 protected override TChannel CreateChannel (TimeSpan timeout)
61 return CreateChannelCore (timeout);
65 TChannel CreateChannelCore (TimeSpan timeout)
67 if (typeof (TChannel) == typeof (IReplyChannel))
68 return (TChannel) (object) new HttpSimpleReplyChannel ((HttpSimpleChannelListener<IReplyChannel>) (object) this);
69 // FIXME: session channel support
70 if (typeof (TChannel) == typeof (IReplySessionChannel))
71 throw new NotImplementedException ();
73 throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
76 protected override void OnOpen (TimeSpan timeout)
78 base.OnOpen (timeout);
79 StartListening (timeout);
82 protected override void OnAbort ()
84 httpChannelManager.Stop (true);
87 protected override void OnClose (TimeSpan timeout)
89 if (State == CommunicationState.Closed)
91 base.OnClose (timeout);
92 // FIXME: it is said that channels are not closed
93 // when the channel listener is closed.
94 // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
95 httpChannelManager.Stop (false);
98 void StartListening (TimeSpan timeout)
100 httpChannelManager = new HttpListenerManager<TChannel> (this);
101 httpChannelManager.Open (timeout);
105 internal class AspNetChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
106 where TChannel : class, IChannel
108 public AspNetChannelListener (HttpTransportBindingElement source,
109 BindingContext context)
110 : base (source, context)
114 SvcHttpHandler http_handler;
115 internal SvcHttpHandler HttpHandler {
117 if (http_handler == null)
118 http_handler = SvcHttpHandlerFactory.GetHandlerForListener (this);
123 protected override TChannel CreateChannel (TimeSpan timeout)
125 if (typeof (TChannel) == typeof (IReplyChannel))
126 return (TChannel) (object) new AspNetReplyChannel ((AspNetChannelListener<IReplyChannel>) (object) this);
127 // FIXME: session channel support
128 if (typeof (TChannel) == typeof (IReplySessionChannel))
129 throw new NotImplementedException ();
131 throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
135 internal abstract class HttpChannelListenerBase<TChannel> : InternalChannelListenerBase<TChannel>
136 where TChannel : class, IChannel
138 HttpTransportBindingElement source;
139 BindingContext context;
140 List<TChannel> channels = new List<TChannel> ();
141 MessageEncoder encoder;
143 public HttpChannelListenerBase (HttpTransportBindingElement source,
144 BindingContext context)
147 foreach (BindingElement be in context.RemainingBindingElements) {
148 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
150 encoder = CreateEncoder<TChannel> (mbe);
155 encoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
158 public MessageEncoder MessageEncoder {
159 get { return encoder; }
162 protected IList<TChannel> Channels {
163 get { return channels; }
166 protected override TChannel OnAcceptChannel (TimeSpan timeout)
168 TChannel ch = CreateChannel (timeout);
173 protected abstract TChannel CreateChannel (TimeSpan timeout);
175 protected override bool OnWaitForChannel (TimeSpan timeout)
177 throw new NotImplementedException ();
180 protected override void OnAbort ()
182 OnClose (TimeSpan.Zero);
185 protected override void OnOpen (TimeSpan timeout)
189 protected override void OnClose (TimeSpan timeout)
191 DateTime start = DateTime.Now;
192 foreach (TChannel ch in new List<TChannel> (Channels)) // they will be removed during iteration, so create another one
193 ch.Close (timeout - (DateTime.Now - start));
194 base.OnClose (timeout - (DateTime.Now - start));