TcpClientTransportSink.cs 4.0 KB

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