2 // System.Runtime.Remoting.Channels.Tcp.TcpChannel.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;
35 namespace System.Runtime.Remoting.Channels.Tcp
37 public class TcpChannel : IChannelReceiver, IChannel, IChannelSender
39 private TcpClientChannel _clientChannel;
40 private TcpServerChannel _serverChannel = null;
41 private string _name = "tcp";
42 private int _priority = 1;
46 Init (new Hashtable(), null, null);
49 public TcpChannel (int port)
51 Hashtable ht = new Hashtable();
52 ht["port"] = port.ToString();
56 void Init (IDictionary properties, IClientChannelSinkProvider clientSink, IServerChannelSinkProvider serverSink)
58 _clientChannel = new TcpClientChannel (properties,clientSink);
60 if (properties != null) {
61 if(properties["port"] != null)
62 _serverChannel = new TcpServerChannel(properties, serverSink);
64 object val = properties ["name"];
65 if (val != null) _name = val as string;
67 val = properties ["priority"];
68 if (val != null) _priority = Convert.ToInt32 (val);
73 public TcpChannel (IDictionary properties,
74 IClientChannelSinkProvider clientSinkProvider,
75 IServerChannelSinkProvider serverSinkProvider)
77 Init (properties, clientSinkProvider, serverSinkProvider);
80 public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
82 return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);
85 public string ChannelName
90 public int ChannelPriority
92 get { return _priority; }
95 public void StartListening (object data)
97 if (_serverChannel != null) _serverChannel.StartListening (data);
100 public void StopListening (object data)
102 if (_serverChannel != null) _serverChannel.StopListening(data);
103 TcpConnectionPool.Shutdown ();
106 public string[] GetUrlsForUri (string uri)
108 if (_serverChannel != null) return _serverChannel.GetUrlsForUri(uri);
112 public object ChannelData
116 if (_serverChannel != null) return _serverChannel.ChannelData;
121 public string Parse (string url, out string objectURI)
123 return TcpChannel.ParseChannelUrl (url, out objectURI);
126 internal static string ParseChannelUrl (string url, out string objectURI)
128 if (url == null) throw new ArgumentNullException ("url");
132 string host = ParseTcpURL (url, out objectURI, out port);
134 return "tcp://" + host + ":" + port;
139 internal static string ParseTcpURL (string url, out string objectURI, out int port)
141 // format: "tcp://host:port/path/to/object"
146 if (!url.StartsWith ("tcp://")) return null;
147 int colon = url.IndexOf (':', 6);
148 if (colon == -1) return null;
149 string host = url.Substring (6, colon - 6);
151 int slash = url.IndexOf ('/', colon + 1);
152 if (slash == -1) slash = url.Length;
153 string port_str = url.Substring (colon + 1, slash - colon - 1);
155 if (slash < url.Length)
156 objectURI = url.Substring (slash + 1);
159 port = Convert.ToInt32 (port_str);
164 if (objectURI == string.Empty)