TcpClientTransportSink.cs 4.3 KB

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