TcpClientTransportSink.cs 3.9 KB

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