TcpChannel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 (): this (0)
  41. {
  42. }
  43. public TcpChannel (int port)
  44. {
  45. Hashtable ht = new Hashtable();
  46. ht["port"] = port.ToString();
  47. Init(ht, null, null);
  48. }
  49. void Init (IDictionary properties, IClientChannelSinkProvider clientSink, IServerChannelSinkProvider serverSink)
  50. {
  51. _clientChannel = new TcpClientChannel (properties,clientSink);
  52. if(properties["port"] != null)
  53. _serverChannel = new TcpServerChannel(properties, serverSink);
  54. object val = properties ["name"];
  55. if (val != null) _name = val as string;
  56. val = properties ["priority"];
  57. if (val != null) _priority = Convert.ToInt32 (val);
  58. }
  59. public TcpChannel (IDictionary properties,
  60. IClientChannelSinkProvider clientSinkProvider,
  61. IServerChannelSinkProvider serverSinkProvider)
  62. {
  63. Init (properties, clientSinkProvider, serverSinkProvider);
  64. }
  65. public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
  66. {
  67. return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);
  68. }
  69. public string ChannelName
  70. {
  71. get { return _name; }
  72. }
  73. public int ChannelPriority
  74. {
  75. get { return _priority; }
  76. }
  77. public void StartListening (object data)
  78. {
  79. if (_serverChannel != null) _serverChannel.StartListening (data);
  80. }
  81. public void StopListening (object data)
  82. {
  83. if (_serverChannel != null) _serverChannel.StopListening(data);
  84. TcpConnectionPool.Shutdown ();
  85. }
  86. public string[] GetUrlsForUri (string uri)
  87. {
  88. if (_serverChannel != null) return _serverChannel.GetUrlsForUri(uri);
  89. else return null;
  90. }
  91. public object ChannelData
  92. {
  93. get
  94. {
  95. if (_serverChannel != null) return _serverChannel.ChannelData;
  96. else return null;
  97. }
  98. }
  99. public string Parse (string url, out string objectURI)
  100. {
  101. return TcpChannel.ParseChannelUrl (url, out objectURI);
  102. }
  103. internal static string ParseChannelUrl (string url, out string objectURI)
  104. {
  105. if (url == null) throw new ArgumentNullException ("url");
  106. int port;
  107. string host = ParseTcpURL (url, out objectURI, out port);
  108. if (host != null)
  109. return "tcp://" + host + ":" + port;
  110. else
  111. return null;
  112. }
  113. internal static string ParseTcpURL (string url, out string objectURI, out int port)
  114. {
  115. // format: "tcp://host:port/path/to/object"
  116. objectURI = null;
  117. port = 0;
  118. Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)/?(.*)");
  119. if (!m.Success)
  120. return null;
  121. string host = m.Groups[1].Value;
  122. string port_str = m.Groups[2].Value;
  123. objectURI = m.Groups[3].Value;
  124. port = Convert.ToInt32 (port_str);
  125. if (objectURI == string.Empty) objectURI = null;
  126. return host;
  127. }
  128. }
  129. }