2 // System.Runtime.Remoting.Channels.BinaryServerFormatterSink.cs
4 // Author: Duncan Mak (duncan@ximian.com)
5 // Lluis Sanchez Gual (lluis@ideary.com)
7 // 2002 (C) Copyright, Ximian, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
33 using System.Runtime.Remoting.Messaging;
34 using System.Runtime.Serialization;
35 using System.Runtime.Serialization.Formatters;
36 using System.Runtime.Serialization.Formatters.Binary;
37 using System.Runtime.InteropServices;
39 namespace System.Runtime.Remoting.Channels {
41 public class BinaryServerFormatterSink : IServerChannelSink, IChannelSinkBase
50 BinaryCore _binaryCore = BinaryCore.DefaultInstance;
52 IServerChannelSink next_sink;
54 IChannelReceiver receiver;
56 public BinaryServerFormatterSink (BinaryServerFormatterSink.Protocol protocol,
57 IServerChannelSink nextSink,
58 IChannelReceiver receiver)
60 this.protocol = protocol;
61 this.next_sink = nextSink;
62 this.receiver = receiver;
65 internal BinaryCore BinaryCore
67 get { return _binaryCore; }
68 set { _binaryCore = value; }
71 public IServerChannelSink NextChannelSink {
77 public IDictionary Properties {
85 public TypeFilterLevel TypeFilterLevel
87 get { return _binaryCore.TypeFilterLevel; }
90 IDictionary props = (IDictionary) ((ICloneable)_binaryCore.Properties).Clone ();
91 props ["typeFilterLevel"] = value;
92 _binaryCore = new BinaryCore (this, props, BinaryServerFormatterSinkProvider.AllowedProperties);
97 public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
98 IMessage msg, ITransportHeaders headers, Stream stream)
100 ITransportHeaders responseHeaders = new TransportHeaders();
102 if (sinkStack != null) stream = sinkStack.GetResponseStream (msg, responseHeaders);
103 if (stream == null) stream = new MemoryStream();
105 _binaryCore.Serializer.Serialize (stream, msg, null);
106 if (stream is MemoryStream) stream.Position = 0;
108 sinkStack.AsyncProcessResponse (msg, responseHeaders, stream);
111 public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
112 IMessage msg, ITransportHeaders headers)
117 public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
118 IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
119 out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
121 // Check whether the request was already processed by another
122 // formatter sink and pass the request to the next sink if so.
123 if (requestMsg != null)
124 return next_sink.ProcessMessage (sinkStack,
132 // Check whether the request is suitable for this formatter
133 // and pass the request to the next sink if not.
134 // Note that a null content-type is handled as suitable,
135 // otherwise no other sink will be able to handle the request.
136 string contentType = requestHeaders["Content-Type"] as string;
137 if (contentType != null && contentType != "application/octet-stream") {
139 return next_sink.ProcessMessage (sinkStack,
147 // Let this formatter handle the exception.
151 sinkStack.Push (this, null);
152 ServerProcessing res;
156 string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
158 receiver.Parse (url, out uri);
159 if (uri == null) uri = url;
161 MethodCallHeaderHandler mhh = new MethodCallHeaderHandler(uri);
162 requestMsg = (IMessage) _binaryCore.Deserializer.Deserialize (requestStream, new HeaderHandler(mhh.HandleHeaders));
164 res = next_sink.ProcessMessage (sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
168 responseMsg = new ReturnMessage (ex, (IMethodCallMessage)requestMsg);
169 res = ServerProcessing.Complete;
170 responseHeaders = null;
171 responseStream = null;
174 if (res == ServerProcessing.Complete)
176 for (int n=0; n<3; n++) {
177 responseStream = null;
178 responseHeaders = new TransportHeaders();
180 if (sinkStack != null) responseStream = sinkStack.GetResponseStream (responseMsg, responseHeaders);
181 if (responseStream == null) responseStream = new MemoryStream();
184 _binaryCore.Serializer.Serialize (responseStream, responseMsg);
186 } catch (Exception ex) {
187 if (n == 2) throw ex;
188 else responseMsg = new ReturnMessage (ex, (IMethodCallMessage)requestMsg);
192 if (responseStream is MemoryStream) responseStream.Position = 0;
195 sinkStack.Pop (this);
202 internal class MethodCallHeaderHandler
206 public MethodCallHeaderHandler (string uri)
211 public object HandleHeaders (Header[] headers)