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
;
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();
53 Init (ht
, null, null);
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"];
66 _name
= val
as string;
68 val
= properties
["priority"];
70 _priority
= Convert
.ToInt32 (val
);
75 public TcpChannel (IDictionary properties
,
76 IClientChannelSinkProvider clientSinkProvider
,
77 IServerChannelSinkProvider serverSinkProvider
)
79 Init (properties
, clientSinkProvider
, serverSinkProvider
);
82 public IMessageSink
CreateMessageSink(string url
, object remoteChannelData
, out string objectURI
)
84 return _clientChannel
.CreateMessageSink(url
,
85 remoteChannelData
, out objectURI
);
88 public string ChannelName
{
92 public int ChannelPriority
{
93 get { return _priority; }
96 public void StartListening (object data
)
98 if (_serverChannel
!= null)
99 _serverChannel
.StartListening (data
);
102 public void StopListening (object data
)
104 if (_serverChannel
!= null)
105 _serverChannel
.StopListening(data
);
106 TcpConnectionPool
.Shutdown ();
109 public string [] GetUrlsForUri (string objectURI
)
111 if (_serverChannel
!= null)
112 return _serverChannel
.GetUrlsForUri (objectURI
);
116 public object ChannelData
{
118 if (_serverChannel
!= null)
119 return _serverChannel
.ChannelData
;
124 public string Parse (string url
, out string objectURI
)
126 return TcpChannel
.ParseChannelUrl (url
, out objectURI
);
129 internal static string ParseChannelUrl (string url
, out string objectURI
)
132 throw new ArgumentNullException ("url");
136 return ParseTcpURL (url
, out host
, out port
, out objectURI
);
139 internal static string ParseTcpURL (string url
, out string host
, out string port
, out string objectURI
)
141 // format: "tcp://host:port/path/to/object"
146 // url needs to be at least "tcp:"
147 if (url
.Length
< 4 || url
[3] != ':' ||
148 (url
[0] != 'T' && url
[0] != 't') ||
149 (url
[1] != 'C' && url
[1] != 'c') ||
150 (url
[2] != 'P' && url
[2] != 'p'))
153 // "tcp:" is acceptable
157 // must be of the form "tcp://"
158 if (url
.Length
<= 5 || url
[4] != '/' || url
[5] != '/')
161 // "tcp://" is acceptable
166 for (i
= 6; i
< url
.Length
; i
++) {
167 if (url
[i
] == ':' || url
[i
] == '/')
171 host
= url
.Substring (6, i
- 6);
173 if (i
+ 1 < url
.Length
&& url
[i
] == ':') {
176 for (i
++; i
< url
.Length
; i
++) {
182 port
= url
.Substring (start
, i
- start
);
185 if (i
>= url
.Length
|| url
[i
] != '/')
188 objectURI
= url
.Substring (i
);
190 return url
.Substring (0, i
);