TcpClientChannel.cs 4.1 KB

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