2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Posix / Mono.Remoting.Channels.Unix / UnixBinaryClientFormatterSink.cs
blobeee4a712270860bdb8911047e342d2ba4e5134bb
1 //
2 // Mono.Remoting.Channels.Unix.UnixBinaryClientFormatterSink.cs
3 //
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Lluis Sanchez Gual (lluis@ideary.com)
7 //
8 // 2002 (C) Copyright, Ximian, Inc.
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Runtime.Remoting.Messaging;
36 using System.Runtime.Remoting.Channels;
37 using System.Runtime.Serialization;
38 using System.Runtime.Serialization.Formatters.Binary;
40 namespace Mono.Remoting.Channels.Unix
42 internal class UnixBinaryClientFormatterSink : IClientFormatterSink, IMessageSink, IClientChannelSink, IChannelSinkBase
44 UnixBinaryCore _binaryCore = UnixBinaryCore.DefaultInstance;
45 IClientChannelSink _nextInChain;
47 public UnixBinaryClientFormatterSink (IClientChannelSink nextSink)
49 _nextInChain = nextSink;
52 internal UnixBinaryCore BinaryCore
54 get { return _binaryCore; }
55 set { _binaryCore = value; }
58 public IClientChannelSink NextChannelSink
60 get {
61 return _nextInChain;
65 public IMessageSink NextSink
67 get {
68 // This is the last sink in the IMessageSink sink chain
69 return null;
73 public IDictionary Properties
75 get {
76 return null;
80 public void AsyncProcessRequest (IClientChannelSinkStack sinkStack,
81 IMessage msg,
82 ITransportHeaders headers,
83 Stream stream)
85 // never called because the formatter sink is
86 // always the first in the chain
87 throw new NotSupportedException("UnixBinaryClientFormatterSink must be the first sink in the IClientChannelSink chain");
90 public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
91 object state,
92 ITransportHeaders headers,
93 Stream stream)
95 IMessage replyMessage = (IMessage)_binaryCore.Deserializer.DeserializeMethodResponse (stream, null, (IMethodCallMessage)state);
96 sinkStack.DispatchReplyMessage (replyMessage);
99 public Stream GetRequestStream (IMessage msg,
100 ITransportHeaders headers)
102 // never called
103 throw new NotSupportedException ();
106 public void ProcessMessage (IMessage msg,
107 ITransportHeaders requestHeaders,
108 Stream requestStream,
109 out ITransportHeaders responseHeaders,
110 out Stream responseStream)
112 // never called because the formatter sink is
113 // always the first in the chain
114 throw new NotSupportedException ();
117 public IMessageCtrl AsyncProcessMessage (IMessage msg,
118 IMessageSink replySink)
120 ITransportHeaders transportHeaders = new TransportHeaders();
121 Stream stream = _nextInChain.GetRequestStream(msg, transportHeaders);
122 if (stream == null) stream = new MemoryStream ();
124 _binaryCore.Serializer.Serialize (stream, msg, null);
125 if (stream is MemoryStream) stream.Position = 0;
127 ClientChannelSinkStack stack = new ClientChannelSinkStack(replySink);
128 stack.Push (this, msg);
130 _nextInChain.AsyncProcessRequest (stack, msg, transportHeaders, stream);
132 // FIXME: No idea about how to implement IMessageCtrl
133 return null;
136 public IMessage SyncProcessMessage (IMessage msg)
138 try {
140 ITransportHeaders call_headers = new TransportHeaders();
141 call_headers["__RequestUri"] = ((IMethodCallMessage)msg).Uri;
142 call_headers["Content-Type"] = "application/octet-stream";
144 Stream call_stream = _nextInChain.GetRequestStream(msg, call_headers);
145 if (call_stream == null) call_stream = new MemoryStream ();
147 // Serialize msg to the stream
149 _binaryCore.Serializer.Serialize (call_stream, msg, null);
150 if (call_stream is MemoryStream) call_stream.Position = 0;
152 Stream response_stream;
153 ITransportHeaders response_headers;
155 _nextInChain.ProcessMessage (msg, call_headers, call_stream, out response_headers,
156 out response_stream);
158 // Deserialize response_stream
160 return (IMessage) _binaryCore.Deserializer.DeserializeMethodResponse (response_stream, null, (IMethodCallMessage)msg);
162 } catch (Exception e) {
163 return new ReturnMessage (e, (IMethodCallMessage)msg);