move to from olive to mcs
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / TcpChannelListener.cs
1 // 
2 // TcpChannelListener.cs
3 // 
4 // Author: 
5 //     Marcos Cobena (marcoscobena@gmail.com)
6 // 
7 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
8 // 
9
10 using System;
11 using System.Collections.Generic;
12 using System.IO;
13 using System.Net;
14 using System.Net.Sockets;
15 using System.ServiceModel.Description;
16 using System.Text;
17
18 namespace System.ServiceModel.Channels
19 {
20         internal class TcpChannelListener<TChannel> : ChannelListenerBase<TChannel> 
21                 where TChannel : class, IChannel
22         {
23                 List<IChannel> channels = new List<IChannel> ();
24                 BindingContext context;
25                 TcpChannelInfo info;
26                 IDuplexSession session;
27                 Uri listen_uri;
28                 TcpListener tcp_listener;
29                 
30                 [MonoTODO]
31                 public TcpChannelListener (TcpTransportBindingElement source, 
32                                            BindingContext context) : base (context.Binding)
33                 {
34                         MessageEncoder encoder = null;
35                         if (context.ListenUriMode == ListenUriMode.Explicit)
36                                 listen_uri =
37                                         context.ListenUriRelativeAddress != null ?
38                                         new Uri (context.ListenUriBaseAddress, context.ListenUriRelativeAddress) :
39                                         context.ListenUriBaseAddress;
40                         else
41                                 throw new NotImplementedException ();
42                         
43                         foreach (BindingElement be in context.RemainingBindingElements) {
44                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
45                                 if (mbe != null) {
46                                         encoder = mbe.CreateMessageEncoderFactory ().Encoder;
47                                         break;
48                                 }
49                         }
50                         
51                         if (encoder == null)
52                                 encoder = new BinaryMessageEncoder ();
53
54                         info = new TcpChannelInfo (source, encoder);
55                 }
56                 
57                 public override Uri Uri {
58                         get { return listen_uri; }
59                 }
60                 
61                 [MonoTODO]
62                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
63                 {
64                         TChannel channel = PopulateChannel (timeout);
65                         channels.Add (channel);
66                         return channel;
67                 }
68                 
69                 TChannel PopulateChannel (TimeSpan timeout)
70                 {
71                         TcpClient cli = tcp_listener.AcceptTcpClient ();
72
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);
76
77                         // FIXME: To implement more.
78                         throw new NotImplementedException ();
79                 }
80
81                 [MonoTODO]
82                 protected override IAsyncResult OnBeginAcceptChannel (TimeSpan timeout,
83                         AsyncCallback callback, object asyncState)
84                 {
85                         throw new NotImplementedException ();
86                 }
87
88                 [MonoTODO]
89                 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
90                 {
91                         throw new NotImplementedException ();
92                 }
93                 
94                 [MonoTODO]
95                 protected override IAsyncResult OnBeginWaitForChannel (
96                         TimeSpan timeout, AsyncCallback callback, object state)
97                 {
98                         throw new NotImplementedException ();
99                 }
100
101                 [MonoTODO]
102                 protected override bool OnEndWaitForChannel (IAsyncResult result)
103                 {
104                         throw new NotImplementedException ();
105                 }
106
107                 [MonoTODO]
108                 protected override bool OnWaitForChannel (TimeSpan timeout)
109                 {
110                         throw new NotImplementedException ();
111                 }
112                 
113                 // CommunicationObject
114                 
115                 [MonoTODO]
116                 protected override void OnAbort ()
117                 {
118                         throw new NotImplementedException ();
119                 }
120
121                 [MonoTODO]
122                 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
123                         AsyncCallback callback, object state)
124                 {
125                         throw new NotImplementedException ();
126                 }
127
128                 [MonoTODO]
129                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
130                         AsyncCallback callback, object state)
131                 {
132                         throw new NotImplementedException ();
133                 }
134
135                 [MonoTODO]
136                 protected override void OnClose (TimeSpan timeout)
137                 {
138                         tcp_listener.Stop ();
139                         tcp_listener = null;
140                 }
141                 
142                 [MonoTODO]
143                 protected override void OnEndClose (IAsyncResult result)
144                 {
145                         throw new NotImplementedException ();
146                 }
147
148                 [MonoTODO]
149                 protected override void OnEndOpen (IAsyncResult result)
150                 {
151                         throw new NotImplementedException ();
152                 }
153                 
154                 [MonoTODO]
155                 protected override void OnOpen (TimeSpan timeout)
156                 {
157                         IPHostEntry entry = Dns.GetHostEntry (listen_uri.Host);
158                         
159                         if (entry.AddressList.Length ==0)
160                                 throw new ArgumentException (String.Format ("Invalid listen URI: {0}", listen_uri));
161                         
162                         int explicitPort = listen_uri.Port;
163                         tcp_listener = new TcpListener (entry.AddressList [0], explicitPort <= 0 ? TcpTransportBindingElement.DefaultPort : explicitPort);
164                         tcp_listener.Start ();
165                 }
166         }
167 }