TcpServerTransportSink.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpServerTransportSink.cs
  3. //
  4. // Author: Rodrigo Moya ([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.Collections;
  31. using System.Runtime.Remoting.Messaging;
  32. using System.Net.Sockets;
  33. using System.IO;
  34. namespace System.Runtime.Remoting.Channels.Tcp
  35. {
  36. internal class TcpServerTransportSink : IServerChannelSink, IChannelSinkBase
  37. {
  38. IServerChannelSink next_sink;
  39. public TcpServerTransportSink (IServerChannelSink next)
  40. {
  41. next_sink = next;
  42. }
  43. public IServerChannelSink NextChannelSink
  44. {
  45. get
  46. {
  47. return next_sink;
  48. }
  49. }
  50. public IDictionary Properties
  51. {
  52. get
  53. {
  54. if (next_sink != null) return next_sink.Properties;
  55. else return null;
  56. }
  57. }
  58. public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
  59. IMessage msg, ITransportHeaders headers, Stream responseStream)
  60. {
  61. ClientConnection connection = (ClientConnection)state;
  62. NetworkStream ns = new NetworkStream (connection.Socket);
  63. TcpMessageIO.SendMessageStream (ns, responseStream, headers, connection.Buffer);
  64. ns.Flush ();
  65. ns.Close ();
  66. }
  67. public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
  68. IMessage msg, ITransportHeaders headers)
  69. {
  70. return null;
  71. }
  72. public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
  73. IMessage requestMsg,
  74. ITransportHeaders requestHeaders,
  75. Stream requestStream,
  76. out IMessage responseMsg,
  77. out ITransportHeaders responseHeaders,
  78. out Stream responseStream)
  79. {
  80. // this is the first sink, and TcpServerChannel does not call it.
  81. throw new NotSupportedException ();
  82. }
  83. internal void InternalProcessMessage (ClientConnection connection, Stream stream)
  84. {
  85. // Reads the headers and the request stream
  86. Stream requestStream;
  87. ITransportHeaders requestHeaders;
  88. requestStream = TcpMessageIO.ReceiveMessageStream (stream, out requestHeaders, connection.Buffer);
  89. requestHeaders [CommonTransportKeys.IPAddress] = connection.ClientAddress;
  90. requestHeaders [CommonTransportKeys.ConnectionId] = connection.Id;
  91. string uri = (string) requestHeaders [CommonTransportKeys.RequestUri];
  92. TcpChannel.ParseChannelUrl (uri, out uri);
  93. if (uri != null)
  94. requestHeaders [CommonTransportKeys.RequestUri] = uri;
  95. // Pushes the connection object together with the sink. This information
  96. // will be used for sending the response in an async call.
  97. ServerChannelSinkStack sinkStack = new ServerChannelSinkStack();
  98. sinkStack.Push(this, connection);
  99. ITransportHeaders responseHeaders;
  100. Stream responseStream;
  101. IMessage responseMsg;
  102. ServerProcessing proc = next_sink.ProcessMessage(sinkStack, null, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
  103. switch (proc)
  104. {
  105. case ServerProcessing.Complete:
  106. TcpMessageIO.SendMessageStream (stream, responseStream, responseHeaders, connection.Buffer);
  107. stream.Flush ();
  108. break;
  109. case ServerProcessing.Async:
  110. case ServerProcessing.OneWay:
  111. break;
  112. }
  113. }
  114. }
  115. }