TcpClientTransportSink.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.cs
  3. //
  4. // Author: Dietmar Maurer ([email protected])
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // 2002 (C) Copyright, Ximian, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Runtime.Remoting.Channels;
  31. using System.Runtime.Remoting.Messaging;
  32. using System.Collections;
  33. using System.IO;
  34. using System.Threading;
  35. namespace System.Runtime.Remoting.Channels.Tcp
  36. {
  37. internal class TcpClientTransportSink : IClientChannelSink
  38. {
  39. string _host;
  40. string _url;
  41. int _port;
  42. public TcpClientTransportSink (string url)
  43. {
  44. string objectUri;
  45. _host = TcpChannel.ParseTcpURL (url, out objectUri, out _port);
  46. _url = url;
  47. }
  48. public IDictionary Properties
  49. {
  50. get
  51. {
  52. return null;
  53. }
  54. }
  55. public IClientChannelSink NextChannelSink
  56. {
  57. get
  58. {
  59. // we are the last one
  60. return null;
  61. }
  62. }
  63. public void AsyncProcessRequest (IClientChannelSinkStack sinkStack, IMessage msg,
  64. ITransportHeaders headers, Stream requestStream)
  65. {
  66. TcpConnection connection = null;
  67. bool isOneWay = RemotingServices.IsOneWay (((IMethodMessage)msg).MethodBase);
  68. try
  69. {
  70. if (headers == null) headers = new TransportHeaders();
  71. headers [CommonTransportKeys.RequestUri] = ((IMethodMessage)msg).Uri;
  72. // Sends the stream using a connection from the pool
  73. // and creates a WorkItem that will wait for the
  74. // response of the server
  75. connection = TcpConnectionPool.GetConnection (_host, _port);
  76. TcpMessageIO.SendMessageStream (connection.Stream, requestStream, headers, connection.Buffer);
  77. if (!isOneWay)
  78. {
  79. sinkStack.Push (this, connection);
  80. ThreadPool.QueueUserWorkItem (new WaitCallback(ReadAsyncTcpMessage), sinkStack);
  81. }
  82. else
  83. connection.Release();
  84. }
  85. catch
  86. {
  87. if (connection != null) connection.Release();
  88. if (!isOneWay) throw;
  89. }
  90. }
  91. private void ReadAsyncTcpMessage(object data)
  92. {
  93. // This method is called by a new thread to asynchronously
  94. // read the response to a request
  95. // The stack was provided as state data in QueueUserWorkItem
  96. IClientChannelSinkStack stack = (IClientChannelSinkStack)data;
  97. // The first sink in the stack is this sink. Pop it and
  98. // get the status data, which is the TcpConnection used to send
  99. // the request
  100. TcpConnection connection = (TcpConnection)stack.Pop(this);
  101. try
  102. {
  103. ITransportHeaders responseHeaders;
  104. // Read the response, blocking if necessary
  105. MessageStatus status = TcpMessageIO.ReceiveMessageStatus (connection.Stream);
  106. if (status != MessageStatus.MethodMessage)
  107. throw new RemotingException ("Unknown response message from server");
  108. Stream responseStream = TcpMessageIO.ReceiveMessageStream (connection.Stream, out responseHeaders, connection.Buffer);
  109. // Free the connection, so it can be reused
  110. connection.Release();
  111. connection = null;
  112. // Ok, proceed with the other sinks
  113. stack.AsyncProcessResponse (responseHeaders, responseStream);
  114. }
  115. catch
  116. {
  117. if (connection != null) connection.Release();
  118. throw;
  119. }
  120. }
  121. public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
  122. object state, ITransportHeaders headers,
  123. Stream stream)
  124. {
  125. // Should never be called
  126. throw new NotSupportedException();
  127. }
  128. public Stream GetRequestStream (IMessage msg, ITransportHeaders headers)
  129. {
  130. return null;
  131. }
  132. public void ProcessMessage (IMessage msg,
  133. ITransportHeaders requestHeaders,
  134. Stream requestStream,
  135. out ITransportHeaders responseHeaders,
  136. out Stream responseStream)
  137. {
  138. TcpConnection connection = null;
  139. try
  140. {
  141. if (requestHeaders == null) requestHeaders = new TransportHeaders();
  142. requestHeaders [CommonTransportKeys.RequestUri] = ((IMethodMessage)msg).Uri;
  143. // Sends the message
  144. connection = TcpConnectionPool.GetConnection (_host, _port);
  145. TcpMessageIO.SendMessageStream (connection.Stream, requestStream, requestHeaders, connection.Buffer);
  146. // Reads the response
  147. MessageStatus status = TcpMessageIO.ReceiveMessageStatus (connection.Stream);
  148. if (status != MessageStatus.MethodMessage)
  149. throw new RemotingException ("Unknown response message from server");
  150. responseStream = TcpMessageIO.ReceiveMessageStream (connection.Stream, out responseHeaders, connection.Buffer);
  151. }
  152. finally
  153. {
  154. if (connection != null)
  155. connection.Release();
  156. }
  157. }
  158. }
  159. }