TcpChannel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. using System.Collections;
  10. using System.Runtime.Remoting.Messaging;
  11. using System.Text.RegularExpressions;
  12. namespace System.Runtime.Remoting.Channels.Tcp
  13. {
  14. public class TcpChannel : IChannelReceiver, IChannel, IChannelSender
  15. {
  16. private TcpClientChannel _clientChannel;
  17. private TcpServerChannel _serverChannel = null;
  18. private string _name = "tcp";
  19. private int _priority = 1;
  20. public TcpChannel (): this (0)
  21. {
  22. }
  23. public TcpChannel (int port)
  24. {
  25. Hashtable ht = new Hashtable();
  26. ht["port"] = port.ToString();
  27. Init(ht, null, null);
  28. }
  29. public void Init (IDictionary properties, IClientChannelSinkProvider clientSink, IServerChannelSinkProvider serverSink)
  30. {
  31. _clientChannel = new TcpClientChannel (properties,clientSink);
  32. if(properties["port"] != null)
  33. _serverChannel = new TcpServerChannel(properties, serverSink);
  34. object val = properties ["name"];
  35. if (val != null) _name = val as string;
  36. val = properties ["priority"];
  37. if (val != null) _priority = Convert.ToInt32 (val);
  38. }
  39. public TcpChannel (IDictionary properties,
  40. IClientChannelSinkProvider clientSinkProvider,
  41. IServerChannelSinkProvider serverSinkProvider)
  42. {
  43. Init (properties, clientSinkProvider, serverSinkProvider);
  44. }
  45. public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
  46. {
  47. return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);
  48. }
  49. public string ChannelName
  50. {
  51. get { return _name; }
  52. }
  53. public int ChannelPriority
  54. {
  55. get { return _priority; }
  56. }
  57. public void StartListening (object data)
  58. {
  59. if (_serverChannel != null) _serverChannel.StartListening (data);
  60. }
  61. public void StopListening (object data)
  62. {
  63. if (_serverChannel != null) _serverChannel.StopListening(data);
  64. TcpConnectionPool.Shutdown ();
  65. }
  66. public string[] GetUrlsForUri (string uri)
  67. {
  68. if (_serverChannel != null) return _serverChannel.GetUrlsForUri(uri);
  69. else return null;
  70. }
  71. public object ChannelData
  72. {
  73. get
  74. {
  75. if (_serverChannel != null) return _serverChannel.ChannelData;
  76. else return null;
  77. }
  78. }
  79. public string Parse (string url, out string objectURI)
  80. {
  81. return TcpChannel.ParseChannelUrl (url, out objectURI);
  82. }
  83. internal static string ParseChannelUrl (string url, out string objectURI)
  84. {
  85. if (url == null) throw new ArgumentNullException ("url");
  86. int port;
  87. string host = ParseTcpURL (url, out objectURI, out port);
  88. if (host != null)
  89. return "tcp://" + host + ":" + port;
  90. else
  91. return null;
  92. }
  93. internal static string ParseTcpURL (string url, out string objectURI, out int port)
  94. {
  95. // format: "tcp://host:port/path/to/object"
  96. objectURI = null;
  97. port = 0;
  98. Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)/?(.*)");
  99. if (!m.Success)
  100. return null;
  101. string host = m.Groups[1].Value;
  102. string port_str = m.Groups[2].Value;
  103. objectURI = m.Groups[3].Value;
  104. port = Convert.ToInt32 (port_str);
  105. if (objectURI == string.Empty) objectURI = null;
  106. return host;
  107. }
  108. }
  109. }