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 message
, ITransportHeaders headers
, Stream stream
)
100 ITransportHeaders responseHeaders
= new TransportHeaders();
102 if (sinkStack
!= null) stream
= sinkStack
.GetResponseStream (message
, responseHeaders
);
103 if (stream
== null) stream
= new MemoryStream();
105 _binaryCore
.Serializer
.Serialize (stream
, message
, null);
106 if (stream
is MemoryStream
) stream
.Position
= 0;
108 sinkStack
.AsyncProcessResponse (message
, 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 sinkStack
.Push (this, null);
122 ServerProcessing res
;
126 string url
= (string)requestHeaders
[CommonTransportKeys
.RequestUri
];
128 receiver
.Parse (url
, out uri
);
129 if (uri
== null) uri
= url
;
131 MethodCallHeaderHandler mhh
= new MethodCallHeaderHandler(uri
);
132 requestMsg
= (IMessage
) _binaryCore
.Deserializer
.Deserialize (requestStream
, new HeaderHandler(mhh
.HandleHeaders
));
134 res
= next_sink
.ProcessMessage (sinkStack
, requestMsg
, requestHeaders
, null, out responseMsg
, out responseHeaders
, out responseStream
);
138 responseMsg
= new ReturnMessage (ex
, (IMethodCallMessage
)requestMsg
);
139 res
= ServerProcessing
.Complete
;
140 responseHeaders
= null;
141 responseStream
= null;
144 if (res
== ServerProcessing
.Complete
)
146 responseStream
= null;
147 responseHeaders
= new TransportHeaders();
149 if (sinkStack
!= null) responseStream
= sinkStack
.GetResponseStream (responseMsg
, responseHeaders
);
150 if (responseStream
== null) responseStream
= new MemoryStream();
152 _binaryCore
.Serializer
.Serialize (responseStream
, responseMsg
);
153 if (responseStream
is MemoryStream
) responseStream
.Position
= 0;
155 sinkStack
.Pop (this);
162 internal class MethodCallHeaderHandler
166 public MethodCallHeaderHandler (string uri
)
171 public object HandleHeaders (Header
[] headers
)