TcpChannel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. }
  60. public TcpChannel (IDictionary properties,
  61. IClientChannelSinkProvider clientSinkProvider,
  62. IServerChannelSinkProvider serverSinkProvider)
  63. {
  64. Init (properties, clientSinkProvider, serverSinkProvider);
  65. }
  66. public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
  67. {
  68. return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);
  69. }
  70. public string ChannelName
  71. {
  72. get { return _name; }
  73. }
  74. public int ChannelPriority
  75. {
  76. get { return _priority; }
  77. }
  78. public void StartListening (object data)
  79. {
  80. if (_serverChannel != null) _serverChannel.StartListening (data);
  81. }
  82. public void StopListening (object data)
  83. {
  84. if (_serverChannel != null) _serverChannel.StopListening(data);
  85. TcpConnectionPool.Shutdown ();
  86. }
  87. public string[] GetUrlsForUri (string uri)
  88. {
  89. if (_serverChannel != null) return _serverChannel.GetUrlsForUri(uri);
  90. else return null;
  91. }
  92. public object ChannelData
  93. {
  94. get
  95. {
  96. if (_serverChannel != null) return _serverChannel.ChannelData;
  97. else return null;
  98. }
  99. }
  100. public string Parse (string url, out string objectURI)
  101. {
  102. return TcpChannel.ParseChannelUrl (url, out objectURI);
  103. }
  104. internal static string ParseChannelUrl (string url, out string objectURI)
  105. {
  106. if (url == null) throw new ArgumentNullException ("url");
  107. int port;
  108. string host = ParseTcpURL (url, out objectURI, out port);
  109. if (host != null)
  110. return "tcp://" + host + ":" + port;
  111. else
  112. return null;
  113. }
  114. internal static string ParseTcpURL (string url, out string objectURI, out int port)
  115. {
  116. // format: "tcp://host:port/path/to/object"
  117. objectURI = null;
  118. port = 0;
  119. if (!url.StartsWith ("tcp://")) return null;
  120. int colon = url.IndexOf (':', 6);
  121. if (colon == -1) return null;
  122. string host = url.Substring (6, colon - 6);
  123. int slash = url.IndexOf ('/', colon + 1);
  124. if (slash == -1) slash = url.Length;
  125. string port_str = url.Substring (colon + 1, slash - colon - 1);
  126. if (slash < url.Length)
  127. objectURI = url.Substring (slash + 1);
  128. try {
  129. port = Convert.ToInt32 (port_str);
  130. } catch {
  131. return null;
  132. }
  133. if (objectURI == string.Empty)
  134. objectURI = null;
  135. return host;
  136. }
  137. }
  138. }