TcpClientChannel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpClientChannel.cs
  3. //
  4. // Author: Dietmar Maurer ([email protected])
  5. //
  6. // 2002 (C) Copyright, Ximian, Inc.
  7. //
  8. using System.Collections;
  9. using System.IO;
  10. using System.Runtime.Remoting.Messaging;
  11. using System.Runtime.Remoting.Channels;
  12. using System.Text.RegularExpressions;
  13. namespace System.Runtime.Remoting.Channels.Tcp
  14. {
  15. public class TcpClientTransportSink : IClientChannelSink
  16. {
  17. public TcpClientTransportSink (string url)
  18. {
  19. }
  20. public IDictionary Properties
  21. {
  22. get {
  23. return null;
  24. }
  25. }
  26. public IClientChannelSink NextChannelSink
  27. {
  28. get {
  29. // we are the last one
  30. return null;
  31. }
  32. }
  33. public void AsyncProcessRequest (IClientChannelSinkStack sinkStack, IMessage msg,
  34. ITransportHeaders headers, Stream stream)
  35. {
  36. throw new NotImplementedException ();
  37. }
  38. public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
  39. object state, ITransportHeaders headers,
  40. Stream stream)
  41. {
  42. throw new NotImplementedException ();
  43. }
  44. public Stream GetRequestStream (IMessage msg, ITransportHeaders headers)
  45. {
  46. // no acces to stream?
  47. return null;
  48. }
  49. public void ProcessMessage (IMessage msg,
  50. ITransportHeaders requestHeaders,
  51. Stream requestStream,
  52. out ITransportHeaders responseHeaders,
  53. out Stream responseStream)
  54. {
  55. throw new NotImplementedException ();
  56. }
  57. }
  58. public class TcpClientTransportSinkProvider : IClientChannelSinkProvider
  59. {
  60. public TcpClientTransportSinkProvider ()
  61. {
  62. // what should we do here ?
  63. }
  64. public IClientChannelSinkProvider Next
  65. {
  66. get {
  67. return null;
  68. }
  69. set {
  70. // ignore, we are always the last in the chain
  71. }
  72. }
  73. public IClientChannelSink CreateSink (IChannelSender channel, string url,
  74. object remoteChannelData)
  75. {
  76. return new TcpClientTransportSink (url);
  77. }
  78. }
  79. public class TcpClientChannel : IChannelSender, IChannel
  80. {
  81. int priority = 1;
  82. string name = "tcp";
  83. IClientChannelSinkProvider sink_provider;
  84. public TcpClientChannel ()
  85. {
  86. sink_provider = new BinaryClientFormatterSinkProvider ();
  87. sink_provider.Next = new TcpClientTransportSinkProvider ();
  88. }
  89. public TcpClientChannel (IDictionary properties, IClientChannelSinkProvider sinkProvider)
  90. {
  91. priority = 1;
  92. sink_provider = sinkProvider;
  93. // add the tcp provider at the end of the chain
  94. IClientChannelSinkProvider prov = sinkProvider;
  95. while (prov.Next != null) prov = prov.Next;
  96. prov.Next = new TcpClientTransportSinkProvider ();
  97. }
  98. public TcpClientChannel (string name, IClientChannelSinkProvider sinkProvider)
  99. {
  100. priority = 1;
  101. this.name = name;
  102. sink_provider = sinkProvider;
  103. // add the tcp provider at the end of the chain
  104. IClientChannelSinkProvider prov = sinkProvider;
  105. while (prov.Next != null) prov = prov.Next;
  106. prov.Next = new TcpClientTransportSinkProvider ();
  107. }
  108. public string ChannelName
  109. {
  110. get {
  111. return name;
  112. }
  113. }
  114. public int ChannelPriority
  115. {
  116. get {
  117. return priority;
  118. }
  119. }
  120. public IMessageSink CreateMessageSink (string url,
  121. object remoteChannelData,
  122. out string objectURI)
  123. {
  124. if (url == null && remoteChannelData != null) {
  125. IChannelDataStore ds = remoteChannelData as IChannelDataStore;
  126. if (ds != null)
  127. url = ds.ChannelUris [0];
  128. }
  129. if (Parse (url, out objectURI) == null)
  130. return null;
  131. return (IMessageSink) sink_provider.CreateSink (this, url, remoteChannelData);
  132. }
  133. public string Parse (string url, out string objectURI)
  134. {
  135. // format: "tcp://host:port/path/to/object"
  136. objectURI = null;
  137. Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)(/.*)");
  138. if (!m.Success)
  139. return null;
  140. string host = m.Groups[1].Value;
  141. string port = m.Groups[2].Value;
  142. objectURI = m.Groups[3].Value;
  143. return "tcp://" + host + ":" + port;
  144. }
  145. }
  146. }