TcpChannel.cs 3.1 KB

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