TcpClientTransportSink.cs 4.1 KB

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