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;
44 public TcpChannel (): this (0)
48 public TcpChannel (int port
)
50 Hashtable ht
= new Hashtable();
51 ht
["port"] = port
.ToString();
55 void Init (IDictionary properties
, IClientChannelSinkProvider clientSink
, IServerChannelSinkProvider serverSink
)
57 _clientChannel
= new TcpClientChannel (properties
,clientSink
);
59 if(properties
["port"] != null)
60 _serverChannel
= new TcpServerChannel(properties
, serverSink
);
62 object val
= properties
["name"];
63 if (val
!= null) _name
= val
as string;
65 val
= properties
["priority"];
66 if (val
!= null) _priority
= Convert
.ToInt32 (val
);
70 public TcpChannel (IDictionary properties
,
71 IClientChannelSinkProvider clientSinkProvider
,
72 IServerChannelSinkProvider serverSinkProvider
)
74 Init (properties
, clientSinkProvider
, serverSinkProvider
);
77 public IMessageSink
CreateMessageSink(string url
, object remoteChannelData
, out string objectURI
)
79 return _clientChannel
.CreateMessageSink(url
, remoteChannelData
, out objectURI
);
82 public string ChannelName
87 public int ChannelPriority
89 get { return _priority; }
92 public void StartListening (object data
)
94 if (_serverChannel
!= null) _serverChannel
.StartListening (data
);
97 public void StopListening (object data
)
99 if (_serverChannel
!= null) _serverChannel
.StopListening(data
);
100 TcpConnectionPool
.Shutdown ();
103 public string[] GetUrlsForUri (string uri
)
105 if (_serverChannel
!= null) return _serverChannel
.GetUrlsForUri(uri
);
109 public object ChannelData
113 if (_serverChannel
!= null) return _serverChannel
.ChannelData
;
118 public string Parse (string url
, out string objectURI
)
120 return TcpChannel
.ParseChannelUrl (url
, out objectURI
);
123 internal static string ParseChannelUrl (string url
, out string objectURI
)
125 if (url
== null) throw new ArgumentNullException ("url");
129 string host
= ParseTcpURL (url
, out objectURI
, out port
);
131 return "tcp://" + host
+ ":" + port
;
136 internal static string ParseTcpURL (string url
, out string objectURI
, out int port
)
138 // format: "tcp://host:port/path/to/object"
143 Match m
= Regex
.Match (url
, "tcp://([^:]+):([0-9]+)/?(.*)");
148 string host
= m
.Groups
[1].Value
;
149 string port_str
= m
.Groups
[2].Value
;
150 objectURI
= m
.Groups
[3].Value
;
151 port
= Convert
.ToInt32 (port_str
);
153 if (objectURI
== string.Empty
) objectURI
= null;