TcpClientTransportSink.cs 5.5 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. int _port;
  41. public TcpClientTransportSink (string url)
  42. {
  43. string objectUri;
  44. _host = TcpChannel.ParseTcpURL (url, out objectUri, out _port);
  45. }
  46. public IDictionary Properties
  47. {
  48. get
  49. {
  50. return null;
  51. }
  52. }
  53. public IClientChannelSink NextChannelSink
  54. {
  55. get
  56. {
  57. // we are the last one
  58. return null;
  59. }
  60. }
  61. public void AsyncProcessRequest (IClientChannelSinkStack sinkStack, IMessage msg,
  62. ITransportHeaders headers, Stream requestStream)
  63. {
  64. TcpConnection connection = null;
  65. bool isOneWay = RemotingServices.IsOneWay (((IMethodMessage)msg).MethodBase);
  66. try
  67. {
  68. if (headers == null) headers = new TransportHeaders();
  69. headers [CommonTransportKeys.RequestUri] = ((IMethodMessage)msg).Uri;
  70. // Sends the stream using a connection from the pool
  71. // and creates a WorkItem that will wait for the
  72. // response of the server
  73. connection = TcpConnectionPool.GetConnection (_host, _port);
  74. TcpMessageIO.SendMessageStream (connection.Stream, requestStream, headers, connection.Buffer);
  75. connection.Stream.Flush ();
  76. if (!isOneWay)
  77. {
  78. sinkStack.Push (this, connection);
  79. ThreadPool.QueueUserWorkItem (new WaitCallback(ReadAsyncTcpMessage), sinkStack);
  80. }
  81. else
  82. connection.Release();
  83. }
  84. catch
  85. {
  86. if (connection != null) connection.Release();
  87. if (!isOneWay) throw;
  88. }
  89. }
  90. private void ReadAsyncTcpMessage(object data)
  91. {
  92. // This method is called by a new thread to asynchronously
  93. // read the response to a request
  94. // The stack was provided as state data in QueueUserWorkItem
  95. IClientChannelSinkStack stack = (IClientChannelSinkStack)data;
  96. // The first sink in the stack is this sink. Pop it and
  97. // get the status data, which is the TcpConnection used to send
  98. // the request
  99. TcpConnection connection = (TcpConnection)stack.Pop(this);
  100. try
  101. {
  102. ITransportHeaders responseHeaders;
  103. // Read the response, blocking if necessary
  104. MessageStatus status = TcpMessageIO.ReceiveMessageStatus (connection.Stream, connection.Buffer);
  105. if (status != MessageStatus.MethodMessage)
  106. throw new RemotingException ("Unknown response message from server");
  107. Stream responseStream = TcpMessageIO.ReceiveMessageStream (connection.Stream, out responseHeaders, connection.Buffer);
  108. // Free the connection, so it can be reused
  109. connection.Release();
  110. connection = null;
  111. // Ok, proceed with the other sinks
  112. stack.AsyncProcessResponse (responseHeaders, responseStream);
  113. }
  114. catch
  115. {
  116. if (connection != null) connection.Release();
  117. throw;
  118. }
  119. }
  120. public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
  121. object state, ITransportHeaders headers,
  122. Stream stream)
  123. {
  124. // Should never be called
  125. throw new NotSupportedException();
  126. }
  127. public Stream GetRequestStream (IMessage msg, ITransportHeaders headers)
  128. {
  129. return null;
  130. }
  131. public void ProcessMessage (IMessage msg,
  132. ITransportHeaders requestHeaders,
  133. Stream requestStream,
  134. out ITransportHeaders responseHeaders,
  135. out Stream responseStream)
  136. {
  137. TcpConnection connection = null;
  138. try
  139. {
  140. if (requestHeaders == null) requestHeaders = new TransportHeaders();
  141. requestHeaders [CommonTransportKeys.RequestUri] = ((IMethodMessage)msg).Uri;
  142. // Sends the message
  143. connection = TcpConnectionPool.GetConnection (_host, _port);
  144. TcpMessageIO.SendMessageStream (connection.Stream, requestStream, requestHeaders, connection.Buffer);
  145. connection.Stream.Flush ();
  146. // Reads the response
  147. MessageStatus status = TcpMessageIO.ReceiveMessageStatus (connection.Stream, connection.Buffer);
  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. }