2 // System.Runtime.Remoting.Channels.Tcp.TcpServerChannel.cs
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 // Lluis Sanchez Gual (lluis@ideary.com)
7 // 2002 (C) Copyright, Ximian, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Runtime.Remoting.Messaging;
33 using System.Text.RegularExpressions;
34 using System.Net.Sockets;
36 using System.Threading;
38 using System.Runtime.Remoting.Channels;
40 namespace System.Runtime.Remoting.Channels.Tcp
42 public class TcpServerChannel : IChannelReceiver, IChannel
48 bool supressChannelData = false;
49 bool useIpAddress = true;
51 IPAddress bindAddress = IPAddress.Any;
52 Thread server_thread = null;
54 TcpServerTransportSink sink;
55 ChannelDataStore channel_data;
57 RemotingThreadPool threadPool;
59 void Init (IServerChannelSinkProvider serverSinkProvider)
61 if (serverSinkProvider == null)
\r
63 serverSinkProvider = new BinaryServerFormatterSinkProvider ();
69 if (!bindAddress.Equals(IPAddress.Any)) host = bindAddress.ToString ();
71 IPHostEntry he = Dns.Resolve (Dns.GetHostName());
72 if (he.AddressList.Length == 0) throw new RemotingException ("IP address could not be determined for this host");
73 host = he.AddressList [0].ToString ();
77 host = Dns.GetHostByName(Dns.GetHostName()).HostName;
80 // Gets channel data from the chain of channel providers
82 channel_data = new ChannelDataStore (null);
83 IServerChannelSinkProvider provider = serverSinkProvider;
\r
84 while (provider != null)
\r
86 provider.GetChannelData(channel_data);
\r
87 provider = provider.Next;
\r
90 // Creates the sink chain that will process all incoming messages
92 IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (serverSinkProvider, this);
\r
93 sink = new TcpServerTransportSink (next_sink);
96 public TcpServerChannel (int port)
102 public TcpServerChannel (IDictionary properties,
103 IServerChannelSinkProvider serverSinkProvider)
105 foreach(DictionaryEntry property in properties)
107 switch((string)property.Key)
\r
110 name = property.Value.ToString();
113 port = Convert.ToInt32(property.Value);
116 priority = Convert.ToInt32(property.Value);
119 bindAddress = IPAddress.Parse((string)property.Value);
121 case "rejectRemoteRequests":
\r
122 if(Convert.ToBoolean(properties["rejectRemoteRequests"]))
123 bindAddress = IPAddress.Loopback;
125 case "supressChannelData":
126 supressChannelData = Convert.ToBoolean (property.Value);
129 useIpAddress = Convert.ToBoolean (property.Value);
132 host = property.Value as string;
136 Init (serverSinkProvider);
139 public TcpServerChannel (string name, int port,
140 IServerChannelSinkProvider serverSinkProvider)
144 Init (serverSinkProvider);
147 public TcpServerChannel (string name, int port)
154 public object ChannelData
157 if (supressChannelData) return null;
158 else return channel_data;
162 public string ChannelName
169 public int ChannelPriority
176 public string GetChannelUri ()
178 return "tcp://" + host + ":" + port;
181 public string[] GetUrlsForUri (string uri)
\r
183 if (!uri.StartsWith ("/")) uri = "/" + uri;
185 string [] chnl_uris = channel_data.ChannelUris;
\r
186 string [] result = new String [chnl_uris.Length];
\r
188 for (int i = 0; i < chnl_uris.Length; i++)
\r
189 result [i] = chnl_uris [i] + uri;
\r
194 public string Parse (string url, out string objectURI)
196 return TcpChannel.ParseChannelUrl (url, out objectURI);
199 void WaitForConnections ()
205 Socket socket = listener.AcceptSocket ();
206 ClientConnection reader = new ClientConnection (this, socket, sink);
208 if (!threadPool.RunThread (new ThreadStart (reader.ProcessMessages)))
217 public void StartListening (object data)
219 listener = new TcpListener (bindAddress, port);
220 if (server_thread == null)
\r
222 threadPool = RemotingThreadPool.GetSharedPool ();
226 port = ((IPEndPoint)listener.LocalEndpoint).Port;
228 string[] uris = new String [1];
229 uris = new String [1];
230 uris [0] = GetChannelUri ();
231 channel_data.ChannelUris = uris;
233 server_thread = new Thread (new ThreadStart (WaitForConnections));
234 server_thread.IsBackground = true;
235 server_thread.Start ();
239 public void StopListening (object data)
241 if (server_thread == null) return;
243 server_thread.Abort ();
246 server_thread.Join ();
247 server_thread = null;
251 class ClientConnection
256 TcpServerTransportSink _sink;
259 byte[] _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
261 public ClientConnection (TcpServerChannel serverChannel, Socket socket, TcpServerTransportSink sink)
270 get { return _stream; }
275 get { return _buffer; }
278 public void ProcessMessages()
280 byte[] buffer = new byte[256];
281 NetworkStream ns = new NetworkStream (_socket);
282 _stream = new BufferedStream (ns);
289 MessageStatus type = TcpMessageIO.ReceiveMessageStatus (_stream, buffer);
293 case MessageStatus.MethodMessage:
294 _sink.InternalProcessMessage (this);
297 case MessageStatus.Unknown:
298 case MessageStatus.CancelSignal:
307 // Console.WriteLine (ex);
324 public IPAddress ClientAddress
327 IPEndPoint ep = _socket.RemoteEndPoint as IPEndPoint;
328 if (ep != null) return ep.Address;