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> : ChannelListenerBase<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 IAsyncResult OnBeginAcceptChannel (TimeSpan timeout,
85 AsyncCallback callback, object asyncState)
87 throw new NotImplementedException ();
91 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
93 throw new NotImplementedException ();
97 protected override IAsyncResult OnBeginWaitForChannel (
98 TimeSpan timeout, AsyncCallback callback, object state)
100 throw new NotImplementedException ();
104 protected override bool OnEndWaitForChannel (IAsyncResult result)
106 throw new NotImplementedException ();
110 protected override bool OnWaitForChannel (TimeSpan timeout)
112 throw new NotImplementedException ();
115 // CommunicationObject
118 protected override void OnAbort ()
120 throw new NotImplementedException ();
124 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
125 AsyncCallback callback, object state)
127 throw new NotImplementedException ();
131 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
132 AsyncCallback callback, object state)
134 throw new NotImplementedException ();
138 protected override void OnClose (TimeSpan timeout)
140 tcp_listener.Stop ();
145 protected override void OnEndClose (IAsyncResult result)
147 throw new NotImplementedException ();
151 protected override void OnEndOpen (IAsyncResult result)
153 throw new NotImplementedException ();
157 protected override void OnOpen (TimeSpan timeout)
159 IPHostEntry entry = Dns.GetHostEntry (listen_uri.Host);
161 if (entry.AddressList.Length ==0)
162 throw new ArgumentException (String.Format ("Invalid listen URI: {0}", listen_uri));
164 int explicitPort = listen_uri.Port;
165 tcp_listener = new TcpListener (entry.AddressList [0], explicitPort <= 0 ? TcpTransportBindingElement.DefaultPort : explicitPort);
166 tcp_listener.Start ();