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;
18 namespace System.ServiceModel.Channels
20 internal class TcpChannelListener<TChannel> : ChannelListenerBase<TChannel>
21 where TChannel : class, IChannel
23 List<IChannel> channels = new List<IChannel> ();
24 BindingContext context;
26 IDuplexSession session;
28 TcpListener tcp_listener;
31 public TcpChannelListener (TcpTransportBindingElement source,
32 BindingContext context) : base (context.Binding)
34 MessageEncoder encoder = null;
35 if (context.ListenUriMode == ListenUriMode.Explicit)
37 context.ListenUriRelativeAddress != null ?
38 new Uri (context.ListenUriBaseAddress, context.ListenUriRelativeAddress) :
39 context.ListenUriBaseAddress;
41 throw new NotImplementedException ();
43 foreach (BindingElement be in context.RemainingBindingElements) {
44 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
46 encoder = mbe.CreateMessageEncoderFactory ().Encoder;
52 encoder = new BinaryMessageEncoder ();
54 info = new TcpChannelInfo (source, encoder);
57 public override Uri Uri {
58 get { return listen_uri; }
62 protected override TChannel OnAcceptChannel (TimeSpan timeout)
64 TChannel channel = PopulateChannel (timeout);
65 channels.Add (channel);
69 TChannel PopulateChannel (TimeSpan timeout)
71 TcpClient cli = tcp_listener.AcceptTcpClient ();
73 // FIXME: pass delegate or something to remove the channel instance from "channels" when it is closed.
74 if (typeof (TChannel) == typeof (IDuplexSessionChannel))
75 return (TChannel) (object) new TcpDuplexSessionChannel (this, info, cli, timeout);
77 // FIXME: To implement more.
78 throw new NotImplementedException ();
82 protected override IAsyncResult OnBeginAcceptChannel (TimeSpan timeout,
83 AsyncCallback callback, object asyncState)
85 throw new NotImplementedException ();
89 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
91 throw new NotImplementedException ();
95 protected override IAsyncResult OnBeginWaitForChannel (
96 TimeSpan timeout, AsyncCallback callback, object state)
98 throw new NotImplementedException ();
102 protected override bool OnEndWaitForChannel (IAsyncResult result)
104 throw new NotImplementedException ();
108 protected override bool OnWaitForChannel (TimeSpan timeout)
110 throw new NotImplementedException ();
113 // CommunicationObject
116 protected override void OnAbort ()
118 throw new NotImplementedException ();
122 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
123 AsyncCallback callback, object state)
125 throw new NotImplementedException ();
129 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
130 AsyncCallback callback, object state)
132 throw new NotImplementedException ();
136 protected override void OnClose (TimeSpan timeout)
138 tcp_listener.Stop ();
143 protected override void OnEndClose (IAsyncResult result)
145 throw new NotImplementedException ();
149 protected override void OnEndOpen (IAsyncResult result)
151 throw new NotImplementedException ();
155 protected override void OnOpen (TimeSpan timeout)
157 IPHostEntry entry = Dns.GetHostEntry (listen_uri.Host);
159 if (entry.AddressList.Length ==0)
160 throw new ArgumentException (String.Format ("Invalid listen URI: {0}", listen_uri));
162 int explicitPort = listen_uri.Port;
163 tcp_listener = new TcpListener (entry.AddressList [0], explicitPort <= 0 ? TcpTransportBindingElement.DefaultPort : explicitPort);
164 tcp_listener.Start ();