BinaryServerFormatterSink.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // System.Runtime.Remoting.Channels.BinaryServerFormatterSink.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // 2002 (C) Copyright, Ximian, Inc.
  8. //
  9. using System.Collections;
  10. using System.IO;
  11. using System.Runtime.Remoting.Messaging;
  12. using System.Runtime.Serialization;
  13. using System.Runtime.Serialization.Formatters.Binary;
  14. namespace System.Runtime.Remoting.Channels {
  15. public class BinaryServerFormatterSink : IServerChannelSink, IChannelSinkBase
  16. {
  17. [Serializable]
  18. public enum Protocol
  19. {
  20. Http = 0,
  21. Other = 1,
  22. }
  23. static BinaryFormatter _serializationFormatter;
  24. static BinaryFormatter _deserializationFormatter;
  25. IServerChannelSink next_sink;
  26. Protocol protocol;
  27. IChannelReceiver receiver;
  28. static BinaryServerFormatterSink()
  29. {
  30. RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
  31. StreamingContext context = new StreamingContext(StreamingContextStates.Remoting, null);
  32. _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
  33. _deserializationFormatter = new BinaryFormatter (null, context);
  34. }
  35. public BinaryServerFormatterSink (BinaryServerFormatterSink.Protocol protocol,
  36. IServerChannelSink nextSink,
  37. IChannelReceiver receiver)
  38. {
  39. this.protocol = protocol;
  40. this.next_sink = nextSink;
  41. this.receiver = receiver;
  42. }
  43. public IServerChannelSink NextChannelSink {
  44. get {
  45. return next_sink;
  46. }
  47. }
  48. public IDictionary Properties {
  49. get {
  50. return null;
  51. }
  52. }
  53. public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
  54. IMessage message, ITransportHeaders headers, Stream stream)
  55. {
  56. ITransportHeaders responseHeaders = new TransportHeaders();
  57. if (sinkStack != null) stream = sinkStack.GetResponseStream (message, responseHeaders);
  58. if (stream == null) stream = new MemoryStream();
  59. _serializationFormatter.Serialize (stream, message, null);
  60. if (stream is MemoryStream) stream.Position = 0;
  61. sinkStack.AsyncProcessResponse (message, responseHeaders, stream);
  62. }
  63. public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
  64. IMessage msg, ITransportHeaders headers)
  65. {
  66. return null;
  67. }
  68. public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
  69. IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
  70. out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
  71. {
  72. sinkStack.Push (this, null);
  73. requestMsg = (IMessage) _deserializationFormatter.Deserialize (requestStream, null);
  74. string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
  75. string uri;
  76. receiver.Parse (url, out uri);
  77. if (uri == null) uri = url;
  78. requestMsg.Properties["__Uri"] = uri;
  79. // Fixme: check if the message is an async msg
  80. ServerProcessing res = next_sink.ProcessMessage (sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
  81. if (res == ServerProcessing.Complete)
  82. {
  83. responseStream = null;
  84. responseHeaders = new TransportHeaders();
  85. if (sinkStack != null) responseStream = sinkStack.GetResponseStream (responseMsg, responseHeaders);
  86. if (responseStream == null) responseStream = new MemoryStream();
  87. _serializationFormatter.Serialize (responseStream, responseMsg);
  88. if (responseStream is MemoryStream) responseStream.Position = 0;
  89. sinkStack.Pop (this);
  90. }
  91. return res;
  92. }
  93. }
  94. }