TcpChannel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpChannel.cs
  3. //
  4. // Author: Rodrigo Moya ([email protected])
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // 2002 (C) Copyright, Ximian, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.Runtime.Remoting.Messaging;
  31. using System.Text.RegularExpressions;
  32. namespace System.Runtime.Remoting.Channels.Tcp
  33. {
  34. public class TcpChannel : IChannelReceiver, IChannel, IChannelSender
  35. {
  36. private TcpClientChannel _clientChannel;
  37. private TcpServerChannel _serverChannel = null;
  38. private string _name = "tcp";
  39. private int _priority = 1;
  40. public TcpChannel ()
  41. {
  42. Init (new Hashtable(), null, null);
  43. }
  44. public TcpChannel (int port)
  45. {
  46. Hashtable ht = new Hashtable();
  47. ht["port"] = port.ToString();
  48. Init(ht, null, null);
  49. }
  50. void Init (IDictionary properties, IClientChannelSinkProvider clientSink, IServerChannelSinkProvider serverSink)
  51. {
  52. _clientChannel = new TcpClientChannel (properties,clientSink);
  53. if(properties["port"] != null)
  54. _serverChannel = new TcpServerChannel(properties, serverSink);
  55. object val = properties ["name"];
  56. if (val != null) _name = val as string;
  57. val = properties ["priority"];
  58. if (val != null) _priority = Convert.ToInt32 (val);
  59. StartListening (null);
  60. }
  61. public TcpChannel (IDictionary properties,
  62. IClientChannelSinkProvider clientSinkProvider,
  63. IServerChannelSinkProvider serverSinkProvider)
  64. {
  65. Init (properties, clientSinkProvider, serverSinkProvider);
  66. }
  67. public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
  68. {
  69. return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);
  70. }
  71. public string ChannelName
  72. {
  73. get { return _name; }
  74. }
  75. public int ChannelPriority
  76. {
  77. get { return _priority; }
  78. }
  79. public void StartListening (object data)
  80. {
  81. if (_serverChannel != null) _serverChannel.StartListening (data);
  82. }
  83. public void StopListening (object data)
  84. {
  85. if (_serverChannel != null) _serverChannel.StopListening(data);
  86. TcpConnectionPool.Shutdown ();
  87. }
  88. public string[] GetUrlsForUri (string uri)
  89. {
  90. if (_serverChannel != null) return _serverChannel.GetUrlsForUri(uri);
  91. else return null;
  92. }
  93. public object ChannelData
  94. {
  95. get
  96. {
  97. if (_serverChannel != null) return _serverChannel.ChannelData;
  98. else return null;
  99. }
  100. }
  101. public string Parse (string url, out string objectURI)
  102. {
  103. return TcpChannel.ParseChannelUrl (url, out objectURI);
  104. }
  105. internal static string ParseChannelUrl (string url, out string objectURI)
  106. {
  107. if (url == null) throw new ArgumentNullException ("url");
  108. int port;
  109. string host = ParseTcpURL (url, out objectURI, out port);
  110. if (host != null)
  111. return "tcp://" + host + ":" + port;
  112. else
  113. return null;
  114. }
  115. internal static string ParseTcpURL (string url, out string objectURI, out int port)
  116. {
  117. // format: "tcp://host:port/path/to/object"
  118. objectURI = null;
  119. port = 0;
  120. if (!url.StartsWith ("tcp://")) return null;
  121. int colon = url.IndexOf (':', 6);
  122. if (colon == -1) return null;
  123. string host = url.Substring (6, colon - 6);
  124. int slash = url.IndexOf ('/', colon + 1);
  125. if (slash == -1) slash = url.Length;
  126. string port_str = url.Substring (colon + 1, slash - colon - 1);
  127. if (slash < url.Length)
  128. objectURI = url.Substring (slash + 1);
  129. try {
  130. port = Convert.ToInt32 (port_str);
  131. } catch {
  132. return null;
  133. }
  134. if (objectURI == string.Empty)
  135. objectURI = null;
  136. return host;
  137. }
  138. }
  139. }