2 // System.Runtime.Remoting.Channels.Tcp.TcpChannel.cs
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 // Lluis Sanchez Gual (lsg@ctv.es)
7 // 2002 (C) Copyright, Ximian, Inc.
10 using System.Collections;
11 using System.Runtime.Remoting.Messaging;
12 using System.Text.RegularExpressions;
14 namespace System.Runtime.Remoting.Channels.Tcp
16 public class TcpChannel : IChannelReceiver, IChannel, IChannelSender
18 private TcpClientChannel _clientChannel;
\r
19 private TcpServerChannel _serverChannel = null;
\r
20 private string _name;
\r
22 public TcpChannel (): this (0)
26 public TcpChannel (int port)
28 Hashtable ht = new Hashtable();
\r
29 ht["port"] = port.ToString();
\r
30 Init(ht, null, null);
\r
33 public void Init(IDictionary properties, IClientChannelSinkProvider clientSink, IServerChannelSinkProvider serverSink)
\r
35 _clientChannel = new TcpClientChannel(properties,clientSink);
\r
37 string port = properties["port"] as string;
\r
38 if (port != null && port != string.Empty)
\r
40 _serverChannel = new TcpServerChannel(properties, serverSink);
\r
43 _name = properties["name"] as string;
\r
47 public TcpChannel (IDictionary properties,
48 IClientChannelSinkProvider clientSinkProvider,
49 IServerChannelSinkProvider serverSinkProvider)
51 Init (properties, clientSinkProvider, serverSinkProvider);
54 public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
\r
56 return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);
\r
59 public string ChannelName
\r
61 get { return _name; }
\r
64 public int ChannelPriority
\r
69 public void StartListening (object data)
\r
71 if (_serverChannel != null) _serverChannel.StartListening(data);
\r
74 public void StopListening (object data)
\r
76 if (_serverChannel != null) _serverChannel.StopListening(data);
\r
79 public string[] GetUrlsForUri (string uri)
\r
81 if (_serverChannel != null) return _serverChannel.GetUrlsForUri(uri);
\r
85 public object ChannelData
\r
89 if (_serverChannel != null) return _serverChannel.ChannelData;
\r
94 public string Parse (string url, out string objectURI)
96 return TcpChannel.ParseChannelUrl (url, out objectURI);
99 internal static string ParseChannelUrl (string url, out string objectURI)
103 string host = ParseTcpURL (url, out objectURI, out port);
105 return "tcp://" + host + ":" + port;
110 internal static string ParseTcpURL (string url, out string objectURI, out int port)
112 // format: "tcp://host:port/path/to/object"
117 Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)[/](.*)");
122 string host = m.Groups[1].Value;
123 string port_str = m.Groups[2].Value;
124 objectURI = m.Groups[3].Value;
125 port = Convert.ToInt32 (port_str);