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