2 // TcpChannelListener.cs
5 // Marcos Cobena (marcoscobena@gmail.com)
7 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
11 using System.Collections.Generic;
14 using System.Net.Sockets;
15 using System.ServiceModel.Description;
19 namespace System.ServiceModel.Channels
21 internal class TcpChannelListener<TChannel> : InternalChannelListenerBase<TChannel>
22 where TChannel : class, IChannel
24 List<IChannel> channels = new List<IChannel> ();
25 BindingContext context;
27 IDuplexSession session;
29 TcpListener tcp_listener;
32 public TcpChannelListener (TcpTransportBindingElement source,
33 BindingContext context) : base (context.Binding)
35 MessageEncoder encoder = null;
36 XmlDictionaryReaderQuotas quotas = null;
38 if (context.ListenUriMode == ListenUriMode.Explicit)
40 context.ListenUriRelativeAddress != null ?
41 new Uri (context.ListenUriBaseAddress, context.ListenUriRelativeAddress) :
42 context.ListenUriBaseAddress;
44 throw new NotImplementedException ();
46 foreach (BindingElement be in context.RemainingBindingElements) {
47 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
49 encoder = CreateEncoder<TChannel> (mbe);
50 quotas = mbe.GetProperty<XmlDictionaryReaderQuotas> (context);
56 encoder = new BinaryMessageEncoder ();
58 info = new TcpChannelInfo (source, encoder, quotas);
61 public override Uri Uri {
62 get { return listen_uri; }
66 protected override TChannel OnAcceptChannel (TimeSpan timeout)
68 TChannel channel = PopulateChannel (timeout);
69 channels.Add (channel);
73 TChannel PopulateChannel (TimeSpan timeout)
75 // FIXME: pass delegate or something to remove the channel instance from "channels" when it is closed.
76 if (typeof (TChannel) == typeof (IDuplexSessionChannel))
77 return (TChannel) (object) new TcpDuplexSessionChannel (this, info, tcp_listener, timeout);
79 // FIXME: To implement more.
80 throw new NotImplementedException ();
84 protected override bool OnWaitForChannel (TimeSpan timeout)
86 throw new NotImplementedException ();
89 // CommunicationObject
91 protected override void OnAbort ()
93 if (State == CommunicationState.Closed)
98 protected override void OnClose (TimeSpan timeout)
100 if (State == CommunicationState.Closed)
107 if (tcp_listener == null)
108 throw new InvalidOperationException ("Current state is " + State);
109 tcp_listener.Stop ();
114 protected override void OnOpen (TimeSpan timeout)
116 IPHostEntry entry = Dns.GetHostEntry (listen_uri.Host);
118 if (entry.AddressList.Length ==0)
119 throw new ArgumentException (String.Format ("Invalid listen URI: {0}", listen_uri));
121 int explicitPort = listen_uri.Port;
122 tcp_listener = new TcpListener (entry.AddressList [0], explicitPort <= 0 ? TcpTransportBindingElement.DefaultPort : explicitPort);
123 tcp_listener.Start ();