TcpChannel.cs 4.6 KB

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