2 // System.Runtime.Remoting.Channels.BinaryClientFormatterSink.cs
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Lluis Sanchez Gual (lluis@ideary.com)
8 // 2002 (C) Copyright, Ximian, Inc.
11 using System.Collections;
13 using System.Runtime.Remoting.Messaging;
14 using System.Runtime.Serialization;
15 using System.Runtime.Serialization.Formatters.Binary;
17 namespace System.Runtime.Remoting.Channels
19 public class BinaryClientFormatterSink : IClientFormatterSink,
20 IMessageSink, IClientChannelSink, IChannelSinkBase
22 BinaryCore _binaryCore = BinaryCore.DefaultInstance;
23 IClientChannelSink _nextInChain;
25 public BinaryClientFormatterSink (IClientChannelSink nextSink)
27 _nextInChain = nextSink;
30 internal BinaryCore BinaryCore
32 get { return _binaryCore; }
33 set { _binaryCore = value; }
36 public IClientChannelSink NextChannelSink
43 public IMessageSink NextSink
46 // This is the last sink in the IMessageSink sink chain
51 public IDictionary Properties
58 public void AsyncProcessRequest (IClientChannelSinkStack sinkStack,
60 ITransportHeaders headers,
63 // never called because the formatter sink is
64 // always the first in the chain
65 throw new NotSupportedException("BinaryClientFormatterSink must be the first sink in the IClientChannelSink chain");
68 public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
70 ITransportHeaders headers,
73 IMessage replyMessage = (IMessage)_binaryCore.Deserializer.DeserializeMethodResponse (stream, null, (IMethodCallMessage)state);
74 sinkStack.DispatchReplyMessage (replyMessage);
77 public Stream GetRequestStream (IMessage msg,
78 ITransportHeaders headers)
81 throw new NotSupportedException ();
84 public void ProcessMessage (IMessage msg,
85 ITransportHeaders requestHeaders,
87 out ITransportHeaders responseHeaders,
88 out Stream responseStream)
90 // never called because the formatter sink is
91 // always the first in the chain
92 throw new NotSupportedException ();
95 public IMessageCtrl AsyncProcessMessage (IMessage msg,
96 IMessageSink replySink)
98 ITransportHeaders transportHeaders = new TransportHeaders();
99 transportHeaders[CommonTransportKeys.RequestUri] = ((IMethodCallMessage)msg).Uri;
101 Stream stream = _nextInChain.GetRequestStream(msg, transportHeaders);
102 if (stream == null) stream = new MemoryStream ();
104 _binaryCore.Serializer.Serialize (stream, msg, null);
105 if (stream is MemoryStream) stream.Position = 0;
107 ClientChannelSinkStack stack = new ClientChannelSinkStack(replySink);
108 stack.Push (this, msg);
110 _nextInChain.AsyncProcessRequest (stack, msg, transportHeaders, stream);
112 // FIXME: No idea about how to implement IMessageCtrl
116 public IMessage SyncProcessMessage (IMessage msg)
120 ITransportHeaders call_headers = new TransportHeaders();
121 call_headers[CommonTransportKeys.RequestUri] = ((IMethodCallMessage)msg).Uri;
122 call_headers["Content-Type"] = "application/octet-stream";
124 Stream call_stream = _nextInChain.GetRequestStream(msg, call_headers);
125 if (call_stream == null) call_stream = new MemoryStream ();
127 // Serialize msg to the stream
129 _binaryCore.Serializer.Serialize (call_stream, msg, null);
130 if (call_stream is MemoryStream) call_stream.Position = 0;
132 Stream response_stream;
133 ITransportHeaders response_headers;
135 _nextInChain.ProcessMessage (msg, call_headers, call_stream, out response_headers,
136 out response_stream);
138 // Deserialize response_stream
140 return (IMessage) _binaryCore.Deserializer.DeserializeMethodResponse (response_stream, null, (IMethodCallMessage)msg);
142 } catch (Exception e) {
143 return new ReturnMessage (e, (IMethodCallMessage)msg);