TcpChannel.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. string port = properties["port"] as string;
  32. if (port != null && port != string.Empty)
  33. {
  34. _serverChannel = new TcpServerChannel(properties, serverSink);
  35. }
  36. _name = properties["name"] as string;
  37. }
  38. public TcpChannel (IDictionary properties,
  39. IClientChannelSinkProvider clientSinkProvider,
  40. IServerChannelSinkProvider serverSinkProvider)
  41. {
  42. Init (properties, clientSinkProvider, serverSinkProvider);
  43. }
  44. public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
  45. {
  46. return _clientChannel.CreateMessageSink(url, remoteChannelData, out objectURI);
  47. }
  48. public string ChannelName
  49. {
  50. get { return _name; }
  51. }
  52. public int ChannelPriority
  53. {
  54. get { return 1; }
  55. }
  56. public void StartListening (object data)
  57. {
  58. if (_serverChannel != null) _serverChannel.StartListening(data);
  59. }
  60. public void StopListening (object data)
  61. {
  62. if (_serverChannel != null) _serverChannel.StopListening(data);
  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. return host;
  103. }
  104. }
  105. }